From 55a23a5b290140816a7f7c215ef90c8af1cb82ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ekl=C3=B6f?= Date: Wed, 20 Nov 2019 19:43:31 +0100 Subject: [PATCH] log: set syslog facility to LOG_DAEMON when run in server mode --- log.c | 15 ++++++++++----- log.h | 4 ++++ main.c | 3 +++ 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/log.c b/log.c index e046cd79..da026d60 100644 --- a/log.c +++ b/log.c @@ -13,16 +13,21 @@ static bool colorize = false; -static void __attribute__((constructor)) -init(void) +void +log_init(enum log_facility syslog_facility) { + static const int facility_map[] = { + [LOG_FACILITY_USER] = LOG_USER, + [LOG_FACILITY_DAEMON] = LOG_DAEMON, + }; + colorize = isatty(STDOUT_FILENO); - openlog(NULL, /*LOG_PID*/0, LOG_USER); + openlog(NULL, /*LOG_PID*/0, facility_map[syslog_facility]); setlogmask(LOG_UPTO(LOG_WARNING)); } -static void __attribute__((destructor)) -fini(void) +void +log_deinit(void) { closelog(); } diff --git a/log.h b/log.h index ba32d037..f0da5158 100644 --- a/log.h +++ b/log.h @@ -1,7 +1,11 @@ #pragma once +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_facility syslog_facility); +void log_deinit(void); + void log_msg(enum log_class log_class, const char *module, const char *file, int lineno, const char *fmt, ...) __attribute__((format (printf, 5, 6))); diff --git a/main.c b/main.c index 4a392533..f241e52d 100644 --- a/main.c +++ b/main.c @@ -190,6 +190,8 @@ main(int argc, char *const *argv) struct server *server = NULL; struct shutdown_context shutdown_ctx = {.term = &term, .exit_code = EXIT_FAILURE}; + log_init(as_server ? LOG_FACILITY_DAEMON : LOG_FACILITY_USER); + /* This ensures we keep a set of fonts in the cache */ if (!initialize_fonts(&conf, fonts)) goto out; @@ -241,5 +243,6 @@ out: font_destroy(fonts[i]); config_free(conf); + log_deinit(); return ret == EXIT_SUCCESS && !as_server ? shutdown_ctx.exit_code : ret; }