add die() to log an error message and exit with a single call

Almost all occurences of err() are either immediately followed by
exit(EXIT_FAILURE), usage(argv[0]) (which itself then calls
exit(EXIT_FAILURE), or that is what's done immediately after returning
from the function that calls err(). Modify the errfn macro so that its
instantiations can include exit(EXIT_FAILURE) at the end, and use that
to create a new function die() that will log an error and then
exit.

Signed-off-by: Laine Stump <laine@redhat.com>
Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
This commit is contained in:
Laine Stump 2023-02-15 03:24:30 -05:00 committed by Stefano Brivio
parent 193385bd2f
commit dac4af81e3
2 changed files with 10 additions and 5 deletions

14
log.c
View file

@ -44,7 +44,7 @@ static char log_header[BUFSIZ]; /* File header, written back on cuts */
static time_t log_start; /* Start timestamp */ static time_t log_start; /* Start timestamp */
int log_trace; /* --trace mode enabled */ int log_trace; /* --trace mode enabled */
#define logfn(name, level) \ #define logfn(name, level, doexit) \
void name(const char *format, ...) { \ void name(const char *format, ...) { \
struct timespec tp; \ struct timespec tp; \
va_list args; \ va_list args; \
@ -74,6 +74,9 @@ void name(const char *format, ...) { \
if (format[strlen(format)] != '\n') \ if (format[strlen(format)] != '\n') \
fprintf(stderr, "\n"); \ fprintf(stderr, "\n"); \
} \ } \
\
if (doexit) \
exit(EXIT_FAILURE); \
} }
/* Prefixes for log file messages, indexed by priority */ /* Prefixes for log file messages, indexed by priority */
@ -86,10 +89,11 @@ const char *logfile_prefix[] = {
" ", /* LOG_DEBUG */ " ", /* LOG_DEBUG */
}; };
logfn(err, LOG_ERR) logfn(die, LOG_ERR, 1)
logfn(warn, LOG_WARNING) logfn(err, LOG_ERR, 0)
logfn(info, LOG_INFO) logfn(warn, LOG_WARNING, 0)
logfn(debug, LOG_DEBUG) logfn(info, LOG_INFO, 0)
logfn(debug,LOG_DEBUG, 0)
/** /**
* trace_init() - Set log_trace depending on trace (debug) mode * trace_init() - Set log_trace depending on trace (debug) mode

1
log.h
View file

@ -10,6 +10,7 @@
#define LOGFILE_CUT_RATIO 30 /* When full, cut ~30% size */ #define LOGFILE_CUT_RATIO 30 /* When full, cut ~30% size */
#define LOGFILE_SIZE_MIN (5UL * MAX(BUFSIZ, PAGE_SIZE)) #define LOGFILE_SIZE_MIN (5UL * MAX(BUFSIZ, PAGE_SIZE))
void die(const char *format, ...);
void err(const char *format, ...); void err(const char *format, ...);
void warn(const char *format, ...); void warn(const char *format, ...);
void info(const char *format, ...); void info(const char *format, ...);