c_logger.h 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. //
  2. // c_logger.hpp
  3. // logging
  4. //
  5. // Created by Sam Jaffe on 4/1/19.
  6. //
  7. #pragma once
  8. #include <cstdarg>
  9. #include <cstdlib>
  10. #include <cstring>
  11. #include <memory>
  12. #include <string>
  13. #include "level.h"
  14. #define VA_LOGN(level, size) \
  15. do { \
  16. va_list vargs; \
  17. va_start(vargs, fmt); \
  18. vlognf(level, size, fmt, vargs); \
  19. va_end(vargs); \
  20. } while (0)
  21. #define VA_LOG(level) VA_LOGN(level, c_logger::LOGF_MAX_SIZE)
  22. #define MAKE_LOGGER_FMT(lvl) \
  23. inline void lvl##f(char const * fmt, ...) { VA_LOG(level::lvl); } \
  24. inline void lvl##nf(size_t n, char const * fmt, ...) { \
  25. VA_LOGN(level::lvl, n); \
  26. } \
  27. inline void lvl(char const * msg) { log(level::lvl, msg); } \
  28. inline void lvl(std::string const & msg) { log(level::lvl, msg); }
  29. namespace logging {
  30. enum class level : int;
  31. class logger_impl;
  32. class c_logger {
  33. private:
  34. friend class manager;
  35. std::string logger_name_;
  36. std::shared_ptr<logger_impl> impl_;
  37. public:
  38. static const size_t LOGF_MAX_SIZE = 2048;
  39. public:
  40. MAKE_LOGGER_FMT(trace)
  41. MAKE_LOGGER_FMT(debug)
  42. MAKE_LOGGER_FMT(info)
  43. MAKE_LOGGER_FMT(warn)
  44. MAKE_LOGGER_FMT(error)
  45. MAKE_LOGGER_FMT(critical)
  46. MAKE_LOGGER_FMT(fatal)
  47. template <typename... Args>
  48. inline void log(level ll, std::string const & interp, Args &&... args);
  49. void flush();
  50. ~c_logger();
  51. protected:
  52. c_logger(std::string const & name, std::shared_ptr<logger_impl> impl);
  53. private:
  54. void vlognf(level, size_t, char const *, va_list);
  55. void log(level, std::string const &);
  56. };
  57. }
  58. #undef VA_LOG
  59. #undef LA_LOGN