Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cli: Create BST_VERBOSITY to enable/disable warnings #92

Merged
merged 5 commits into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 36 additions & 2 deletions err.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,34 @@ 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(void)
{
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\n");
}
return iVerbosity;
}

void init_logverbosity(void)
{
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)
Expand Down Expand Up @@ -62,7 +90,7 @@ 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_USE_SYSLOG) {
vwarnsyslog(errno, fmt, vl);
Expand All @@ -73,7 +101,7 @@ 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_USE_SYSLOG) {
vwarnsyslog(0, fmt, vl);
Expand All @@ -86,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);
Expand All @@ -94,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);
Expand Down
6 changes: 6 additions & 0 deletions errutil.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,18 @@
#ifndef ERRUTIL_H_
# define ERRUTIL_H_

#include <stdnoreturn.h>

enum {
ERR_USE_SYSLOG = 1,

ERR_VERBOSE = 2,
};

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 */
2 changes: 2 additions & 0 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -263,6 +264,7 @@ int usage(int error, char *argv0)

int main(int argc, char *argv[], char *envp[])
{
init_logverbosity();
init_capabilities();

static struct entry_settings opts;
Expand Down
Loading