c_logger.h 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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 "logger_fwd.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. class logger_impl;
  31. class c_logger {
  32. public:
  33. static const size_t LOGF_MAX_SIZE = 2048;
  34. public:
  35. MAKE_LOGGER_FMT(trace)
  36. MAKE_LOGGER_FMT(debug)
  37. MAKE_LOGGER_FMT(info)
  38. MAKE_LOGGER_FMT(warn)
  39. MAKE_LOGGER_FMT(error)
  40. MAKE_LOGGER_FMT(critical)
  41. MAKE_LOGGER_FMT(fatal)
  42. template <typename... Args>
  43. inline void log(level ll, std::string const & interp, Args && ...args);
  44. void flush();
  45. ~c_logger();
  46. protected:
  47. friend class manager;
  48. c_logger(std::string const & name, std::shared_ptr<logger_impl> impl);
  49. private:
  50. void vlognf(level, size_t, char const*, va_list);
  51. void log(level, std::string const&);
  52. private:
  53. std::string logger_name_;
  54. std::shared_ptr<logger_impl> impl_;
  55. };
  56. }
  57. #undef VA_LOG
  58. #undef LA_LOGN