logger.hpp 1.9 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.hpp"
  22. #include "format.hpp"
  23. #define log_message( logger, level, ... ) logger.log( level, { __FILE__, __LINE__, STRING( FUNCTION ) }, __VA_ARGS__ )
  24. namespace logging {
  25. class logger_impl;
  26. typedef logger_impl& (*_binding)(void);
  27. bool bind_logger_impl(_binding impl);
  28. const char* level_header(log_level);
  29. class logger {
  30. public:
  31. static logger& instance();
  32. static logger& instance(std::string const & key);
  33. public:
  34. template <typename... Args>
  35. inline void log(log_level ll,
  36. std::string const & interp, Args && ...args) {
  37. log( ll, location_info{}, interp, std::forward<Args>(args)... );
  38. }
  39. template <typename... Args>
  40. inline void log(log_level ll, location_info info,
  41. std::string const & interp, Args && ...args) {
  42. if ( should_log( ll ) ) {
  43. log( ll, info, format_msg( interp, std::forward<Args>(args)... ) );
  44. }
  45. }
  46. void flush();
  47. ~logger();
  48. private:
  49. bool should_log( log_level ) const;
  50. void log( log_level ll, location_info info, std::string const& );
  51. explicit logger(std::string const & name = "");
  52. logger(logger const&);
  53. logger& operator=(logger const&);
  54. log_level min_level_;
  55. std::string const logger_name_;
  56. logger_impl& impl_;
  57. };
  58. class logger_impl {
  59. public:
  60. bool should_log( log_level ll ) const;
  61. virtual void write( logpacket const & pkt ) = 0;
  62. virtual void flush() = 0;
  63. virtual ~logger_impl() = default;
  64. private:
  65. log_level min_log_level;
  66. };
  67. }