Simplify logger filtering options, fix log error double printing issue

This commit is contained in:
winckler 2016-09-16 11:46:51 +02:00 committed by Mohammad Al-Turany
parent 9ceab6099c
commit e0a03242ac
11 changed files with 527 additions and 278 deletions

View File

@ -107,6 +107,7 @@ EndIf(NANOMSG_FOUND)
Set(FAIRMQHEADERS
FairMQParts.h
options/FairProgOptionsHelper.h
options/FairMQEventManager.h
tools/FairMQTools.h
tools/FairMQDDSTools.h
tools/runSimpleMQStateMachine.h
@ -156,6 +157,7 @@ Set(Exe_Names
splitter
merger
proxy
runConfigExample
)
If(DDS_FOUND)
@ -171,6 +173,7 @@ Set(Exe_Source
run/runSplitter.cxx
run/runMerger.cxx
run/runProxy.cxx
options/runConfigEx.cxx
)
If(DDS_FOUND)
@ -191,3 +194,7 @@ ForEach(_file RANGE 0 ${_length})
Set(DEPENDENCIES FairMQ)
GENERATE_EXECUTABLE()
EndForEach(_file RANGE 0 ${_length})
configure_file( ${CMAKE_SOURCE_DIR}/fairmq/options/startConfigExample.sh.in
${CMAKE_BINARY_DIR}/bin/startConfigExample.sh )

View File

@ -16,6 +16,35 @@
#define FAIRMQLOGGER_H_
#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,
WARN,
ERROR,
STATE,
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)
*/
typedef unsigned long long timestamp_t;
timestamp_t get_timestamp();

View File

@ -33,29 +33,56 @@ 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;
global_logger.add_attribute("TimeStamp", attrs::local_clock());
init_log_console();
DefaultConsoleInit();
return global_logger;
}
void init_log_console(bool color/* = true*/)
namespace FairMQ{
namespace Logger{
std::vector<boost::shared_ptr< boost::log::sinks::basic_sink_frontend > >sinkList;// global var
}}//end namespace
void RemoveRegisteredSinks()
{
if(FairMQ::Logger::sinkList.size() > 0)
{
for(const auto& sink : FairMQ::Logger::sinkList)
logging::core::get()->remove_sink(sink);
FairMQ::Logger::sinkList.clear();
}
}
void reinit_logger(bool color, const std::string& filename, custom_severity_level threshold)
{
BOOST_LOG_SEV(global_logger::get(), custom_severity_level::NOLOG) << "";
RemoveRegisteredSinks();
DefaultConsoleInit(color);
if(threshold!=SEVERITY_NOLOG)
{
if(!filename.empty())
{
DefaultAddFileSink(filename,threshold);
}
}
}
// console sink related functions
void DefaultConsoleInit(bool color/* = true*/)
{
// add a text sink
typedef sinks::synchronous_sink<sinks::text_ostream_backend> text_sink;
logging::core::get()->remove_all_sinks();
RemoveRegisteredSinks();
// CONSOLE - all severity except error
boost::shared_ptr<text_sink> sink = boost::make_shared<text_sink>();
@ -65,11 +92,11 @@ void init_log_console(bool color/* = true*/)
// specify the format of the log message
if (color)
{
sink->set_formatter(&init_log_formatter<tag_console>);
sink->set_formatter(&InitLogFormatter<tag_console>);
}
else
{
sink->set_formatter(&init_log_formatter<tag_file>);
sink->set_formatter(&InitLogFormatter<tag_file>);
}
sink->set_filter(severity != SEVERITY_ERROR && severity < SEVERITY_NOLOG);
@ -83,11 +110,11 @@ void init_log_console(bool color/* = true*/)
if (color)
{
sink_error->set_formatter(&init_log_formatter<tag_console>);
sink_error->set_formatter(&InitLogFormatter<tag_console>);
}
else
{
sink_error->set_formatter(&init_log_formatter<tag_file>);
sink_error->set_formatter(&InitLogFormatter<tag_file>);
}
sink_error->set_filter(severity == SEVERITY_ERROR);
@ -95,24 +122,52 @@ void init_log_console(bool color/* = true*/)
logging::core::get()->add_sink(sink_error);
}
void reinit_logger(bool color)
int DefaultConsoleSetFilter(custom_severity_level threshold)
{
LOG(NOLOG) << "";
logging::core::get()->remove_all_sinks();
init_log_console(color);
if(FairMQ::Logger::sinkList.size()>=2)
{
FairMQ::Logger::sinkList.at(0)->set_filter([threshold](const boost::log::attribute_value_set& attr_set)
{
auto sev = attr_set["Severity"].extract<custom_severity_level>();
auto mainConsoleSinkCondition = (sev != SEVERITY_ERROR) && (sev < SEVERITY_NOLOG);
return mainConsoleSinkCondition && (sev>=threshold);
});
FairMQ::Logger::sinkList.at(1)->set_filter([threshold](const boost::log::attribute_value_set& attr_set)
{
auto sev = attr_set["Severity"].extract<custom_severity_level>();
auto errorConsoleSinkCondition = sev == SEVERITY_ERROR;
return errorConsoleSinkCondition && (sev>=threshold);
});
return 0;
}
else
{
return 1;
}
void init_log_file(const std::string& filename, custom_severity_level threshold, log_op::operation op, const std::string& id)
return 0;
}
// file sink related functions
void DefaultAddFileSink(const std::string& filename, custom_severity_level threshold)
{
// 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>(
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,
// rotate at midnight every day
boost::log::keywords::time_based_rotation = sinks::file::rotation_at_time_point(0, 0, 0),
boost::log::keywords::time_based_rotation = boost::log::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
@ -121,142 +176,7 @@ void init_log_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));
// 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:
FairMQ::Logger::sinkList.at(0)->set_filter(severity == threshold);
// boost::log::core::get()->set_filter(severity == threshold);
break;
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:
FairMQ::Logger::sinkList.at(0)->set_filter(severity >= threshold);
// boost::log::core::get()->set_filter(severity >= threshold);
break;
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:
FairMQ::Logger::sinkList.at(0)->set_filter(severity <= threshold);
// 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);
}

