logger.h 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. class logger {
  30. public:
  31. ~logger();
  32. template <typename... Args>
  33. inline void log(log_level ll, std::string const & interp,
  34. Args && ...args) {
  35. log( ll, location_info{}, interp, std::forward<Args>(args)... );
  36. }
  37. template <typename... Args>
  38. inline void log(log_level ll, location_info info,
  39. std::string const & interp, Args && ...args) {
  40. if ( should_log( ll ) ) {
  41. log( ll, info, format_msg( interp, std::forward<Args>(args)... ) );
  42. }
  43. }
  44. void flush();
  45. protected:
  46. logger(std::string const & name, std::shared_ptr<logger_impl> impl);
  47. private:
  48. friend class manager;
  49. bool should_log( log_level ) const;
  50. void log( log_level ll, location_info info, std::string const& );
  51. private:
  52. log_level min_level_;
  53. std::string const logger_name_;
  54. std::shared_ptr<logger_impl> impl_;
  55. };
  56. }