diff --git a/fairmq/DeviceRunner.cxx b/fairmq/DeviceRunner.cxx index dcdaedde..465997bf 100644 --- a/fairmq/DeviceRunner.cxx +++ b/fairmq/DeviceRunner.cxx @@ -12,6 +12,7 @@ #include #include +using namespace std; using namespace fair::mq; DeviceRunner::DeviceRunner(int argc, char* const argv[], bool printLogo) @@ -23,6 +24,78 @@ DeviceRunner::DeviceRunner(int argc, char* const argv[], bool printLogo) , 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("severity"); + string logFile = fConfig.GetProperty("log-to-file"); + string logFileSeverity = fConfig.GetProperty("file-severity"); + bool color = fConfig.GetProperty("color"); + + string verbosity = fConfig.GetProperty("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("device-runner", [](const std::string& key, const bool val) { + if (key == "color") { + fair::Logger::SetConsoleColor(val); + } + }); + fConfig.Subscribe("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("file-severity"); + fair::Logger::InitFileSink(fileSeverity, val); + } + }); +} +void DeviceRunner::UnsubscribeFromConfigChange() +{ + fConfig.Unsubscribe("device-runner"); + fConfig.Unsubscribe("device-runner"); +} + auto DeviceRunner::Run() -> int { fPluginManager.LoadPlugin("s:config"); @@ -49,48 +122,15 @@ auto DeviceRunner::Run() -> int fConfig.ParseAll(fRawCmdLineArgs, true); - if (fConfig.Count("help")) { - fConfig.PrintHelp(); + if (!HandleGeneralOptions()) { 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("severity"); - std::string logFile = fConfig.GetProperty("log-to-file"); - bool color = fConfig.GetProperty("color"); - - std::string verbosity = fConfig.GetProperty("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(); + // handle configuration updates (for general options) + SubscribeForConfigChange(); + ////// CALL HOOK /////// fEvents.Emit(*this); //////////////////////// @@ -112,8 +152,8 @@ auto DeviceRunner::Run() -> int // Handle --version if (fConfig.Count("version")) { - std::cout << "FairMQ version: " << FAIRMQ_GIT_VERSION << std::endl; - std::cout << "User device version: " << fDevice->GetVersion() << std::endl; + cout << "FairMQ version: " << FAIRMQ_GIT_VERSION << endl; + cout << "User device version: " << fDevice->GetVersion() << endl; fDevice->ChangeState(fair::mq::Transition::End); return 0; } @@ -135,6 +175,9 @@ auto DeviceRunner::Run() -> int // Wait for control plugin to release device control fPluginManager.WaitForPluginsToReleaseDeviceControl(); + // stop handling configuration updates (for general options) + UnsubscribeFromConfigChange(); + return 0; } @@ -142,7 +185,7 @@ auto DeviceRunner::RunWithExceptionHandlers() -> int { try { return Run(); - } catch (std::exception& e) { + } catch (exception& e) { LOG(error) << "Uncaught exception reached the top of DeviceRunner: " << e.what(); return 1; } catch (...) { diff --git a/fairmq/DeviceRunner.h b/fairmq/DeviceRunner.h index f129fa1b..4e6a1267 100644 --- a/fairmq/DeviceRunner.h +++ b/fairmq/DeviceRunner.h @@ -56,6 +56,11 @@ class DeviceRunner auto Run() -> int; auto RunWithExceptionHandlers() -> int; + bool HandleGeneralOptions(); + + void SubscribeForConfigChange(); + void UnsubscribeFromConfigChange(); + template auto AddHook(std::function hook) -> void { diff --git a/fairmq/ProgOptions.cxx b/fairmq/ProgOptions.cxx index 0355a72f..67cb8319 100644 --- a/fairmq/ProgOptions.cxx +++ b/fairmq/ProgOptions.cxx @@ -79,7 +79,8 @@ ProgOptions::ProgOptions() fAllOptions.add_options() ("help,h", "Print help") ("version,v", "Print version") - ("severity", po::value()->default_value("debug"), "Log severity level: trace, debug, info, state, warn, error, fatal, nolog") + ("severity", po::value()->default_value("debug"), "Log severity level (console): trace, debug, info, state, warn, error, fatal, nolog") + ("file-severity", po::value()->default_value("debug"), "Log severity level (file): trace, debug, info, state, warn, error, fatal, nolog") ("verbosity", po::value()->default_value("medium"), "Log verbosity level: veryhigh, high, medium, low") ("color", po::value()->default_value(true), "Log color (true/false)") ("log-to-file", po::value()->default_value(""), "Log output to a file.") diff --git a/fairmq/plugins/config/Config.cxx b/fairmq/plugins/config/Config.cxx index 0dcbb8d0..ea0e1ac2 100644 --- a/fairmq/plugins/config/Config.cxx +++ b/fairmq/plugins/config/Config.cxx @@ -61,7 +61,7 @@ Plugin::ProgOptions ConfigPluginProgramOptions() namespace po = boost::program_options; auto pluginOptions = po::options_description{"FairMQ device options"}; pluginOptions.add_options() - ("id", po::value()->default_value(""), "Device ID (required argument).") + ("id", po::value()->default_value(""), "Device ID.") ("io-threads", po::value()->default_value(1), "Number of I/O threads.") ("transport", po::value()->default_value("zeromq"), "Transport ('zeromq'/'nanomsg'/'shmem').") ("network-interface", po::value()->default_value("default"), "Network interface to bind on (e.g. eth0, ib0..., default will try to detect the interface of the default route).")