log: don’t default to syslog enabled

Initialize the global ‘do_syslog’ variable to false. This ensures any
log calls done before log_init() has been called (e.g. unit tests)
doesn’t syslog anything.

As a side effect, such log calls no longer open an implicit syslog
file descriptor; this is how this “bug” was found: valgrind detected
an unclosed file descriptor at exit.

Finally, completely disable syslogging if log-level is “none”.
This commit is contained in:
Daniel Eklöf 2022-12-02 11:45:10 +01:00
parent 76d494484f
commit b43a41df6a
No known key found for this signature in database
GPG key ID: 5BBD4992C116573F

9
log.c
View file

@ -15,7 +15,7 @@
#include "xsnprintf.h" #include "xsnprintf.h"
static bool colorize = false; static bool colorize = false;
static bool do_syslog = true; static bool do_syslog = false;
static enum log_class log_level = LOG_CLASS_NONE; static enum log_class log_level = LOG_CLASS_NONE;
static const struct { static const struct {
@ -45,8 +45,13 @@ log_init(enum log_colorize _colorize, bool _do_syslog,
log_level = _log_level; log_level = _log_level;
int slvl = log_level_map[_log_level].syslog_equivalent; int slvl = log_level_map[_log_level].syslog_equivalent;
if (do_syslog && slvl != -1) { if (slvl < 0)
do_syslog = false;
if (do_syslog) {
openlog(NULL, /*LOG_PID*/0, facility_map[syslog_facility]); openlog(NULL, /*LOG_PID*/0, facility_map[syslog_facility]);
xassert(slvl >= 0);
setlogmask(LOG_UPTO(slvl)); setlogmask(LOG_UPTO(slvl));
} }
} }