30namespace sinks = boost::log::sinks;
31namespace src = boost::log::sources;
32namespace expr = boost::log::expressions;
33namespace attrs = boost::log::attributes;
34namespace keywords = boost::log::keywords;
38 using Severity = logging::trivial::severity_level;
39 using ChannelLogger = src::severity_channel_logger_mt<Severity, std::string>;
40 using ConsoleSink = sinks::synchronous_sink<sinks::text_ostream_backend>;
42 LogLevel fromBoostSeverity(Severity level);
44 class CallbackSinkBackend
45 :
public sinks::basic_formatted_sink_backend<char, sinks::synchronized_feeding> {
48 : callback(std::move(callback))
52 void consume(
const logging::record_view& rec,
const string_type& formatted)
57 const auto severityRef = rec[logging::trivial::severity];
58 const auto channelRef = rec.attribute_values()[
"Channel"].extract<std::string>();
59 const auto rawMessageRef = rec.attribute_values()[
"Message"].extract<std::string>();
62 .level = severityRef ? fromBoostSeverity(*severityRef) :
LogLevel::Info,
63 .category = channelRef ? *channelRef : std::string{},
64 .message = rawMessageRef ? *rawMessageRef : formatted,
65 .formatted = formatted,
75 using CallbackFrontendSink = sinks::synchronous_sink<CallbackSinkBackend>;
77 Severity toBoostSeverity(
const LogLevel level)
88 return logging::trivial::info;
91 LogLevel fromBoostSeverity(
const Severity level)
105 struct LoggingState {
107 bool initialized =
false;
108 bool commonAttrsAdded =
false;
110 boost::shared_ptr<ConsoleSink> consoleSink;
111 std::map<Logger::SinkHandle, boost::shared_ptr<logging::sinks::sink>> callbackSinks;
112 std::atomic<Logger::SinkHandle> nextSinkHandle{1};
115 LoggingState& state()
117 static LoggingState value;
121 const char* toLevelString(
const LogLevel level)
137 return expr::stream <<
"["
138 << expr::format_date_time<boost::posix_time::ptime>(
139 "TimeStamp",
"%Y-%m-%d %H:%M:%S.%f")
140 <<
"]" <<
" [" << logging::trivial::severity <<
"]" <<
" ["
141 << expr::attr<std::string>(
"Channel") <<
"] " << expr::smessage;
146 logging::core::get()->set_filter(logging::trivial::severity
147 >= toBoostSeverity(state().minimumLevel));
150 void ensureInitializedLocked(LoggingState& loggingState)
152 if (loggingState.initialized)
155 if (!loggingState.commonAttrsAdded) {
156 logging::add_common_attributes();
157 loggingState.commonAttrsAdded =
true;
161 loggingState.initialized =
true;
164 void writeRecord(
const LogLevel level, std::string_view category,
165 std::string_view message)
167 ChannelLogger logger(keywords::channel = std::string(category));
168 BOOST_LOG_SEV(logger, toBoostSeverity(level)) << message;
175 return stream << toLevelString(level);
182 auto& loggingState = state();
183 const std::lock_guard lock(loggingState.mutex);
184 ensureInitializedLocked(loggingState);
189 auto& loggingState = state();
190 const std::lock_guard lock(loggingState.mutex);
192 auto&
core = *logging::core::get();
193 if (loggingState.consoleSink) {
194 core.remove_sink(loggingState.consoleSink);
195 loggingState.consoleSink->flush();
196 loggingState.consoleSink.reset();
199 for (
auto& [handle, sink] : loggingState.callbackSinks) {
201 core.remove_sink(sink);
204 loggingState.callbackSinks.clear();
205 loggingState.initialized =
false;
210 auto& loggingState = state();
211 const std::lock_guard lock(loggingState.mutex);
212 ensureInitializedLocked(loggingState);
214 auto&
core = *logging::core::get();
215 if (loggingState.consoleSink) {
216 core.remove_sink(loggingState.consoleSink);
217 loggingState.consoleSink->flush();
218 loggingState.consoleSink.reset();
221 auto backend = boost::make_shared<sinks::text_ostream_backend>();
222 backend->add_stream(boost::shared_ptr<std::ostream>(&stream, boost::null_deleter{}));
223 backend->auto_flush(
true);
225 auto sink = boost::make_shared<ConsoleSink>(backend);
226 sink->set_formatter(makeFormatter());
228 loggingState.consoleSink = sink;
233 auto& loggingState = state();
234 const std::lock_guard lock(loggingState.mutex);
235 loggingState.minimumLevel = level;
236 ensureInitializedLocked(loggingState);
242 auto& loggingState = state();
243 const std::lock_guard lock(loggingState.mutex);
244 ensureInitializedLocked(loggingState);
246 auto backend = boost::make_shared<CallbackSinkBackend>(std::move(callback));
247 auto sink = boost::make_shared<CallbackFrontendSink>(backend);
248 sink->set_formatter(makeFormatter());
250 logging::core::get()->add_sink(sink);
253 loggingState.nextSinkHandle.fetch_add(1, std::memory_order_relaxed);
254 loggingState.callbackSinks.emplace(handle, sink);
260 auto& loggingState = state();
261 const std::lock_guard lock(loggingState.mutex);
263 const auto it = loggingState.callbackSinks.find(handle);
264 if (it == loggingState.callbackSinks.end())
267 logging::core::get()->remove_sink(it->second);
269 loggingState.callbackSinks.erase(it);
333 std::string_view message)
336 writeRecord(level,
category, message);