log: set syslog facility to LOG_DAEMON when run in server mode

This commit is contained in:
Daniel Eklöf 2019-11-20 19:43:31 +01:00
parent 4f4ee5b39d
commit 55a23a5b29
No known key found for this signature in database
GPG key ID: 5BBD4992C116573F
3 changed files with 17 additions and 5 deletions

15
log.c
View file

@ -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();
}

4
log.h
View file

@ -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)));

3
main.c
View file

@ -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;
}