Skip to content

Commit

Permalink
Add cli options to set log level for primary log and console
Browse files Browse the repository at this point in the history
Add '--console-level' and '--log-level' which take a severity argument
(error, warn, info, debug, none).

Documentation pending.
  • Loading branch information
davmac314 committed Nov 2, 2023
1 parent a76e4d5 commit aebcd39
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions src/dinit.cc
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,35 @@ static int process_commandline_arg(char **argv, int argc, int &i, options &opts)
service_dir_opt &service_dir_opts = opts.service_dir_opts;
list<const char *> &services_to_start = opts.services_to_start;

auto arg_to_loglevel = [&](const char *option_name, loglevel_t &wanted_level) {
if (++i < argc && argv[i][0] != '\0') {
if (strcmp(argv[i], "none") == 0) {
wanted_level = loglevel_t::ZERO;
}
else if (strcmp(argv[i], "error") == 0) {
wanted_level = loglevel_t::ERROR;
}
else if (strcmp(argv[i], "warn") == 0) {
wanted_level = loglevel_t::WARN;
}
else if (strcmp(argv[i], "info") == 0) {
wanted_level = loglevel_t::NOTICE;
}
else if (strcmp(argv[i], "debug") == 0) {
wanted_level = loglevel_t::DEBUG;
}
else {
cerr << "dinit: '" << option_name << "' accepts only arguments: 'none', 'error', 'warn', 'info', 'debug'\n";
return false;
}
return true;
}
else {
cerr << "dinit: '" << option_name << "' requires an argument\n";
return false;
}
};

if (argv[i][0] == '-') {
// An option...
if (strcmp(argv[i], "--env-file") == 0 || strcmp(argv[i], "-e") == 0) {
Expand Down Expand Up @@ -312,6 +341,16 @@ static int process_commandline_arg(char **argv, int argc, int &i, options &opts)
console_service_status = false;
log_level[DLOG_CONS] = loglevel_t::ZERO;
}
else if (strcmp(argv[i], "--console-level") == 0) {
loglevel_t wanted_level;
if (!arg_to_loglevel("--console-level", wanted_level)) return 1;
log_level[DLOG_CONS] = wanted_level;
}
else if (strcmp(argv[i], "--log-level") == 0) {
loglevel_t wanted_level;
if (!arg_to_loglevel("--log-level", wanted_level)) return 1;
log_level[DLOG_MAIN] = wanted_level;
}
#ifdef SUPPORT_CGROUPS
else if (strcmp(argv[i], "--cgroup-path") == 0 || strcmp(argv[i], "-b") == 0) {
if (++i < argc && argv[i][0] != '\0') {
Expand Down

0 comments on commit aebcd39

Please sign in to comment.