- Replace the fairmq logger with one based on Boost.Log

- Adapt FairProgOptions to the new logger
This commit is contained in:
winckler 2015-09-07 18:51:50 +02:00 committed by Mohammad Al-Turany
parent bea05ea6c1
commit e0ca1f62b3
14 changed files with 722 additions and 145 deletions

View File

@ -9,12 +9,13 @@
configure_file(${CMAKE_SOURCE_DIR}/fairmq/options/ProgOptionTest/macro/bsampler-sink.json ${CMAKE_BINARY_DIR}/bin/config/bsampler-sink.json) configure_file(${CMAKE_SOURCE_DIR}/fairmq/options/ProgOptionTest/macro/bsampler-sink.json ${CMAKE_BINARY_DIR}/bin/config/bsampler-sink.json)
configure_file(${CMAKE_SOURCE_DIR}/fairmq/examples/1-sampler-sink/sampler-sink.json ${CMAKE_BINARY_DIR}/bin/config/ex1-sampler-sink.json) configure_file(${CMAKE_SOURCE_DIR}/fairmq/examples/1-sampler-sink/sampler-sink.json ${CMAKE_BINARY_DIR}/bin/config/ex1-sampler-sink.json)
configure_file(${CMAKE_SOURCE_DIR}/fairmq/examples/2-sampler-processor-sink/sampler-processor-sink.json ${CMAKE_BINARY_DIR}/bin/config/ex2-sampler-processor-sink.json) configure_file(${CMAKE_SOURCE_DIR}/fairmq/examples/2-sampler-processor-sink/sampler-processor-sink.json ${CMAKE_BINARY_DIR}/bin/config/ex2-sampler-processor-sink.json)
add_subdirectory(logger)
Set(INCLUDE_DIRECTORIES Set(INCLUDE_DIRECTORIES
${CMAKE_SOURCE_DIR}/fairmq ${CMAKE_SOURCE_DIR}/fairmq
${CMAKE_SOURCE_DIR}/fairmq/devices ${CMAKE_SOURCE_DIR}/fairmq/devices
${CMAKE_SOURCE_DIR}/fairmq/tools ${CMAKE_SOURCE_DIR}/fairmq/tools
${CMAKE_SOURCE_DIR}/fairmq/options ${CMAKE_SOURCE_DIR}/fairmq/options
${CMAKE_SOURCE_DIR}/fairmq/logger
${CMAKE_SOURCE_DIR}/fairmq/examples/1-sampler-sink ${CMAKE_SOURCE_DIR}/fairmq/examples/1-sampler-sink
${CMAKE_SOURCE_DIR}/fairmq/examples/2-sampler-processor-sink ${CMAKE_SOURCE_DIR}/fairmq/examples/2-sampler-processor-sink
${CMAKE_SOURCE_DIR}/fairmq/examples/req-rep ${CMAKE_SOURCE_DIR}/fairmq/examples/req-rep
@ -164,7 +165,7 @@ Set(FAIRMQHEADERS
install(FILES ${FAIRMQHEADERS} DESTINATION include) install(FILES ${FAIRMQHEADERS} DESTINATION include)
set(DEPENDENCIES set(DEPENDENCIES
${DEPENDENCIES} ${DEPENDENCIES} fairmq_logger
boost_thread boost_timer boost_system boost_filesystem boost_program_options boost_random boost_chrono boost_exception boost_thread boost_timer boost_system boost_filesystem boost_program_options boost_random boost_chrono boost_exception
) )

View File

@ -12,74 +12,10 @@
* @author D. Klein, A. Rybalchenko * @author D. Klein, A. Rybalchenko
*/ */
#include <sys/time.h>
#include <ctime>
#include "FairMQLogger.h" #include "FairMQLogger.h"
int FairMQLogger::fMinLogLevel = FairMQLogger::DEBUG;
FairMQLogger::FairMQLogger()
: os()
, fLogLevel(DEBUG)
{
}
FairMQLogger::~FairMQLogger()
{
if (fLogLevel >= FairMQLogger::fMinLogLevel && fLogLevel < FairMQLogger::NOLOG)
{
std::cout << os.str() << std::endl;
}
}
std::ostringstream& FairMQLogger::Log(int type)
{
std::string type_str;
fLogLevel = type;
switch (type)
{
case DEBUG :
type_str = "\033[01;34mDEBUG\033[0m";
break;
case INFO :
type_str = "\033[01;32mINFO\033[0m";
break;
case WARN :
type_str = "\033[01;33mWARN\033[0m";
break;
case ERROR :
type_str = "\033[01;31mERROR\033[0m";
break;
case STATE :
type_str = "\033[01;35mSTATE\033[0m";
break;
case NOLOG :
type_str = "\033[01;31mNOLOG\033[0m";
break;
default:
break;
}
timestamp_t tm = get_timestamp();
timestamp_t ms = tm / 1000.0L;
timestamp_t s = ms / 1000.0L;
time_t t = s;
// size_t fractional_seconds = ms % 1000;
char mbstr[100];
strftime(mbstr, 100, "%H:%M:%S", localtime(&t));
os << "[\033[01;36m" << mbstr << "\033[0m]"
<< "[" << type_str << "]"
<< " ";
return os;
}
timestamp_t get_timestamp() timestamp_t get_timestamp()
{ {
struct timeval now; struct timeval now;

View File

@ -15,49 +15,10 @@
#ifndef FAIRMQLOGGER_H_ #ifndef FAIRMQLOGGER_H_
#define FAIRMQLOGGER_H_ #define FAIRMQLOGGER_H_
#include <sstream> #include "logger/logger.h"
#include <sys/time.h>
#include <iostream>
#include <iomanip>
#include <ctime>
#include <string>
#include <stdio.h>
class FairMQLogger
{
public:
enum Level
{
DEBUG,
INFO,
WARN,
ERROR,
STATE,
NOLOG
};
FairMQLogger();
virtual ~FairMQLogger();
std::ostringstream& Log(int type);
static void SetLogLevel(int logLevel)
{
FairMQLogger::fMinLogLevel = logLevel;
}
private:
std::ostringstream os;
int fLogLevel;
static int fMinLogLevel;
};
typedef unsigned long long timestamp_t; typedef unsigned long long timestamp_t;
timestamp_t get_timestamp(); timestamp_t get_timestamp();
#define LOG(type) FairMQLogger().Log(FairMQLogger::type)
#define MQLOG(type) FairMQLogger().Log(FairMQLogger::type)
#define SET_LOG_LEVEL(loglevel) FairMQLogger::SetLogLevel(FairMQLogger::loglevel)
#define SET_LOGGER_LEVEL(loglevel) FairMQLogger::SetLogLevel(loglevel)
#endif /* FAIRMQLOGGER_H_ */ #endif /* FAIRMQLOGGER_H_ */

View File

@ -15,7 +15,6 @@
#ifndef FAIRMQTRANSPORTFACTORY_H_ #ifndef FAIRMQTRANSPORTFACTORY_H_
#define FAIRMQTRANSPORTFACTORY_H_ #define FAIRMQTRANSPORTFACTORY_H_
#include "FairMQMessage.h" #include "FairMQMessage.h"
#include "FairMQChannel.h" #include "FairMQChannel.h"
#include "FairMQSocket.h" #include "FairMQSocket.h"

View File

@ -0,0 +1,43 @@
################################################################################
# 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" #
################################################################################
# Create a library
set(INCLUDE_DIRECTORIES
${CMAKE_SOURCE_DIR}/fairmq/logger
${CMAKE_SOURCE_DIR}/fairmq/logger/run
)
include_directories(${INCLUDE_DIRECTORIES})
include_directories(${Boost_INCLUDE_DIRS})
set(LINK_DIRECTORIES ${Boost_LIBRARY_DIRS})
link_directories(${LINK_DIRECTORIES})
set(SRCS logger.cxx)
set(LIBRARY_NAME fairmq_logger)
set(DEPENDENCIES
boost_log
boost_log_setup
boost_thread
boost_date_time
boost_filesystem
boost_system
pthread
)
GENERATE_LIBRARY()
# generate test executable
set(EXE_NAME runtestLogger)
set(SRCS run/testLogger.cxx)
set(DEPENDENCIES fairmq_logger)
GENERATE_EXECUTABLE()

234
fairmq/logger/logger.cxx Normal file
View File

@ -0,0 +1,234 @@
/********************************************************************************
* 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" *
********************************************************************************/
#include "logger.h"
#include <boost/log/core/core.hpp>
#include <boost/log/expressions/formatters/date_time.hpp>
#include <boost/log/expressions.hpp>
#include <boost/log/sinks/sync_frontend.hpp>
#include <boost/log/sinks/text_ostream_backend.hpp>
#include <boost/log/sources/severity_logger.hpp>
#include <boost/log/support/date_time.hpp>
#include <boost/core/null_deleter.hpp>
#include <boost/log/sinks/text_file_backend.hpp>
#include <boost/log/utility/setup/common_attributes.hpp>
#include <boost/make_shared.hpp>
#include <boost/shared_ptr.hpp>
#include <fstream>
#include <ostream>
namespace logging = boost::log;
namespace src = boost::log::sources;
namespace expr = boost::log::expressions;
namespace sinks = boost::log::sinks;
namespace attrs = boost::log::attributes;
BOOST_LOG_GLOBAL_LOGGER_INIT(global_logger, src::severity_logger_mt)
{
src::severity_logger_mt<custom_severity_level> global_logger;
global_logger.add_attribute("TimeStamp", attrs::local_clock());
init_log_console();
return global_logger;
}
void init_log_console()
{
// add a text sink
typedef sinks::synchronous_sink<sinks::text_ostream_backend> text_sink;
boost::shared_ptr<text_sink> sink = boost::make_shared<text_sink>();
// add "console" output stream to our sink
sink->locked_backend()->add_stream(boost::shared_ptr<std::ostream>(&std::clog, boost::null_deleter()));
// specify the format of the log message
sink->set_formatter(&init_log_formatter<tag_console>);
// add sink to the core
logging::core::get()->add_sink(sink);
}
void init_log_file(const std::string& filename, custom_severity_level threshold, log_op::operation op, const std::string& id)
{
// add a text sink
std::string formatted_filename(filename);
formatted_filename+=id;
formatted_filename+="_%Y-%m-%d_%H-%M-%S.%N.log";
boost::shared_ptr< sinks::text_file_backend > backend =
boost::make_shared< sinks::text_file_backend >
(
boost::log::keywords::file_name = formatted_filename,
boost::log::keywords::rotation_size = 10 * 1024 * 1024,
// rotate at midnight every day
boost::log::keywords::time_based_rotation = 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
);
typedef sinks::synchronous_sink< sinks::text_file_backend > sink_t;
boost::shared_ptr< sink_t > sink(new sink_t(backend));
// specify the format of the log message
sink->set_formatter(&init_log_formatter<tag_file>);
switch (op)
{
case log_op::operation::EQUAL :
sink->set_filter(severity == threshold);
break;
case log_op::operation::GREATER_THAN :
sink->set_filter(severity > threshold);
break;
case log_op::operation::GREATER_EQ_THAN :
sink->set_filter(severity >= threshold);
break;
case log_op::operation::LESS_THAN :
sink->set_filter(severity < threshold);
break;
case log_op::operation::LESS_EQ_THAN :
sink->set_filter(severity <= threshold);
break;
default:
break;
}
logging::core::get()->add_sink(sink);
}
// temporary : to be replaced with c++11 lambda
void set_global_log_level(log_op::operation op, custom_severity_level threshold )
{
switch (threshold)
{
case custom_severity_level::TRACE :
set_global_log_level_operation(op,custom_severity_level::TRACE);
break;
case custom_severity_level::DEBUG :
set_global_log_level_operation(op,custom_severity_level::DEBUG);
break;
case custom_severity_level::RESULTS :
set_global_log_level_operation(op,custom_severity_level::RESULTS);
break;
case custom_severity_level::INFO :
set_global_log_level_operation(op,custom_severity_level::INFO);
break;
case custom_severity_level::WARN :
set_global_log_level_operation(op,custom_severity_level::WARN);
break;
case custom_severity_level::STATE :
set_global_log_level_operation(op,custom_severity_level::STATE);
break;
case custom_severity_level::ERROR :
set_global_log_level_operation(op,custom_severity_level::ERROR);
break;
case custom_severity_level::NOLOG :
set_global_log_level_operation(op,custom_severity_level::NOLOG);
break;
default:
break;
}
}
void set_global_log_level_operation(log_op::operation op, custom_severity_level threshold )
{
switch (op)
{
case log_op::operation::EQUAL :
boost::log::core::get()->set_filter(severity == threshold);
break;
case log_op::operation::GREATER_THAN :
boost::log::core::get()->set_filter(severity > threshold);
break;
case log_op::operation::GREATER_EQ_THAN :
boost::log::core::get()->set_filter(severity >= threshold);
break;
case log_op::operation::LESS_THAN :
boost::log::core::get()->set_filter(severity < threshold);
break;
case log_op::operation::LESS_EQ_THAN :
boost::log::core::get()->set_filter(severity <= threshold);
break;
default:
break;
}
}
void init_new_file(const std::string& filename, custom_severity_level threshold, log_op::operation op)
{
// add a file text sink with filters but without any formatting
std::string formatted_filename(filename);
formatted_filename+=".%N.txt";
boost::shared_ptr< sinks::text_file_backend > backend =
boost::make_shared< sinks::text_file_backend >
(
boost::log::keywords::file_name = formatted_filename,
boost::log::keywords::rotation_size = 10 * 1024 * 1024,
// rotate at midnight every day
boost::log::keywords::time_based_rotation = 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
);
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);
switch (op)
{
case log_op::operation::EQUAL :
sink->set_filter(severity == threshold);
break;
case log_op::operation::GREATER_THAN :
sink->set_filter(severity > threshold);
break;
case log_op::operation::GREATER_EQ_THAN :
sink->set_filter(severity >= threshold);
break;
case log_op::operation::LESS_THAN :
sink->set_filter(severity < threshold);
break;
case log_op::operation::LESS_EQ_THAN :
sink->set_filter(severity <= threshold);
break;
default:
break;
}
logging::core::get()->add_sink(sink);
}

129
fairmq/logger/logger.h Normal file
View File

@ -0,0 +1,129 @@
/********************************************************************************
* 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.h
* Author: winckler
*
* Created on August 21, 2015, 6:12 PM
*/
#ifndef LOGGER_H
#define LOGGER_H
#define BOOST_LOG_DYN_LINK 1 // necessary when linking the boost_log library dynamically
// std
#include <type_traits>
#include <cstddef>
#include <iostream>
// boost
#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/sources/severity_logger.hpp>
#include <boost/log/utility/formatting_ostream.hpp>
#include <boost/log/expressions.hpp>
#include <boost/log/expressions/attr_fwd.hpp>
#include <boost/log/expressions/attr.hpp>
// fairmq
#include "logger_def.h"
// 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
// Note : operation enum temporary : (until we replace it with c++11 lambda expression)
namespace log_op
{
enum operation
{
EQUAL,
GREATER_THAN,
GREATER_EQ_THAN,
LESS_THAN,
LESS_EQ_THAN
};
}
// declaration of the init function for the global logger
void init_log_console();
void init_log_file( const std::string& filename,
custom_severity_level threshold=SEVERITY_THRESHOLD,
log_op::operation=log_op::GREATER_EQ_THAN,
const std::string& id=""
);
void init_new_file( const std::string& filename,
custom_severity_level threshold,
log_op::operation op
);
void set_global_log_level( log_op::operation op=log_op::GREATER_EQ_THAN,
custom_severity_level threshold=SEVERITY_THRESHOLD );
void set_global_log_level_operation( log_op::operation op=log_op::GREATER_EQ_THAN,
custom_severity_level threshold=SEVERITY_THRESHOLD );
// 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)
template<typename T>
void init_log_formatter(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>();
}
// helper macros
// global macros (core). Level filters are set globally here, that is to all register sinks
#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 SET_LOG_LEVEL(loglevel) boost::log::core::get()->set_filter(severity >= custom_severity_level::loglevel);
#define SET_LOG_FILTER(op,loglevel) set_global_log_level(log_op::op,custom_severity_level::loglevel)
// local init macros (sinks)
// Notes : when applying a filter to the sink, and then to the core, the resulting filter will
// be the intersection of the two sets defined by the two filters, i.e., core and sinks
// filename : path to file name without extension
#define INIT_LOG_FILE(filename) init_log_file(filename);
#define INIT_LOG_FILE_LVL(filename,loglevel) init_log_file(filename,custom_severity_level::loglevel);
#define INIT_LOG_FILE_FILTER(filename,op,loglevel) init_log_file(filename,custom_severity_level::loglevel,log_op::op);
//INIT_LOG_FILE_FILTER_MP : add id to log filename for multiprocess
#define INIT_LOG_FILE_FILTER_MP(filename,op,loglevel,id) init_log_file(filename,custom_severity_level::loglevel,log_op::GREATER_EQ_THAN,id);
// create new file without formatting
#define INIT_NEW_FILE(filename,op,loglevel) init_new_file(filename,custom_severity_level::loglevel,log_op::op);
#endif

183
fairmq/logger/logger_def.h Normal file
View File

@ -0,0 +1,183 @@
/********************************************************************************
* 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,
WARN,
ERROR,
STATE,
NOLOG
};
static const std::array<std::string, 8> g_LogSeverityLevelString
{
{
"TRACE",
"DEBUG",
"RESULTS",
"INFO",
"WARN",
"ERROR",
"STATE",
"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
};
}
}
// 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();
}
// typedef
typedef fairmq::severity_level custom_severity_level;
#define SEVERITY_THRESHOLD custom_severity_level::TRACE
// 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 */

View File

@ -0,0 +1,66 @@
/********************************************************************************
* 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" *
********************************************************************************/
#include "logger.h"
void test_logger()
{
LOG(TRACE) << "this is a trace message";
LOG(DEBUG) << "this is a debug message";
LOG(RESULTS) << "this is a results message";
LOG(INFO) << "this is a info message";
LOG(WARN) << "this is a warning message";
LOG(ERROR) << "this is an error message";
LOG(STATE) << "this is a state message";
}
void test_set_level()
{
std::cout<<"********* test logger : SET_LOG_LEVEL(lvl) *********"<<std::endl;
SET_LOG_LEVEL(TRACE);
test_logger();
std::cout << "----------------------------"<<std::endl;
SET_LOG_LEVEL(DEBUG);
test_logger();
std::cout << "----------------------------"<<std::endl;
SET_LOG_LEVEL(RESULTS);
test_logger();
std::cout << "----------------------------"<<std::endl;
SET_LOG_LEVEL(INFO);
test_logger();
std::cout << "----------------------------"<<std::endl;
SET_LOG_LEVEL(WARN);
test_logger();
std::cout << "----------------------------"<<std::endl;
SET_LOG_LEVEL(ERROR);
test_logger();
std::cout << "----------------------------"<<std::endl;
SET_LOG_LEVEL(STATE);
test_logger();
std::cout << "----------------------------"<<std::endl;
}
int main()
{
INIT_LOG_FILE_FILTER("test_log_file",GREATER_EQ_THAN,ERROR);// init and add one sink to the core
test_set_level();
INIT_LOG_FILE_FILTER("test_another_log_file",EQUAL,INFO);// init and add another sink to the core
test_set_level();
return 0;
}

View File

@ -48,13 +48,13 @@ FairMQMap ptreeToMQMap(const boost::property_tree::ptree& pt, const std::string&
if (formatFlag == "xml") if (formatFlag == "xml")
{ {
deviceIdKey = p.second.get<std::string>("<xmlattr>.id"); deviceIdKey = p.second.get<std::string>("<xmlattr>.id");
MQLOG(DEBUG) << "Found device id '" << deviceIdKey << "' in XML input"; LOG(DEBUG) << "Found device id '" << deviceIdKey << "' in XML input";
} }
if (formatFlag == "json") if (formatFlag == "json")
{ {
deviceIdKey = p.second.get<std::string>("id"); deviceIdKey = p.second.get<std::string>("id");
MQLOG(DEBUG) << "Found device id '"<< deviceIdKey << "' in JSON input"; LOG(DEBUG) << "Found device id '"<< deviceIdKey << "' in JSON input";
} }
} }
@ -88,7 +88,7 @@ FairMQMap ptreeToMQMap(const boost::property_tree::ptree& pt, const std::string&
std::stringstream deviceStream; std::stringstream deviceStream;
deviceStream << "[node = " << p.first deviceStream << "[node = " << p.first
<< "] id = " << deviceIdKey; << "] id = " << deviceIdKey;
MQLOG(DEBUG) << deviceStream.str(); LOG(DEBUG) << deviceStream.str();
// for each channel in device // for each channel in device
for(const auto& q : p.second.get_child("")) for(const auto& q : p.second.get_child(""))
@ -113,7 +113,7 @@ FairMQMap ptreeToMQMap(const boost::property_tree::ptree& pt, const std::string&
std::stringstream channelStream; std::stringstream channelStream;
channelStream << "\t [node = " << q.first channelStream << "\t [node = " << q.first
<< "] name = " << channelKey; << "] name = " << channelKey;
MQLOG(DEBUG) << channelStream.str(); LOG(DEBUG) << channelStream.str();
// temporary FairMQChannel container // temporary FairMQChannel container
std::vector<FairMQChannel> channelList; std::vector<FairMQChannel> channelList;
@ -134,13 +134,13 @@ FairMQMap ptreeToMQMap(const boost::property_tree::ptree& pt, const std::string&
std::stringstream socket; std::stringstream socket;
socket << "\t \t [node = " << r.first socket << "\t \t [node = " << r.first
<< "] socket index = " << socketCounter; << "] socket index = " << socketCounter;
MQLOG(DEBUG) << socket.str(); LOG(DEBUG) << socket.str();
MQLOG(DEBUG) << "\t \t \t type = " << r.second.get<std::string>("type", channel.GetType()); LOG(DEBUG) << "\t \t \t type = " << r.second.get<std::string>("type", channel.GetType());
MQLOG(DEBUG) << "\t \t \t method = " << r.second.get<std::string>("method", channel.GetMethod()); LOG(DEBUG) << "\t \t \t method = " << r.second.get<std::string>("method", channel.GetMethod());
MQLOG(DEBUG) << "\t \t \t address = " << r.second.get<std::string>("address", channel.GetAddress()); LOG(DEBUG) << "\t \t \t address = " << r.second.get<std::string>("address", channel.GetAddress());
MQLOG(DEBUG) << "\t \t \t sndBufSize = " << r.second.get<int>("sndBufSize", channel.GetSndBufSize()); LOG(DEBUG) << "\t \t \t sndBufSize = " << r.second.get<int>("sndBufSize", channel.GetSndBufSize());
MQLOG(DEBUG) << "\t \t \t rcvBufSize = " << r.second.get<int>("rcvBufSize", channel.GetRcvBufSize()); LOG(DEBUG) << "\t \t \t rcvBufSize = " << r.second.get<int>("rcvBufSize", channel.GetRcvBufSize());
MQLOG(DEBUG) << "\t \t \t rateLogging = " << r.second.get<int>("rateLogging", channel.GetRateLogging()); LOG(DEBUG) << "\t \t \t rateLogging = " << r.second.get<int>("rateLogging", channel.GetRateLogging());
channel.UpdateType(r.second.get<std::string>("type", channel.GetType())); channel.UpdateType(r.second.get<std::string>("type", channel.GetType()));
channel.UpdateMethod(r.second.get<std::string>("method", channel.GetMethod())); channel.UpdateMethod(r.second.get<std::string>("method", channel.GetMethod()));
@ -159,16 +159,16 @@ FairMQMap ptreeToMQMap(const boost::property_tree::ptree& pt, const std::string&
if (MQChannelMap.size() > 0) if (MQChannelMap.size() > 0)
{ {
MQLOG(DEBUG) << "---- Channel-keys found are :"; LOG(DEBUG) << "---- Channel-keys found are :";
for (const auto& p : MQChannelMap) for (const auto& p : MQChannelMap)
{ {
MQLOG(DEBUG) << p.first; LOG(DEBUG) << p.first;
} }
} }
else else
{ {
MQLOG(WARN) << "---- No channel-keys found for device-id " << deviceId; LOG(WARN) << "---- No channel-keys found for device-id " << deviceId;
MQLOG(WARN) << "---- Check the "<< formatFlag << " inputs and/or command line inputs"; LOG(WARN) << "---- Check the "<< formatFlag << " inputs and/or command line inputs";
} }
return MQChannelMap; return MQChannelMap;
} }

View File

@ -57,8 +57,17 @@ int FairMQProgOptions::ParseAll(const int argc, char** argv, bool AllowUnregiste
} }
// set log level before printing (default is 0 = DEBUG level) // set log level before printing (default is 0 = DEBUG level)
int verbose=GetValue<int>("verbose"); std::string verbose=GetValue<std::string>("verbose");
SET_LOGGER_LEVEL(verbose); //SET_LOG_LEVEL(DEBUG);
if(fSeverity_map.count(verbose))
{
set_global_log_level(log_op::operation::GREATER_EQ_THAN,fSeverity_map.at(verbose));
}
else
{
LOG(ERROR)<<" verbosity level '"<<verbose<<"' unknown, it will be set to INFO";
set_global_log_level(log_op::operation::GREATER_EQ_THAN,fSeverity_map.at("RESULTS"));
}
PrintOptions(); PrintOptions();

View File

@ -45,7 +45,7 @@ public:
} }
catch (std::exception& e) catch (std::exception& e)
{ {
MQLOG(ERROR) << e.what(); LOG(ERROR) << e.what();
return 1; return 1;
} }
return 0; return 0;

View File

@ -27,23 +27,34 @@ FairProgOptions::FairProgOptions() :
fCmdline_options("Command line options"), fCmdline_options("Command line options"),
fConfig_file_options("Configuration file options"), fConfig_file_options("Configuration file options"),
fVisible_options("Visible options"), fVisible_options("Visible options"),
fVerboseLvl(0), fUseConfigFile(false), fConfigFile() fVerboseLvl("INFO"), fUseConfigFile(false), fConfigFile()
{ {
// ////////////////////////////////// // //////////////////////////////////
// define generic options // define generic options
fGenericDesc.add_options() fGenericDesc.add_options()
("help,h", "produce help") ("help,h", "produce help")
("version,v", "print version") ("version,v", "print version")
("verbose", po::value<int>(&fVerboseLvl)->default_value(0), "Verbosity level : \n" ("verbose", po::value<std::string>(&fVerboseLvl)->default_value("INFO"), "Verbosity level : \n"
" 0=DEBUG \n" " TRACE \n"
" 1=INFO \n" " DEBUG \n"
" 2=WARN \n" " RESULTS \n"
" 3=ERROR \n" " INFO \n"
" 4=STATE \n" " WARN \n"
" 5=NOLOG" " ERROR \n"
" STATE \n"
" NOLOG"
) )
; ;
fSeverity_map["TRACE"] = fairmq::severity_level::TRACE;
fSeverity_map["DEBUG"] = fairmq::severity_level::DEBUG;
fSeverity_map["RESULTS"] = fairmq::severity_level::RESULTS;
fSeverity_map["INFO"] = fairmq::severity_level::INFO;
fSeverity_map["WARN"] = fairmq::severity_level::WARN;
fSeverity_map["ERROR"] = fairmq::severity_level::ERROR;
fSeverity_map["STATE"] = fairmq::severity_level::STATE;
fSeverity_map["NOLOG"] = fairmq::severity_level::NOLOG;
} }
FairProgOptions::~FairProgOptions() FairProgOptions::~FairProgOptions()
@ -255,8 +266,8 @@ std::string FairProgOptions::GetStringValue(const std::string& key)
} }
catch(std::exception& e) catch(std::exception& e)
{ {
MQLOG(ERROR) << "Exception thrown for the key '" << key << "'"; LOG(ERROR) << "Exception thrown for the key '" << key << "'";
MQLOG(ERROR) << e.what(); LOG(ERROR) << e.what();
} }
return val_str; return val_str;
@ -320,7 +331,7 @@ int FairProgOptions::PrintOptions()
// formatting and printing // formatting and printing
MQLOG(INFO)<<std::setfill ('*') << std::setw (total_len+3)<<"*";// +3 because of string " = " LOG(INFO)<<std::setfill ('*') << std::setw (total_len+3)<<"*";// +3 because of string " = "
std::string PrintOptionsTitle=" Program options found "; std::string PrintOptionsTitle=" Program options found ";
int leftSpace_len=0; int leftSpace_len=0;
@ -342,11 +353,12 @@ int FairProgOptions::PrintOptions()
rightSpace_len=(total_len+3)/2-rightTitle_shift_len; rightSpace_len=(total_len+3)/2-rightTitle_shift_len;
MQLOG(INFO) <<std::setfill ('*') << std::setw(leftSpace_len) <<"*" LOG(INFO) << std::setfill ('*') << std::setw(leftSpace_len) <<"*"
<< std::setfill(' ')
<< std::setw(PrintOptionsTitle.length()) << PrintOptionsTitle << std::setw(PrintOptionsTitle.length()) << PrintOptionsTitle
<< std::setfill ('*') << std::setw(rightSpace_len) <<"*"; << std::setfill ('*') << std::setw(rightSpace_len) <<"*";
MQLOG(INFO) <<std::setfill ('*') << std::setw (total_len+3)<<"*"; LOG(INFO) <<std::setfill ('*') << std::setw (total_len+3)<<"*";
for (const auto& p : mapinfo) for (const auto& p : mapinfo)
{ {
@ -357,7 +369,8 @@ int FairProgOptions::PrintOptions()
std::string empty_str; std::string empty_str;
key_str=p.first; key_str=p.first;
std::tie(val_str,typeInfo_str,default_str,empty_str)=p.second; std::tie(val_str,typeInfo_str,default_str,empty_str)=p.second;
MQLOG(INFO) << std::setw(maxlen_1st)<<std::left LOG(INFO) << std::setfill(' ')
<< std::setw(maxlen_1st)<<std::left
<< p.first << " = " << p.first << " = "
<< std::setw(maxlen_2nd) << std::setw(maxlen_2nd)
<< val_str << val_str
@ -368,7 +381,7 @@ int FairProgOptions::PrintOptions()
<< std::setw(maxlen_empty) << std::setw(maxlen_empty)
<< empty_str; << empty_str;
} }
MQLOG(INFO)<<std::setfill ('*') << std::setw (total_len+3)<<"*";// +3 for " = " LOG(INFO)<<std::setfill ('*') << std::setw (total_len+3)<<"*";// +3 for " = "
return 0; return 0;
} }

View File

@ -137,7 +137,10 @@ protected:
// Description which is printed in help command line // Description which is printed in help command line
po::options_description fVisible_options; po::options_description fVisible_options;
int fVerboseLvl; // to handle logger severity
std::map<std::string,fairmq::severity_level> fSeverity_map;
std::string fVerboseLvl;
bool fUseConfigFile; bool fUseConfigFile;
boost::filesystem::path fConfigFile; boost::filesystem::path fConfigFile;
virtual int NotifySwitchOption(); virtual int NotifySwitchOption();