From 6cc1388e06e5e12670dc28b500b40fdacba38adf Mon Sep 17 00:00:00 2001 From: Jan Friesse Date: Mon, 12 Aug 2019 14:04:04 +0200 Subject: [PATCH] qnetd: Add function to set log target Signed-off-by: Jan Friesse --- qdevices/corosync-qnetd.c | 2 +- qdevices/log.c | 22 +++++++++++++++++----- qdevices/log.h | 4 +++- 3 files changed, 21 insertions(+), 7 deletions(-) diff --git a/qdevices/corosync-qnetd.c b/qdevices/corosync-qnetd.c index b9d2954..379ae27 100644 --- a/qdevices/corosync-qnetd.c +++ b/qdevices/corosync-qnetd.c @@ -544,7 +544,7 @@ main(int argc, char * const argv[]) log_target |= LOG_TARGET_STDERR; } - res = log_init(QNETD_PROGRAM_NAME, log_target); + res = log_init(QNETD_PROGRAM_NAME, log_target, LOG_DAEMON); if (res == -1) { errx(1, "Can't initialize logging"); } diff --git a/qdevices/log.c b/qdevices/log.c index 47ca167..e08790e 100644 --- a/qdevices/log.c +++ b/qdevices/log.c @@ -43,6 +43,7 @@ static int log_config_target = 0; static int log_config_debug = 0; static int log_config_priority_bump = 0; +static int log_config_syslog_facility = 0; static char *log_config_ident = NULL; static const char log_month_str[][4] = { @@ -67,19 +68,16 @@ static struct log_syslog_prio_to_str_item syslog_prio_to_str_array[] = { {-1, NULL}}; int -log_init(const char *ident, int target) +log_init(const char *ident, int target, int syslog_facility) { - log_config_target = target; log_config_ident = strdup(ident); if (log_config_ident == NULL) { return (-1); } - if (log_config_target & LOG_TARGET_SYSLOG) { - openlog(QNETD_PROGRAM_NAME, LOG_PID, LOG_DAEMON); - } + log_set_target(target, syslog_facility); return (0); } @@ -168,6 +166,20 @@ log_set_priority_bump(int enabled) log_config_priority_bump = enabled; } +void +log_set_target(int target, int syslog_facility) +{ + + log_close(); + + log_config_target = target; + log_config_syslog_facility = syslog_facility; + + if (log_config_target & LOG_TARGET_SYSLOG) { + openlog(log_config_ident, LOG_PID, log_config_syslog_facility); + } +} + void log_msg_decode_error(int ret) { diff --git a/qdevices/log.h b/qdevices/log.h index c376a70..dc43c9f 100644 --- a/qdevices/log.h +++ b/qdevices/log.h @@ -53,7 +53,7 @@ extern "C" { #define log_err(priority, str) log_printf(priority, "%s (%d): %s", \ str, errno, strerror(errno)) -extern int log_init(const char *ident, int target); +extern int log_init(const char *ident, int target, int syslog_facility); extern void log_printf(int priority, const char *format, ...) __attribute__((__format__(__printf__, 2, 3))); @@ -67,6 +67,8 @@ extern void log_set_debug(int enabled); extern void log_set_priority_bump(int enabled); +extern void log_set_target(int target, int syslog_facility); + extern void log_msg_decode_error(int ret); #ifdef __cplusplus -- 2.39.5