From be10465a3bc9fc4cdde67ff73ffe6bb4f8e4d575 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ekl=C3=B6f?= Date: Sun, 15 Aug 2021 11:43:49 +0200 Subject: [PATCH] main: add -d,--log-level=info|warning|error|none Closes #84 --- CHANGELOG.md | 2 ++ completions/zsh/_yambar | 1 + doc/yambar.1.scd | 4 ++++ main.c | 37 +++++++++++++++++++++++++++---------- 4 files changed, 34 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e82a0f8..8316b08 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,8 @@ four borders to the same value (https://codeberg.org/dnkl/yambar/issues/77). * river: `per-output: false|true`. +* `-d,--log-level=info|warning|error|none` command line option + (https://codeberg.org/dnkl/yambar/issues/84). ### Changed diff --git a/completions/zsh/_yambar b/completions/zsh/_yambar index aa8172f..65b3100 100644 --- a/completions/zsh/_yambar +++ b/completions/zsh/_yambar @@ -8,5 +8,6 @@ _arguments \ '(-c --config)'{-c,--config}'[alternative configuration file]:filename:_files' \ '(-C --validate)'{-C,--validate}'[verify configuration then quit]' \ '(-p --print-pid)'{-p,--print-pid}'[print PID to this file or FD when up and running]:pidfile:_files' \ + '(-d --log-level)'{-d,--log-level}'[log level (info)]:loglevel:(info warning error none)' \ '(-l --log-colorize)'{-l,--log-colorize}'[enable or disable colorization of log output on stderr]:logcolor:(never always auto)' \ '(-s --log-no-syslog)'{-s,--log-no-syslog}'[disable syslog logging]' diff --git a/doc/yambar.1.scd b/doc/yambar.1.scd index f2526a2..a34f13c 100644 --- a/doc/yambar.1.scd +++ b/doc/yambar.1.scd @@ -27,6 +27,10 @@ yambar - modular status panel for X11 and Wayland (or FD) is closed immediately after writing the PID. When a _FILE_ as been specified, the file is unlinked exit. +*-d*,*--log-level*={*info*,*warning*,*error*,*none*} + Log level, used both for log output on stderr as well as + syslog. Default: _info_. + *-l*,*--log-colorize*=[{*never*,*always*,*auto*}] Enables or disables colorization of log output on stderr. diff --git a/main.c b/main.c index 48a94f6..e92e2e5 100644 --- a/main.c +++ b/main.c @@ -127,13 +127,14 @@ print_usage(const char *prog_name) printf("Usage: %s [OPTION]...\n", prog_name); printf("\n"); printf("Options:\n"); - printf(" -b,--backend={xcb,wayland,auto} backend to use (default: auto)\n" - " -c,--config=FILE alternative configuration file\n" - " -C,--validate verify configuration then quit\n" - " -p,--print-pid=FILE|FD print PID to file or FD\n" - " -l,--log-colorize=[never|always|auto] enable/disable colorization of log output on stderr\n" - " -s,--log-no-syslog disable syslog logging\n" - " -v,--version show the version number and quit\n"); + printf(" -b,--backend={xcb,wayland,auto} backend to use (default: auto)\n" + " -c,--config=FILE alternative configuration file\n" + " -C,--validate verify configuration then quit\n" + " -p,--print-pid=FILE|FD print PID to file or FD\n" + " -d,--log-level={info|warning|error|none} log level (info)\n" + " -l,--log-colorize=[never|always|auto] enable/disable colorization of log output on stderr\n" + " -s,--log-no-syslog disable syslog logging\n" + " -v,--version show the version number and quit\n"); } static bool @@ -181,6 +182,7 @@ main(int argc, char *const *argv) {"config", required_argument, 0, 'c'}, {"validate", no_argument, 0, 'C'}, {"print-pid", required_argument, 0, 'p'}, + {"log-level", required_argument, 0, 'd'}, {"log-colorize", optional_argument, 0, 'l'}, {"log-no-syslog", no_argument, 0, 's'}, {"version", no_argument, 0, 'v'}, @@ -195,11 +197,12 @@ main(int argc, char *const *argv) char *config_path = NULL; enum bar_backend backend = BAR_BACKEND_AUTO; + enum log_class log_level = LOG_CLASS_INFO; enum log_colorize log_colorize = LOG_COLORIZE_AUTO; bool log_syslog = true; while (true) { - int c = getopt_long(argc, argv, ":b:c:Cp:l::svh", longopts, NULL); + int c = getopt_long(argc, argv, ":b:c:Cp:d:l::svh", longopts, NULL); if (c == -1) break; @@ -238,6 +241,20 @@ main(int argc, char *const *argv) pid_file = optarg; break; + case 'd': { + int lvl = log_level_from_string(optarg); + if (lvl < 0) { + fprintf( + stderr, + "-d,--log-level: %s: argument must be one of %s\n", + optarg, + log_level_string_hint()); + return EXIT_FAILURE; + } + log_level = lvl; + break; + } + case 'l': if (optarg == NULL || strcmp(optarg, "auto") == 0) log_colorize = LOG_COLORIZE_AUTO; @@ -273,14 +290,14 @@ main(int argc, char *const *argv) } } - log_init(log_colorize, log_syslog, LOG_FACILITY_DAEMON, LOG_CLASS_INFO); + log_init(log_colorize, log_syslog, LOG_FACILITY_DAEMON, log_level); _Static_assert((int)LOG_CLASS_ERROR == (int)FCFT_LOG_CLASS_ERROR, "fcft log level enum offset"); _Static_assert((int)LOG_COLORIZE_ALWAYS == (int)FCFT_LOG_COLORIZE_ALWAYS, "fcft colorize enum mismatch"); fcft_log_init( - (enum fcft_log_colorize)log_colorize, log_syslog, FCFT_LOG_CLASS_INFO); + (enum fcft_log_colorize)log_colorize, log_syslog, (enum fcft_log_class)log_level); const struct sigaction sa = {.sa_handler = &signal_handler}; sigaction(SIGINT, &sa, NULL);