From 46c0a3adf1278c197be77eba1c7a92c1b98a2c24 Mon Sep 17 00:00:00 2001 From: Andrew Nelson Date: Tue, 30 Jan 2024 12:59:48 -0800 Subject: [PATCH 1/5] cli: Create BST_VERBOSITY to enable/disable warnings Warnings are usually helpful to end-users but when bst is invoked by automations in a well controlled environment it is often preferable to only print fatal errors. With this change, setting BST_VERBOSITY=0 will turn off non-fatal warnings. --- err.c | 36 ++++++++++++++++++++++++++++++++++++ errutil.h | 3 +++ main.c | 1 + 3 files changed, 40 insertions(+) diff --git a/err.c b/err.c index fd0ce6a..186bf2a 100644 --- a/err.c +++ b/err.c @@ -26,6 +26,36 @@ void (*err_exit)(int) = exit; const char *err_line_ending = "\n"; int err_flags = 0; +/* Parse the BST_VERBOSITY environment variable to determine the requested + verbosity. Higher numbers are associated with more verbosity, with 9 + being the most verbose and 0 being the least verbose. The default + verbosity level is 1. */ + +static int parsedverbosity() +{ + char *pszVerbosity = getenv("BST_VERBOSITY"); + if (pszVerbosity == NULL) { + return 1; + } + char *endPtr; + int iVerbosity = strtol(pszVerbosity, &endPtr, 10); + if (*endPtr != '\0') { + // Soemthing went wrong during parsing. This isn't + // critical so let's just ignore the parameter. + return 1; + } + return iVerbosity; +} + +void init_logverbosity() +{ + int iVerbosity = parsedverbosity(); + + if (iVerbosity >= 1) { + err_flags |= ERR_VERBOSE; + } +} + /* fdprintf and vfdprintf are fork-safe versions of fprintf and vfprintf. */ static void vfdprintf(int fd, const char *fmt, va_list vl) @@ -64,6 +94,9 @@ static void vwarnsyslog(int errcode, const char *fmt, va_list vl) void vwarn(const char *fmt, va_list vl) { + if (!(err_flags & ERR_VERBOSE)) { + return; + } if (err_flags & ERR_USE_SYSLOG) { vwarnsyslog(errno, fmt, vl); return; @@ -75,6 +108,9 @@ void vwarn(const char *fmt, va_list vl) void vwarnx(const char *fmt, va_list vl) { + if (!(err_flags & ERR_VERBOSE)) { + return; + } if (err_flags & ERR_USE_SYSLOG) { vwarnsyslog(0, fmt, vl); return; diff --git a/errutil.h b/errutil.h index 015d7c7..94b8cb5 100644 --- a/errutil.h +++ b/errutil.h @@ -9,10 +9,13 @@ enum { ERR_USE_SYSLOG = 1, + + ERR_VERBOSE = 2, }; extern void (*err_exit)(int); extern const char *err_line_ending; +void init_logverbosity(); extern int err_flags; #endif /* !ERRUTIL_H */ diff --git a/main.c b/main.c index ae9761e..57583d8 100644 --- a/main.c +++ b/main.c @@ -263,6 +263,7 @@ int usage(int error, char *argv0) int main(int argc, char *argv[], char *envp[]) { + init_logverbosity(); init_capabilities(); static struct entry_settings opts; From 38601534fe5b4ab742c143af43bd6036452be4fa Mon Sep 17 00:00:00 2001 From: Andrew Nelson Date: Tue, 30 Jan 2024 13:42:41 -0800 Subject: [PATCH 2/5] Fix build error --- main.c | 1 + 1 file changed, 1 insertion(+) diff --git a/main.c b/main.c index 57583d8..f1fc7ab 100644 --- a/main.c +++ b/main.c @@ -24,6 +24,7 @@ #include "capable.h" #include "compat.h" #include "enter.h" +#include "errutil.h" #include "kvlist.h" #include "util.h" #include "path.h" From 107f126749074409d49aa56a58f9c1c78725f067 Mon Sep 17 00:00:00 2001 From: Andrew Nelson Date: Wed, 31 Jan 2024 11:58:16 -0800 Subject: [PATCH 3/5] Address review comments --- err.c | 35 +++++++++++++++++------------------ errutil.h | 2 +- 2 files changed, 18 insertions(+), 19 deletions(-) diff --git a/err.c b/err.c index 186bf2a..0066ef7 100644 --- a/err.c +++ b/err.c @@ -31,29 +31,28 @@ int err_flags = 0; being the most verbose and 0 being the least verbose. The default verbosity level is 1. */ -static int parsedverbosity() +noreturn void err(int eval, const char *fmt, ...); +static int parsedverbosity(void) { - char *pszVerbosity = getenv("BST_VERBOSITY"); - if (pszVerbosity == NULL) { - return 1; - } - char *endPtr; - int iVerbosity = strtol(pszVerbosity, &endPtr, 10); - if (*endPtr != '\0') { - // Soemthing went wrong during parsing. This isn't - // critical so let's just ignore the parameter. - return 1; - } - return iVerbosity; + char *pszVerbosity = getenv("BST_VERBOSITY"); + if (pszVerbosity == NULL) { + return 1; + } + char *endPtr; + int iVerbosity = strtol(pszVerbosity, &endPtr, 10); + if (*endPtr != '\0') { + err(2, "un-parsable value of BST_VERBOSITY"); + } + return iVerbosity; } -void init_logverbosity() +void init_logverbosity(void) { - int iVerbosity = parsedverbosity(); + int iVerbosity = parsedverbosity(); - if (iVerbosity >= 1) { - err_flags |= ERR_VERBOSE; - } + if (iVerbosity >= 1) { + err_flags |= ERR_VERBOSE; + } } /* fdprintf and vfdprintf are fork-safe versions of fprintf and vfprintf. */ diff --git a/errutil.h b/errutil.h index 94b8cb5..e33a257 100644 --- a/errutil.h +++ b/errutil.h @@ -10,7 +10,7 @@ enum { ERR_USE_SYSLOG = 1, - ERR_VERBOSE = 2, + ERR_VERBOSE = 2, }; extern void (*err_exit)(int); From 151e7486457a280ef5770bec5004634165f86a53 Mon Sep 17 00:00:00 2001 From: Andrew Nelson Date: Wed, 31 Jan 2024 12:51:49 -0800 Subject: [PATCH 4/5] Move err prototype to errutil.h --- err.c | 3 +-- errutil.h | 3 +++ 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/err.c b/err.c index 0066ef7..19bc753 100644 --- a/err.c +++ b/err.c @@ -31,7 +31,6 @@ int err_flags = 0; being the most verbose and 0 being the least verbose. The default verbosity level is 1. */ -noreturn void err(int eval, const char *fmt, ...); static int parsedverbosity(void) { char *pszVerbosity = getenv("BST_VERBOSITY"); @@ -41,7 +40,7 @@ static int parsedverbosity(void) char *endPtr; int iVerbosity = strtol(pszVerbosity, &endPtr, 10); if (*endPtr != '\0') { - err(2, "un-parsable value of BST_VERBOSITY"); + err(2, "un-parsable value of BST_VERBOSITY"); } return iVerbosity; } diff --git a/errutil.h b/errutil.h index e33a257..b95572c 100644 --- a/errutil.h +++ b/errutil.h @@ -7,6 +7,8 @@ #ifndef ERRUTIL_H_ # define ERRUTIL_H_ +#include + enum { ERR_USE_SYSLOG = 1, @@ -16,6 +18,7 @@ enum { extern void (*err_exit)(int); extern const char *err_line_ending; void init_logverbosity(); +noreturn void err(int eval, const char *fmt, ...); extern int err_flags; #endif /* !ERRUTIL_H */ From 9c72446272bf0980806861ec9fd78b9710ccd6df Mon Sep 17 00:00:00 2001 From: Andrew Nelson Date: Wed, 31 Jan 2024 15:06:45 -0800 Subject: [PATCH 5/5] Fix handling of mis-parsed BST_VERBOSITY env var --- err.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/err.c b/err.c index 19bc753..15daa02 100644 --- a/err.c +++ b/err.c @@ -40,7 +40,7 @@ static int parsedverbosity(void) char *endPtr; int iVerbosity = strtol(pszVerbosity, &endPtr, 10); if (*endPtr != '\0') { - err(2, "un-parsable value of BST_VERBOSITY"); + err(2, "un-parsable value of BST_VERBOSITY\n"); } return iVerbosity; } @@ -90,11 +90,8 @@ static void vwarnsyslog(int errcode, const char *fmt, va_list vl) vsyslog(LOG_ERR, fmt, vl); } -void vwarn(const char *fmt, va_list vl) +static void vwarn(const char *fmt, va_list vl) { - if (!(err_flags & ERR_VERBOSE)) { - return; - } if (err_flags & ERR_USE_SYSLOG) { vwarnsyslog(errno, fmt, vl); return; @@ -104,11 +101,8 @@ void vwarn(const char *fmt, va_list vl) fdprintf(STDERR_FILENO, ": %s%s", strerror(errno), err_line_ending); } -void vwarnx(const char *fmt, va_list vl) +static void vwarnx(const char *fmt, va_list vl) { - if (!(err_flags & ERR_VERBOSE)) { - return; - } if (err_flags & ERR_USE_SYSLOG) { vwarnsyslog(0, fmt, vl); return; @@ -120,6 +114,9 @@ void vwarnx(const char *fmt, va_list vl) void warn(const char *fmt, ...) { + if (!(err_flags & ERR_VERBOSE)) { + return; + } va_list vl; va_start(vl, fmt); vwarn(fmt, vl); @@ -128,6 +125,9 @@ void warn(const char *fmt, ...) void warnx(const char *fmt, ...) { + if (!(err_flags & ERR_VERBOSE)) { + return; + } va_list vl; va_start(vl, fmt); vwarnx(fmt, vl);