c_logger.h 1.8 KB

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