Skip to content

Commit

Permalink
Merge pull request #43 from sy-c/master
Browse files Browse the repository at this point in the history
daemon extra options
  • Loading branch information
sy-c authored Jan 22, 2021
2 parents 3006dfe + 5cdcd96 commit 82c24b3
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
14 changes: 12 additions & 2 deletions include/Common/Daemon.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include "SimpleLog.h"
#include <string>
#include <vector>

#include <Common/Configuration.h>

Expand Down Expand Up @@ -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<std::string> extraCommandLineOptions = {});

// destructor
virtual ~Daemon();
Expand Down Expand Up @@ -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<ConfigOption> 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();

Expand Down
16 changes: 13 additions & 3 deletions src/Daemon.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string> extraCommandLineOptions)
{
isInitialized = 0;

Expand Down Expand Up @@ -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;

Expand Down

0 comments on commit 82c24b3

Please sign in to comment.