diff --git a/include/Common/Daemon.h b/include/Common/Daemon.h index 70caf8f..1b8592a 100644 --- a/include/Common/Daemon.h +++ b/include/Common/Daemon.h @@ -5,6 +5,7 @@ #include "SimpleLog.h" #include +#include #include @@ -35,13 +36,15 @@ class Daemon // takes as input main(argc,argv) command line parameters, and custom configuration options. // accepted command line parameters: // -z configFileURI : load provided config file (e.g. file:../.../daemon.cfg. See Configuration.h for URI specs) - // -okey=value : set a given key/value pair parameter. + // -o key=value : set a given key/value pair parameter. // // Configuration options may are overwritten by those provided in [daemon] section of the config file, if any. // There is a 1-to-1 match between key names and members of the DaemonConfigParameters class. // i.e. user name running the daemon can be set by passing a pointer to a custom DaemonConfigParameters object, // but it is overwritten by value of [daemon] userName key in configuration file, if defined. - Daemon(int argc = 0, char* argv[] = nullptr, DaemonConfigParameters* = nullptr); + // Optional argument "extraCommandLineOptions" gives a list of extra option keys (-o key=value) accepted on the command line. + // They are parsed and made available in the "execOptions" variable after initialization. + Daemon(int argc = 0, char* argv[] = nullptr, DaemonConfigParameters* = nullptr, std::vector extraCommandLineOptions = {}); // destructor virtual ~Daemon(); @@ -69,6 +72,13 @@ class Daemon SimpleLog log; // object for output logging. ConfigFile config; // input configuration file, if any. Loaded if path provided on command line. + struct ConfigOption { + std::string key; + std::string value; + }; + + std::vector execOptions; // options extracted from command line arguments (-o key=value) + // check daemon status (e.g. after constructor, before starting main loop by calling run(), to know if init success) bool isOk(); diff --git a/src/Daemon.cxx b/src/Daemon.cxx index fd55293..2d7f1d5 100644 --- a/src/Daemon.cxx +++ b/src/Daemon.cxx @@ -73,7 +73,7 @@ void print_usage() printf("\n"); } -Daemon::Daemon(int argc, char* argv[], DaemonConfigParameters* dConfigParams) +Daemon::Daemon(int argc, char* argv[], DaemonConfigParameters* dConfigParams, std::vector extraCommandLineOptions) { isInitialized = 0; @@ -127,8 +127,18 @@ Daemon::Daemon(int argc, char* argv[], DaemonConfigParameters* dConfigParams) } else if (key == "logRotateNow") { params.logRotateNow = std::stoi(value); } else { - log.error("Unkown option key %s in option %s", key.c_str(), optarg); - throw __LINE__; + bool keyOk = 0; + for (auto const& k : extraCommandLineOptions) { + if (k == key) { + keyOk = 1; + execOptions.push_back({ key, value }); + break; + } + } + if (!keyOk) { + log.error("Unkown option key %s in option %s", key.c_str(), optarg); + throw __LINE__; + } } } break;