add dumpconfigto option, JSON formatting
This commit is contained in:
+30
-3
@@ -88,6 +88,7 @@ static struct options s_opt = {
|
|||||||
getenv("LANG"), // preferLanguage
|
getenv("LANG"), // preferLanguage
|
||||||
false, // checkConfig
|
false, // checkConfig
|
||||||
OF_NONE, // dumpConfig
|
OF_NONE, // dumpConfig
|
||||||
|
nullptr, // dumpConfigTo
|
||||||
5, // pollInterval
|
5, // pollInterval
|
||||||
false, // injectMessages
|
false, // injectMessages
|
||||||
false, // stopAfterInject
|
false, // stopAfterInject
|
||||||
@@ -154,7 +155,8 @@ static const char argpdoc[] =
|
|||||||
#define O_CFGLNG (O_DEVLAT+1)
|
#define O_CFGLNG (O_DEVLAT+1)
|
||||||
#define O_CHKCFG (O_CFGLNG+1)
|
#define O_CHKCFG (O_CFGLNG+1)
|
||||||
#define O_DMPCFG (O_CHKCFG+1)
|
#define O_DMPCFG (O_CHKCFG+1)
|
||||||
#define O_POLINT (O_DMPCFG+1)
|
#define O_DMPCTO (O_DMPCFG+1)
|
||||||
|
#define O_POLINT (O_DMPCTO+1)
|
||||||
#define O_CAFILE (O_POLINT+1)
|
#define O_CAFILE (O_POLINT+1)
|
||||||
#define O_CAPATH (O_CAFILE+1)
|
#define O_CAPATH (O_CAFILE+1)
|
||||||
#define O_ANSWER (O_CAPATH+1)
|
#define O_ANSWER (O_CAPATH+1)
|
||||||
@@ -206,6 +208,7 @@ static const struct argp_option argpoptions[] = {
|
|||||||
{"checkconfig", O_CHKCFG, nullptr, 0, "Check config files, then stop", 0 },
|
{"checkconfig", O_CHKCFG, nullptr, 0, "Check config files, then stop", 0 },
|
||||||
{"dumpconfig", O_DMPCFG, "FORMAT", OPTION_ARG_OPTIONAL,
|
{"dumpconfig", O_DMPCFG, "FORMAT", OPTION_ARG_OPTIONAL,
|
||||||
"Check and dump config files in FORMAT (\"json\" or \"csv\"), then stop", 0 },
|
"Check and dump config files in FORMAT (\"json\" or \"csv\"), then stop", 0 },
|
||||||
|
{"dumpconfigto", O_DMPCTO, "FILE", 0, "Dump config files to FILE", 0 },
|
||||||
{"pollinterval", O_POLINT, "SEC", 0, "Poll for data every SEC seconds (0=disable) [5]", 0 },
|
{"pollinterval", O_POLINT, "SEC", 0, "Poll for data every SEC seconds (0=disable) [5]", 0 },
|
||||||
{"inject", 'i', "stop", OPTION_ARG_OPTIONAL, "Inject remaining arguments as already seen messages (e.g. "
|
{"inject", 'i', "stop", OPTION_ARG_OPTIONAL, "Inject remaining arguments as already seen messages (e.g. "
|
||||||
"\"FF08070400/0AB5454850303003277201\"), optionally stop afterwards", 0 },
|
"\"FF08070400/0AB5454850303003277201\"), optionally stop afterwards", 0 },
|
||||||
@@ -377,6 +380,13 @@ error_t parse_opt(int key, char *arg, struct argp_state *state) {
|
|||||||
}
|
}
|
||||||
opt->checkConfig = true;
|
opt->checkConfig = true;
|
||||||
break;
|
break;
|
||||||
|
case O_DMPCTO: // --dumpconfigto=FILE
|
||||||
|
if (!arg || arg[0] == 0) {
|
||||||
|
argp_error(state, "invalid dumpconfigto");
|
||||||
|
return EINVAL;
|
||||||
|
}
|
||||||
|
opt->dumpConfigTo = arg;
|
||||||
|
break;
|
||||||
case O_POLINT: // --pollinterval=5
|
case O_POLINT: // --pollinterval=5
|
||||||
opt->pollInterval = parseInt(arg, 10, 0, 3600, &result);
|
opt->pollInterval = parseInt(arg, 10, 0, 3600, &result);
|
||||||
if (result != RESULT_OK) {
|
if (result != RESULT_OK) {
|
||||||
@@ -1409,8 +1419,25 @@ int main(int argc, char* argv[]) {
|
|||||||
overallResult = result;
|
overallResult = result;
|
||||||
}
|
}
|
||||||
if (result == RESULT_OK && s_opt.dumpConfig) {
|
if (result == RESULT_OK && s_opt.dumpConfig) {
|
||||||
logNotice(lf_main, "configuration dump:");
|
ostream* out = &cout;
|
||||||
s_messageMap->dump(true, s_opt.dumpConfig, &cout);
|
std::ofstream fout;
|
||||||
|
if (s_opt.dumpConfigTo) {
|
||||||
|
fout.open(s_opt.dumpConfigTo);
|
||||||
|
if (!fout.is_open()) {
|
||||||
|
logError(lf_main, "error dumping config to %s", s_opt.dumpConfigTo);
|
||||||
|
} else {
|
||||||
|
out = &fout;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (fout.is_open()) {
|
||||||
|
logNotice(lf_main, "saving configuration dump to %s", s_opt.dumpConfigTo);
|
||||||
|
} else {
|
||||||
|
logNotice(lf_main, "configuration dump:");
|
||||||
|
}
|
||||||
|
s_messageMap->dump(true, s_opt.dumpConfig, out);
|
||||||
|
if (fout.is_open()) {
|
||||||
|
fout.close();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
cleanup();
|
cleanup();
|
||||||
|
|||||||
+2
-1
@@ -48,7 +48,8 @@ struct options {
|
|||||||
symbol_t initialScan;
|
symbol_t initialScan;
|
||||||
const char* preferLanguage; //!< preferred language in configuration files
|
const char* preferLanguage; //!< preferred language in configuration files
|
||||||
bool checkConfig; //!< check config files, then stop
|
bool checkConfig; //!< check config files, then stop
|
||||||
OutputFormat dumpConfig; //!< dump config files, then stop
|
OutputFormat dumpConfig; //!< dump config files, then stop
|
||||||
|
const char* dumpConfigTo; //!< file to dump config to
|
||||||
unsigned int pollInterval; //!< poll interval in seconds, 0 to disable [5]
|
unsigned int pollInterval; //!< poll interval in seconds, 0 to disable [5]
|
||||||
bool injectMessages; //!< inject remaining arguments as already seen messages
|
bool injectMessages; //!< inject remaining arguments as already seen messages
|
||||||
bool stopAfterInject; //!< only inject messages once, then stop
|
bool stopAfterInject; //!< only inject messages once, then stop
|
||||||
|
|||||||
@@ -2833,21 +2833,19 @@ void MessageMap::dump(bool withConditions, OutputFormat outputFormat, ostream* o
|
|||||||
if (!message) {
|
if (!message) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
bool wasFirst = first;
|
|
||||||
if (first) {
|
if (first) {
|
||||||
first = false;
|
first = false;
|
||||||
|
} else if (isJson) {
|
||||||
|
*output << ",\n";
|
||||||
} else {
|
} else {
|
||||||
*output << endl;
|
*output << endl;
|
||||||
}
|
}
|
||||||
if (isJson) {
|
if (isJson) {
|
||||||
if (!wasFirst) {
|
|
||||||
*output << ",\n";
|
|
||||||
}
|
|
||||||
ostringstream str;
|
ostringstream str;
|
||||||
message->decodeJson(false, false, false, false, outputFormat, &str);
|
message->decodeJson(false, false, false, false, outputFormat, &str);
|
||||||
string add = str.str();
|
string add = str.str();
|
||||||
size_t pos = add.find('{');
|
size_t pos = add.find('{');
|
||||||
*output << "\n {\"circuit\": \"" << message->getCircuit() << "\", " << add.substr(pos+1);
|
*output << " {\n \"circuit\": \"" << message->getCircuit() << "\", " << add.substr(pos+1);
|
||||||
} else {
|
} else {
|
||||||
message->dump(nullptr, withConditions, outputFormat, output);
|
message->dump(nullptr, withConditions, outputFormat, output);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user