mirror of
https://github.com/FairRootGroup/FairMQ.git
synced 2025-10-13 00:31:14 +00:00
Refactor FairMQLogger
This commit is contained in:
parent
6ecd0e9085
commit
6d7009b331
|
@ -84,7 +84,6 @@ set(FAIRMQ_HEADER_FILES
|
|||
devices/FairMQSink.h
|
||||
devices/FairMQSplitter.h
|
||||
logger/logger.h
|
||||
logger/logger_def.h
|
||||
options/FairMQEventManager.h
|
||||
options/FairMQParser.h
|
||||
options/FairMQProgOptions.h
|
||||
|
|
|
@ -12,9 +12,10 @@
|
|||
* @author D. Klein, A. Rybalchenko
|
||||
*/
|
||||
|
||||
#include "FairMQLogger.h"
|
||||
|
||||
#include <sys/time.h>
|
||||
#include <ctime>
|
||||
#include "FairMQLogger.h"
|
||||
|
||||
timestamp_t get_timestamp()
|
||||
{
|
||||
|
|
|
@ -17,34 +17,6 @@
|
|||
|
||||
#include "logger/logger.h"
|
||||
|
||||
|
||||
// FairMQLogger helper macros
|
||||
/*
|
||||
Definition :
|
||||
|
||||
#define LOG(severity) BOOST_LOG_SEV(global_logger::get(),fairmq::severity)
|
||||
#define SET_LOG_CONSOLE_LEVEL(loglevel) DefaultConsoleSetFilter(fairmq::loglevel)
|
||||
#define ADD_LOG_FILESINK(filename,loglevel) DefaultAddFileSink(filename, fairmq::loglevel)
|
||||
|
||||
enum severity_level
|
||||
{
|
||||
TRACE,
|
||||
DEBUG,
|
||||
RESULTS,
|
||||
INFO,
|
||||
STATE,
|
||||
WARN,
|
||||
ERROR,
|
||||
NOLOG
|
||||
};
|
||||
|
||||
Use :
|
||||
|
||||
LOG(DEBUG)<<"Hello World";
|
||||
SET_LOG_CONSOLE_LEVEL(INFO); // => Print severity >= INFO to console
|
||||
ADD_LOG_FILESINK(filename,ERROR); // => Print severity >= ERROR to file (extension is added)
|
||||
*/
|
||||
|
||||
using timestamp_t = unsigned long long;
|
||||
|
||||
timestamp_t get_timestamp();
|
||||
|
|
|
@ -1,17 +1,32 @@
|
|||
/********************************************************************************
|
||||
* Copyright (C) 2014 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
|
||||
* *
|
||||
* This software is distributed under the terms of the *
|
||||
* GNU Lesser General Public Licence version 3 (LGPL) version 3, *
|
||||
* This software is distributed under the terms of the *
|
||||
* GNU Lesser General Public Licence version 3 (LGPL) version 3, *
|
||||
* copied verbatim in the file "LICENSE" *
|
||||
********************************************************************************/
|
||||
#include "logger.h"
|
||||
|
||||
#include <boost/version.hpp>
|
||||
|
||||
#include <boost/log/core/core.hpp>
|
||||
#include <boost/log/expressions/formatters/date_time.hpp>
|
||||
#include <boost/log/sinks/text_ostream_backend.hpp>
|
||||
#include <boost/date_time/posix_time/posix_time.hpp>
|
||||
|
||||
#include <boost/log/support/date_time.hpp>
|
||||
|
||||
#include <boost/log/utility/setup/common_attributes.hpp>
|
||||
#include <boost/log/utility/manipulators/to_log.hpp>
|
||||
#include <boost/log/utility/formatting_ostream.hpp>
|
||||
|
||||
#include <boost/log/sinks/text_ostream_backend.hpp>
|
||||
#include <boost/log/sinks/text_file_backend.hpp>
|
||||
#include <boost/log/sinks/sync_frontend.hpp>
|
||||
#include <boost/log/sinks/basic_sink_frontend.hpp>
|
||||
|
||||
#include <boost/log/expressions.hpp>
|
||||
#include <boost/log/expressions/attr.hpp>
|
||||
#include <boost/log/expressions/attr_fwd.hpp>
|
||||
#include <boost/log/expressions/keyword.hpp>
|
||||
|
||||
#if BOOST_VERSION < 105600
|
||||
#include "fairroot_null_deleter.h"
|
||||
|
@ -26,51 +41,156 @@ using empty_deleter_t = boost::null_deleter;
|
|||
|
||||
#include <fstream>
|
||||
#include <ostream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <sstream>
|
||||
|
||||
namespace logging = boost::log;
|
||||
namespace src = boost::log::sources;
|
||||
namespace expr = boost::log::expressions;
|
||||
namespace sinks = boost::log::sinks;
|
||||
namespace attrs = boost::log::attributes;
|
||||
using namespace std;
|
||||
namespace blog = boost::log;
|
||||
namespace bptime = boost::posix_time;
|
||||
|
||||
BOOST_LOG_GLOBAL_LOGGER_INIT(global_logger, src::severity_logger_mt)
|
||||
struct TagConsole;
|
||||
struct TagFile;
|
||||
|
||||
BOOST_LOG_ATTRIBUTE_KEYWORD(fairmq_logger_timestamp, "TimeStamp", bptime::ptime)
|
||||
BOOST_LOG_ATTRIBUTE_KEYWORD(severity, "Severity", fair::mq::logger::SeverityLevel)
|
||||
|
||||
BOOST_LOG_GLOBAL_LOGGER_INIT(global_logger, blog::sources::severity_logger_mt)
|
||||
{
|
||||
src::severity_logger_mt<custom_severity_level> global_logger;
|
||||
global_logger.add_attribute("TimeStamp", attrs::local_clock());
|
||||
DefaultConsoleInit();
|
||||
return global_logger;
|
||||
blog::sources::severity_logger_mt<fair::mq::logger::SeverityLevel> globalLogger;
|
||||
globalLogger.add_attribute("TimeStamp", blog::attributes::local_clock());
|
||||
fair::mq::logger::DefaultConsoleInit();
|
||||
return globalLogger;
|
||||
}
|
||||
|
||||
namespace FairMQ
|
||||
namespace fair
|
||||
{
|
||||
namespace Logger
|
||||
namespace mq
|
||||
{
|
||||
std::vector<boost::shared_ptr<boost::log::sinks::basic_sink_frontend>> sinkList;// global var
|
||||
} // end Logger namespace
|
||||
} // end FairMQ namespace
|
||||
namespace logger
|
||||
{
|
||||
|
||||
vector<boost::shared_ptr<blog::sinks::basic_sink_frontend>> sinkList;// global var
|
||||
|
||||
void InitConsoleLogFormatter(const blog::record_view& view, blog::formatting_ostream& os)
|
||||
{
|
||||
os << "[\033[01;36m";
|
||||
|
||||
auto dateTimeFormatter = blog::expressions::stream << blog::expressions::format_date_time<bptime::ptime>("TimeStamp", "%H:%M:%S");
|
||||
dateTimeFormatter(view, os);
|
||||
|
||||
os << "\033[0m][" << view.attribute_values()["Severity"].extract<SeverityLevel, TagConsole>() << "] " << view.attribute_values()["Message"].extract<string>();
|
||||
}
|
||||
|
||||
void InitFileLogFormatter(const blog::record_view& view, blog::formatting_ostream& os)
|
||||
{
|
||||
os << "[";
|
||||
|
||||
auto dateTimeFormatter = blog::expressions::stream << blog::expressions::format_date_time<bptime::ptime>("TimeStamp", "%H:%M:%S");
|
||||
dateTimeFormatter(view, os);
|
||||
|
||||
os << "][" << view.attribute_values()["Severity"].extract<SeverityLevel, TagFile>() << "] " << view.attribute_values()["Message"].extract<string>();
|
||||
}
|
||||
|
||||
|
||||
// helper function to format in color console output
|
||||
string writeIn(const string& textInBold, color::Code color)
|
||||
{
|
||||
ostringstream os;
|
||||
os << "\033[01;" << color << "m" << textInBold << "\033[0m";
|
||||
return os.str();
|
||||
}
|
||||
|
||||
// overload operator for console output
|
||||
blog::formatting_ostream& operator<<(blog::formatting_ostream& stream, blog::to_log_manip<SeverityLevel, TagConsole> const& manip)
|
||||
{
|
||||
SeverityLevel level = manip.get();
|
||||
size_t idx = static_cast<size_t>(level);
|
||||
if (idx < gLogSeverityLevelString.size())
|
||||
{
|
||||
switch (level)
|
||||
{
|
||||
case SeverityLevel::TRACE:
|
||||
stream << writeIn(gLogSeverityLevelString.at(idx), color::FG_BLUE);
|
||||
break;
|
||||
|
||||
case SeverityLevel::DEBUG:
|
||||
stream << writeIn(gLogSeverityLevelString.at(idx), color::FG_BLUE);
|
||||
break;
|
||||
|
||||
case SeverityLevel::RESULTS:
|
||||
stream << writeIn(gLogSeverityLevelString.at(idx), color::FG_MAGENTA);
|
||||
break;
|
||||
|
||||
case SeverityLevel::INFO:
|
||||
stream << writeIn(gLogSeverityLevelString.at(idx), color::FG_GREEN);
|
||||
break;
|
||||
|
||||
case SeverityLevel::WARN:
|
||||
stream << writeIn(gLogSeverityLevelString.at(idx), color::FG_YELLOW);
|
||||
break;
|
||||
|
||||
case SeverityLevel::STATE:
|
||||
stream << writeIn(gLogSeverityLevelString.at(idx), color::FG_MAGENTA);
|
||||
break;
|
||||
|
||||
case SeverityLevel::ERROR:
|
||||
stream << writeIn(gLogSeverityLevelString.at(idx), color::FG_RED);
|
||||
break;
|
||||
|
||||
case SeverityLevel::NOLOG:
|
||||
stream << writeIn(gLogSeverityLevelString.at(idx), color::FG_DEFAULT);
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
stream << writeIn("Unknown log level ", color::FG_RED) << "(int level = " << static_cast<int>(level) << ")";
|
||||
}
|
||||
return stream;
|
||||
}
|
||||
|
||||
// overload operator for file output
|
||||
blog::formatting_ostream& operator<<(blog::formatting_ostream& stream, blog::to_log_manip<SeverityLevel, TagFile> const& manip)
|
||||
{
|
||||
SeverityLevel level = manip.get();
|
||||
size_t idx = static_cast<size_t>(level);
|
||||
if (idx < gLogSeverityLevelString.size())
|
||||
{
|
||||
stream << gLogSeverityLevelString.at(idx);
|
||||
}
|
||||
else
|
||||
{
|
||||
stream << writeIn("Unknown log level ", color::FG_RED) << "(int level = " << static_cast<int>(level) << ")";
|
||||
}
|
||||
return stream;
|
||||
}
|
||||
|
||||
void RemoveRegisteredSinks()
|
||||
{
|
||||
if (FairMQ::Logger::sinkList.size() > 0)
|
||||
if (sinkList.size() > 0)
|
||||
{
|
||||
for (const auto& sink : FairMQ::Logger::sinkList)
|
||||
for (const auto& sink : sinkList)
|
||||
{
|
||||
logging::core::get()->remove_sink(sink);
|
||||
blog::core::get()->remove_sink(sink);
|
||||
}
|
||||
FairMQ::Logger::sinkList.clear();
|
||||
sinkList.clear();
|
||||
}
|
||||
}
|
||||
|
||||
void reinit_logger(bool color, const std::string& filename, custom_severity_level threshold)
|
||||
void ReinitLogger(bool color, const string& filename, SeverityLevel level)
|
||||
{
|
||||
BOOST_LOG_SEV(global_logger::get(), custom_severity_level::NOLOG) << "";
|
||||
BOOST_LOG_SEV(global_logger::get(), SeverityLevel::NOLOG) << "";
|
||||
RemoveRegisteredSinks();
|
||||
DefaultConsoleInit(color);
|
||||
if (threshold != SEVERITY_NOLOG)
|
||||
if (level != SeverityLevel::NOLOG)
|
||||
{
|
||||
if (!filename.empty())
|
||||
{
|
||||
DefaultAddFileSink(filename, threshold);
|
||||
DefaultAddFileSink(filename, level);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -79,64 +199,64 @@ void reinit_logger(bool color, const std::string& filename, custom_severity_leve
|
|||
void DefaultConsoleInit(bool color/* = true*/)
|
||||
{
|
||||
// add a text sink
|
||||
using text_sink = sinks::synchronous_sink<sinks::text_ostream_backend>;
|
||||
using TextSink = blog::sinks::synchronous_sink<blog::sinks::text_ostream_backend>;
|
||||
|
||||
RemoveRegisteredSinks();
|
||||
|
||||
// CONSOLE - all severity except error
|
||||
boost::shared_ptr<text_sink> sink = boost::make_shared<text_sink>();
|
||||
boost::shared_ptr<TextSink> sink = boost::make_shared<TextSink>();
|
||||
// add "console" output stream to our sink
|
||||
sink->locked_backend()->add_stream(boost::shared_ptr<std::ostream>(&std::clog, empty_deleter_t()));
|
||||
sink->locked_backend()->add_stream(boost::shared_ptr<ostream>(&clog, empty_deleter_t()));
|
||||
|
||||
// specify the format of the log message
|
||||
// specify the format of the log message
|
||||
if (color)
|
||||
{
|
||||
sink->set_formatter(&InitLogFormatter<tag_console>);
|
||||
sink->set_formatter(&InitConsoleLogFormatter);
|
||||
}
|
||||
else
|
||||
{
|
||||
sink->set_formatter(&InitLogFormatter<tag_file>);
|
||||
sink->set_formatter(&InitFileLogFormatter);
|
||||
}
|
||||
|
||||
sink->set_filter(severity != SEVERITY_ERROR && severity < SEVERITY_NOLOG);
|
||||
sink->set_filter(severity != SeverityLevel::ERROR && severity < SeverityLevel::NOLOG);
|
||||
// add sink to the core
|
||||
FairMQ::Logger::sinkList.push_back(sink);
|
||||
logging::core::get()->add_sink(sink);
|
||||
sinkList.push_back(sink);
|
||||
blog::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()));
|
||||
boost::shared_ptr<TextSink> sinkError = boost::make_shared<TextSink>();
|
||||
sinkError->locked_backend()->add_stream(boost::shared_ptr<ostream>(&cerr, empty_deleter_t()));
|
||||
|
||||
if (color)
|
||||
{
|
||||
sink_error->set_formatter(&InitLogFormatter<tag_console>);
|
||||
sinkError->set_formatter(&InitConsoleLogFormatter);
|
||||
}
|
||||
else
|
||||
{
|
||||
sink_error->set_formatter(&InitLogFormatter<tag_file>);
|
||||
sinkError->set_formatter(&InitFileLogFormatter);
|
||||
}
|
||||
|
||||
sink_error->set_filter(severity == SEVERITY_ERROR);
|
||||
FairMQ::Logger::sinkList.push_back(sink_error);
|
||||
logging::core::get()->add_sink(sink_error);
|
||||
sinkError->set_filter(severity == SeverityLevel::ERROR);
|
||||
sinkList.push_back(sinkError);
|
||||
blog::core::get()->add_sink(sinkError);
|
||||
}
|
||||
|
||||
int DefaultConsoleSetFilter(custom_severity_level threshold)
|
||||
int DefaultConsoleSetFilter(SeverityLevel level)
|
||||
{
|
||||
if (FairMQ::Logger::sinkList.size()>=2)
|
||||
if (sinkList.size() >= 2)
|
||||
{
|
||||
FairMQ::Logger::sinkList.at(0)->set_filter([threshold](const boost::log::attribute_value_set& attr_set)
|
||||
sinkList.at(0)->set_filter([level](const blog::attribute_value_set& attrSet)
|
||||
{
|
||||
auto sev = attr_set["Severity"].extract<custom_severity_level>();
|
||||
auto mainConsoleSinkCondition = (sev != SEVERITY_ERROR) && (sev < SEVERITY_NOLOG);
|
||||
return mainConsoleSinkCondition && (sev>=threshold);
|
||||
auto sev = attrSet["Severity"].extract<SeverityLevel>();
|
||||
auto mainConsoleSinkCondition = (sev != SeverityLevel::ERROR) && (sev < SeverityLevel::NOLOG);
|
||||
return mainConsoleSinkCondition && (sev >= level);
|
||||
});
|
||||
|
||||
FairMQ::Logger::sinkList.at(1)->set_filter([threshold](const boost::log::attribute_value_set& attr_set)
|
||||
sinkList.at(1)->set_filter([level](const blog::attribute_value_set& attrSet)
|
||||
{
|
||||
auto sev = attr_set["Severity"].extract<custom_severity_level>();
|
||||
auto errorConsoleSinkCondition = sev == SEVERITY_ERROR;
|
||||
return errorConsoleSinkCondition && (sev>=threshold);
|
||||
auto sev = attrSet["Severity"].extract<SeverityLevel>();
|
||||
auto errorConsoleSinkCondition = sev == SeverityLevel::ERROR;
|
||||
return errorConsoleSinkCondition && (sev >= level);
|
||||
});
|
||||
return 0;
|
||||
}
|
||||
|
@ -149,26 +269,46 @@ int DefaultConsoleSetFilter(custom_severity_level threshold)
|
|||
}
|
||||
|
||||
// file sink related functions
|
||||
void DefaultAddFileSink(const std::string& filename, custom_severity_level threshold)
|
||||
void DefaultAddFileSink(const string& filename, SeverityLevel level)
|
||||
{
|
||||
// add a text sink
|
||||
std::string formatted_filename(filename);
|
||||
formatted_filename += "_%Y-%m-%d_%H-%M-%S.%N.log";
|
||||
AddFileSink([threshold](const boost::log::attribute_value_set& attr_set)
|
||||
{
|
||||
auto sev = attr_set["Severity"].extract<custom_severity_level>();
|
||||
return (sev >= threshold) && (sev < SEVERITY_NOLOG);
|
||||
},
|
||||
boost::log::keywords::file_name = formatted_filename,
|
||||
boost::log::keywords::rotation_size = 10 * 1024 * 1024,
|
||||
string formattedFilename(filename);
|
||||
formattedFilename += "_%Y-%m-%d_%H-%M-%S.%N.log";
|
||||
|
||||
// add a text sink
|
||||
using SinkBackend = blog::sinks::text_file_backend;
|
||||
using Sink = blog::sinks::synchronous_sink<SinkBackend>;
|
||||
|
||||
boost::shared_ptr<SinkBackend> backend = boost::make_shared<SinkBackend>(
|
||||
blog::keywords::file_name = formattedFilename,
|
||||
blog::keywords::rotation_size = 10 * 1024 * 1024,
|
||||
// rotate at midnight every day
|
||||
boost::log::keywords::time_based_rotation = boost::log::sinks::file::rotation_at_time_point(0, 0, 0),
|
||||
blog::keywords::time_based_rotation = blog::sinks::file::rotation_at_time_point(0, 0, 0),
|
||||
// log collector,
|
||||
// -- maximum total size of the stored log files is 1GB.
|
||||
// -- minimum free space on the drive is 2GB
|
||||
boost::log::keywords::max_size = 1000 * 1024 * 1024,
|
||||
boost::log::keywords::min_free_space = 2000 * 1024 * 1024,
|
||||
boost::log::keywords::auto_flush = true
|
||||
//keywords::time_based_rotation = &is_it_time_to_rotate
|
||||
);
|
||||
blog::keywords::max_size = 1000 * 1024 * 1024,
|
||||
blog::keywords::min_free_space = 2000 * 1024 * 1024,
|
||||
blog::keywords::auto_flush = true
|
||||
//keywords::time_based_rotation = &is_it_time_to_rotate
|
||||
);
|
||||
boost::shared_ptr<Sink> sink = boost::make_shared<Sink>(backend);
|
||||
|
||||
// specify the format of the log message
|
||||
sink->set_formatter(&InitFileLogFormatter);
|
||||
|
||||
// forward lambda for setting the filter
|
||||
sink->set_filter([level](const blog::attribute_value_set& attrSet)
|
||||
{
|
||||
auto sev = attrSet["Severity"].extract<SeverityLevel>();
|
||||
return (sev >= level) && (sev < SeverityLevel::NOLOG);
|
||||
});
|
||||
|
||||
// add file sinks to core and list
|
||||
blog::core::get()->add_sink(sink);
|
||||
sinkList.push_back(sink);
|
||||
}
|
||||
|
||||
} // namespace logger
|
||||
} // namespace mq
|
||||
} // namespace fair
|
||||
|
|
|
@ -1,18 +1,18 @@
|
|||
/********************************************************************************
|
||||
* Copyright (C) 2014 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
|
||||
* *
|
||||
* This software is distributed under the terms of the *
|
||||
* GNU Lesser General Public Licence version 3 (LGPL) version 3, *
|
||||
* This software is distributed under the terms of the *
|
||||
* GNU Lesser General Public Licence version 3 (LGPL) version 3, *
|
||||
* copied verbatim in the file "LICENSE" *
|
||||
********************************************************************************/
|
||||
/*
|
||||
/*
|
||||
* File: logger.h
|
||||
* Author: winckler
|
||||
*
|
||||
* Created on August 21, 2015, 6:12 PM
|
||||
*/
|
||||
#ifndef LOGGER_H
|
||||
#define LOGGER_H
|
||||
#ifndef FAIR_MQ_LOGGER_H
|
||||
#define FAIR_MQ_LOGGER_H
|
||||
|
||||
#define BOOST_LOG_DYN_LINK 1 // necessary when linking the boost_log library dynamically
|
||||
#define FUSION_MAX_VECTOR_SIZE 20
|
||||
|
@ -22,35 +22,82 @@
|
|||
#warning "The symbol 'DEBUG' is used in FairMQLogger. undefining..."
|
||||
#endif
|
||||
|
||||
// std
|
||||
#include <type_traits>
|
||||
#include <cstddef>
|
||||
#include <iostream>
|
||||
|
||||
// boost
|
||||
#include <boost/version.hpp>
|
||||
#include <boost/date_time/posix_time/posix_time.hpp>
|
||||
#include <boost/date_time/posix_time/posix_time_io.hpp>
|
||||
|
||||
#include <boost/log/sources/global_logger_storage.hpp>
|
||||
#include <boost/log/expressions/attr_fwd.hpp>
|
||||
#include <boost/log/sources/severity_logger.hpp>
|
||||
#include <boost/log/utility/formatting_ostream.hpp>
|
||||
#include <boost/log/expressions.hpp>
|
||||
#include <boost/log/expressions/attr.hpp>
|
||||
#include <boost/log/utility/formatting_ostream.hpp>
|
||||
#include <boost/log/sinks/text_file_backend.hpp>
|
||||
#include <boost/log/sinks/sync_frontend.hpp>
|
||||
#include <boost/log/sources/record_ostream.hpp>
|
||||
|
||||
#include <boost/log/sinks/basic_sink_frontend.hpp>
|
||||
// fairmq
|
||||
#include "logger_def.h"
|
||||
#include <array>
|
||||
|
||||
// Note : the following types and values must be defined in the included logger_def.h :
|
||||
// 1- custom_severity_level
|
||||
// 2- SEVERITY_THRESHOLD
|
||||
// 3- tag_console
|
||||
// 4- tag_file
|
||||
namespace fair
|
||||
{
|
||||
namespace mq
|
||||
{
|
||||
namespace logger
|
||||
{
|
||||
|
||||
enum SeverityLevel
|
||||
{
|
||||
TRACE,
|
||||
DEBUG,
|
||||
RESULTS,
|
||||
INFO,
|
||||
STATE,
|
||||
WARN,
|
||||
ERROR,
|
||||
NOLOG
|
||||
};
|
||||
|
||||
static const std::array<std::string, 8> gLogSeverityLevelString
|
||||
{
|
||||
{
|
||||
"TRACE",
|
||||
"DEBUG",
|
||||
"RESULTS",
|
||||
"INFO",
|
||||
"STATE",
|
||||
"WARN",
|
||||
"ERROR",
|
||||
"NOLOG"
|
||||
}
|
||||
};
|
||||
|
||||
namespace color
|
||||
{
|
||||
|
||||
enum Code
|
||||
{
|
||||
FG_BLACK = 30,
|
||||
FG_RED = 31,
|
||||
FG_GREEN = 32,
|
||||
FG_YELLOW = 33,
|
||||
FG_BLUE = 34,
|
||||
FG_MAGENTA = 35,
|
||||
FG_CYAN = 36,
|
||||
FG_WHITE = 37,
|
||||
FG_DEFAULT = 39,
|
||||
BG_RED = 41,
|
||||
BG_GREEN = 42,
|
||||
BG_BLUE = 44,
|
||||
BG_DEFAULT = 49
|
||||
};
|
||||
|
||||
} // namespace color
|
||||
|
||||
void ReinitLogger(bool color, const std::string& filename = "", SeverityLevel level = SeverityLevel::NOLOG);
|
||||
void RemoveRegisteredSinks();
|
||||
|
||||
// console sink related functions
|
||||
void DefaultConsoleInit(bool color = true);
|
||||
int DefaultConsoleSetFilter(SeverityLevel level);
|
||||
|
||||
// file sink related functions
|
||||
void DefaultAddFileSink(const std::string& filename, SeverityLevel level);
|
||||
|
||||
} // namespace logger
|
||||
} // namespace mq
|
||||
} // namespace fair
|
||||
|
||||
#if defined(__GNUC__) || defined(__GNUG__)
|
||||
#pragma GCC diagnostic push
|
||||
|
@ -58,113 +105,25 @@
|
|||
#endif
|
||||
|
||||
// register a global logger (declaration)
|
||||
BOOST_LOG_GLOBAL_LOGGER(global_logger, boost::log::sources::severity_logger_mt<custom_severity_level>)
|
||||
|
||||
BOOST_LOG_ATTRIBUTE_KEYWORD(fairmq_logger_timestamp, "TimeStamp", boost::posix_time::ptime)
|
||||
BOOST_LOG_ATTRIBUTE_KEYWORD(severity, "Severity", custom_severity_level)
|
||||
BOOST_LOG_GLOBAL_LOGGER(global_logger, boost::log::sources::severity_logger_mt<fair::mq::logger::SeverityLevel>)
|
||||
|
||||
#if defined(__GNUC__) || defined(__GNUG__)
|
||||
#pragma GCC diagnostic pop
|
||||
#endif
|
||||
|
||||
|
||||
namespace FairMQ
|
||||
{
|
||||
namespace Logger
|
||||
{
|
||||
// common
|
||||
extern std::vector<boost::shared_ptr< boost::log::sinks::basic_sink_frontend > >sinkList;// global var
|
||||
}}
|
||||
void reinit_logger(bool color, const std::string& filename = "", custom_severity_level threshold = SEVERITY_NOLOG);
|
||||
void RemoveRegisteredSinks();
|
||||
|
||||
template<typename T>
|
||||
void InitLogFormatter(const boost::log::record_view &view, boost::log::formatting_ostream &os)
|
||||
{
|
||||
os << "[";
|
||||
|
||||
if (std::is_same<T,tag_console>::value)
|
||||
{
|
||||
os << "\033[01;36m";
|
||||
}
|
||||
|
||||
auto date_time_formatter = boost::log::expressions::stream << boost::log::expressions::format_date_time<boost::posix_time::ptime>("TimeStamp", "%H:%M:%S");
|
||||
date_time_formatter(view, os);
|
||||
|
||||
if (std::is_same<T,tag_console>::value)
|
||||
{
|
||||
os << "\033[0m";
|
||||
}
|
||||
|
||||
os << "]"
|
||||
<< "["
|
||||
<< view.attribute_values()["Severity"].extract<custom_severity_level, T>()
|
||||
<< "] "
|
||||
//<< " - "
|
||||
<< view.attribute_values()["Message"].extract<std::string>();
|
||||
}
|
||||
|
||||
|
||||
template<typename FunT>
|
||||
int SetSinkFilterImpl(std::size_t index, FunT&& func)
|
||||
{
|
||||
if(index<FairMQ::Logger::sinkList.size())
|
||||
{
|
||||
FairMQ::Logger::sinkList.at(index)->set_filter(std::forward<FunT>(func));
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
// console sink related functions
|
||||
|
||||
|
||||
void DefaultConsoleInit(bool color = true);
|
||||
int DefaultConsoleSetFilter(custom_severity_level threshold);
|
||||
|
||||
|
||||
|
||||
// file sink related functions
|
||||
|
||||
void DefaultAddFileSink(const std::string& filename, custom_severity_level threshold);
|
||||
|
||||
template<typename FunT, typename... Args>
|
||||
void AddFileSink(FunT&& func, Args&&... args)
|
||||
{
|
||||
// add a text sink
|
||||
using sink_backend_t = boost::log::sinks::text_file_backend;
|
||||
using sink_t = boost::log::sinks::synchronous_sink<sink_backend_t>;
|
||||
|
||||
// forward keywords args for setting log file properties
|
||||
boost::shared_ptr<sink_backend_t> backend = boost::make_shared<sink_backend_t>(std::forward<Args>(args)...);
|
||||
boost::shared_ptr<sink_t> sink = boost::make_shared<sink_t>(backend);
|
||||
|
||||
// specify the format of the log message
|
||||
sink->set_formatter(&InitLogFormatter<tag_file>);
|
||||
|
||||
// forward lambda for setting the filter
|
||||
sink->set_filter(std::forward<FunT>(func));
|
||||
|
||||
// add file sinks to core and list
|
||||
boost::log::core::get()->add_sink(sink);
|
||||
FairMQ::Logger::sinkList.push_back(sink);
|
||||
|
||||
}
|
||||
|
||||
|
||||
// helper macros
|
||||
// helper macros
|
||||
|
||||
// global macros (core). Level filters are set globally here, that is to all register sinks
|
||||
// add empty string if boost 1.59.0 (see : https://svn.boost.org/trac/boost/ticket/11549 )
|
||||
#if BOOST_VERSION == 105900
|
||||
#define LOG(severity) BOOST_LOG_SEV(global_logger::get(),custom_severity_level::severity) << ""
|
||||
#define MQLOG(severity) BOOST_LOG_SEV(global_logger::get(),custom_severity_level::severity) << ""
|
||||
#define LOG(severity) BOOST_LOG_SEV(global_logger::get(), fair::mq::logger::SeverityLevel::severity) << ""
|
||||
#define MQLOG(severity) BOOST_LOG_SEV(global_logger::get(), fair::mq::logger::SeverityLevel::severity) << ""
|
||||
#else
|
||||
#define LOG(severity) BOOST_LOG_SEV(global_logger::get(),custom_severity_level::severity)
|
||||
#define MQLOG(severity) BOOST_LOG_SEV(global_logger::get(),custom_severity_level::severity)
|
||||
#define LOG(severity) BOOST_LOG_SEV(global_logger::get(), fair::mq::logger::SeverityLevel::severity)
|
||||
#define MQLOG(severity) BOOST_LOG_SEV(global_logger::get(), fair::mq::logger::SeverityLevel::severity)
|
||||
#endif
|
||||
|
||||
#define SET_LOG_CONSOLE_LEVEL(loglevel) DefaultConsoleSetFilter(custom_severity_level::loglevel)
|
||||
#define ADD_LOG_FILESINK(filename,loglevel) DefaultAddFileSink(filename, custom_severity_level::loglevel)
|
||||
#define SET_LOG_CONSOLE_LEVEL(loglevel) DefaultConsoleSetFilter(fair::mq::logger::SeverityLevel::loglevel)
|
||||
#define ADD_LOG_FILESINK(filename, loglevel) DefaultAddFileSink(filename, fair::mq::logger::SeverityLevel::loglevel)
|
||||
|
||||
// Use : SET_LOG_CONSOLE_LEVEL(INFO); ADD_LOG_FILESINK(filename,ERROR);
|
||||
#endif
|
||||
#endif // FAIR_MQ_LOGGER_H
|
||||
|
|
|
@ -1,176 +0,0 @@
|
|||
/********************************************************************************
|
||||
* Copyright (C) 2014 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
|
||||
* *
|
||||
* This software is distributed under the terms of the *
|
||||
* GNU Lesser General Public Licence version 3 (LGPL) version 3, *
|
||||
* copied verbatim in the file "LICENSE" *
|
||||
********************************************************************************/
|
||||
/*
|
||||
* File: logger_def.h
|
||||
* Author: winckler
|
||||
*
|
||||
* Created on September 2, 2015, 11:58 AM
|
||||
*/
|
||||
|
||||
#ifndef LOGGER_DEF_H
|
||||
#define LOGGER_DEF_H
|
||||
|
||||
#include <array>
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
|
||||
#include <boost/log/utility/manipulators/to_log.hpp>
|
||||
#include <boost/log/sources/record_ostream.hpp>
|
||||
|
||||
namespace FairMQ
|
||||
{
|
||||
|
||||
enum severity_level
|
||||
{
|
||||
TRACE,
|
||||
DEBUG,
|
||||
RESULTS,
|
||||
INFO,
|
||||
STATE,
|
||||
WARN,
|
||||
ERROR,
|
||||
NOLOG
|
||||
};
|
||||
|
||||
static const std::array<std::string, 8> g_LogSeverityLevelString
|
||||
{
|
||||
{
|
||||
"TRACE",
|
||||
"DEBUG",
|
||||
"RESULTS",
|
||||
"INFO",
|
||||
"STATE",
|
||||
"WARN",
|
||||
"ERROR",
|
||||
"NOLOG"
|
||||
}
|
||||
};
|
||||
|
||||
namespace Color
|
||||
{
|
||||
|
||||
enum code
|
||||
{
|
||||
FG_BLACK = 30,
|
||||
FG_RED = 31,
|
||||
FG_GREEN = 32,
|
||||
FG_YELLOW = 33,
|
||||
FG_BLUE = 34,
|
||||
FG_MAGENTA = 35,
|
||||
FG_CYAN = 36,
|
||||
FG_WHITE = 37,
|
||||
FG_DEFAULT = 39,
|
||||
BG_RED = 41,
|
||||
BG_GREEN = 42,
|
||||
BG_BLUE = 44,
|
||||
BG_DEFAULT = 49
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
} // FairMQ namespace
|
||||
|
||||
// helper function to format in color console output
|
||||
template <FairMQ::Color::code color>
|
||||
inline std::string write_in(const std::string& text_in_bold)
|
||||
{
|
||||
std::ostringstream os;
|
||||
os << "\033[01;" << color << "m" << text_in_bold << "\033[0m";
|
||||
|
||||
return os.str();
|
||||
}
|
||||
|
||||
using custom_severity_level = FairMQ::severity_level;
|
||||
#define SEVERITY_MINIMUM custom_severity_level::TRACE
|
||||
#define SEVERITY_ERROR custom_severity_level::ERROR
|
||||
#define SEVERITY_NOLOG custom_severity_level::NOLOG
|
||||
|
||||
// tags used for log console or file formatting
|
||||
struct tag_console;
|
||||
struct tag_file;
|
||||
|
||||
// overload operator for console output
|
||||
inline boost::log::formatting_ostream& operator<<
|
||||
(
|
||||
boost::log::formatting_ostream& strm,
|
||||
boost::log::to_log_manip<custom_severity_level, tag_console> const& manip
|
||||
)
|
||||
{
|
||||
custom_severity_level level = manip.get();
|
||||
std::size_t idx = static_cast<std::size_t>(level);
|
||||
if (idx < FairMQ::g_LogSeverityLevelString.size())
|
||||
{
|
||||
// strm << " idx = " << idx << " ";
|
||||
switch (level)
|
||||
{
|
||||
case custom_severity_level::TRACE :
|
||||
strm << write_in<FairMQ::Color::FG_BLUE>(FairMQ::g_LogSeverityLevelString.at(idx));
|
||||
break;
|
||||
|
||||
case custom_severity_level::DEBUG :
|
||||
strm << write_in<FairMQ::Color::FG_BLUE>(FairMQ::g_LogSeverityLevelString.at(idx));
|
||||
break;
|
||||
|
||||
case custom_severity_level::RESULTS :
|
||||
strm << write_in<FairMQ::Color::FG_MAGENTA>(FairMQ::g_LogSeverityLevelString.at(idx));
|
||||
break;
|
||||
|
||||
case custom_severity_level::INFO :
|
||||
strm << write_in<FairMQ::Color::FG_GREEN>(FairMQ::g_LogSeverityLevelString.at(idx));
|
||||
break;
|
||||
|
||||
case custom_severity_level::WARN :
|
||||
strm << write_in<FairMQ::Color::FG_YELLOW>(FairMQ::g_LogSeverityLevelString.at(idx));
|
||||
break;
|
||||
|
||||
case custom_severity_level::STATE :
|
||||
strm << write_in<FairMQ::Color::FG_MAGENTA>(FairMQ::g_LogSeverityLevelString.at(idx));
|
||||
break;
|
||||
|
||||
case custom_severity_level::ERROR :
|
||||
strm << write_in<FairMQ::Color::FG_RED>(FairMQ::g_LogSeverityLevelString.at(idx));
|
||||
break;
|
||||
|
||||
case custom_severity_level::NOLOG :
|
||||
strm << write_in<FairMQ::Color::FG_DEFAULT>(FairMQ::g_LogSeverityLevelString.at(idx));
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
strm << write_in<FairMQ::Color::FG_RED>("Unknown log level ")
|
||||
<< "(int level = " << static_cast<int>(level) << ")";
|
||||
}
|
||||
return strm;
|
||||
}
|
||||
|
||||
// overload operator for file output
|
||||
inline boost::log::formatting_ostream& operator<<
|
||||
(
|
||||
boost::log::formatting_ostream& strm,
|
||||
boost::log::to_log_manip<custom_severity_level, tag_file> const& manip
|
||||
)
|
||||
{
|
||||
custom_severity_level level = manip.get();
|
||||
std::size_t idx = static_cast<std::size_t>(level);
|
||||
if (idx < FairMQ::g_LogSeverityLevelString.size())
|
||||
{
|
||||
strm << FairMQ::g_LogSeverityLevelString.at(idx);
|
||||
}
|
||||
else
|
||||
{
|
||||
strm << write_in<FairMQ::Color::FG_RED>("Unknown log level ")
|
||||
<< "(int level = " << static_cast<int>(level) << ")";
|
||||
}
|
||||
return strm;
|
||||
}
|
||||
|
||||
#endif /* LOGGER_DEF_H */
|
|
@ -84,15 +84,15 @@ int main()
|
|||
// advanced commands
|
||||
std::cout << "----------------------------"<<std::endl;
|
||||
LOG(INFO)<<"open log file 3";// custom file sink setting
|
||||
AddFileSink([](const boost::log::attribute_value_set& attr_set)
|
||||
{
|
||||
auto sev = attr_set["Severity"].extract<custom_severity_level>();
|
||||
return (sev == FairMQ::ERROR);
|
||||
},
|
||||
boost::log::keywords::file_name = "test_log3_%5N.log",
|
||||
boost::log::keywords::rotation_size = 5 * 1024 * 1024,
|
||||
boost::log::keywords::time_based_rotation = boost::log::sinks::file::rotation_at_time_point(12, 0, 0)
|
||||
);
|
||||
// AddFileSink([](const boost::log::attribute_value_set& attr_set)
|
||||
// {
|
||||
// auto sev = attr_set["Severity"].extract<custom_severity_level>();
|
||||
// return (sev == FairMQ::ERROR);
|
||||
// },
|
||||
// boost::log::keywords::file_name = "test_log3_%5N.log",
|
||||
// boost::log::keywords::rotation_size = 5 * 1024 * 1024,
|
||||
// boost::log::keywords::time_based_rotation = boost::log::sinks::file::rotation_at_time_point(12, 0, 0)
|
||||
// );
|
||||
|
||||
test_logger();
|
||||
|
||||
|
@ -107,7 +107,7 @@ int main()
|
|||
test_logger();
|
||||
|
||||
// remove all sinks, and restart console sinks
|
||||
reinit_logger(false);
|
||||
ReinitLogger(false);
|
||||
test_logger();
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -79,7 +79,7 @@ void FairMQProgOptions::ParseAll(const int argc, char const* const* argv, bool a
|
|||
// if these options are provided, do no further checks and let the device handle them
|
||||
if (fVarMap.count("print-channels") || fVarMap.count("version"))
|
||||
{
|
||||
DefaultConsoleSetFilter(fSeverityMap.at("NOLOG"));
|
||||
fair::mq::logger::DefaultConsoleSetFilter(fSeverityMap.at("NOLOG"));
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -96,17 +96,17 @@ void FairMQProgOptions::ParseAll(const int argc, char const* const* argv, bool a
|
|||
|
||||
if (logFile != "")
|
||||
{
|
||||
reinit_logger(false, logFile, fSeverityMap.at(verbosity));
|
||||
DefaultConsoleSetFilter(fSeverityMap.at("NOLOG"));
|
||||
fair::mq::logger::ReinitLogger(false, logFile, fSeverityMap.at(verbosity));
|
||||
fair::mq::logger::DefaultConsoleSetFilter(fSeverityMap.at("NOLOG"));
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!color)
|
||||
{
|
||||
reinit_logger(false);
|
||||
fair::mq::logger::ReinitLogger(false);
|
||||
}
|
||||
|
||||
DefaultConsoleSetFilter(fSeverityMap.at(verbosity));
|
||||
fair::mq::logger::DefaultConsoleSetFilter(fSeverityMap.at(verbosity));
|
||||
}
|
||||
|
||||
// check if one of required MQ config option is there
|
||||
|
|
|
@ -8,16 +8,16 @@
|
|||
/*
|
||||
* File: FairProgOptions.cxx
|
||||
* Author: winckler
|
||||
*
|
||||
*
|
||||
* Created on March 11, 2015, 5:38 PM
|
||||
*/
|
||||
|
||||
#include "FairProgOptions.h"
|
||||
|
||||
#include <iomanip>
|
||||
|
||||
using namespace std;
|
||||
|
||||
/// //////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
/// Constructor
|
||||
FairProgOptions::FairProgOptions() :
|
||||
fVarMap(),
|
||||
fGenericDesc("Generic options description"),
|
||||
|
@ -42,14 +42,14 @@ FairProgOptions::FairProgOptions() :
|
|||
("log-color", po::value<bool>()->default_value(true), "logger color: true or false")
|
||||
("print-options", po::value<bool>()->implicit_value(true), "print options in machine-readable format (<option>:<computed-value>:<type>:<description>)");
|
||||
|
||||
fSeverityMap["TRACE"] = FairMQ::severity_level::TRACE;
|
||||
fSeverityMap["DEBUG"] = FairMQ::severity_level::DEBUG;
|
||||
fSeverityMap["RESULTS"] = FairMQ::severity_level::RESULTS;
|
||||
fSeverityMap["INFO"] = FairMQ::severity_level::INFO;
|
||||
fSeverityMap["WARN"] = FairMQ::severity_level::WARN;
|
||||
fSeverityMap["ERROR"] = FairMQ::severity_level::ERROR;
|
||||
fSeverityMap["STATE"] = FairMQ::severity_level::STATE;
|
||||
fSeverityMap["NOLOG"] = FairMQ::severity_level::NOLOG;
|
||||
fSeverityMap["TRACE"] = fair::mq::logger::SeverityLevel::TRACE;
|
||||
fSeverityMap["DEBUG"] = fair::mq::logger::SeverityLevel::DEBUG;
|
||||
fSeverityMap["RESULTS"] = fair::mq::logger::SeverityLevel::RESULTS;
|
||||
fSeverityMap["INFO"] = fair::mq::logger::SeverityLevel::INFO;
|
||||
fSeverityMap["WARN"] = fair::mq::logger::SeverityLevel::WARN;
|
||||
fSeverityMap["ERROR"] = fair::mq::logger::SeverityLevel::ERROR;
|
||||
fSeverityMap["STATE"] = fair::mq::logger::SeverityLevel::STATE;
|
||||
fSeverityMap["NOLOG"] = fair::mq::logger::SeverityLevel::NOLOG;
|
||||
}
|
||||
|
||||
/// Destructor
|
||||
|
|
|
@ -135,14 +135,14 @@ class FairProgOptions
|
|||
template<typename T>
|
||||
T ConvertTo(const std::string& strValue)
|
||||
{
|
||||
if (std::is_arithmetic<T>::value)
|
||||
if (std::is_arithmetic<T>::value)
|
||||
{
|
||||
std::istringstream iss(strValue);
|
||||
T val;
|
||||
iss >> val;
|
||||
return val;
|
||||
}
|
||||
else
|
||||
}
|
||||
else
|
||||
{
|
||||
LOG(ERROR) << "the provided string " << strValue << " cannot be converted in the requested type. The target types must be arithmetic types";
|
||||
}
|
||||
|
@ -183,7 +183,7 @@ class FairProgOptions
|
|||
|
||||
// Description which is printed in help command line
|
||||
// to handle logger severity
|
||||
std::map<std::string, FairMQ::severity_level> fSeverityMap;
|
||||
std::map<std::string, fair::mq::logger::SeverityLevel> fSeverityMap;
|
||||
po::options_description fVisibleOptions;
|
||||
|
||||
mutable std::mutex fConfigMutex;
|
||||
|
|
Loading…
Reference in New Issue
Block a user