bitz-server  2.0.3
logger.h
1 //
2 // Copyright(c) 2015 Gabi Melman.
3 // Distributed under the MIT License (http://opensource.org/licenses/MIT)
4 //
5 
6 #pragma once
7 
8 // Thread safe logger (except for set_pattern(..), set_formatter(..) and set_error_handler())
9 // Has name, log level, vector of std::shared sink pointers and formatter
10 // Upon each log write the logger:
11 // 1. Checks if its log level is enough to log the message
12 // 2. Format the message using the formatter function
13 // 3. Pass the formatted message to its sinks to performa the actual logging
14 
15 #include "common.h"
16 #include "sinks/base_sink.h"
17 
18 #include <memory>
19 #include <string>
20 #include <vector>
21 
22 namespace spdlog {
23 
24 class logger
25 {
26 public:
27  logger(const std::string &name, sink_ptr single_sink);
28  logger(const std::string &name, sinks_init_list sinks);
29 
30  template<class It>
31  logger(std::string name, const It &begin, const It &end);
32 
33  virtual ~logger();
34 
35  logger(const logger &) = delete;
36  logger &operator=(const logger &) = delete;
37 
38  template<typename... Args>
39  void log(level::level_enum lvl, const char *fmt, const Args &... args);
40 
41  template<typename... Args>
42  void log(level::level_enum lvl, const char *msg);
43 
44  template<typename Arg1, typename... Args>
45  void trace(const char *fmt, const Arg1 &, const Args &... args);
46 
47  template<typename Arg1, typename... Args>
48  void debug(const char *fmt, const Arg1 &, const Args &... args);
49 
50  template<typename Arg1, typename... Args>
51  void info(const char *fmt, const Arg1 &, const Args &... args);
52 
53  template<typename Arg1, typename... Args>
54  void warn(const char *fmt, const Arg1 &, const Args &... args);
55 
56  template<typename Arg1, typename... Args>
57  void error(const char *fmt, const Arg1 &, const Args &... args);
58 
59  template<typename Arg1, typename... Args>
60  void critical(const char *fmt, const Arg1 &, const Args &... args);
61 
62 #ifdef SPDLOG_WCHAR_TO_UTF8_SUPPORT
63  template<typename... Args>
64  void log(level::level_enum lvl, const wchar_t *msg);
65 
66  template<typename... Args>
67  void log(level::level_enum lvl, const wchar_t *fmt, const Args &... args);
68 
69  template<typename... Args>
70  void trace(const wchar_t *fmt, const Args &... args);
71 
72  template<typename... Args>
73  void debug(const wchar_t *fmt, const Args &... args);
74 
75  template<typename... Args>
76  void info(const wchar_t *fmt, const Args &... args);
77 
78  template<typename... Args>
79  void warn(const wchar_t *fmt, const Args &... args);
80 
81  template<typename... Args>
82  void error(const wchar_t *fmt, const Args &... args);
83 
84  template<typename... Args>
85  void critical(const wchar_t *fmt, const Args &... args);
86 #endif // SPDLOG_WCHAR_TO_UTF8_SUPPORT
87 
88  template<typename T>
89  void log(level::level_enum lvl, const T &);
90 
91  template<typename T>
92  void trace(const T &msg);
93 
94  template<typename T>
95  void debug(const T &msg);
96 
97  template<typename T>
98  void info(const T &msg);
99 
100  template<typename T>
101  void warn(const T &msg);
102 
103  template<typename T>
104  void error(const T &msg);
105 
106  template<typename T>
107  void critical(const T &msg);
108 
109  bool should_log(level::level_enum msg_level) const;
110  void set_level(level::level_enum log_level);
111  level::level_enum level() const;
112  const std::string &name() const;
113  void set_pattern(const std::string &pattern, pattern_time_type pattern_time = pattern_time_type::local);
114  void set_formatter(formatter_ptr msg_formatter);
115 
116  // automatically call flush() if message level >= log_level
117  void flush_on(level::level_enum log_level);
118 
119  virtual void flush();
120 
121  const std::vector<sink_ptr> &sinks() const;
122 
123  // error handler
124  virtual void set_error_handler(log_err_handler err_handler);
125  virtual log_err_handler error_handler();
126 
127 protected:
128  virtual void _sink_it(details::log_msg &msg);
129  virtual void _set_pattern(const std::string &pattern, pattern_time_type pattern_time);
130  virtual void _set_formatter(formatter_ptr msg_formatter);
131 
132  // default error handler: print the error to stderr with the max rate of 1 message/minute
133  virtual void _default_err_handler(const std::string &msg);
134 
135  // return true if the given message level should trigger a flush
136  bool _should_flush_on(const details::log_msg &msg);
137 
138  // increment the message count (only if defined(SPDLOG_ENABLE_MESSAGE_COUNTER))
139  void _incr_msg_counter(details::log_msg &msg);
140 
141  const std::string _name;
142  std::vector<sink_ptr> _sinks;
143  formatter_ptr _formatter;
144  spdlog::level_t _level;
145  spdlog::level_t _flush_level;
146  log_err_handler _err_handler;
147  std::atomic<time_t> _last_err_time;
148  std::atomic<size_t> _msg_counter;
149 };
150 } // namespace spdlog
151 
152 #include "details/logger_impl.h"
Definition: logger.h:24
Definition: async_logger.h:26
Definition: log_msg.h:16
Definition: null_mutex.h:23
Definition: format.cc:84