/src/spdlog/include/spdlog/logger-inl.h
Line | Count | Source (jump to first uncovered line) |
1 | | // Copyright(c) 2015-present, Gabi Melman & spdlog contributors. |
2 | | // Distributed under the MIT License (http://opensource.org/licenses/MIT) |
3 | | |
4 | | #pragma once |
5 | | |
6 | | #ifndef SPDLOG_HEADER_ONLY |
7 | | #include <spdlog/logger.h> |
8 | | #endif |
9 | | |
10 | | #include <spdlog/details/backtracer.h> |
11 | | #include <spdlog/pattern_formatter.h> |
12 | | #include <spdlog/sinks/sink.h> |
13 | | |
14 | | #include <cstdio> |
15 | | |
16 | | namespace spdlog { |
17 | | |
18 | | // public methods |
19 | | SPDLOG_INLINE logger::logger(const logger &other) |
20 | | : name_(other.name_), |
21 | | sinks_(other.sinks_), |
22 | | level_(other.level_.load(std::memory_order_relaxed)), |
23 | | flush_level_(other.flush_level_.load(std::memory_order_relaxed)), |
24 | | custom_err_handler_(other.custom_err_handler_), |
25 | 0 | tracer_(other.tracer_) {} |
26 | | |
27 | | SPDLOG_INLINE logger::logger(logger &&other) SPDLOG_NOEXCEPT |
28 | | : name_(std::move(other.name_)), |
29 | | sinks_(std::move(other.sinks_)), |
30 | | level_(other.level_.load(std::memory_order_relaxed)), |
31 | | flush_level_(other.flush_level_.load(std::memory_order_relaxed)), |
32 | | custom_err_handler_(std::move(other.custom_err_handler_)), |
33 | | tracer_(std::move(other.tracer_)) |
34 | | |
35 | | {} |
36 | | |
37 | 0 | SPDLOG_INLINE logger &logger::operator=(logger other) SPDLOG_NOEXCEPT { |
38 | 0 | this->swap(other); |
39 | 0 | return *this; |
40 | 0 | } |
41 | | |
42 | 0 | SPDLOG_INLINE void logger::swap(spdlog::logger &other) SPDLOG_NOEXCEPT { |
43 | 0 | name_.swap(other.name_); |
44 | 0 | sinks_.swap(other.sinks_); |
45 | 0 |
|
46 | 0 | // swap level_ |
47 | 0 | auto other_level = other.level_.load(); |
48 | 0 | auto my_level = level_.exchange(other_level); |
49 | 0 | other.level_.store(my_level); |
50 | 0 |
|
51 | 0 | // swap flush level_ |
52 | 0 | other_level = other.flush_level_.load(); |
53 | 0 | my_level = flush_level_.exchange(other_level); |
54 | 0 | other.flush_level_.store(my_level); |
55 | 0 |
|
56 | 0 | custom_err_handler_.swap(other.custom_err_handler_); |
57 | 0 | std::swap(tracer_, other.tracer_); |
58 | 0 | } |
59 | | |
60 | 0 | SPDLOG_INLINE void swap(logger &a, logger &b) { a.swap(b); } |
61 | | |
62 | 1.07k | SPDLOG_INLINE void logger::set_level(level::level_enum log_level) { level_.store(log_level); } |
63 | | |
64 | 0 | SPDLOG_INLINE level::level_enum logger::level() const { |
65 | 0 | return static_cast<level::level_enum>(level_.load(std::memory_order_relaxed)); |
66 | 0 | } |
67 | | |
68 | 1.63k | SPDLOG_INLINE const std::string &logger::name() const { return name_; } |
69 | | |
70 | | // set formatting for the sinks in this logger. |
71 | | // each sink will get a separate instance of the formatter object. |
72 | 19.7k | SPDLOG_INLINE void logger::set_formatter(std::unique_ptr<formatter> f) { |
73 | 19.7k | for (auto it = sinks_.begin(); it != sinks_.end(); ++it) { |
74 | 19.7k | if (std::next(it) == sinks_.end()) { |
75 | | // last element - we can be move it. |
76 | 19.7k | (*it)->set_formatter(std::move(f)); |
77 | 19.7k | break; // to prevent clang-tidy warning |
78 | 19.7k | } else { |
79 | 0 | (*it)->set_formatter(f->clone()); |
80 | 0 | } |
81 | 19.7k | } |
82 | 19.7k | } |
83 | | |
84 | 0 | SPDLOG_INLINE void logger::set_pattern(std::string pattern, pattern_time_type time_type) { |
85 | 0 | auto new_formatter = details::make_unique<pattern_formatter>(std::move(pattern), time_type); |
86 | 0 | set_formatter(std::move(new_formatter)); |
87 | 0 | } |
88 | | |
89 | | // create new backtrace sink and move to it all our child sinks |
90 | 1.06k | SPDLOG_INLINE void logger::enable_backtrace(size_t n_messages) { tracer_.enable(n_messages); } |
91 | | |
92 | | // restore orig sinks and level and delete the backtrace sink |
93 | 0 | SPDLOG_INLINE void logger::disable_backtrace() { tracer_.disable(); } |
94 | | |
95 | 534 | SPDLOG_INLINE void logger::dump_backtrace() { dump_backtrace_(); } |
96 | | |
97 | | // flush functions |
98 | 0 | SPDLOG_INLINE void logger::flush() { flush_(); } |
99 | | |
100 | 5 | SPDLOG_INLINE void logger::flush_on(level::level_enum log_level) { flush_level_.store(log_level); } |
101 | | |
102 | 0 | SPDLOG_INLINE level::level_enum logger::flush_level() const { |
103 | 0 | return static_cast<level::level_enum>(flush_level_.load(std::memory_order_relaxed)); |
104 | 0 | } |
105 | | |
106 | | // sinks |
107 | 0 | SPDLOG_INLINE const std::vector<sink_ptr> &logger::sinks() const { return sinks_; } |
108 | | |
109 | 0 | SPDLOG_INLINE std::vector<sink_ptr> &logger::sinks() { return sinks_; } |
110 | | |
111 | | // error handler |
112 | 0 | SPDLOG_INLINE void logger::set_error_handler(err_handler handler) { |
113 | 0 | custom_err_handler_ = std::move(handler); |
114 | 0 | } |
115 | | |
116 | | // create new logger with same sinks and configuration. |
117 | 0 | SPDLOG_INLINE std::shared_ptr<logger> logger::clone(std::string logger_name) { |
118 | 0 | auto cloned = std::make_shared<logger>(*this); |
119 | 0 | cloned->name_ = std::move(logger_name); |
120 | 0 | return cloned; |
121 | 0 | } |
122 | | |
123 | | // protected methods |
124 | | SPDLOG_INLINE void logger::log_it_(const spdlog::details::log_msg &log_msg, |
125 | | bool log_enabled, |
126 | 15.1M | bool traceback_enabled) { |
127 | 15.1M | if (log_enabled) { |
128 | 31.7k | sink_it_(log_msg); |
129 | 31.7k | } |
130 | 15.1M | if (traceback_enabled) { |
131 | 15.1M | tracer_.push_back(log_msg); |
132 | 15.1M | } |
133 | 15.1M | } |
134 | | |
135 | 15.1M | SPDLOG_INLINE void logger::sink_it_(const details::log_msg &msg) { |
136 | 15.1M | for (auto &sink : sinks_) { |
137 | 15.1M | if (sink->should_log(msg.level)) { |
138 | 15.1M | SPDLOG_TRY { sink->log(msg); } |
139 | 15.1M | SPDLOG_LOGGER_CATCH(msg.source) |
140 | 15.1M | } |
141 | 15.1M | } |
142 | | |
143 | 15.1M | if (should_flush_(msg)) { |
144 | 0 | flush_(); |
145 | 0 | } |
146 | 15.1M | } |
147 | | |
148 | 0 | SPDLOG_INLINE void logger::flush_() { |
149 | 0 | for (auto &sink : sinks_) { |
150 | 0 | SPDLOG_TRY { sink->flush(); } |
151 | 0 | SPDLOG_LOGGER_CATCH(source_loc()) |
152 | 0 | } |
153 | 0 | } |
154 | | |
155 | 534 | SPDLOG_INLINE void logger::dump_backtrace_() { |
156 | 534 | using details::log_msg; |
157 | 534 | if (tracer_.enabled() && !tracer_.empty()) { |
158 | 532 | sink_it_( |
159 | 532 | log_msg{name(), level::info, "****************** Backtrace Start ******************"}); |
160 | 15.1M | tracer_.foreach_pop([this](const log_msg &msg) { this->sink_it_(msg); }); |
161 | 532 | sink_it_( |
162 | 532 | log_msg{name(), level::info, "****************** Backtrace End ********************"}); |
163 | 532 | } |
164 | 534 | } |
165 | | |
166 | 15.1M | SPDLOG_INLINE bool logger::should_flush_(const details::log_msg &msg) { |
167 | 15.1M | auto flush_level = flush_level_.load(std::memory_order_relaxed); |
168 | 15.1M | return (msg.level >= flush_level) && (msg.level != level::off); |
169 | 15.1M | } |
170 | | |
171 | 12.8k | SPDLOG_INLINE void logger::err_handler_(const std::string &msg) { |
172 | 12.8k | if (custom_err_handler_) { |
173 | 0 | custom_err_handler_(msg); |
174 | 12.8k | } else { |
175 | 12.8k | using std::chrono::system_clock; |
176 | 12.8k | static std::mutex mutex; |
177 | 12.8k | static std::chrono::system_clock::time_point last_report_time; |
178 | 12.8k | static size_t err_counter = 0; |
179 | 12.8k | std::lock_guard<std::mutex> lk{mutex}; |
180 | 12.8k | auto now = system_clock::now(); |
181 | 12.8k | err_counter++; |
182 | 12.8k | if (now - last_report_time < std::chrono::seconds(1)) { |
183 | 12.2k | return; |
184 | 12.2k | } |
185 | 555 | last_report_time = now; |
186 | 555 | auto tm_time = details::os::localtime(system_clock::to_time_t(now)); |
187 | 555 | char date_buf[64]; |
188 | 555 | std::strftime(date_buf, sizeof(date_buf), "%Y-%m-%d %H:%M:%S", &tm_time); |
189 | | #if defined(USING_R) && defined(R_R_H) // if in R environment |
190 | | REprintf("[*** LOG ERROR #%04zu ***] [%s] [%s] %s\n", err_counter, date_buf, name().c_str(), |
191 | | msg.c_str()); |
192 | | #else |
193 | 555 | std::fprintf(stderr, "[*** LOG ERROR #%04zu ***] [%s] [%s] %s\n", err_counter, date_buf, |
194 | 555 | name().c_str(), msg.c_str()); |
195 | 555 | #endif |
196 | 555 | } |
197 | 12.8k | } |
198 | | } // namespace spdlog |