]> Untitled Git - mpdstream/commitdiff
Add support to log to syslog
authorHugo Villeneuve <hvilleneuve@dimonoff.com>
Tue, 31 Mar 2026 18:54:47 +0000 (14:54 -0400)
committerHugo Villeneuve <hvilleneuve@dimonoff.com>
Tue, 31 Mar 2026 20:34:21 +0000 (16:34 -0400)
Signed-off-by: Hugo Villeneuve <hvilleneuve@dimonoff.com>
src/log.c
src/log.h
src/main.c
src/options.c

index c85447e7fc264872c36ad4722c0a493edff4f339..5649e949cbe218bdfa1c90c53a9811f6cad7c99c 100644 (file)
--- a/src/log.c
+++ b/src/log.c
 #include <stdlib.h>
 #include <stdarg.h>
 #include <string.h>
+#include <syslog.h>
 
 #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);
 }
index 5b07242a0037bc87290d4e08fcfbf56601b18418..0b9ff16813a3d9618e68c7a4f05ba383fb63f0c1 100644 (file)
--- 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 */
 };
index 111582b5a7cd285361a655a222bb32fba0b29070..817c9ba8dc063c1b1929fd63438ad3dc4303f032 100644 (file)
@@ -12,6 +12,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <syslog.h>
 #include <sys/stat.h>
 #include <unistd.h>
 
@@ -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);
 }
index 22b930f33892b2d7b04a20bb10f0f2d404f2ddad..4a10590518ad9b0a9af55056bba86e843435d8dd 100644 (file)
@@ -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);