logger.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 "message.h"
  22. #define log_message(logger, lvl, ...) \
  23. logger.log(level::lvl, log_here, __VA_ARGS__)
  24. namespace logging {
  25. enum class level : int;
  26. struct location_info;
  27. class logger_impl;
  28. struct logpacket;
  29. /*
  30. * The main top-level class that should be used when performing logging.
  31. * Provides various overrides of the function log() that handle various use
  32. * cases. In comparison with the C-Style logger, this one uses a Python style
  33. * of string formatting.
  34. * Currently, it is only able to support "{}", and cannot use positional,
  35. * named, or reflection-based interpolations. Under the hood, the loggers are
  36. * configured in a similar fashion to those in Log4J, such as PatternLayout
  37. * and FileAppender.
  38. * One of the useful features of this logger is the log_here macro. Log here
  39. * uses the compiler to generate all of the necessary information for file
  40. * name, class/namespace path, method name, and line number. It is not
  41. * supported for the logger to learn that information on its own through
  42. * creating a backtrace.
  43. */
  44. class logger {
  45. private:
  46. friend class manager;
  47. std::string const logger_name_;
  48. std::shared_ptr<logger_impl> impl_;
  49. public:
  50. ~logger();
  51. void log(level ll, char const * str) { log(ll, message(str)); }
  52. void log(level ll, std::string const & str) { log(ll, message(str)); }
  53. template <typename... Args>
  54. void log(level ll, std::string const & interp, Args &&... args) {
  55. log(ll, message(interp, std::forward<Args>(args)...));
  56. }
  57. template <typename... Args>
  58. void log(level ll, location_info const & info, std::string const & interp,
  59. Args &&... args) {
  60. log(ll, info, message(interp, std::forward<Args>(args)...));
  61. }
  62. void flush();
  63. protected:
  64. logger(std::string const & name, std::shared_ptr<logger_impl> impl);
  65. private:
  66. void log(level ll, message const &);
  67. void log(level ll, location_info const & info, message const &);
  68. };
  69. }