Subscribe DeviceRunner for config properties

This commit is contained in:
Alexey Rybalchenko 2019-06-11 13:05:27 +02:00 committed by Dennis Klein
parent 7c9744760e
commit 4281d7b27e
4 changed files with 91 additions and 42 deletions

View File

@ -12,6 +12,7 @@
#include <fairmq/Tools.h> #include <fairmq/Tools.h>
#include <fairmq/Version.h> #include <fairmq/Version.h>
using namespace std;
using namespace fair::mq; using namespace fair::mq;
DeviceRunner::DeviceRunner(int argc, char* const argv[], bool printLogo) DeviceRunner::DeviceRunner(int argc, char* const argv[], bool printLogo)
@ -23,6 +24,78 @@ DeviceRunner::DeviceRunner(int argc, char* const argv[], bool printLogo)
, fEvents() , fEvents()
{} {}
bool DeviceRunner::HandleGeneralOptions()
{
if (fConfig.Count("help")) {
fConfig.PrintHelp();
return false;
}
if (fConfig.Count("print-options")) {
fConfig.PrintOptionsRaw();
return false;
}
if (fConfig.Count("print-channels") || fConfig.Count("version")) {
fair::Logger::SetConsoleSeverity("nolog");
} else {
string severity = fConfig.GetProperty<string>("severity");
string logFile = fConfig.GetProperty<string>("log-to-file");
string logFileSeverity = fConfig.GetProperty<string>("file-severity");
bool color = fConfig.GetProperty<bool>("color");
string verbosity = fConfig.GetProperty<string>("verbosity");
fair::Logger::SetVerbosity(verbosity);
if (logFile != "") {
fair::Logger::InitFileSink(logFileSeverity, logFile);
fair::Logger::SetConsoleSeverity("nolog");
} else {
fair::Logger::SetConsoleColor(color);
fair::Logger::SetConsoleSeverity(severity);
}
if (fPrintLogo) {
LOG(info) << endl
<< " ______ _ _______ _________ " << endl
<< " / ____/___ _(_)_______ |/ /_ __ \\ version " << FAIRMQ_GIT_VERSION << endl
<< " / /_ / __ `/ / ___/__ /|_/ /_ / / / build " << FAIRMQ_BUILD_TYPE << endl
<< " / __/ / /_/ / / / _ / / / / /_/ / " << FAIRMQ_REPO_URL << endl
<< " /_/ \\__,_/_/_/ /_/ /_/ \\___\\_\\ " << FAIRMQ_LICENSE << " © " << FAIRMQ_COPYRIGHT << endl;
}
fConfig.PrintOptions();
}
return true;
}
void DeviceRunner::SubscribeForConfigChange()
{
fConfig.Subscribe<bool>("device-runner", [](const std::string& key, const bool val) {
if (key == "color") {
fair::Logger::SetConsoleColor(val);
}
});
fConfig.Subscribe<string>("device-runner", [&](const std::string& key, const std::string val) {
if (key == "severity") {
fair::Logger::SetConsoleSeverity(val);
} else if (key == "file-severity") {
fair::Logger::SetFileSeverity(val);
} else if (key == "verbosity") {
fair::Logger::SetVerbosity(val);
} else if (key == "log-to-file") {
string fileSeverity = fConfig.GetProperty<string>("file-severity");
fair::Logger::InitFileSink(fileSeverity, val);
}
});
}
void DeviceRunner::UnsubscribeFromConfigChange()
{
fConfig.Unsubscribe<bool>("device-runner");
fConfig.Unsubscribe<string>("device-runner");
}
auto DeviceRunner::Run() -> int auto DeviceRunner::Run() -> int
{ {
fPluginManager.LoadPlugin("s:config"); fPluginManager.LoadPlugin("s:config");
@ -49,48 +122,15 @@ auto DeviceRunner::Run() -> int
fConfig.ParseAll(fRawCmdLineArgs, true); fConfig.ParseAll(fRawCmdLineArgs, true);
if (fConfig.Count("help")) { if (!HandleGeneralOptions()) {
fConfig.PrintHelp();
return 0; return 0;
} }
if (fConfig.Count("print-options")) {
fConfig.PrintOptionsRaw();
return 0;
}
if (fConfig.Count("print-channels") || fConfig.Count("version")) {
fair::Logger::SetConsoleSeverity("nolog");
} else {
std::string severity = fConfig.GetProperty<std::string>("severity");
std::string logFile = fConfig.GetProperty<std::string>("log-to-file");
bool color = fConfig.GetProperty<bool>("color");
std::string verbosity = fConfig.GetProperty<std::string>("verbosity");
fair::Logger::SetVerbosity(verbosity);
if (logFile != "") {
fair::Logger::InitFileSink(severity, logFile);
fair::Logger::SetConsoleSeverity("nolog");
} else {
fair::Logger::SetConsoleColor(color);
fair::Logger::SetConsoleSeverity(severity);
}
if (fPrintLogo) {
LOG(info) << std::endl
<< " ______ _ _______ _________ " << std::endl
<< " / ____/___ _(_)_______ |/ /_ __ \\ version " << FAIRMQ_GIT_VERSION << std::endl
<< " / /_ / __ `/ / ___/__ /|_/ /_ / / / build " << FAIRMQ_BUILD_TYPE << std::endl
<< " / __/ / /_/ / / / _ / / / / /_/ / " << FAIRMQ_REPO_URL << std::endl
<< " /_/ \\__,_/_/_/ /_/ /_/ \\___\\_\\ " << FAIRMQ_LICENSE << " © " << FAIRMQ_COPYRIGHT << std::endl;
}
fConfig.PrintOptions();
}
fConfig.Notify(); fConfig.Notify();
// handle configuration updates (for general options)
SubscribeForConfigChange();
////// CALL HOOK /////// ////// CALL HOOK ///////
fEvents.Emit<hooks::InstantiateDevice>(*this); fEvents.Emit<hooks::InstantiateDevice>(*this);
//////////////////////// ////////////////////////
@ -112,8 +152,8 @@ auto DeviceRunner::Run() -> int
// Handle --version // Handle --version
if (fConfig.Count("version")) { if (fConfig.Count("version")) {
std::cout << "FairMQ version: " << FAIRMQ_GIT_VERSION << std::endl; cout << "FairMQ version: " << FAIRMQ_GIT_VERSION << endl;
std::cout << "User device version: " << fDevice->GetVersion() << std::endl; cout << "User device version: " << fDevice->GetVersion() << endl;
fDevice->ChangeState(fair::mq::Transition::End); fDevice->ChangeState(fair::mq::Transition::End);
return 0; return 0;
} }
@ -135,6 +175,9 @@ auto DeviceRunner::Run() -> int
// Wait for control plugin to release device control // Wait for control plugin to release device control
fPluginManager.WaitForPluginsToReleaseDeviceControl(); fPluginManager.WaitForPluginsToReleaseDeviceControl();
// stop handling configuration updates (for general options)
UnsubscribeFromConfigChange();
return 0; return 0;
} }
@ -142,7 +185,7 @@ auto DeviceRunner::RunWithExceptionHandlers() -> int
{ {
try { try {
return Run(); return Run();
} catch (std::exception& e) { } catch (exception& e) {
LOG(error) << "Uncaught exception reached the top of DeviceRunner: " << e.what(); LOG(error) << "Uncaught exception reached the top of DeviceRunner: " << e.what();
return 1; return 1;
} catch (...) { } catch (...) {

View File

@ -56,6 +56,11 @@ class DeviceRunner
auto Run() -> int; auto Run() -> int;
auto RunWithExceptionHandlers() -> int; auto RunWithExceptionHandlers() -> int;
bool HandleGeneralOptions();
void SubscribeForConfigChange();
void UnsubscribeFromConfigChange();
template<typename H> template<typename H>
auto AddHook(std::function<void(DeviceRunner&)> hook) -> void auto AddHook(std::function<void(DeviceRunner&)> hook) -> void
{ {

View File

@ -79,7 +79,8 @@ ProgOptions::ProgOptions()
fAllOptions.add_options() fAllOptions.add_options()
("help,h", "Print help") ("help,h", "Print help")
("version,v", "Print version") ("version,v", "Print version")
("severity", po::value<string>()->default_value("debug"), "Log severity level: trace, debug, info, state, warn, error, fatal, nolog") ("severity", po::value<string>()->default_value("debug"), "Log severity level (console): trace, debug, info, state, warn, error, fatal, nolog")
("file-severity", po::value<string>()->default_value("debug"), "Log severity level (file): trace, debug, info, state, warn, error, fatal, nolog")
("verbosity", po::value<string>()->default_value("medium"), "Log verbosity level: veryhigh, high, medium, low") ("verbosity", po::value<string>()->default_value("medium"), "Log verbosity level: veryhigh, high, medium, low")
("color", po::value<bool >()->default_value(true), "Log color (true/false)") ("color", po::value<bool >()->default_value(true), "Log color (true/false)")
("log-to-file", po::value<string>()->default_value(""), "Log output to a file.") ("log-to-file", po::value<string>()->default_value(""), "Log output to a file.")

View File

@ -61,7 +61,7 @@ Plugin::ProgOptions ConfigPluginProgramOptions()
namespace po = boost::program_options; namespace po = boost::program_options;
auto pluginOptions = po::options_description{"FairMQ device options"}; auto pluginOptions = po::options_description{"FairMQ device options"};
pluginOptions.add_options() pluginOptions.add_options()
("id", po::value<string >()->default_value(""), "Device ID (required argument).") ("id", po::value<string >()->default_value(""), "Device ID.")
("io-threads", po::value<int >()->default_value(1), "Number of I/O threads.") ("io-threads", po::value<int >()->default_value(1), "Number of I/O threads.")
("transport", po::value<string >()->default_value("zeromq"), "Transport ('zeromq'/'nanomsg'/'shmem').") ("transport", po::value<string >()->default_value("zeromq"), "Transport ('zeromq'/'nanomsg'/'shmem').")
("network-interface", po::value<string >()->default_value("default"), "Network interface to bind on (e.g. eth0, ib0..., default will try to detect the interface of the default route).") ("network-interface", po::value<string >()->default_value("default"), "Network interface to bind on (e.g. eth0, ib0..., default will try to detect the interface of the default route).")