]> git.ipfire.org Git - thirdparty/mlmmj.git/commitdiff
log_error: rework syslog
authorBaptiste Daroussin <bapt@FreeBSD.org>
Wed, 26 Oct 2022 13:36:24 +0000 (15:36 +0200)
committerBaptiste Daroussin <bapt@FreeBSD.org>
Wed, 26 Oct 2022 13:36:24 +0000 (15:36 +0200)
- always open/close syslog when the log_name is changed
- add a new function to allow writing the log to stderr if needed
- log_error does not have anymore any length limit anymore

include/log_error.h
src/log_error.c
src/mlmmj-maintd.c
tests/functional-tests.sh
tests/mlmmj-maintd.sh

index 9d6f414b72e8eb2489144376cd433ebd39ea56c5..95270310139c6bf41f6a4623f469decebc05dd13 100644 (file)
@@ -31,6 +31,7 @@
 
 void log_set_name(const char *name);
 void log_set_namef(const char *fmt, ...);
+void log_activate_stderr(void);
 void log_free_name(void);
 void log_error(const char *file, int line, const char *errstr,
                const char *fmt, ...);
index a8a02a94784d4ae42730d256f13e28e8138909ec..c8846fe079527e2e8f23f0ca430e1c7a08c1cd2a 100644 (file)
@@ -1,6 +1,6 @@
-/* Copyright (C) 2004 Morten K. Poulsen <morten at afdelingp.dk>
- *
- * $Id$
+/*
+ * Copyright (C) 2004 Morten K. Poulsen <morten at afdelingp.dk>
+ * Copyright (C) 2022 Baptiste Daroussin <bapt@FreeBSD.org>
  *
  * Permission is hereby granted, free of charge, to any person obtaining a copy
  * of this software and associated documentation files (the "Software"), to
@@ -21,6 +21,7 @@
  * IN THE SOFTWARE.
  */
 
+#include <stdbool.h>
 #include <stdio.h>
 #include <string.h>
 #include <stdlib.h>
 #endif
 
 static char *log_name = NULL;
+static bool syslog_is_open = false;
+#ifdef HAVE_SYSLOG
+static int syslog_logopt = LOG_PID|LOG_CONS;
+#endif
+
+static void
+_openlog(bool reopen)
+{
+#ifdef HAVE_SYSLOG
+       if (!reopen) {
+               if (syslog_is_open)
+                       return;
+       } else {
+               if (!syslog_is_open)
+                       closelog();
+       }
+       openlog(log_name, syslog_logopt, LOG_MAIL);
+       syslog_is_open = true;
+#endif
+}
 
 void log_set_name(const char* name)
 {
        free(log_name);
        log_name = xstrdup(name);
+
+       _openlog(true);
+}
+
+void
+log_activate_stderr(void)
+{
+#ifdef HAVE_SYSLOG
+       syslog_logopt |= LOG_PERROR;
+#endif
 }
 
 void
@@ -59,6 +90,8 @@ log_set_namef(const char *fmt, ...)
 
        free(log_name);
        log_name = buf;
+
+       _openlog(true);
 }
 
 void log_free_name()
@@ -69,24 +102,25 @@ void log_free_name()
 void log_error(const char *file, int line, const char *errstr,
        const char *fmt, ...)
 {
-       static int syslog_is_open = 0;
-       char buf[1024];
+       char *buf;
        va_list ap;
+       int i;
 
        va_start(ap, fmt);
-       vsnprintf(buf, sizeof(buf), fmt, ap);
+       i = vasprintf(&buf, fmt, ap);
        va_end(ap);
 
+       if (i < 0 || buf == NULL)
+               abort();
+
        if (!log_name) log_name = "mlmmj-UNKNOWN";
 
 #ifdef HAVE_SYSLOG
-       if(!syslog_is_open) {
-               openlog(log_name, LOG_PID|LOG_CONS, LOG_MAIL);
-               syslog_is_open = 1;
-       }
+       _openlog(true);
        syslog(LOG_ERR, "%s:%d: %s: %s", file, line, buf, errstr);
 #else
        fprintf(stderr, "%s[%d]: %s:%d: %s: %s\n", log_name, (int)getpid(),
                        file, line, buf, errstr);
 #endif
+       free(buf);
 }
