Photon 1.0.0
Loading...
Searching...
No Matches
logger-inl.h
Go to the documentation of this file.
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/sinks/sink.h>
13
14#include <cstdio>
15
16namespace spdlog
17{
18
19 // public methods
21 : name_(other.name_), sinks_(other.sinks_), level_(other.level_.load(std::memory_order_relaxed)), flush_level_(other.flush_level_.load(std::memory_order_relaxed)), custom_err_handler_(other.custom_err_handler_), tracer_(other.tracer_)
22 {
23 }
24
25 SPDLOG_INLINE logger::logger(logger&& other) SPDLOG_NOEXCEPT : name_(std::move(other.name_)),
26 sinks_(std::move(other.sinks_)),
27 level_(other.level_.load(std::memory_order_relaxed)),
28 flush_level_(other.flush_level_.load(std::memory_order_relaxed)),
29 custom_err_handler_(std::move(other.custom_err_handler_)),
30 tracer_(std::move(other.tracer_))
31
32 {
33 }
34
36 {
37 this->swap(other);
38 return *this;
39 }
40
42 {
43 name_.swap(other.name_);
44 sinks_.swap(other.sinks_);
45
46 // swap level_
47 auto other_level = other.level_.load();
48 auto my_level = level_.exchange(other_level);
49 other.level_.store(my_level);
50
51 // swap flush level_
52 other_level = other.flush_level_.load();
53 my_level = flush_level_.exchange(other_level);
54 other.flush_level_.store(my_level);
55
56 custom_err_handler_.swap(other.custom_err_handler_);
57 std::swap(tracer_, other.tracer_);
58 }
59
61 {
62 a.swap(b);
63 }
64
66 {
67 level_.store(log_level);
68 }
69
71 {
72 return static_cast<level::level_enum>(level_.load(std::memory_order_relaxed));
73 }
74
75 SPDLOG_INLINE const std::string& logger::name() const
76 {
77 return name_;
78 }
79
80 // set formatting for the sinks in this logger.
81 // each sink will get a separate instance of the formatter object.
82 SPDLOG_INLINE void logger::set_formatter(std::unique_ptr<formatter> f)
83 {
84 for (auto it = sinks_.begin(); it != sinks_.end(); ++it)
85 {
86 if (std::next(it) == sinks_.end())
87 {
88 // last element - we can be move it.
89 (*it)->set_formatter(std::move(f));
90 break; // to prevent clang-tidy warning
91 }
92 else
93 {
94 (*it)->set_formatter(f->clone());
95 }
96 }
97 }
98
99 SPDLOG_INLINE void logger::set_pattern(std::string pattern, pattern_time_type time_type)
100 {
101 auto new_formatter = details::make_unique<pattern_formatter>(std::move(pattern), time_type);
102 set_formatter(std::move(new_formatter));
103 }
104
105 // create new backtrace sink and move to it all our child sinks
107 {
108 tracer_.enable(n_messages);
109 }
110
111 // restore orig sinks and level and delete the backtrace sink
116
121
122 // flush functions
124 {
125 flush_();
126 }
127
129 {
130 flush_level_.store(log_level);
131 }
132
134 {
135 return static_cast<level::level_enum>(flush_level_.load(std::memory_order_relaxed));
136 }
137
138 // sinks
139 SPDLOG_INLINE const std::vector<sink_ptr>& logger::sinks() const
140 {
141 return sinks_;
142 }
143
144 SPDLOG_INLINE std::vector<sink_ptr>& logger::sinks()
145 {
146 return sinks_;
147 }
148
149 // error handler
151 {
152 custom_err_handler_ = std::move(handler);
153 }
154
155 // create new logger with same sinks and configuration.
156 SPDLOG_INLINE std::shared_ptr<logger> logger::clone(std::string logger_name)
157 {
158 auto cloned = std::make_shared<logger>(*this);
159 cloned->name_ = std::move(logger_name);
160 return cloned;
161 }
162
163 // protected methods
164 SPDLOG_INLINE void logger::log_it_(const spdlog::details::log_msg& log_msg, bool log_enabled, bool traceback_enabled)
165 {
166 if (log_enabled)
167 {
168 sink_it_(log_msg);
169 }
170 if (traceback_enabled)
171 {
172 tracer_.push_back(log_msg);
173 }
174 }
175
177 {
178 for (auto& sink : sinks_)
179 {
180 if (sink->should_log(msg.level))
181 {
183 {
184 sink->log(msg);
185 }
187 }
188 }
189
190 if (should_flush_(msg))
191 {
192 flush_();
193 }
194 }
195
197 {
198 for (auto& sink : sinks_)
199 {
201 {
202 sink->flush();
203 }
205 }
206 }
207
209 {
210 using details::log_msg;
211 if (tracer_.enabled() && !tracer_.empty())
212 {
213 sink_it_(log_msg{name(), level::info, "****************** Backtrace Start ******************"});
214 tracer_.foreach_pop([this](const log_msg& msg) { this->sink_it_(msg); });
215 sink_it_(log_msg{name(), level::info, "****************** Backtrace End ********************"});
216 }
217 }
218
220 {
221 auto flush_level = flush_level_.load(std::memory_order_relaxed);
222 return (msg.level >= flush_level) && (msg.level != level::off);
223 }
224
225 SPDLOG_INLINE void logger::err_handler_(const std::string& msg)
226 {
228 {
230 }
231 else
232 {
233 using std::chrono::system_clock;
234 static std::mutex mutex;
235 static std::chrono::system_clock::time_point last_report_time;
236 static size_t err_counter = 0;
237 std::lock_guard<std::mutex> lk{mutex};
238 auto now = system_clock::now();
239 err_counter++;
240 if (now - last_report_time < std::chrono::seconds(1))
241 {
242 return;
243 }
244 last_report_time = now;
245 auto tm_time = details::os::localtime(system_clock::to_time_t(now));
246 char date_buf[64];
247 std::strftime(date_buf, sizeof(date_buf), "%Y-%m-%d %H:%M:%S", &tm_time);
248#if defined(USING_R) && defined(R_R_H) // if in R environment
249 REprintf("[*** LOG ERROR #%04zu ***] [%s] [%s] {%s}\n", err_counter, date_buf, name().c_str(), msg.c_str());
250#else
251 std::fprintf(stderr, "[*** LOG ERROR #%04zu ***] [%s] [%s] {%s}\n", err_counter, date_buf, name().c_str(), msg.c_str());
252#endif
253 }
254 }
255} // namespace spdlog
void disable()
Definition backtracer-inl.h:42
void push_back(const log_msg &msg)
Definition backtracer-inl.h:53
bool enabled() const
Definition backtracer-inl.h:48
void enable(size_t size)
Definition backtracer-inl.h:35
bool empty() const
Definition backtracer-inl.h:59
void foreach_pop(std::function< void(const details::log_msg &)> fun)
Definition backtracer-inl.h:66
Definition logger.h:56
level::level_enum level() const
Definition logger-inl.h:70
void set_formatter(std::unique_ptr< formatter > f)
Definition logger-inl.h:82
void set_pattern(std::string pattern, pattern_time_type time_type=pattern_time_type::local)
Definition logger-inl.h:99
std::vector< sink_ptr > sinks_
Definition logger.h:355
err_handler custom_err_handler_
Definition logger.h:358
virtual void flush_()
Definition logger-inl.h:196
logger(std::string name)
Definition logger.h:59
level::level_enum flush_level() const
Definition logger-inl.h:133
void flush_on(level::level_enum log_level)
Definition logger-inl.h:128
spdlog::level_t flush_level_
Definition logger.h:357
void err_handler_(const std::string &msg)
Definition logger-inl.h:225
const std::vector< sink_ptr > & sinks() const
Definition logger-inl.h:139
std::string name_
Definition logger.h:354
void enable_backtrace(size_t n_messages)
Definition logger-inl.h:106
virtual void sink_it_(const details::log_msg &msg)
Definition logger-inl.h:176
virtual std::shared_ptr< logger > clone(std::string logger_name)
Definition logger-inl.h:156
details::backtracer tracer_
Definition logger.h:359
const std::string & name() const
Definition logger-inl.h:75
void set_error_handler(err_handler)
Definition logger-inl.h:150
logger & operator=(logger other) SPDLOG_NOEXCEPT
Definition logger-inl.h:35
bool should_flush_(const details::log_msg &msg)
Definition logger-inl.h:219
void dump_backtrace()
Definition logger-inl.h:117
void swap(spdlog::logger &other) SPDLOG_NOEXCEPT
Definition logger-inl.h:41
void flush()
Definition logger-inl.h:123
void set_level(level::level_enum log_level)
Definition logger-inl.h:65
void dump_backtrace_()
Definition logger-inl.h:208
spdlog::level_t level_
Definition logger.h:356
void disable_backtrace()
Definition logger-inl.h:112
void log_it_(const details::log_msg &log_msg, bool log_enabled, bool traceback_enabled)
Definition logger-inl.h:164
#define SPDLOG_NOEXCEPT
Definition common.h:69
#define SPDLOG_TRY
Definition common.h:107
#define SPDLOG_INLINE
Definition common.h:47
#define SPDLOG_LOGGER_CATCH(location)
Definition logger.h:31
SPDLOG_INLINE std::tm localtime() SPDLOG_NOEXCEPT
Definition os-inl.h:102
level_enum
Definition common.h:233
@ info
Definition common.h:236
@ off
Definition common.h:240
Definition async.h:26
std::function< void(const std::string &err_msg)> err_handler
Definition common.h:138
SPDLOG_INLINE void swap(logger &a, logger &b)
Definition logger-inl.h:60
pattern_time_type
Definition common.h:289
Definition uuid.h:926
Definition log_msg.h:14
level::level_enum level
Definition log_msg.h:23
source_loc source
Definition log_msg.h:31
Definition common.h:312
b
Definition tag_strings.h:61
a
Definition tag_strings.h:43