You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

logger.h 3.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. //
  2. // Copyright(c) 2015 Gabi Melman.
  3. // Distributed under the MIT License (http://opensource.org/licenses/MIT)
  4. //
  5. #pragma once
  6. // Thread safe logger
  7. // Has name, log level, vector of std::shared sink pointers and formatter
  8. // Upon each log write the logger:
  9. // 1. Checks if its log level is enough to log the message
  10. // 2. Format the message using the formatter function
  11. // 3. Pass the formatted message to its sinks to performa the actual logging
  12. #include <spdlog/sinks/base_sink.h>
  13. #include <spdlog/common.h>
  14. #include <vector>
  15. #include <memory>
  16. #include <string>
  17. namespace spdlog
  18. {
  19. class logger
  20. {
  21. public:
  22. logger(const std::string& logger_name, sink_ptr single_sink);
  23. logger(const std::string& name, sinks_init_list);
  24. template<class It>
  25. logger(const std::string& name, const It& begin, const It& end);
  26. virtual ~logger();
  27. logger(const logger&) = delete;
  28. logger& operator=(const logger&) = delete;
  29. template <typename... Args> void log(level::level_enum lvl, const char* fmt, const Args&... args);
  30. template <typename... Args> void log(level::level_enum lvl, const char* msg);
  31. template <typename... Args> void trace(const char* fmt, const Args&... args);
  32. template <typename... Args> void debug(const char* fmt, const Args&... args);
  33. template <typename... Args> void info(const char* fmt, const Args&... args);
  34. template <typename... Args> void warn(const char* fmt, const Args&... args);
  35. template <typename... Args> void error(const char* fmt, const Args&... args);
  36. template <typename... Args> void critical(const char* fmt, const Args&... args);
  37. template <typename T> void log(level::level_enum lvl, const T&);
  38. template <typename T> void trace(const T&);
  39. template <typename T> void debug(const T&);
  40. template <typename T> void info(const T&);
  41. template <typename T> void warn(const T&);
  42. template <typename T> void error(const T&);
  43. template <typename T> void critical(const T&);
  44. bool should_log(level::level_enum) const;
  45. void set_level(level::level_enum);
  46. level::level_enum level() const;
  47. const std::string& name() const;
  48. void set_pattern(const std::string&);
  49. void set_formatter(formatter_ptr);
  50. // error handler
  51. void set_error_handler(log_err_handler);
  52. log_err_handler error_handler();
  53. // automatically call flush() if message level >= log_level
  54. void flush_on(level::level_enum log_level);
  55. virtual void flush();
  56. protected:
  57. virtual void _sink_it(details::log_msg&);
  58. virtual void _set_pattern(const std::string&);
  59. virtual void _set_formatter(formatter_ptr);
  60. // default error handler: print the error to stderr with the max rate of 1 message/minute
  61. virtual void _default_err_handler(const std::string &msg);
  62. // return true if the given message level should trigger a flush
  63. bool _should_flush_on(const details::log_msg&);
  64. const std::string _name;
  65. std::vector<sink_ptr> _sinks;
  66. formatter_ptr _formatter;
  67. spdlog::level_t _level;
  68. spdlog::level_t _flush_level;
  69. log_err_handler _err_handler;
  70. std::atomic<time_t> _last_err_time;
  71. };
  72. }
  73. #include <spdlog/details/logger_impl.h>