1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
|
From 989841a0c4e83b0ac1b9d276be3797bcfa83bcb1 Mon Sep 17 00:00:00 2001
From: Phil Sutter <phil@nwl.cc>
Date: Sun, 27 Nov 2022 23:45:41 +0100
Subject: [PATCH] log: Try syslog if no log path was given
Signed-off-by: Phil Sutter <phil@nwl.cc>
---
log.c | 44 ++++++++++++++++++++++++++++++++++++++++++--
minidlna.c | 2 --
2 files changed, 42 insertions(+), 4 deletions(-)
diff --git a/log.c b/log.c
index a989904a24d93..840a8c02e678b 100644
--- a/log.c
+++ b/log.c
@@ -23,6 +23,7 @@
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
+#include <syslog.h>
#include <time.h>
#include "upnpglobalvars.h"
@@ -147,6 +148,40 @@ log_init(const char *debug)
return 0;
}
+static int
+syslog_err(int level, enum _log_facility facility,
+ char *fname, int lineno, char *fmt, va_list *ap)
+{
+ char myfmt[1024];
+ int level_to_syslog[E_MAXDEBUG + 1] = {
+ /* [E_OFF] = 0, */
+ [E_FATAL] = LOG_CRIT,
+ [E_ERROR] = LOG_ERR,
+ [E_WARN] = LOG_WARNING,
+ [E_INFO] = LOG_NOTICE,
+ [E_DEBUG] = LOG_INFO,
+ [E_MAXDEBUG] = LOG_DEBUG,
+ };
+
+ if (level == E_OFF)
+ return 0;
+
+ if (level > E_MAXDEBUG)
+ return -1;
+
+ if (level)
+ snprintf(myfmt, 1024, "%s:%d: %s: %s", fname, lineno, level_name[level], fmt);
+ else
+ snprintf(myfmt, 1024, "%s:%d: %s", fname, lineno, fmt);
+
+ vsyslog(level_to_syslog[level], myfmt, *ap);
+
+ if (level == E_FATAL)
+ exit(-1);
+
+ return 0;
+}
+
void
log_err(int level, enum _log_facility facility, char *fname, int lineno, char *fmt, ...)
{
@@ -155,8 +190,14 @@ log_err(int level, enum _log_facility facility, char *fname, int lineno, char *f
if (level && level>log_level[facility] && level>E_FATAL)
return;
- if (!log_fp)
+ va_start(ap, fmt);
+ if (!log_fp) {
+ if (!syslog_err(level, facility, fname, lineno, fmt, &ap)) {
+ va_end(ap);
+ return;
+ }
log_fp = stdout;
+ }
// timestamp
if (!GETFLAG(SYSTEMD_MASK))
@@ -176,7 +217,6 @@ log_err(int level, enum _log_facility facility, char *fname, int lineno, char *f
fprintf(log_fp, "%s:%d: ", fname, lineno);
// user log
- va_start(ap, fmt);
if (vfprintf(log_fp, fmt, ap) == -1)
{
va_end(ap);
diff --git a/minidlna.c b/minidlna.c
index 999adee977353..db29d603d1e28 100644
--- a/minidlna.c
+++ b/minidlna.c
@@ -817,8 +817,6 @@ init(int argc, char **argv)
optionsfile);
}
}
- if (!log_path[0])
- strncpyt(log_path, DEFAULT_LOG_PATH, sizeof(log_path));
if (!db_path[0])
strncpyt(db_path, DEFAULT_DB_PATH, sizeof(db_path));
--
2.38.1
|