diff --git a/userspace/digwatch/config_digwatch.h.in b/userspace/digwatch/config_digwatch.h.in index 2a1fd477..252493cb 100644 --- a/userspace/digwatch/config_digwatch.h.in +++ b/userspace/digwatch/config_digwatch.h.in @@ -4,7 +4,8 @@ #define DIGWATCH_LUA_DIR "/usr/share/digwatch/lua/" #define DIGWATCH_SOURCE_DIR "${PROJECT_SOURCE_DIR}" -#define DIGWATCH_CONF_FILE "${PROJECT_SOURCE_DIR}/digwatch.yaml" +#define DIGWATCH_SOURCE_CONF_FILE "${PROJECT_SOURCE_DIR}/digwatch.yaml" +#define DIGWATCH_INSTALL_CONF_FILE "/etc/digwatch.yaml" #define DIGWATCH_SOURCE_LUA_DIR "${PROJECT_SOURCE_DIR}/userspace/digwatch/lua/" diff --git a/userspace/digwatch/digwatch.cpp b/userspace/digwatch/digwatch.cpp index 137f6868..43de8178 100644 --- a/userspace/digwatch/digwatch.cpp +++ b/userspace/digwatch/digwatch.cpp @@ -173,6 +173,7 @@ int digwatch_init(int argc, char **argv) string lua_main_filename; string output_name = "stdout"; string infile; + string conf_filename; string lua_dir = DIGWATCH_LUA_DIR; lua_State* ls = NULL; @@ -192,7 +193,7 @@ int digwatch_init(int argc, char **argv) // Parse the args // while((op = getopt_long(argc, argv, - "ho:r:", + "c:ho:R:", long_options, &long_index)) != -1) { switch(op) @@ -200,6 +201,9 @@ int digwatch_init(int argc, char **argv) case 'h': usage(); goto exit; + case 'c': + conf_filename = optarg; + break; case 'o': valid = std::find(valid_output_names.begin(), valid_output_names.end(), optarg) != valid_output_names.end(); if (!valid) @@ -250,8 +254,49 @@ int digwatch_init(int argc, char **argv) } + ifstream* conf_stream; + if (conf_filename.size()) + { + conf_stream = new ifstream(conf_filename); + if (!conf_stream->good()) + { + fprintf(stderr, "Could not find configuration file at %s \n", conf_filename.c_str()); + result = EXIT_FAILURE; + goto exit; + } + } + else + { + conf_stream = new ifstream(DIGWATCH_SOURCE_CONF_FILE); + if (conf_stream->good()) + { + conf_filename = DIGWATCH_SOURCE_CONF_FILE; + } + else + { + conf_stream = new ifstream(DIGWATCH_INSTALL_CONF_FILE); + if (conf_stream->good()) + { + conf_filename = DIGWATCH_INSTALL_CONF_FILE; + } + else + { + conf_filename = ""; + } + } + } + digwatch_configuration config; - config.init(); + if (conf_filename.size()) + { + cout << "Using configuration file " + conf_filename + "\n"; + config.init(conf_filename); + } + else + { + cout << "No configuration file found, proceeding with defaults\n"; + config.init(); + } if(signal(SIGINT, signal_callback) == SIG_ERR) {