View File

@ -36,6 +36,7 @@
#include <boost/log/sinks/text_file_backend.hpp>
#include <boost/log/sinks/sync_frontend.hpp>
#include <boost/log/sinks/basic_sink_frontend.hpp>
// fairmq
#include "logger_def.h"
@ -45,35 +46,6 @@
// 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(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,
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);
#if defined(__GNUC__) || defined(__GNUG__)
#pragma GCC diagnostic push
@ -90,8 +62,19 @@ BOOST_LOG_ATTRIBUTE_KEYWORD(severity, "Severity", custom_severity_level)
#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 init_log_formatter(const boost::log::record_view &view, boost::log::formatting_ostream &os)
void InitLogFormatter(const boost::log::record_view &view, boost::log::formatting_ostream &os)
{
os << "[";
@ -116,6 +99,53 @@ void init_log_formatter(const boost::log::record_view &view, boost::log::formatt
<< 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
typedef boost::log::sinks::text_file_backend sink_backend_t;
typedef boost::log::sinks::synchronous_sink<sink_backend_t> sink_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
// global macros (core). Level filters are set globally here, that is to all register sinks
@ -128,20 +158,8 @@ void init_log_formatter(const boost::log::record_view &view, boost::log::formatt
#define MQLOG(severity) BOOST_LOG_SEV(global_logger::get(),custom_severity_level::severity)
#endif
#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);
#define SET_LOG_CONSOLE_LEVEL(loglevel) DefaultConsoleSetFilter(custom_severity_level::loglevel)
#define ADD_LOG_FILESINK(filename,loglevel) DefaultAddFileSink(filename, custom_severity_level::loglevel)
// Use : SET_LOG_CONSOLE_LEVEL(INFO); ADD_LOG_FILESINK(filename,ERROR);
#endif

View File

@ -21,6 +21,8 @@
_Pragma("GCC diagnostic pop")
#endif
#include <boost/log/support/date_time.hpp>
void test_logger()
{
LOG(TRACE) << "this is a trace message";
@ -32,34 +34,34 @@
LOG(STATE) << "this is a state message";
}
void test_set_level()
void test_console_level()
{
std::cout<<"********* test logger : SET_LOG_LEVEL(lvl) *********"<<std::endl;
SET_LOG_LEVEL(TRACE);
std::cout<<"********* test logger : SET_LOG_CONSOLE_LEVEL(lvl) *********"<<std::endl;
SET_LOG_CONSOLE_LEVEL(TRACE);
test_logger();
std::cout << "----------------------------"<<std::endl;
SET_LOG_LEVEL(DEBUG);
SET_LOG_CONSOLE_LEVEL(DEBUG);
test_logger();
std::cout << "----------------------------"<<std::endl;
SET_LOG_LEVEL(RESULTS);
SET_LOG_CONSOLE_LEVEL(RESULTS);
test_logger();
std::cout << "----------------------------"<<std::endl;
SET_LOG_LEVEL(INFO);
SET_LOG_CONSOLE_LEVEL(INFO);
test_logger();
std::cout << "----------------------------"<<std::endl;
SET_LOG_LEVEL(WARN);
SET_LOG_CONSOLE_LEVEL(WARN);
test_logger();
std::cout << "----------------------------"<<std::endl;
SET_LOG_LEVEL(ERROR);
SET_LOG_CONSOLE_LEVEL(ERROR);
test_logger();
std::cout << "----------------------------"<<std::endl;
SET_LOG_LEVEL(STATE);
SET_LOG_CONSOLE_LEVEL(STATE);
test_logger();
std::cout << "----------------------------"<<std::endl;
}
@ -68,12 +70,48 @@
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();
test_console_level();
SET_LOG_CONSOLE_LEVEL(INFO);
std::cout << "----------------------------"<<std::endl;
LOG(INFO)<<"open log file 1";
ADD_LOG_FILESINK("test_log1",ERROR);
test_logger();
std::cout << "----------------------------"<<std::endl;
LOG(INFO)<<"open log file 2";
ADD_LOG_FILESINK("test_log2",STATE);
test_logger();
// 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)
);
test_logger();
std::cout << "----------------------------"<<std::endl;
LOG(INFO)<<"set filter of last sink";// custom file sink setting
// get last added sink and reset filter to WARN and ERROR
FairMQ::Logger::sinkList.back()->set_filter([](const boost::log::attribute_value_set& attr_set)
{
auto sev = attr_set["Severity"].extract<custom_severity_level>();
return (sev == fairmq::WARN) || (sev == fairmq::ERROR);
});
test_logger();
// remove all sinks, and restart console sinks
reinit_logger(false);
test_set_level();
test_logger();
return 0;
}

View File

@ -15,6 +15,7 @@
#include "FairMQProgOptions.h"
#include <algorithm>
#include "FairMQParser.h"
#include "FairMQLogger.h"
using namespace std;
FairMQProgOptions::FairMQProgOptions()
@ -80,12 +81,14 @@ void FairMQProgOptions::ParseAll(const int argc, char** argv, bool allowUnregist
if (fSeverityMap.count(verbosity))
{
set_global_log_level(log_op::operation::GREATER_EQ_THAN, fSeverityMap.at(verbosity));
DefaultConsoleSetFilter(fSeverityMap.at(verbosity));// return 1 if not success
//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"));
//set_global_log_level(log_op::operation::GREATER_EQ_THAN, fSeverityMap.at("DEBUG"));
DefaultConsoleSetFilter(fSeverityMap.at("DEBUG"));
}

View File

@ -0,0 +1,223 @@
/********************************************************************************
* 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: runSamplerRoot.cxx
* Author: winckler
*/
// FairRoot - FairMQ
#include "FairMQLogger.h"
#include "FairMQProgOptions.h"
#include "FairMQDevice.h"
typedef std::unordered_map<std::string, std::vector<FairMQChannel>> FairMQMap;
class MyDevice : public FairMQDevice
{
public:
MyDevice() : rate(0.5) {}
virtual ~MyDevice() {}
void SetRate(double r){rate=r;}
void Print(){LOG(INFO)<<"[MyDevice] rate = "<<rate;}
private:
double rate;
};
void MyCallBack(MyDevice& d, double val)
{
d.SetRate(val);
d.Print();
}
void PrintMQParam(const FairMQMap& channels, const FairMQProgOptions& config)
{
for(const auto& p : channels)
{
int index = 0;
for(const auto& channel : p.second)
{
std::string typeKey = p.first + "." + std::to_string(index) + ".type";
std::string methodKey = p.first + "." + std::to_string(index) + ".method";
std::string addressKey = p.first + "." + std::to_string(index) + ".address";
std::string propertyKey = p.first + "." + std::to_string(index) + ".property";
std::string sndBufSizeKey = p.first + "." + std::to_string(index) + ".sndBufSize";
std::string rcvBufSizeKey = p.first + "." + std::to_string(index) + ".rcvBufSize";
std::string rateLoggingKey = p.first + "." + std::to_string(index) + ".rateLogging";
LOG(DEBUG) << "Channel name = "<<p.first;
LOG(DEBUG) << "key = " << typeKey <<"\t value = " << config.GetValue<std::string>(typeKey);
LOG(DEBUG) << "key = " << methodKey <<"\t value = " << config.GetValue<std::string>(methodKey);
LOG(DEBUG) << "key = " << addressKey <<"\t value = " << config.GetValue<std::string>(addressKey);
LOG(DEBUG) << "key = " << propertyKey <<"\t value = " << config.GetValue<std::string>(propertyKey);
LOG(DEBUG) << "key = " << sndBufSizeKey << "\t value = " << config.GetValue<int>(sndBufSizeKey);
LOG(DEBUG) << "key = " << rcvBufSizeKey <<"\t value = " << config.GetValue<int>(rcvBufSizeKey);
LOG(DEBUG) << "key = " << rateLoggingKey <<"\t value = " << config.GetValue<int>(rateLoggingKey);
}
}
}
int main(int argc, char** argv)
{
try
{
// create option manager object
FairMQProgOptions config;
// add key description to cmd line options
config.GetCmdLineOptions().add_options()
("data-rate", po::value<double>()->default_value(0.5), "Data rate");
// parse command lines, parse json file and init FairMQMap
config.ParseAll(argc, argv);
// get FairMQMap
auto map1 = config.GetFairMQMap();
// form keys from map1 and print the value stored in variable map
PrintMQParam(map1,config);
// update value in variable map, and propagate the update to the FairMQMap
config.UpdateValue("data.0.address","tcp://localhost:1234");
// get the updated FairMQMap
auto map2 = config.GetFairMQMap();
// modify one channel value
map2.at("data").at(0).UpdateSndBufSize(500);
// update the FairMQMap and propagate the change in variable map
config.UpdateChannelMap(map2);
// print values stored in variable map
PrintMQParam(map2,config);
MyDevice device;
device.CatchSignals();
device.SetConfig(config);
std::string blah = config.GetStringValue("data-rate");
double blah2 = config.ConvertTo<double>(blah);
LOG(INFO)<<"blah2 "<<blah2;
LOG(INFO)<<"---- Connect 1";
config.Subscribe<std::string >("data.0.address",[&device](const std::string& key, const std::string& value)
{
LOG(INFO) << "[Lambda] Update parameter (0) " << key << " = " << value;
device.fChannels.at("data").at(0).UpdateAddress(value);
});
std::string key1("data.0.address");
std::string value1("tcp://localhost:4321");
config.UpdateValue(key1,value1);
LOG(INFO)<<"device.fChannels.GetAddress = "<<device.fChannels.at("data").at(0).GetAddress();
LOG(INFO)<<"config.GetValue = "<<config.GetValue<std::string>(key1);
LOG(INFO)<<"---- Connect 2";
config.Subscribe<std::string>("data.0.method",[&device](const std::string& key, const std::string& value)
{
//value="abcd";
LOG(INFO) << "[Lambda] Update parameter " << key << " = " << value;
device.fChannels.at("data").at(0).UpdateMethod(value);
});
LOG(INFO)<<"---- Connect 3";
config.Subscribe<int>("data.0.rcvBufSize",[&device](const std::string& key, int value)
{
LOG(INFO) << "[Lambda] Update parameter " << key << " = " << value;
device.fChannels.at("data").at(0).UpdateRcvBufSize(value);
});
LOG(INFO)<<"---- Connect 4";
config.Subscribe<double>("data-rate",[&device](const std::string& key, double value)
{
LOG(INFO) << "[Lambda] Update parameter " << key << " = " << value;
device.SetRate(value);
});
std::string key2("data.0.rcvBufSize");
int value2(100);
std::string key3("data.0.method");
std::string value3("bind");
LOG(INFO)<<"-------------------- start update";
//config.EmitUpdate(key,value);
config.UpdateValue(key1,value1);
LOG(INFO)<<"device.fChannels.GetAddress = "<<device.fChannels.at("data").at(0).GetAddress();
LOG(INFO)<<"config.GetValue = "<<config.GetValue<std::string>(key1);
config.UpdateValue(key2,value2);
LOG(INFO)<<"device.fChannels.GetRcvBufSize = "<<device.fChannels.at("data").at(0).GetRcvBufSize();
LOG(INFO)<<"config.GetValue = "<<config.GetValue<int>(key2);
config.UpdateValue(key3,value3);
LOG(INFO)<<"device.fChannels.Method = "<<device.fChannels.at("data").at(0).GetMethod();
LOG(INFO)<<"config.GetValue = "<<config.GetValue<std::string>(key3);
device.Print();
double rate=0.9;
config.UpdateValue<double>("data-rate",rate);
LOG(INFO)<<"config.GetValue = "<<config.GetValue<double>("data-rate");
device.Print();
LOG(INFO)<<" double rate = " <<rate;
// advanced commands
LOG(INFO)<<"-------------------- start custom 1";
config.Connect<EventId::Custom, MyDevice&, double>("myNewKey",[](MyDevice& d, double val)
{
d.SetRate(val);
d.Print();
});
double value4=0.123;
config.Emit<EventId::Custom, MyDevice&, double>("myNewKey",device,value4);
LOG(INFO)<<"-------------------- start custom 2 with function";
config.Connect<EventId::Custom, MyDevice&, double>("function example",&MyCallBack);
value4=6.66;
config.Emit<EventId::Custom, MyDevice&, double>("function example",device,value4);
}
catch (std::exception& e)
{
LOG(ERROR) << "Unhandled Exception reached the top of main: "
<< e.what() << ", application will now exit";
return 1;
}
return 0;
}

View File

@ -0,0 +1,11 @@
#!/bin/bash
TRANSPORT="zeromq"
VERBOSE="DEBUG"
JSONCONFIGFILE="@CMAKE_BINARY_DIR@/bin/config/ex1-sampler-sink.json"
########################## start DEVICE
DEVICE="runConfigExample --transport $TRANSPORT --verbose $VERBOSE"
DEVICE+=" --id sampler1 --mq-config $JSONCONFIGFILE"
@CMAKE_BINARY_DIR@/bin/$DEVICE

View File

@ -41,7 +41,7 @@ int main(int argc, char** argv)
}
reinit_logger(false);
set_global_log_level(log_op::operation::GREATER_EQ_THAN, fairmq::NOLOG);
SET_LOG_CONSOLE_LEVEL(NOLOG);
testSub.SetProperty(FairMQTestSub::Id, "testSub_" + std::to_string(getpid()));

View File

@ -39,7 +39,7 @@ int main(int argc, char** argv)
}
reinit_logger(false);
set_global_log_level(log_op::operation::GREATER_EQ_THAN, fairmq::NOLOG);
SET_LOG_CONSOLE_LEVEL(NOLOG);
testPush.SetProperty(FairMQTestPush::Id, "testPush");

View File

@ -41,7 +41,7 @@ int main(int argc, char** argv)
}
reinit_logger(false);
set_global_log_level(log_op::operation::GREATER_EQ_THAN, fairmq::NOLOG);
SET_LOG_CONSOLE_LEVEL(NOLOG);
testReq.SetProperty(FairMQTestReq::Id, "testReq" + std::to_string(getpid()));