logger.h 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. //
  2. // logger.hpp
  3. // logger
  4. //
  5. // Created by Sam Jaffe on 9/3/16.
  6. //
  7. #pragma once
  8. //
  9. // Logger.hpp
  10. // DanmakuRPG
  11. //
  12. // Created by Sam Jaffe on 7/18/15.
  13. // Copyright (c) 2015 Sam Jaffe. All rights reserved.
  14. //
  15. #pragma once
  16. #include <cstdarg>
  17. #include <cstdlib>
  18. #include <cstring>
  19. #include <memory>
  20. #include <string>
  21. #include "logger_fwd.h"
  22. #include "format.h"
  23. #define log_here { __FILE__, __LINE__, STRING( FUNCTION ) }
  24. #define log_message( logger, level, ... ) \
  25. logger.log( level, log_here, __VA_ARGS__ )
  26. namespace logging {
  27. class logger_impl;
  28. class manager;
  29. const char* level_header(log_level);
  30. class logger {
  31. public:
  32. ~logger();
  33. template <typename... Args>
  34. inline void log(log_level ll, std::string const & interp,
  35. Args && ...args) {
  36. log( ll, location_info{}, interp, std::forward<Args>(args)... );
  37. }
  38. template <typename... Args>
  39. inline void log(log_level ll, location_info info,
  40. std::string const & interp, Args && ...args) {
  41. if ( should_log( ll ) ) {
  42. log( ll, info, format_msg( interp, std::forward<Args>(args)... ) );
  43. }
  44. }
  45. void flush();
  46. protected:
  47. logger(std::string const & name, std::shared_ptr<logger_impl> impl);
  48. private:
  49. friend class manager;
  50. bool should_log( log_level ) const;
  51. void log( log_level ll, location_info info, std::string const& );
  52. private:
  53. log_level min_level_;
  54. std::string const logger_name_;
  55. std::shared_ptr<logger_impl> impl_;
  56. };
  57. class logger_impl {
  58. public:
  59. bool should_log( log_level ll ) const;
  60. virtual void write( logpacket const & pkt ) = 0;
  61. virtual void flush() = 0;
  62. virtual ~logger_impl() = default;
  63. protected:
  64. log_level min_log_level;
  65. };
  66. }