From: Hugo Villeneuve Date: Tue, 31 Mar 2026 18:54:47 +0000 (-0400) Subject: Add support to log to syslog X-Git-Url: http://gitweb.hugovil.com/sitemap.xml?a=commitdiff_plain;h=9a7e4ff075e1c069a9161974f9a11ba8bc05b761;p=mpdstream Add support to log to syslog Signed-off-by: Hugo Villeneuve --- diff --git a/src/log.c b/src/log.c index c85447e..5649e94 100644 --- a/src/log.c +++ b/src/log.c @@ -12,12 +12,12 @@ #include #include #include +#include #include "options.h" #include "log.h" #define PREFIX_PACKAGE_NAME 1 -#define ADD_LINEFEED 0 extern struct options_t options; @@ -30,99 +30,88 @@ log_prefix_package_name(FILE *stream, const char *severity) #endif /* PREFIX_PACKAGE_NAME */ } -static void -log_suffix_newline(FILE *stream) +static void log_common(const int priority, const char* format, va_list ap) { -#if ADD_LINEFEED - fprintf(stream, "\n"); -#else - (void) stream; -#endif /* ADD_LINEFEED */ + va_list ap_syslog; + FILE *stream; + + va_copy(ap_syslog, ap); + + switch (priority) { + case LOG_ERR: + case LOG_WARNING: + case LOG_EMERG: + case LOG_ALERT: + case LOG_CRIT: + stream = stderr; + break; + default: + stream = stdout; + break; + } + + log_prefix_package_name(stream, "debug"); + + (void) vfprintf(stream, format, ap); + va_end(ap); + + vsyslog(priority, format, ap_syslog); + va_end(ap_syslog); } void log_debug(const char *format, ...) { - FILE *stream = stdout; - - if (options.log_level >= LOG_LEVEL_DEBUG) { - va_list ap; - - log_prefix_package_name(stream, "debug"); + va_list ap; - va_start(ap, format); - (void) vfprintf(stream, format, ap); - va_end(ap); + if (options.log_level < LOG_LEVEL_DEBUG) + return; - log_suffix_newline(stream); - } + va_start(ap, format); + log_common(LOG_DEBUG, format, ap); } void log_info(const char *format, ...) { - FILE *stream = stdout; - - if (options.log_level >= LOG_LEVEL_INFO) { - va_list ap; - - log_prefix_package_name(stream, "info"); + va_list ap; - va_start(ap, format); - (void) vfprintf(stream, format, ap); - va_end(ap); + if (options.log_level < LOG_LEVEL_INFO) + return; - log_suffix_newline(stream); - } + va_start(ap, format); + log_common(LOG_INFO, format, ap); } void log_warn(const char *format, ...) { - FILE *stream = stderr; - - if (options.log_level >= LOG_LEVEL_WARN) { - va_list ap; - - log_prefix_package_name(stream, "warn"); + va_list ap; - va_start(ap, format); - (void) vfprintf(stream, format, ap); - va_end(ap); + if (options.log_level < LOG_LEVEL_WARNING) + return; - log_suffix_newline(stream); - } + va_start(ap, format); + log_common(LOG_WARNING, format, ap); } void log_err(const char *format, ...) { - FILE *stream = stderr; va_list ap; - log_prefix_package_name(stream, "error"); - va_start(ap, format); - (void) vfprintf(stream, format, ap); - va_end(ap); - - log_suffix_newline(stream); + log_common(LOG_ERR, format, ap); } /* Log error message and exits with error code. */ void log_fail(const char *format, ...) { - FILE *stream = stderr; va_list ap; - log_prefix_package_name(stream, "error"); - va_start(ap, format); - (void) vfprintf(stream, format, ap); - va_end(ap); - - log_suffix_newline(stream); + log_common(LOG_ERR, format, ap); exit(EXIT_FAILURE); } diff --git a/src/log.h b/src/log.h index 5b07242..0b9ff16 100644 --- a/src/log.h +++ b/src/log.h @@ -9,7 +9,7 @@ enum LOG_LEVEL { LOG_LEVEL_ERR = 0, /* Display only errors */ - LOG_LEVEL_WARN, /* Display warnings */ + LOG_LEVEL_WARNING, /* Display warnings */ LOG_LEVEL_INFO, /* Display information messages */ LOG_LEVEL_DEBUG, /* Display all messages */ }; diff --git a/src/main.c b/src/main.c index 111582b..817c9ba 100644 --- a/src/main.c +++ b/src/main.c @@ -12,6 +12,7 @@ #include #include #include +#include #include #include @@ -148,6 +149,8 @@ main(int argc, char **argv) { int rc; + openlog(PACKAGE_NAME, 0, LOG_USER); + rc = parse_command_line_options(argc, argv); if (rc) exit(EXIT_FAILURE); @@ -211,9 +214,12 @@ main(int argc, char **argv) } mpd_status_free(status); + + last_state = state; } mpd_connection_free(mpdc); + closelog(); exit(EXIT_SUCCESS); } diff --git a/src/options.c b/src/options.c index 22b930f..4a10590 100644 --- a/src/options.c +++ b/src/options.c @@ -148,7 +148,7 @@ int parse_command_line_options(int argc, char *argv[]) options.host = NULL; options.port = 0; options.start_delay = 1; - options.log_level = LOG_LEVEL_WARN; + options.log_level = LOG_LEVEL_WARNING; /* Parse our arguments. */ rc = argp_parse(&argp, argc, argv, ARGP_NO_HELP, 0, NULL);