| 1 | // |
|---|
| 2 | // Copyright(c) 2015-2108 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 |
|---|
| 9 | // set_error_handler()) |
|---|
| 10 | // Has name, log level, vector of std::shared sink pointers and formatter |
|---|
| 11 | // Upon each log write the logger: |
|---|
| 12 | // 1. Checks if its log level is enough to log the message and if yes: |
|---|
| 13 | // 2. Call the underlying sinks to do the job. |
|---|
| 14 | // 3. Each sink use its own private copy of a formatter to format the message |
|---|
| 15 | // and send to its destination. |
|---|
| 16 | // |
|---|
| 17 | // The use of private formatter per sink provides the opportunity to cache some |
|---|
| 18 | // formatted data, |
|---|
| 19 | // and support customize format per each sink. |
|---|
| 20 | |
|---|
| 21 | #include "spdlog/common.h" |
|---|
| 22 | #include "spdlog/formatter.h" |
|---|
| 23 | #include "spdlog/sinks/sink.h" |
|---|
| 24 | |
|---|
| 25 | #include <memory> |
|---|
| 26 | #include <string> |
|---|
| 27 | #include <vector> |
|---|
| 28 | |
|---|
| 29 | namespace spdlog { |
|---|
| 30 | |
|---|
| 31 | class logger |
|---|
| 32 | { |
|---|
| 33 | public: |
|---|
| 34 | logger(std::string name, sink_ptr single_sink); |
|---|
| 35 | logger(std::string name, sinks_init_list sinks); |
|---|
| 36 | |
|---|
| 37 | template<typename It> |
|---|
| 38 | logger(std::string name, It begin, It end); |
|---|
| 39 | |
|---|
| 40 | virtual ~logger(); |
|---|
| 41 | |
|---|
| 42 | logger(const logger &) = delete; |
|---|
| 43 | logger &operator=(const logger &) = delete; |
|---|
| 44 | |
|---|
| 45 | template<typename... Args> |
|---|
| 46 | void log(level::level_enum lvl, const char *fmt, const Args &... args); |
|---|
| 47 | |
|---|
| 48 | template<typename... Args> |
|---|
| 49 | void log(source_loc loc, level::level_enum lvl, const char *fmt, const Args &... args); |
|---|
| 50 | |
|---|
| 51 | void log(level::level_enum lvl, const char *msg); |
|---|
| 52 | |
|---|
| 53 | void log(source_loc loc, level::level_enum lvl, const char *msg); |
|---|
| 54 | |
|---|
| 55 | template<typename... Args> |
|---|
| 56 | void trace(const char *fmt, const Args &... args); |
|---|
| 57 | |
|---|
| 58 | template<typename... Args> |
|---|
| 59 | void debug(const char *fmt, const Args &... args); |
|---|
| 60 | |
|---|
| 61 | template<typename... Args> |
|---|
| 62 | void info(const char *fmt, const Args &... args); |
|---|
| 63 | |
|---|
| 64 | template<typename... Args> |
|---|
| 65 | void warn(const char *fmt, const Args &... args); |
|---|
| 66 | |
|---|
| 67 | template<typename... Args> |
|---|
| 68 | void error(const char *fmt, const Args &... args); |
|---|
| 69 | |
|---|
| 70 | template<typename... Args> |
|---|
| 71 | void critical(const char *fmt, const Args &... args); |
|---|
| 72 | |
|---|
| 73 | #ifdef SPDLOG_WCHAR_TO_UTF8_SUPPORT |
|---|
| 74 | #ifndef _WIN32 |
|---|
| 75 | #error SPDLOG_WCHAR_TO_UTF8_SUPPORT only supported on windows |
|---|
| 76 | #else |
|---|
| 77 | template<typename... Args> |
|---|
| 78 | void log(level::level_enum lvl, const wchar_t *fmt, const Args &... args); |
|---|
| 79 | |
|---|
| 80 | template<typename... Args> |
|---|
| 81 | void log(source_loc source, level::level_enum lvl, const wchar_t *fmt, const Args &... args); |
|---|
| 82 | |
|---|
| 83 | template<typename... Args> |
|---|
| 84 | void trace(const wchar_t *fmt, const Args &... args); |
|---|
| 85 | |
|---|
| 86 | template<typename... Args> |
|---|
| 87 | void debug(const wchar_t *fmt, const Args &... args); |
|---|
| 88 | |
|---|
| 89 | template<typename... Args> |
|---|
| 90 | void info(const wchar_t *fmt, const Args &... args); |
|---|
| 91 | |
|---|
| 92 | template<typename... Args> |
|---|
| 93 | void warn(const wchar_t *fmt, const Args &... args); |
|---|
| 94 | |
|---|
| 95 | template<typename... Args> |
|---|
| 96 | void error(const wchar_t *fmt, const Args &... args); |
|---|
| 97 | |
|---|
| 98 | template<typename... Args> |
|---|
| 99 | void critical(const wchar_t *fmt, const Args &... args); |
|---|
| 100 | #endif // _WIN32 |
|---|
| 101 | #endif // SPDLOG_WCHAR_TO_UTF8_SUPPORT |
|---|
| 102 | |
|---|
| 103 | // T can be statically converted to string_view |
|---|
| 104 | template<class T, typename std::enable_if<std::is_convertible<T, spdlog::string_view_t>::value, T>::type * = nullptr> |
|---|
| 105 | void log(level::level_enum lvl, const T &); |
|---|
| 106 | |
|---|
| 107 | // T can be statically converted to string_view |
|---|
| 108 | template<class T, typename std::enable_if<std::is_convertible<T, spdlog::string_view_t>::value, T>::type * = nullptr> |
|---|
| 109 | void log(source_loc loc, level::level_enum lvl, const T &); |
|---|
| 110 | |
|---|
| 111 | // T cannot be statically converted to string_view |
|---|
| 112 | template<class T, typename std::enable_if<!std::is_convertible<T, spdlog::string_view_t>::value, T>::type * = nullptr> |
|---|
| 113 | void log(level::level_enum lvl, const T &); |
|---|
| 114 | |
|---|
| 115 | // T cannot be statically converted to string_view |
|---|
| 116 | template<class T, typename std::enable_if<!std::is_convertible<T, spdlog::string_view_t>::value, T>::type * = nullptr> |
|---|
| 117 | void log(source_loc loc, level::level_enum lvl, const T &); |
|---|
| 118 | |
|---|
| 119 | template<typename T> |
|---|
| 120 | void trace(const T &msg); |
|---|
| 121 | |
|---|
| 122 | template<typename T> |
|---|
| 123 | void debug(const T &msg); |
|---|
| 124 | |
|---|
| 125 | template<typename T> |
|---|
| 126 | void info(const T &msg); |
|---|
| 127 | |
|---|
| 128 | template<typename T> |
|---|
| 129 | void warn(const T &msg); |
|---|
| 130 | |
|---|
| 131 | template<typename T> |
|---|
| 132 | void error(const T &msg); |
|---|
| 133 | |
|---|
| 134 | template<typename T> |
|---|
| 135 | void critical(const T &msg); |
|---|
| 136 | |
|---|
| 137 | bool should_log(level::level_enum msg_level) const; |
|---|
| 138 | void set_level(level::level_enum log_level); |
|---|
| 139 | |
|---|
| 140 | static level::level_enum default_level(); |
|---|
| 141 | level::level_enum level() const; |
|---|
| 142 | const std::string &name() const; |
|---|
| 143 | |
|---|
| 144 | // set formatting for the sinks in this logger. |
|---|
| 145 | // each sink will get a seperate instance of the formatter object. |
|---|
| 146 | void set_formatter(std::unique_ptr<formatter> formatter); |
|---|
| 147 | void set_pattern(std::string pattern, pattern_time_type time_type = pattern_time_type::local); |
|---|
| 148 | |
|---|
| 149 | // flush functions |
|---|
| 150 | void flush(); |
|---|
| 151 | void flush_on(level::level_enum log_level); |
|---|
| 152 | level::level_enum flush_level() const; |
|---|
| 153 | |
|---|
| 154 | // sinks |
|---|
| 155 | const std::vector<sink_ptr> &sinks() const; |
|---|
| 156 | std::vector<sink_ptr> &sinks(); |
|---|
| 157 | |
|---|
| 158 | // error handler |
|---|
| 159 | void set_error_handler(log_err_handler err_handler); |
|---|
| 160 | log_err_handler error_handler() const; |
|---|
| 161 | |
|---|
| 162 | // create new logger with same sinks and configuration. |
|---|
| 163 | virtual std::shared_ptr<logger> clone(std::string logger_name); |
|---|
| 164 | |
|---|
| 165 | protected: |
|---|
| 166 | virtual void sink_it_(details::log_msg &msg); |
|---|
| 167 | virtual void flush_(); |
|---|
| 168 | |
|---|
| 169 | bool should_flush_(const details::log_msg &msg); |
|---|
| 170 | |
|---|
| 171 | // default error handler. |
|---|
| 172 | // print the error to stderr with the max rate of 1 message/minute. |
|---|
| 173 | void default_err_handler_(const std::string &msg); |
|---|
| 174 | |
|---|
| 175 | // increment the message count (only if defined(SPDLOG_ENABLE_MESSAGE_COUNTER)) |
|---|
| 176 | void incr_msg_counter_(details::log_msg &msg); |
|---|
| 177 | |
|---|
| 178 | const std::string name_; |
|---|
| 179 | std::vector<sink_ptr> sinks_; |
|---|
| 180 | spdlog::level_t level_{spdlog::logger::default_level()}; |
|---|
| 181 | spdlog::level_t flush_level_{level::off}; |
|---|
| 182 | log_err_handler err_handler_{[this](const std::string &msg) { this->default_err_handler_(msg); }}; |
|---|
| 183 | std::atomic<time_t> last_err_time_{0}; |
|---|
| 184 | std::atomic<size_t> msg_counter_{1}; |
|---|
| 185 | }; |
|---|
| 186 | } // namespace spdlog |
|---|
| 187 | |
|---|
| 188 | #include "details/logger_impl.h" |
|---|