From d66182605e15273b27b32d11311c3fa0e55d525c Mon Sep 17 00:00:00 2001 From: Alex 'AdUser' Z Date: Sat, 12 Mar 2016 20:48:56 +1000 Subject: [PATCH] * work on logs --- src/log.c | 36 ++++++++++++++++++++---------------- src/log.h | 4 +++- 2 files changed, 23 insertions(+), 17 deletions(-) diff --git a/src/log.c b/src/log.c index 37b1f71..09dc0c6 100644 --- a/src/log.c +++ b/src/log.c @@ -62,24 +62,28 @@ void f2b_log_set_level(const char *level) { if (strcmp(level, "fatal") == 0) { minlevel = log_fatal; return; } } -void f2b_log_setup(const char *target, const char *path) { - if (strcmp(target, "syslog") == 0) { - dest = log_syslog; - openlog("f2b", LOG_CONS, LOG_DAEMON); +void f2b_log_to_stderr() { + if (logfile && logfile != stderr) + fclose(logfile); + dest = log_stderr; + logfile = stderr; +} + +void f2b_log_to_file(const char *path) { + FILE *new = NULL; + if (path == NULL || *path == '\0') return; - } else - if (strcmp(target, "file") == 0 && *path != '\0') { - dest = log_file; - if (logfile) + if ((new = fopen(path, "a")) != NULL) { + if (logfile && logfile != stderr) fclose(logfile); - if ((logfile = fopen(path, "a")) == NULL) - dest = log_stderr, logfile = stderr; - f2b_log_msg(log_error, "can't open logfile: %s -- %s", path, strerror(errno)); - return; - } else { - dest = log_stderr; - logfile = stderr; + dest = log_file; + logfile = new; } +} - return; +void f2b_log_to_syslog() { + if (logfile && logfile != stderr) + fclose(logfile); + dest = log_syslog; + openlog("f2b", 0 | LOG_PID, LOG_DAEMON); } diff --git a/src/log.h b/src/log.h index eba9be5..ad212cf 100644 --- a/src/log.h +++ b/src/log.h @@ -13,6 +13,8 @@ typedef enum { void f2b_log_msg(log_msgtype_t l, const char *fmt, ...); void f2b_log_set_level(const char *level); -void f2b_log_setup(const char *target, const char *path); +void f2b_log_to_file (const char *path); +void f2b_log_to_stderr(); +void f2b_log_to_syslog(); #endif /* F2B_LOG_H_ */