Updates to Tomato RAF including NGINX && PHP
[tomato.git] / release / src / router / minidlna / log.c
blob731f29af4a92745fa4d5316810aa9dbbb69736df
1 /* MiniDLNA media server
2 * Copyright (C) 2008-2010 NETGEAR, Inc. All Rights Reserved.
4 * This file is part of MiniDLNA.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #define SYSLOG
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <stdarg.h>
26 #include <string.h>
27 #include <time.h>
28 #ifdef SYSLOG
29 #include <syslog.h>
30 #endif
32 #include "log.h"
34 #ifndef SYSLOG
35 static FILE *log_fp = NULL;
36 #endif
37 static int default_log_level = E_WARN;
38 int log_level[L_MAX];
40 char *facility_name[] = {
41 "general",
42 "artwork",
43 "database",
44 "inotify",
45 "scanner",
46 "metadata",
47 "http",
48 "ssdp",
49 "tivo",
53 char *level_name[] = {
54 "off", // E_OFF
55 "fatal", // E_FATAL
56 "error", // E_ERROR
57 "warn", // E_WARN
58 "info", // E_INFO
59 "debug", // E_DEBUG
63 #ifdef SYSLOG
64 int level_prio[] = {
65 LOG_NOTICE, // E_OFF
66 LOG_CRIT, // E_FATAL
67 LOG_ERR, // E_ERROR
68 LOG_WARNING, // E_WARN
69 LOG_INFO, // E_INFO
70 LOG_DEBUG, // E_DEBUG
73 #endif
75 int
76 log_init(const char *fname, const char *debug)
78 int i;
79 #ifndef SYSLOG
80 FILE *fp;
81 #endif
82 short int log_level_set[L_MAX];
84 if (debug)
86 const char *rhs, *lhs, *nlhs, *p;
87 int n;
88 int level, facility;
89 memset(&log_level_set, 0, sizeof(log_level_set));
90 rhs = nlhs = debug;
91 while (rhs && (rhs = strchr(rhs, '='))) {
92 rhs++;
93 p = strchr(rhs, ',');
94 n = p ? p - rhs : strlen(rhs);
95 for (level=0; level_name[level]; level++) {
96 if (!(strncasecmp(level_name[level], rhs, n)))
97 break;
99 lhs = nlhs;
100 rhs = nlhs = p;
101 if (!(level_name[level])) {
102 // unknown level
103 continue;
105 do {
106 if (*lhs==',') lhs++;
107 p = strpbrk(lhs, ",=");
108 n = p ? p - lhs : strlen(lhs);
109 for (facility=0; facility_name[facility]; facility++) {
110 if (!(strncasecmp(facility_name[facility], lhs, n)))
111 break;
113 if ((facility_name[facility])) {
114 log_level[facility] = level;
115 log_level_set[facility] = 1;
117 lhs = p;
118 } while (*lhs && *lhs==',');
120 for (i=0; i<L_MAX; i++)
122 if( !log_level_set[i] )
124 log_level[i] = default_log_level;
128 else {
129 for (i=0; i<L_MAX; i++)
130 log_level[i] = default_log_level;
133 #ifdef SYSLOG
134 int openlog_option;
136 openlog_option = LOG_PID | LOG_CONS;
137 if (!fname)
138 openlog_option |= LOG_PERROR; /* also log on stderr */
139 openlog("minidlna", openlog_option, LOG_DAEMON);
141 if (fname) {
142 /* speed things up and ignore LOG_INFO and LOG_DEBUG */
143 setlogmask(LOG_UPTO(LOG_NOTICE));
145 #else
146 if (!fname) // use default i.e. stdout
147 return 0;
149 if (!(fp = fopen(fname, "a")))
150 return 1;
151 log_fp = fp;
152 #endif
153 return 0;
156 void
157 log_err(int level, enum _log_facility facility, char *fname, int lineno, char *fmt, ...)
159 //char errbuf[1024];
160 char * errbuf;
161 va_list ap;
162 #ifndef SYSLOG
163 time_t t;
164 struct tm *tm;
165 #endif
167 if (level && level>log_level[facility] && level>E_FATAL)
168 return;
170 #ifndef SYSLOG
171 if (!log_fp)
172 log_fp = stdout;
173 #endif
175 // user log
176 va_start(ap, fmt);
177 //vsnprintf(errbuf, sizeof(errbuf), fmt, ap);
178 if (vasprintf(&errbuf, fmt, ap) == -1)
180 va_end(ap);
181 return;
183 va_end(ap);
185 #ifdef SYSLOG
186 if (log_level[facility] < E_DEBUG)
187 syslog(level_prio[level], errbuf);
188 else
189 syslog(level_prio[level], "%s:%d: %s", fname, lineno, errbuf);
190 #else
191 // timestamp
192 t = time(NULL);
193 tm = localtime(&t);
194 fprintf(log_fp, "[%04d/%02d/%02d %02d:%02d:%02d] ",
195 tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday,
196 tm->tm_hour, tm->tm_min, tm->tm_sec);
198 if (level)
199 fprintf(log_fp, "%s:%d: %s: %s", fname, lineno, level_name[level], errbuf);
200 else
201 fprintf(log_fp, "%s:%d: %s", fname, lineno, errbuf);
202 fflush(log_fp);
203 #endif
204 free(errbuf);
206 if (level==E_FATAL)
207 exit(-1);
209 return;