From 1fe847cb4e7f366a19fe0866c8ad8009dded4577 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ekl=C3=B6f?= Date: Sat, 26 Jan 2019 14:52:40 +0100 Subject: [PATCH 1/4] log: remove dead code --- log.c | 1 - 1 file changed, 1 deletion(-) diff --git a/log.c b/log.c index 3ec75b6..c12a99e 100644 --- a/log.c +++ b/log.c @@ -35,7 +35,6 @@ _log(enum log_class log_class, const char *module, const char *file, int lineno, if (colorize) printf("\e[0m"); - //printf("%%s\n", buf); vprintf(fmt, va); if (sys_errno != 0) From 453364e9f881e49f1572366cb6dc7b061a295c2f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ekl=C3=B6f?= Date: Sat, 26 Jan 2019 14:52:56 +0100 Subject: [PATCH 2/4] log: constructor/destructor to initialize/close syslog --- log.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/log.c b/log.c index c12a99e..ccb425e 100644 --- a/log.c +++ b/log.c @@ -6,6 +6,20 @@ #include #include +#include + +static void __attribute__((constructor)) +init(void) +{ + openlog(NULL, /*LOG_PID*/0, LOG_USER); +} + +static void __attribute__((destructor)) +fini(void) +{ + closelog(); +} + static void _log(enum log_class log_class, const char *module, const char *file, int lineno, const char *fmt, int sys_errno, va_list va) From 89a61eed7b21daa6754a7a96ee788b3b9d681907 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ekl=C3=B6f?= Date: Sat, 26 Jan 2019 14:53:28 +0100 Subject: [PATCH 3/4] log: add internal function that logs to syslog Differences to the console logger: * No coloring * Always use module name, not filename:line-number --- log.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/log.c b/log.c index ccb425e..f0287e4 100644 --- a/log.c +++ b/log.c @@ -1,10 +1,12 @@ #include "log.h" +#include #include #include #include #include #include +#include #include @@ -57,6 +59,50 @@ _log(enum log_class log_class, const char *module, const char *file, int lineno, printf("\n"); } +static void +_sys_log(enum log_class log_class, const char *module, const char *file, + int lineno, const char *fmt, int sys_errno, va_list va) +{ + /* Map our log level to syslog's level */ + int level = -1; + switch (log_class) { + case LOG_CLASS_ERROR: level = LOG_ERR; break; + case LOG_CLASS_WARNING: level = LOG_WARNING; break; + case LOG_CLASS_INFO: level = LOG_INFO; break; + case LOG_CLASS_DEBUG: level = LOG_DEBUG; break; + } + + assert(level != -1); + + const char *sys_err = sys_errno != 0 ? strerror(sys_errno) : NULL; + + va_list va2; + va_copy(va2, va); + + /* Calculate required size of buffer holding the entire log message */ + int required_len = 0; + required_len += strlen(module) + 2; /* "%s: " */ + required_len += vsnprintf(NULL, 0, fmt, va2); va_end(va2); + + if (sys_errno != 0) + required_len += strlen(sys_err) + 2; /* ": %s" */ + + /* Format the msg */ + char *msg = malloc(required_len + 1); + int idx = 0; + + idx += snprintf(&msg[idx], required_len + 1 - idx, "%s: ", module); + idx += vsnprintf(&msg[idx], required_len + 1 - idx, fmt, va); + + if (sys_errno != 0) { + idx += snprintf( + &msg[idx], required_len + 1 - idx, ": %s", strerror(sys_errno)); + } + + syslog(level, "%s", msg); + free(msg); +} + void log_msg(enum log_class log_class, const char *module, const char *file, int lineno, const char *fmt, ...) From 735ab8daa6d0b11b66032290a8f2ef194dbcbd87 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ekl=C3=B6f?= Date: Sat, 26 Jan 2019 14:54:04 +0100 Subject: [PATCH 4/4] log: always log to both console and syslog --- log.c | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/log.c b/log.c index f0287e4..ff7346f 100644 --- a/log.c +++ b/log.c @@ -107,28 +107,37 @@ void log_msg(enum log_class log_class, const char *module, const char *file, int lineno, const char *fmt, ...) { - va_list ap; - va_start(ap, fmt); - _log(log_class, module, file, lineno, fmt, 0, ap); - va_end(ap); + va_list ap1, ap2; + va_start(ap1, fmt); + va_copy(ap2, ap1); + _log(log_class, module, file, lineno, fmt, 0, ap1); + _sys_log(log_class, module, file, lineno, fmt, 0, ap2); + va_end(ap1); + va_end(ap2); } void log_errno(enum log_class log_class, const char *module, const char *file, int lineno, const char *fmt, ...) { - va_list ap; - va_start(ap, fmt); - _log(log_class, module, file, lineno, fmt, errno, ap); - va_end(ap); + va_list ap1, ap2; + va_start(ap1, fmt); + va_copy(ap2, ap1); + _log(log_class, module, file, lineno, fmt, errno, ap1); + _sys_log(log_class, module, file, lineno, fmt, errno, ap2); + va_end(ap1); + va_end(ap2); } void log_errno_provided(enum log_class log_class, const char *module, const char *file, int lineno, int _errno, const char *fmt, ...) { - va_list ap; - va_start(ap, fmt); - _log(log_class, module, file, lineno, fmt, _errno, ap); - va_end(ap); + va_list ap1, ap2; + va_start(ap1, fmt); + va_copy(ap2, ap1); + _log(log_class, module, file, lineno, fmt, _errno, ap1); + _sys_log(log_class, module, file, lineno, fmt, _errno, ap2); + va_end(ap1); + va_end(ap2); }