Fix the logger conflict issue

This commit is contained in:
Alexey Rybalchenko 2016-04-27 17:00:27 +02:00
parent 5421922668
commit e039931098
3 changed files with 52 additions and 36 deletions

View File

@ -33,6 +33,14 @@ namespace expr = boost::log::expressions;
namespace sinks = boost::log::sinks;
namespace attrs = boost::log::attributes;
namespace FairMQ
{
namespace Logger
{
std::vector<boost::shared_ptr<sinks::synchronous_sink<sinks::text_ostream_backend>>> sinkList;
}
}
BOOST_LOG_GLOBAL_LOGGER_INIT(global_logger, src::severity_logger_mt)
{
src::severity_logger_mt<custom_severity_level> global_logger;
@ -41,10 +49,11 @@ BOOST_LOG_GLOBAL_LOGGER_INIT(global_logger, src::severity_logger_mt)
return global_logger;
}
void init_log_console(bool color_format)
void init_log_console(bool color/* = true*/)
{
// add a text sink
typedef sinks::synchronous_sink<sinks::text_ostream_backend> text_sink;
logging::core::get()->remove_all_sinks();
// CONSOLE - all severity except error
@ -53,7 +62,7 @@ void init_log_console(bool color_format)
sink->locked_backend()->add_stream(boost::shared_ptr<std::ostream>(&std::clog, empty_deleter_t()));
// specify the format of the log message
if (color_format)
if (color)
{
sink->set_formatter(&init_log_formatter<tag_console>);
}
@ -64,13 +73,14 @@ void init_log_console(bool color_format)
sink->set_filter(severity != SEVERITY_ERROR && severity < SEVERITY_NOLOG);
// add sink to the core
FairMQ::Logger::sinkList.push_back(sink);
logging::core::get()->add_sink(sink);
// CONSOLE - only severity error
boost::shared_ptr<text_sink> sink_error = boost::make_shared<text_sink>();
sink_error->locked_backend()->add_stream(boost::shared_ptr<std::ostream>(&std::cerr, empty_deleter_t()));
if (color_format)
if (color)
{
sink_error->set_formatter(&init_log_formatter<tag_console>);
}
@ -80,14 +90,15 @@ void init_log_console(bool color_format)
}
sink_error->set_filter(severity == SEVERITY_ERROR);
FairMQ::Logger::sinkList.push_back(sink_error);
logging::core::get()->add_sink(sink_error);
}
void reinit_logger(bool color_format)
void reinit_logger(bool color)
{
LOG(NOLOG) << "";
logging::core::get()->remove_all_sinks();
init_log_console(color_format);
init_log_console(color);
}
void init_log_file(const std::string& filename, custom_severity_level threshold, log_op::operation op, const std::string& id)
@ -177,20 +188,25 @@ void set_global_log_level_operation(log_op::operation op, custom_severity_level
{
switch (op)
{
case log_op::operation::EQUAL :
boost::log::core::get()->set_filter(severity == threshold);
case log_op::operation::EQUAL:
FairMQ::Logger::sinkList.at(0)->set_filter(severity == threshold);
// boost::log::core::get()->set_filter(severity == threshold);
break;
case log_op::operation::GREATER_THAN :
boost::log::core::get()->set_filter(severity > threshold);
case log_op::operation::GREATER_THAN:
FairMQ::Logger::sinkList.at(0)->set_filter(severity > threshold);
// boost::log::core::get()->set_filter(severity > threshold);
break;
case log_op::operation::GREATER_EQ_THAN :
boost::log::core::get()->set_filter(severity >= threshold);
case log_op::operation::GREATER_EQ_THAN:
FairMQ::Logger::sinkList.at(0)->set_filter(severity >= threshold);
// boost::log::core::get()->set_filter(severity >= threshold);
break;
case log_op::operation::LESS_THAN :
boost::log::core::get()->set_filter(severity < threshold);
case log_op::operation::LESS_THAN:
FairMQ::Logger::sinkList.at(0)->set_filter(severity < threshold);
// boost::log::core::get()->set_filter(severity < threshold);
break;
case log_op::operation::LESS_EQ_THAN :
boost::log::core::get()->set_filter(severity <= threshold);
case log_op::operation::LESS_EQ_THAN:
FairMQ::Logger::sinkList.at(0)->set_filter(severity <= threshold);
// boost::log::core::get()->set_filter(severity <= threshold);
break;
default:
break;
@ -215,8 +231,8 @@ void init_new_file(const std::string& filename, custom_severity_level threshold,
boost::log::keywords::auto_flush = true
//keywords::time_based_rotation = &is_it_time_to_rotate
);
typedef sinks::synchronous_sink< sinks::text_file_backend > sink_t;
boost::shared_ptr< sink_t > sink(new sink_t(backend));
typedef sinks::synchronous_sink<sinks::text_file_backend> sink_t;
boost::shared_ptr<sink_t> sink(new sink_t(backend));
// sink->set_formatter(&init_file_formatter);

View File

@ -59,8 +59,8 @@ namespace log_op
}
// declaration of the init function for the global logger
void init_log_console(bool color_format = true);
void reinit_logger(bool color_format);
void init_log_console(bool color = true);
void reinit_logger(bool color);
void init_log_file(const std::string& filename,
custom_severity_level threshold = SEVERITY_THRESHOLD,
log_op::operation = log_op::GREATER_EQ_THAN,

View File

@ -61,23 +61,23 @@ int FairMQProgOptions::ParseAll(const int argc, char** argv, bool allowUnregiste
}
}
// // set log level before printing (default is 0 = DEBUG level)
// std::string verbose = GetValue<std::string>("verbose");
// bool color = GetValue<bool>("log-color");
// if (!color)
// {
// reinit_logger(false);
// }
// //SET_LOG_LEVEL(DEBUG);
// if (fSeverityMap.count(verbose))
// {
// set_global_log_level(log_op::operation::GREATER_EQ_THAN, fSeverityMap.at(verbose));
// }
// else
// {
// LOG(ERROR) << " verbosity level '" << verbose << "' unknown, it will be set to DEBUG";
// set_global_log_level(log_op::operation::GREATER_EQ_THAN, fSeverityMap.at("DEBUG"));
// }
// set log level before printing (default is 0 = DEBUG level)
std::string verbosity = GetValue<std::string>("verbose");
bool color = GetValue<bool>("log-color");
if (!color)
{
reinit_logger(false);
}
if (fSeverityMap.count(verbosity))
{
set_global_log_level(log_op::operation::GREATER_EQ_THAN, fSeverityMap.at(verbosity));
}
else
{
LOG(ERROR) << " verbosity level '" << verbosity << "' unknown, it will be set to DEBUG";
set_global_log_level(log_op::operation::GREATER_EQ_THAN, fSeverityMap.at("DEBUG"));
}
PrintOptions();