From 5dca0458a09ebc586fda49c50533976f055d8de2 Mon Sep 17 00:00:00 2001 From: Craig Barnes Date: Sat, 26 Jun 2021 22:15:09 +0100 Subject: [PATCH] log: add LOG_CLASS_NONE and use as initializer for log_level This means that logging will be completely disabled until log_init() has been called, which is useful to prevent log spam when running UNITTEST{} blocks in debug builds. Note that this doesn't change the default log level at runtime, which was already being set to LOG_CLASS_INFO in main.c and client.c. The new log level is also exposed to the command-line interface as `--log-level=none`, which allows disabling logging entirely. --- CHANGELOG.md | 6 +++ client.c | 32 ++++++------ completions/bash/foot | 2 +- completions/bash/footclient | 2 +- completions/fish/foot.fish | 2 +- completions/fish/footclient.fish | 2 +- completions/zsh/_foot | 2 +- completions/zsh/_footclient | 2 +- config.c | 6 ++- doc/foot.1.scd | 2 +- doc/footclient.1.scd | 2 +- log.c | 88 ++++++++++++++++---------------- log.h | 9 +++- main.c | 49 +++++++++--------- 14 files changed, 113 insertions(+), 93 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3934dadf..0e6b7a73 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -28,7 +28,11 @@ ## Unreleased + ### Added + +* `--log-level=none` command-line option. + ### Changed ### Deprecated ### Removed @@ -40,6 +44,8 @@ ### Security ### Contributors +* [craigbarnes](https://codeberg.org/craigbarnes) + ## 1.8.0 diff --git a/client.c b/client.c index 9764da11..7b5cd770 100644 --- a/client.c +++ b/client.c @@ -58,22 +58,22 @@ print_usage(const char *prog_name) printf("Usage: %s [OPTIONS...] command [ARGS...]\n", prog_name); printf("\n"); printf("Options:\n"); - printf(" -t,--term=TERM value to set the environment variable TERM to (foot)\n" - " -T,--title=TITLE initial window title (foot)\n" - " -a,--app-id=ID window application ID (foot)\n" - " -w,--window-size-pixels=WIDTHxHEIGHT initial width and height, in pixels\n" - " -W,--window-size-chars=WIDTHxHEIGHT initial width and height, in characters\n" - " -m,--maximized start in maximized mode\n" - " -F,--fullscreen start in fullscreen mode\n" - " -L,--login-shell start shell as a login shell\n" - " -D,--working-directory=DIR directory to start in (CWD)\n" - " -s,--server-socket=PATH path to the server UNIX domain socket (default=$XDG_RUNTIME_DIR/foot-$WAYLAND_DISPLAY.sock)\n" - " -H,--hold remain open after child process exits\n" - " -N,--no-wait detach the client process from the running terminal, exiting immediately\n" - " -o,--override=[section.]key=value override configuration option\n" - " -d,--log-level={info|warning|error} log level (info)\n" - " -l,--log-colorize=[{never|always|auto}] enable/disable colorization of log output on stderr\n" - " -v,--version show the version number and quit\n"); + printf(" -t,--term=TERM value to set the environment variable TERM to (foot)\n" + " -T,--title=TITLE initial window title (foot)\n" + " -a,--app-id=ID window application ID (foot)\n" + " -w,--window-size-pixels=WIDTHxHEIGHT initial width and height, in pixels\n" + " -W,--window-size-chars=WIDTHxHEIGHT initial width and height, in characters\n" + " -m,--maximized start in maximized mode\n" + " -F,--fullscreen start in fullscreen mode\n" + " -L,--login-shell start shell as a login shell\n" + " -D,--working-directory=DIR directory to start in (CWD)\n" + " -s,--server-socket=PATH path to the server UNIX domain socket (default=$XDG_RUNTIME_DIR/foot-$WAYLAND_DISPLAY.sock)\n" + " -H,--hold remain open after child process exits\n" + " -N,--no-wait detach the client process from the running terminal, exiting immediately\n" + " -o,--override=[section.]key=value override configuration option\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" + " -v,--version show the version number and quit\n"); } static bool NOINLINE diff --git a/completions/bash/foot b/completions/bash/foot index f03a9f53..f9d9d0fd 100644 --- a/completions/bash/foot +++ b/completions/bash/foot @@ -68,7 +68,7 @@ _foot() which fc-list > /dev/null || return 1 COMPREPLY=( $(compgen -W "$(fc-list : family | sed 's/,/\n/g' | uniq | tr -d ' ')" -- ${cur}) ) elif [[ ${prev} == '--log-level' ]] ; then - COMPREPLY=( $(compgen -W "error warning info" -- ${cur}) ) + COMPREPLY=( $(compgen -W "none error warning info" -- ${cur}) ) elif [[ ${prev} == '--log-colorize' ]] ; then COMPREPLY=( $(compgen -W "never always auto" -- ${cur}) ) elif [[ ${prev} =~ ^(--app-id|--help|--override|--title|--version|--window-size-chars|--window-size-pixels|--check-config)$ ]] ; then diff --git a/completions/bash/footclient b/completions/bash/footclient index 8e0aa017..fea45945 100644 --- a/completions/bash/footclient +++ b/completions/bash/footclient @@ -59,7 +59,7 @@ _footclient() which toe > /dev/null || return 1 COMPREPLY=( $(compgen -W "$(toe -a | awk '$1 ~ /[+]/ {next}; {print $1}')" -- ${cur}) ) elif [[ ${prev} == '--log-level' ]] ; then - COMPREPLY=( $(compgen -W "error warning info" -- ${cur}) ) + COMPREPLY=( $(compgen -W "none error warning info" -- ${cur}) ) elif [[ ${prev} == '--log-colorize' ]] ; then COMPREPLY=( $(compgen -W "never always auto" -- ${cur}) ) elif [[ ${prev} =~ ^(--app-id|--help|--override|--title|--version|--window-size-chars|--window-size-pixels|)$ ]] ; then diff --git a/completions/fish/foot.fish b/completions/fish/foot.fish index dbec61e7..81c7da61 100644 --- a/completions/fish/foot.fish +++ b/completions/fish/foot.fish @@ -15,7 +15,7 @@ complete -c foot -x -s W -l window-size-chars complete -c foot -F -s s -l server -d "run as server; open terminals by running footclient" complete -c foot -s H -l hold -d "remain open after child process exits" complete -c foot -r -s p -l print-pid -d "print PID to this file or FD when up and running (server mode only)" -complete -c foot -x -s d -l log-level -a "info warning error" -d "log-level (info)" +complete -c foot -x -s d -l log-level -a "info warning error none" -d "log-level (info)" complete -c foot -x -s l -l log-colorize -a "always never auto" -d "enable or disable colorization of log output on stderr" complete -c foot -s S -l log-no-syslog -d "disable syslog logging (server mode only)" complete -c foot -s v -l version -d "show the version number and quit" diff --git a/completions/fish/footclient.fish b/completions/fish/footclient.fish index 9b8caa84..8133f663 100644 --- a/completions/fish/footclient.fish +++ b/completions/fish/footclient.fish @@ -12,7 +12,7 @@ complete -c footclient -F -s s -l server-socket complete -c footclient -s H -l hold -d "remain open after child process exits" complete -c footclient -s N -l no-wait -d "detach the client process from the running terminal, exiting immediately" complete -c footclient -x -s o -l override -d "configuration option to override, in form SECTION.KEY=VALUE" -complete -c footclient -x -s d -l log-level -a "info warning error" -d "log-level (info)" +complete -c footclient -x -s d -l log-level -a "info warning error none" -d "log-level (info)" complete -c footclient -x -s l -l log-colorize -a "always never auto" -d "enable or disable colorization of log output on stderr" complete -c footclient -s v -l version -d "show the version number and quit" complete -c footclient -s h -l help -d "show help message and quit" diff --git a/completions/zsh/_foot b/completions/zsh/_foot index b3f9b42c..0f184cc0 100644 --- a/completions/zsh/_foot +++ b/completions/zsh/_foot @@ -18,7 +18,7 @@ _arguments \ '(-s --server)'{-s,--server}'[run as server; open terminals by running footclient]:server:_files' \ '(-H --hold)'{-H,--hold}'[remain open after child process exits]' \ '(-p --print-pid)'{-p,--print-pid}'[print PID to this file or FD when up and running (server mode only)]:pidfile:_files' \ - '(-d --log-level)'{-d,--log-level}'[log level (info)]:loglevel:(info warning error)' \ + '(-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 (server mode only)]' \ '(-v --version)'{-v,--version}'[show the version number and quit]' \ diff --git a/completions/zsh/_footclient b/completions/zsh/_footclient index 3889c488..81b2ea95 100644 --- a/completions/zsh/_footclient +++ b/completions/zsh/_footclient @@ -15,7 +15,7 @@ _arguments \ '(-H --hold)'{-H,--hold}'[remain open after child process exits]' \ '(-N --no-wait)'{-N,--no-wait}'[detach the client process from the running terminal, exiting immediately]' \ '(-o --override)'{-o,--override}'[configuration option to override, in form SECTION.KEY=VALUE]:()' \ - '(-d --log-level)'{-d,--log-level}'[log level (info)]:loglevel:(info warning error)' \ + '(-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)' \ '(-v --version)'{-v,--version}'[show the version number and quit]' \ '(-h --help)'{-h,--help}'[show help message and quit]' \ diff --git a/config.c b/config.c index a95f8485..c417fb19 100644 --- a/config.c +++ b/config.c @@ -103,8 +103,10 @@ log_and_notify(struct config *conf, enum log_class log_class, case LOG_CLASS_INFO: case LOG_CLASS_DEBUG: - BUG("unsupported log class: %d", log_class); - break; + case LOG_CLASS_NONE: + default: + BUG("unsupported log class: %d", (int)log_class); + return; } va_list va1, va2; diff --git a/doc/foot.1.scd b/doc/foot.1.scd index 80203ad9..ab34a2dd 100644 --- a/doc/foot.1.scd +++ b/doc/foot.1.scd @@ -126,7 +126,7 @@ the foot command line This option can only be used in combination with *-s*,*--server*. -*-d*,*--log-level*={*info*,*warning*,*error*} +*-d*,*--log-level*={*info*,*warning*,*error*,*none*} Log level, used both for log output on stderr as well as syslog. Default: _info_. diff --git a/doc/footclient.1.scd b/doc/footclient.1.scd index 8cb05316..a002a899 100644 --- a/doc/footclient.1.scd +++ b/doc/footclient.1.scd @@ -69,7 +69,7 @@ terminal has terminated. Override an option set in the configuration file. If _SECTION_ is not given, defaults to _main_. -*-d*,*--log-level*={*info*,*warning*,*error*} +*-d*,*--log-level*={*info*,*warning*,*error*,*none*} Log level, used both for log output on stderr as well as syslog. Default: _info_. diff --git a/log.c b/log.c index fba53876..9dfc842d 100644 --- a/log.c +++ b/log.c @@ -3,6 +3,7 @@ #include #include #include +#include #include #include #include @@ -15,15 +16,19 @@ static bool colorize = false; static bool do_syslog = true; -static enum log_class log_level = LOG_CLASS_INFO; +static enum log_class log_level = LOG_CLASS_NONE; -static const char log_level_map[][8] = { - [LOG_CLASS_ERROR] = "error", - [LOG_CLASS_WARNING] = "warning", - [LOG_CLASS_INFO] = "info", -#if defined(_DEBUG) - [LOG_CLASS_DEBUG] = "debug", -#endif +static const struct { + const char name[8]; + const char log_prefix[7]; + uint8_t color; + int syslog_equivalent; +} log_level_map[] = { + [LOG_CLASS_NONE] = {"none", "none", 5, -1}, + [LOG_CLASS_ERROR] = {"error", " err", 31, LOG_ERR}, + [LOG_CLASS_WARNING] = {"warning", "warn", 33, LOG_WARNING}, + [LOG_CLASS_INFO] = {"info", "info", 97, LOG_INFO}, + [LOG_CLASS_DEBUG] = {"debug", " dbg", 36, LOG_DEBUG}, }; void @@ -35,20 +40,14 @@ log_init(enum log_colorize _colorize, bool _do_syslog, [LOG_FACILITY_DAEMON] = LOG_DAEMON, }; - static const int level_map[] = { - [LOG_CLASS_ERROR] = LOG_ERR, - [LOG_CLASS_WARNING] = LOG_WARNING, - [LOG_CLASS_INFO] = LOG_INFO, - [LOG_CLASS_DEBUG] = LOG_DEBUG, - }; - colorize = _colorize == LOG_COLORIZE_NEVER ? false : _colorize == LOG_COLORIZE_ALWAYS ? true : isatty(STDERR_FILENO); do_syslog = _do_syslog; log_level = _log_level; - if (do_syslog) { + int slvl = log_level_map[_log_level].syslog_equivalent; + if (do_syslog && slvl != -1) { openlog(NULL, /*LOG_PID*/0, facility_map[syslog_facility]); - setlogmask(LOG_UPTO(level_map[_log_level])); + setlogmask(LOG_UPTO(slvl)); } } @@ -63,34 +62,31 @@ static void _log(enum log_class log_class, const char *module, const char *file, int lineno, const char *fmt, int sys_errno, va_list va) { + xassert(log_class > LOG_CLASS_NONE); + xassert(log_class < ALEN(log_level_map)); + if (log_class > log_level) return; - const char *class = "abcd"; - int class_clr = 0; - switch (log_class) { - case LOG_CLASS_ERROR: class = " err"; class_clr = 31; break; - case LOG_CLASS_WARNING: class = "warn"; class_clr = 33; break; - case LOG_CLASS_INFO: class = "info"; class_clr = 97; break; - case LOG_CLASS_DEBUG: class = " dbg"; class_clr = 36; break; - } + const char *prefix = log_level_map[log_class].log_prefix; + unsigned int class_clr = log_level_map[log_class].color; char clr[16]; - snprintf(clr, sizeof(clr), "\033[%dm", class_clr); - fprintf(stderr, "%s%s%s: ", colorize ? clr : "", class, colorize ? "\033[0m" : ""); + xsnprintf(clr, sizeof(clr), "\033[%um", class_clr); + fprintf(stderr, "%s%s%s: ", colorize ? clr : "", prefix, colorize ? "\033[0m" : ""); if (colorize) - fprintf(stderr, "\033[2m"); + fputs("\033[2m", stderr); fprintf(stderr, "%s:%d: ", file, lineno); if (colorize) - fprintf(stderr, "\033[0m"); + fputs("\033[0m", stderr); vfprintf(stderr, fmt, va); if (sys_errno != 0) fprintf(stderr, ": %s", strerror(sys_errno)); - fprintf(stderr, "\n"); + fputc('\n', stderr); } static void @@ -98,19 +94,14 @@ _sys_log(enum log_class log_class, const char *module, const char UNUSED *file, int UNUSED lineno, const char *fmt, int sys_errno, va_list va) { + xassert(log_class > LOG_CLASS_NONE); + xassert(log_class < ALEN(log_level_map)); + if (!do_syslog) return; /* Map our log level to syslog's level */ - int level = -1; - switch (log_class) { - case LOG_CLASS_ERROR: level = LOG_ERR; break; - case LOG_CLASS_WARNING: level = LOG_WARNING; break; - case LOG_CLASS_INFO: level = LOG_INFO; break; - case LOG_CLASS_DEBUG: level = LOG_DEBUG; break; - } - - xassert(level != -1); + int level = log_level_map[log_class].syslog_equivalent; char msg[4096]; int n = vsnprintf(msg, sizeof(msg), fmt, va); @@ -185,14 +176,25 @@ log_errno_provided(enum log_class log_class, const char *module, va_end(va); } +static size_t +map_len(void) +{ + size_t len = ALEN(log_level_map); +#ifndef _DEBUG + /* Exclude "debug" entry for non-debug builds */ + len--; +#endif + return len; +} + int log_level_from_string(const char *str) { if (unlikely(str[0] == '\0')) return -1; - for (int i = 0, n = ALEN(log_level_map); i < n; i++) - if (strcmp(str, log_level_map[i]) == 0) + for (int i = 0, n = map_len(); i < n; i++) + if (strcmp(str, log_level_map[i].name) == 0) return i; return -1; @@ -205,8 +207,8 @@ log_level_string_hint(void) if (buf[0] != '\0') return buf; - for (size_t i = 0, pos = 0, n = ALEN(log_level_map); i < n; i++) { - const char *entry = log_level_map[i]; + for (size_t i = 0, pos = 0, n = map_len(); i < n; i++) { + const char *entry = log_level_map[i].name; const char *delim = (i + 1 < n) ? ", " : ""; pos += xsnprintf(buf + pos, sizeof(buf) - pos, "'%s'%s", entry, delim); } diff --git a/log.h b/log.h index 367fd60e..dc458199 100644 --- a/log.h +++ b/log.h @@ -5,7 +5,14 @@ enum log_colorize { LOG_COLORIZE_NEVER, LOG_COLORIZE_ALWAYS, LOG_COLORIZE_AUTO }; enum log_facility { LOG_FACILITY_USER, LOG_FACILITY_DAEMON }; -enum log_class { LOG_CLASS_ERROR, LOG_CLASS_WARNING, LOG_CLASS_INFO, LOG_CLASS_DEBUG }; + +enum log_class { + LOG_CLASS_NONE, + LOG_CLASS_ERROR, + LOG_CLASS_WARNING, + LOG_CLASS_INFO, + LOG_CLASS_DEBUG +}; void log_init(enum log_colorize colorize, bool do_syslog, enum log_facility syslog_facility, enum log_class log_level); diff --git a/main.c b/main.c index bbe89052..f58bdb52 100644 --- a/main.c +++ b/main.c @@ -61,27 +61,27 @@ print_usage(const char *prog_name) "Usage: %s [OPTIONS...] command [ARGS...]\n" "\n" "Options:\n" - " -c,--config=PATH load configuration from PATH ($XDG_CONFIG_HOME/foot/foot.ini)\n" - " -C,--check-config verify configuration, exit with 0 if ok, otherwise exit with 1\n" - " -o,--override=[section.]key=value override configuration option\n" - " -f,--font=FONT comma separated list of fonts in fontconfig format (monospace)\n" - " -t,--term=TERM value to set the environment variable TERM to (%s)\n" - " -T,--title=TITLE initial window title (foot)\n" - " -a,--app-id=ID window application ID (foot)\n" - " -m,--maximized start in maximized mode\n" - " -F,--fullscreen start in fullscreen mode\n" - " -L,--login-shell start shell as a login shell\n" - " -D,--working-directory=DIR directory to start in (CWD)\n" - " -w,--window-size-pixels=WIDTHxHEIGHT initial width and height, in pixels\n" - " -W,--window-size-chars=WIDTHxHEIGHT initial width and height, in characters\n" - " -s,--server[=PATH] run as a server (use 'footclient' to start terminals).\n" - " Without PATH, $XDG_RUNTIME_DIR/foot-$WAYLAND_DISPLAY.sock will be used.\n" - " -H,--hold remain open after child process exits\n" - " -p,--print-pid=FILE|FD print PID to file or FD (only applicable in server mode)\n" - " -d,--log-level={info|warning|error} 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 (only applicable in server mode)\n" - " -v,--version show the version number and quit\n", + " -c,--config=PATH load configuration from PATH ($XDG_CONFIG_HOME/foot/foot.ini)\n" + " -C,--check-config verify configuration, exit with 0 if ok, otherwise exit with 1\n" + " -o,--override=[section.]key=value override configuration option\n" + " -f,--font=FONT comma separated list of fonts in fontconfig format (monospace)\n" + " -t,--term=TERM value to set the environment variable TERM to (%s)\n" + " -T,--title=TITLE initial window title (foot)\n" + " -a,--app-id=ID window application ID (foot)\n" + " -m,--maximized start in maximized mode\n" + " -F,--fullscreen start in fullscreen mode\n" + " -L,--login-shell start shell as a login shell\n" + " -D,--working-directory=DIR directory to start in (CWD)\n" + " -w,--window-size-pixels=WIDTHxHEIGHT initial width and height, in pixels\n" + " -W,--window-size-chars=WIDTHxHEIGHT initial width and height, in characters\n" + " -s,--server[=PATH] run as a server (use 'footclient' to start terminals).\n" + " Without PATH, $XDG_RUNTIME_DIR/foot-$WAYLAND_DISPLAY.sock will be used.\n" + " -H,--hold remain open after child process exits\n" + " -p,--print-pid=FILE|FD print PID to file or FD (only applicable in server mode)\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 (only applicable in server mode)\n" + " -v,--version show the version number and quit\n", prog_name, prog_name, DEFAULT_TERM); } @@ -383,11 +383,14 @@ main(int argc, char *const *argv) log_init(log_colorize, as_server && log_syslog, as_server ? LOG_FACILITY_DAEMON : LOG_FACILITY_USER, log_level); - _Static_assert(LOG_CLASS_ERROR + 1 == FCFT_LOG_CLASS_ERROR, + _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, as_server && log_syslog, log_level + 1); + fcft_log_init( + (enum fcft_log_colorize)log_colorize, + as_server && log_syslog, + (enum fcft_log_class)log_level); argc -= optind; argv += optind;