index 4382d1f67f31d200b06b1dca8433d61d72366739..baf4a1da37bcc67c2376e84f9ef2c549a0efeefe 100644 (file)
@@ -842,6 +842,9 @@ int main(int argc, char **argv)
                daemonize = false;
        }
 
+       if (!daemonize)
+               log_activate_stderr();
+
        while(1) {
                if(listdir) {
                        log_set_namef("%s(%s)", argv[0], listdir);
index 7d8d4007908b50f2151056f296d79e570597896b..85baa0a1ba29cd72a504fe5d06f7146bf0d34712 100644 (file)
@@ -18,10 +18,10 @@ bla
 EOF
        atf_check -o inline:"bla\n" cat list/queue/discarded/*
        # the discarded file is too new, do not drop it yet
-       atf_check -s exit:0 $mlmmjmaint -F -L ${here}/list
+       atf_check -s exit:0 -e ignore $mlmmjmaint -F -L ${here}/list
        atf_check -o inline:"bla\n" cat list/queue/discarded/*
        atf_check touch -m -t 197001010101 list/queue/discarded/*
-       atf_check -s exit:0 $mlmmjmaint -F -L ${here}/list
+       atf_check -s exit:0 -e ignore $mlmmjmaint -F -L ${here}/list
        atf_check -o inline:"list/queue/discarded\n" find list/queue/discarded  -type d -empty
 
        #now with relative path
@@ -29,9 +29,9 @@ EOF
 bla
 EOF
        atf_check -o inline:"bla\n" cat list/queue/discarded/*
-       atf_check -s exit:0 $mlmmjmaint -F -L list
+       atf_check -s exit:0 -e ignore $mlmmjmaint -F -L list
        atf_check -o inline:"bla\n" cat list/queue/discarded/*
        atf_check touch -m -t 197001010101 list/queue/discarded/*
-       atf_check -s exit:0 $mlmmjmaint -F -L list
+       atf_check -s exit:0 -e ignore $mlmmjmaint -F -L list
        atf_check -o inline:"list/queue/discarded\n" find list/queue/discarded  -type d -empty
 }
index 90f4dc21e6d053b966b55e33fdd1429ad059b0c0..4949036702f4176d41dfa97a24c8d8a7fa89d1e1 100755 (executable)
@@ -25,7 +25,7 @@ nolongerbouncing_body()
        echo "$now" > ${f2}-probe
        touch ${f2}
        touch ${f2}.lastmsg
-       atf_check -s exit:0 -o empty -e empty $mlmmjmaintd -L lists/ml -F
+       atf_check -s exit:0 -o empty -e ignore $mlmmjmaintd -L lists/ml -F
        atf_check -s exit:1 -o empty -e empty test -f $f1
        atf_check -s exit:1 -o empty -e empty test -f $f1-probe
        atf_check -s exit:1 -o empty -e empty test -f $f1.lastmsg
@@ -46,7 +46,7 @@ discarded_body()
        f2=lists/ml/queue/discarded/notsoold
        atf_check touch -t "197001020000"  $f1
        atf_check touch $f2
-       atf_check -s exit:0 -o empty -e empty $mlmmjmaintd -L lists/ml -F
+       atf_check -s exit:0 -o empty -e ignore $mlmmjmaintd -L lists/ml -F
        atf_check -s exit:1 -o empty -e empty test -f $f1
        atf_check -s exit:0 -o empty -e empty test -f $f2
 }
@@ -72,7 +72,7 @@ basics_body()
        atf_check -s exit:0 -o "inline:mlmmj-maintd version 1.3.0\n" $mlmmjmaintd -V
        atf_check -s exit:0 -o "inline:$helptxt" $mlmmjmaintd -h
        echo test@mlmmjtest > lists/ml/control/listaddress
-       atf_check -s exit:0 -o empty -e empty $mlmmjmaintd -L lists/ml -F
+       atf_check -s exit:0 -o empty -e ignore $mlmmjmaintd -L lists/ml -F
        output="Starting maintenance run at
 
 clean_moderation