From: Baptiste Daroussin Date: Wed, 26 Oct 2022 13:36:24 +0000 (+0200) Subject: log_error: rework syslog X-Git-Tag: RELEASE_1_4_0a1~38 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=df6a5779c3cc9affbbe27b042eee5b020d0f5ada;p=thirdparty%2Fmlmmj.git log_error: rework syslog - 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 --- diff --git a/include/log_error.h b/include/log_error.h index 9d6f414b..95270310 100644 --- a/include/log_error.h +++ b/include/log_error.h @@ -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, ...); diff --git a/src/log_error.c b/src/log_error.c index a8a02a94..c8846fe0 100644 --- a/src/log_error.c +++ b/src/log_error.c @@ -1,6 +1,6 @@ -/* Copyright (C) 2004 Morten K. Poulsen - * - * $Id$ +/* + * Copyright (C) 2004 Morten K. Poulsen + * Copyright (C) 2022 Baptiste Daroussin * * 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 #include #include #include @@ -37,11 +38,41 @@ #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); } diff --git a/src/mlmmj-maintd.c b/src/mlmmj-maintd.c index 4382d1f6..baf4a1da 100644 --- a/src/mlmmj-maintd.c +++ b/src/mlmmj-maintd.c @@ -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); diff --git a/tests/functional-tests.sh b/tests/functional-tests.sh index 7d8d4007..85baa0a1 100644 --- a/tests/functional-tests.sh +++ b/tests/functional-tests.sh @@ -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 } diff --git a/tests/mlmmj-maintd.sh b/tests/mlmmj-maintd.sh index 90f4dc21..49490367 100755 --- a/tests/mlmmj-maintd.sh +++ b/tests/mlmmj-maintd.sh @@ -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