From 208014d6c9b396f4a4e070180ac63a22090c20d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ekl=C3=B6f?= Date: Tue, 9 Feb 2021 20:54:17 +0100 Subject: [PATCH 1/9] log: log-level applies to messages logged to stderr too --- log.c | 9 +++++++-- log.h | 2 +- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/log.c b/log.c index 24d26ed0..10ba5346 100644 --- a/log.c +++ b/log.c @@ -13,10 +13,11 @@ static bool colorize = false; static bool do_syslog = true; +static enum log_class log_level = LOG_CLASS_INFO; void log_init(enum log_colorize _colorize, bool _do_syslog, - enum log_facility syslog_facility, enum log_class syslog_level) + enum log_facility syslog_facility, enum log_class _log_level) { static const int facility_map[] = { [LOG_FACILITY_USER] = LOG_USER, @@ -32,10 +33,11 @@ log_init(enum log_colorize _colorize, bool _do_syslog, 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) { openlog(NULL, /*LOG_PID*/0, facility_map[syslog_facility]); - setlogmask(LOG_UPTO(level_map[syslog_level])); + setlogmask(LOG_UPTO(level_map[_log_level])); } } @@ -50,6 +52,9 @@ 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) { + if (log_class > log_level) + return; + const char *class = "abcd"; int class_clr = 0; switch (log_class) { diff --git a/log.h b/log.h index 656a8ca0..5baddfcd 100644 --- a/log.h +++ b/log.h @@ -7,7 +7,7 @@ enum log_facility { LOG_FACILITY_USER, LOG_FACILITY_DAEMON }; enum log_class { 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 syslog_level); + enum log_facility syslog_facility, enum log_class log_level); void log_deinit(void); void log_msg(enum log_class log_class, const char *module, From ca3974cc24e2eda811d2dc3985ae914c2cf88d6c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ekl=C3=B6f?= Date: Tue, 9 Feb 2021 21:04:56 +0100 Subject: [PATCH 2/9] main: add -d,--log-level=[debug,info,warning,error] --- main.c | 79 +++++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 59 insertions(+), 20 deletions(-) diff --git a/main.c b/main.c index b3953d22..4ea6e1fb 100644 --- a/main.c +++ b/main.c @@ -58,24 +58,25 @@ 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" - " --check-config verify configuration, exit with 0 if ok, otherwise exit with 1\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 (foot)\n" - " --title=TITLE initial window title (foot)\n" - " -a,--app-id=ID window application ID (foot)\n" - " --maximized start in maximized mode\n" - " --fullscreen start in fullscreen mode\n" - " --login-shell start shell as a login shell\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" - " --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" - " -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" + " --check-config verify configuration, exit with 0 if ok, otherwise exit with 1\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 (foot)\n" + " --title=TITLE initial window title (foot)\n" + " -a,--app-id=ID window application ID (foot)\n" + " --maximized start in maximized mode\n" + " --fullscreen start in fullscreen mode\n" + " --login-shell start shell as a login shell\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" + " --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", prog_name, prog_name); } @@ -171,6 +172,7 @@ main(int argc, char *const *argv) {"fullscreen", no_argument, NULL, 'F'}, {"presentation-timings", no_argument, NULL, 'P'}, /* Undocumented */ {"print-pid", required_argument, NULL, 'p'}, + {"log-level", required_argument, NULL, 'd'}, {"log-colorize", optional_argument, NULL, 'l'}, {"log-no-syslog", no_argument, NULL, 'S'}, {"version", no_argument, NULL, 'v'}, @@ -196,12 +198,13 @@ main(int argc, char *const *argv) bool fullscreen = false; bool unlink_pid_file = false; const char *pid_file = NULL; + enum log_class log_level = LOG_CLASS_INFO; enum log_colorize log_colorize = LOG_COLORIZE_AUTO; bool log_syslog = true; user_notifications_t user_notifications = tll_init(); while (true) { - int c = getopt_long(argc, argv, "+c:Ct:T:a:Lf:w:W:s::HmFPp:l::Svh", longopts, NULL); + int c = getopt_long(argc, argv, "+c:Ct:T:a:Lf:w:W:s::HmFPp:d:l::Svh", longopts, NULL); if (c == -1) break; @@ -306,6 +309,42 @@ main(int argc, char *const *argv) pid_file = optarg; break; + case 'd': { + static const char *log_level_map[] = { +#if defined(_DEBUG) + [LOG_CLASS_DEBUG] = "debug", +#endif + [LOG_CLASS_INFO] = "info", + [LOG_CLASS_WARNING] = "warning", + [LOG_CLASS_ERROR] = "error", + }; + + bool valid_log_level = false; + for (size_t i = 0; i < ALEN(log_level_map); i++) { + if (strcmp(optarg, log_level_map[i]) == 0) { + log_level = i; + valid_log_level = true; + break; + } + } + + if (!valid_log_level) { + fprintf( + stderr, "-d,--log-level: %s: argument must be one of ", + optarg); + + for (size_t i = 0; i < ALEN(log_level_map); i++) { + fprintf(stderr, "'%s'%s", + log_level_map[i], + i < ALEN(log_level_map) - 1 ? " , " : ""); + } + fprintf(stderr, "\n"); + + return EXIT_FAILURE; + } + break; + } + case 'l': if (optarg == NULL || strcmp(optarg, "auto") == 0) log_colorize = LOG_COLORIZE_AUTO; @@ -337,7 +376,7 @@ main(int argc, char *const *argv) } log_init(log_colorize, as_server && log_syslog, - as_server ? LOG_FACILITY_DAEMON : LOG_FACILITY_USER, LOG_CLASS_WARNING); + as_server ? LOG_FACILITY_DAEMON : LOG_FACILITY_USER, log_level); argc -= optind; argv += optind; From 4d42094a64825b8c3ce63bfeb1a26ae5a128a460 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ekl=C3=B6f?= Date: Tue, 9 Feb 2021 21:07:30 +0100 Subject: [PATCH 3/9] client: add -d,--log-level --- client.c | 68 ++++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 54 insertions(+), 14 deletions(-) diff --git a/client.c b/client.c index dead7117..a1b86d30 100644 --- a/client.c +++ b/client.c @@ -17,6 +17,7 @@ #include "client-protocol.h" #include "debug.h" #include "foot-features.h" +#include "util.h" #include "version.h" #include "xmalloc.h" @@ -44,18 +45,19 @@ print_usage(const char *prog_name) printf("Usage: %s [OPTIONS...] [ARGS...]\n", prog_name); printf("\n"); printf("Options:\n"); - printf(" -t,--term=TERM value to set the environment variable TERM to (foot)\n" - " --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" - " --maximized start in maximized mode\n" - " --fullscreen start in fullscreen mode\n" - " --login-shell start shell as a login shell\n" - " -s,--server-socket=PATH path to the server UNIX domain socket (default=$XDG_RUNTIME_DIR/foot-$WAYLAND_DISPLAY.sock)\n" - " --hold remain open after child process exits\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" + " --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" + " --maximized start in maximized mode\n" + " --fullscreen start in fullscreen mode\n" + " --login-shell start shell as a login shell\n" + " -s,--server-socket=PATH path to the server UNIX domain socket (default=$XDG_RUNTIME_DIR/foot-$WAYLAND_DISPLAY.sock)\n" + " --hold remain open after child process exits\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"); } int @@ -76,6 +78,7 @@ main(int argc, char *const *argv) {"login-shell", no_argument, NULL, 'L'}, {"server-socket", required_argument, NULL, 's'}, {"hold", no_argument, NULL, 'H'}, + {"log-level", required_argument, NULL, 'd'}, {"log-colorize", optional_argument, NULL, 'l'}, {"version", no_argument, NULL, 'v'}, {"help", no_argument, NULL, 'h'}, @@ -89,6 +92,7 @@ main(int argc, char *const *argv) unsigned width = 0; unsigned height = 0; const char *server_socket_path = NULL; + enum log_class log_level = LOG_CLASS_INFO; enum log_colorize log_colorize = LOG_COLORIZE_AUTO; bool login_shell = false; bool maximized = false; @@ -96,7 +100,7 @@ main(int argc, char *const *argv) bool hold = false; while (true) { - int c = getopt_long(argc, argv, "+t:T:a:w:W:mFLs:Hl::vh", longopts, NULL); + int c = getopt_long(argc, argv, "+t:T:a:w:W:mFLs:Hd:l::vh", longopts, NULL); if (c == -1) break; @@ -151,6 +155,42 @@ main(int argc, char *const *argv) hold = true; break; + case 'd': { + static const char *log_level_map[] = { +#if defined(_DEBUG) + [LOG_CLASS_DEBUG] = "debug", +#endif + [LOG_CLASS_INFO] = "info", + [LOG_CLASS_WARNING] = "warning", + [LOG_CLASS_ERROR] = "error", + }; + + bool valid_log_level = false; + for (size_t i = 0; i < ALEN(log_level_map); i++) { + if (strcmp(optarg, log_level_map[i]) == 0) { + log_level = i; + valid_log_level = true; + break; + } + } + + if (!valid_log_level) { + fprintf( + stderr, "-d,--log-level: %s: argument must be one of ", + optarg); + + for (size_t i = 0; i < ALEN(log_level_map); i++) { + fprintf(stderr, "'%s'%s", + log_level_map[i], + i < ALEN(log_level_map) - 1 ? " , " : ""); + } + fprintf(stderr, "\n"); + + return EXIT_FAILURE; + } + break; + } + case 'l': if (optarg == NULL || strcmp(optarg, "auto") == 0) log_colorize = LOG_COLORIZE_AUTO; @@ -180,7 +220,7 @@ main(int argc, char *const *argv) argc -= optind; argv += optind; - log_init(log_colorize, false, LOG_FACILITY_USER, LOG_CLASS_WARNING); + log_init(log_colorize, false, LOG_FACILITY_USER, log_level); /* malloc:ed and needs to be in scope of all goto's */ char *cwd = NULL; From 50e9cfd2e6189223b172ab044ac44e65464ad934 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ekl=C3=B6f?= Date: Tue, 9 Feb 2021 21:11:22 +0100 Subject: [PATCH 4/9] doc: foot.1: document -d,--log-level --- doc/foot.1.scd | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/doc/foot.1.scd b/doc/foot.1.scd index 4965ca56..71071dac 100644 --- a/doc/foot.1.scd +++ b/doc/foot.1.scd @@ -117,6 +117,10 @@ the foot command line This option can only be used in combination with *-s*,*--server*. +*-d*,*--log-level*={*info*,*warning*,*error*} + 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. Default: _auto_. From 4ddd2cad91c1c54ed96f79010069fed6ecaaa215 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ekl=C3=B6f?= Date: Tue, 9 Feb 2021 21:11:30 +0100 Subject: [PATCH 5/9] doc: footclient.1: document -d,--log-level --- doc/footclient.1.scd | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/doc/footclient.1.scd b/doc/footclient.1.scd index f9c95913..374a9a3a 100644 --- a/doc/footclient.1.scd +++ b/doc/footclient.1.scd @@ -56,6 +56,10 @@ terminal has terminated). *-H*,*--hold* Remain open after child process exits. +*-d*,*--log-level*={*info*,*warning*,*error*} + 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. From 2c5beed362e52c35d6b7498db993e09a335c5dd0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ekl=C3=B6f?= Date: Tue, 9 Feb 2021 21:13:44 +0100 Subject: [PATCH 6/9] completions: zsh: -d,--log-level --- completions/zsh/_foot | 1 + completions/zsh/_footclient | 1 + 2 files changed, 2 insertions(+) diff --git a/completions/zsh/_foot b/completions/zsh/_foot index c6f0f93b..b706ea2d 100644 --- a/completions/zsh/_foot +++ b/completions/zsh/_foot @@ -16,6 +16,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)' \ '(-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 e355527f..df9b7f25 100644 --- a/completions/zsh/_footclient +++ b/completions/zsh/_footclient @@ -12,6 +12,7 @@ _arguments \ '(-W --window-size-chars)'{-W,--window-size-chars}'[window WIDTHxHEIGHT, in characters (not set)]:size_chars:()' \ '(-s --server-socket)'{-s,--server-socket}'[override the default path to the foot server socket ($XDG_RUNTIME_DIR/foot-$WAYLAND_DISPLAY.sock)]:server:_files' \ '(-H --hold)'{-H,--hold}'[remain open after child process exits]' \ + '(-d --log-level)'{-d,--log-level}'[log level (info)]:loglevel:(info warning error)' \ '(-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]' \ From 80cd3df790a16a10e827a355eed37373baf3b72f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ekl=C3=B6f?= Date: Tue, 9 Feb 2021 21:16:40 +0100 Subject: [PATCH 7/9] completions: fish: -d,--log-level --- completions/fish/foot.fish | 1 + completions/fish/footclient.fish | 1 + 2 files changed, 2 insertions(+) diff --git a/completions/fish/foot.fish b/completions/fish/foot.fish index ab59fb75..de31c547 100644 --- a/completions/fish/foot.fish +++ b/completions/fish/foot.fish @@ -13,6 +13,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 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 9ffeeff5..880aaed7 100644 --- a/completions/fish/footclient.fish +++ b/completions/fish/footclient.fish @@ -9,6 +9,7 @@ complete -c footclient -x -s w -l window-size-pixels complete -c footclient -x -s W -l window-size-chars -d "window WIDTHxHEIGHT, in characters (not set)" complete -c footclient -F -s s -l server-socket -d "override the default path to the foot server socket ($XDG_RUNTIME_DIR/foot-$WAYLAND_DISPLAY.sock)" complete -c footclient -s H -l hold -d "remain open after child process exits" +complete -c footclient -x -s d -l log-level -a "info warning error" -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" From 76980773314279e53d0a7af56fb97886bcfa976a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ekl=C3=B6f?= Date: Tue, 9 Feb 2021 21:20:18 +0100 Subject: [PATCH 8/9] changelog: -d,--log-level --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6fc679df..c7391f4b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -45,6 +45,8 @@ (**no** mouse support). See **foot**(1)::URLs, or [README.md](README.md#urls) for details (https://codeberg.org/dnkl/foot/issues/14). +* `-d,--log-level={info|warning|error}` to both `foot` and + `footclient` (https://codeberg.org/dnkl/foot/issues/337). ### Changed From 5437321f97e21a3fc6fca79fbdf05ca8d6199b35 Mon Sep 17 00:00:00 2001 From: Craig Barnes Date: Thu, 11 Feb 2021 11:08:18 +0000 Subject: [PATCH 9/9] main: client: factor out some common code for "--log-level" option --- client.c | 37 ++++++++----------------------------- log.c | 40 ++++++++++++++++++++++++++++++++++++++++ log.h | 3 +++ main.c | 37 ++++++++----------------------------- meson.build | 2 ++ 5 files changed, 61 insertions(+), 58 deletions(-) diff --git a/client.c b/client.c index a1b86d30..bf9264ee 100644 --- a/client.c +++ b/client.c @@ -17,6 +17,7 @@ #include "client-protocol.h" #include "debug.h" #include "foot-features.h" +#include "macros.h" #include "util.h" #include "version.h" #include "xmalloc.h" @@ -156,38 +157,16 @@ main(int argc, char *const *argv) break; case 'd': { - static const char *log_level_map[] = { -#if defined(_DEBUG) - [LOG_CLASS_DEBUG] = "debug", -#endif - [LOG_CLASS_INFO] = "info", - [LOG_CLASS_WARNING] = "warning", - [LOG_CLASS_ERROR] = "error", - }; - - bool valid_log_level = false; - for (size_t i = 0; i < ALEN(log_level_map); i++) { - if (strcmp(optarg, log_level_map[i]) == 0) { - log_level = i; - valid_log_level = true; - break; - } - } - - if (!valid_log_level) { + int lvl = log_level_from_string(optarg); + if (unlikely(lvl < 0)) { fprintf( - stderr, "-d,--log-level: %s: argument must be one of ", - optarg); - - for (size_t i = 0; i < ALEN(log_level_map); i++) { - fprintf(stderr, "'%s'%s", - log_level_map[i], - i < ALEN(log_level_map) - 1 ? " , " : ""); - } - fprintf(stderr, "\n"); - + stderr, + "-d,--log-level: %s: argument must be one of %s\n", + optarg, + log_level_string_hint()); return EXIT_FAILURE; } + log_level = lvl; break; } diff --git a/log.c b/log.c index 10ba5346..1c33c723 100644 --- a/log.c +++ b/log.c @@ -10,11 +10,22 @@ #include #include "debug.h" +#include "util.h" +#include "xsnprintf.h" static bool colorize = false; static bool do_syslog = true; static enum log_class log_level = LOG_CLASS_INFO; +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 +}; + void log_init(enum log_colorize _colorize, bool _do_syslog, enum log_facility syslog_facility, enum log_class _log_level) @@ -149,3 +160,32 @@ void log_errno_provided(enum log_class log_class, const char *module, va_end(ap1); va_end(ap2); } + +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) + return i; + + return -1; +} + +const char * +log_level_string_hint(void) +{ + static char buf[64]; + 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]; + const char *delim = (i + 1 < n) ? ", " : ""; + pos += xsnprintf(buf + pos, sizeof(buf) - pos, "'%s'%s", entry, delim); + } + + return buf; +} diff --git a/log.h b/log.h index 5baddfcd..f0c28d7b 100644 --- a/log.h +++ b/log.h @@ -23,6 +23,9 @@ void log_errno_provided( const char *file, int lineno, int _errno, const char *fmt, ...) PRINTF(6); +int log_level_from_string(const char *str); +const char *log_level_string_hint(void); + #define LOG_ERR(...) \ log_msg(LOG_CLASS_ERROR, LOG_MODULE, __FILE__, __LINE__, __VA_ARGS__) #define LOG_ERRNO(...) \ diff --git a/main.c b/main.c index 4ea6e1fb..0fbf4e8b 100644 --- a/main.c +++ b/main.c @@ -23,6 +23,7 @@ #include "config.h" #include "foot-features.h" #include "fdm.h" +#include "macros.h" #include "reaper.h" #include "render.h" #include "server.h" @@ -310,38 +311,16 @@ main(int argc, char *const *argv) break; case 'd': { - static const char *log_level_map[] = { -#if defined(_DEBUG) - [LOG_CLASS_DEBUG] = "debug", -#endif - [LOG_CLASS_INFO] = "info", - [LOG_CLASS_WARNING] = "warning", - [LOG_CLASS_ERROR] = "error", - }; - - bool valid_log_level = false; - for (size_t i = 0; i < ALEN(log_level_map); i++) { - if (strcmp(optarg, log_level_map[i]) == 0) { - log_level = i; - valid_log_level = true; - break; - } - } - - if (!valid_log_level) { + int lvl = log_level_from_string(optarg); + if (unlikely(lvl < 0)) { fprintf( - stderr, "-d,--log-level: %s: argument must be one of ", - optarg); - - for (size_t i = 0; i < ALEN(log_level_map); i++) { - fprintf(stderr, "'%s'%s", - log_level_map[i], - i < ALEN(log_level_map) - 1 ? " , " : ""); - } - fprintf(stderr, "\n"); - + stderr, + "-d,--log-level: %s: argument must be one of %s\n", + optarg, + log_level_string_hint()); return EXIT_FAILURE; } + log_level = lvl; break; } diff --git a/meson.build b/meson.build index f2e6da2b..85875e7a 100644 --- a/meson.build +++ b/meson.build @@ -196,7 +196,9 @@ executable( 'foot-features.h', 'log.c', 'log.h', 'macros.h', + 'util.h', 'xmalloc.c', 'xmalloc.h', + 'xsnprintf.c', 'xsnprintf.h', version, install: true)