logger.h 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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, lvl, ... ) \
  25. logger.log( level::lvl, 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(level ll, std::string const & interp, Args && ...args) {
  34. log( ll, location_info{}, interp, std::forward<Args>(args)... );
  35. }
  36. template <typename... Args>
  37. inline void log(level ll, location_info info,
  38. std::string const & interp, Args && ...args) {
  39. if ( should_log( ll ) ) {
  40. log( ll, info, message( interp, std::forward<Args>(args)... ) );
  41. }
  42. }
  43. void flush();
  44. protected:
  45. logger(std::string const & name, std::shared_ptr<logger_impl> impl);
  46. private:
  47. friend class manager;
  48. bool should_log( level ) const;
  49. void log( level ll, location_info info, message const& );
  50. private:
  51. std::string const logger_name_;
  52. std::shared_ptr<logger_impl> impl_;
  53. };
  54. }