1#include "Framework/Logger.h"
9#include <boost/core/null_deleter.hpp>
10#include <boost/log/utility/setup/common_attributes.hpp>
25level convertLevel(
int iLvl) {
33logger makeLogger(
const std::string& name) {
34 logger lg(log::keywords::channel = name);
35 return boost::move(lg);
39const T& safe_extract(log::attribute_value attr) {
40 static const T empty_value = {};
41 auto attr_val = log::extract<T>(attr);
57 level fallback_level_;
58 std::unordered_map<std::string, level> custom_levels_;
61 Filter(level fallback, std::unordered_map<std::string, level> custom)
62 : fallback_level_{fallback}, custom_levels_{custom} {}
64 bool operator()(log::attribute_value_set
const& attrs) {
65 const std::string& channel{safe_extract<std::string>(attrs[
"Channel"])};
66 const level& msg_level{safe_extract<level>(attrs[
"Severity"])};
67 auto it = custom_levels_.find(channel);
68 if (it != custom_levels_.end()) {
69 return msg_level >= it->second;
71 return msg_level >= fallback_level_;
77 typedef sinks::text_ostream_backend ourSinkBack_t;
78 typedef sinks::synchronous_sink<ourSinkBack_t> ourSinkFront_t;
80 level fileLevel{convertLevel(p.
getParameter<
int>(
"fileLevel", 0))};
81 std::string filePath{p.
getParameter<std::string>(
"filePath",
"")};
83 level termLevel{convertLevel(p.
getParameter<
int>(
"termLevel", 4))};
85 p.
getParameter<std::vector<framework::config::Parameters>>(
"logRules",
87 std::unordered_map<std::string, level> custom_levels;
88 for (
const auto& logRule : logRules) {
89 custom_levels[logRule.getParameter<std::string>(
"name")] =
90 convertLevel(logRule.getParameter<
int>(
"level"));
98 log::add_common_attributes();
101 boost::shared_ptr<log::core> core = log::core::get();
105 if (not filePath.empty()) {
106 boost::shared_ptr<ourSinkBack_t> fileBack =
107 boost::make_shared<ourSinkBack_t>();
108 fileBack->add_stream(boost::make_shared<std::ofstream>(filePath));
110 boost::shared_ptr<ourSinkFront_t> fileSink =
111 boost::make_shared<ourSinkFront_t>(fileBack);
114 fileSink->set_filter(Filter(fileLevel, custom_levels));
115 fileSink->set_formatter(
116 [](
const log::record_view& view, log::formatting_ostream& os) {
119 core->add_sink(fileSink);
123 boost::shared_ptr<ourSinkBack_t> termBack =
124 boost::make_shared<ourSinkBack_t>();
125 termBack->add_stream(boost::shared_ptr<std::ostream>(
127 boost::null_deleter()
130 termBack->auto_flush(
true);
132 boost::shared_ptr<ourSinkFront_t> termSink =
133 boost::make_shared<ourSinkFront_t>(termBack);
136 termSink->set_filter(Filter(termLevel, custom_levels));
138 termSink->set_formatter(
139 [](
const log::record_view& view, log::formatting_ostream& os) {
142 core->add_sink(termSink);
150 log::core::get()->remove_all_sinks();
157 return the_formatter;
163 log::formatting_ostream& os) {
164 os <<
"[ " << log::extract<std::string>(
"Channel", view) <<
" ] "
165 << event_number_ <<
" ";
170 const level& msg_level{safe_extract<level>(view[
"Severity"])};
197 os <<
": " << view[log::expressions::smessage];
Class encapsulating parameters for configuring a processor.
T getParameter(const std::string &name) const
Retrieve the parameter of the given name.
Our filter implementation aligning with Boost.Log.
All classes in the ldmx-sw project use this namespace.