minidlna support now Samsung TV C550/C650 (thx amir909)
[tomato.git] / release / src / router / minidlna / log.c
blobff767bf6aea5707d9e69746fa6095ba76342c7a9
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 char *rhs, *lhs, *p;
87 int n;
88 int level, facility;
89 memset(&log_level_set, 0, sizeof(log_level_set));
90 rhs = lhs = (char*) 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 rhs = p;
100 if (!(level_name[level])) {
101 // unknown level
102 continue;
104 do {
105 if (*lhs==',') lhs++;
106 p = strpbrk(lhs, ",=");
107 n = p ? p - lhs : strlen(lhs);
108 for (facility=0; facility_name[facility]; facility++) {
109 if (!(strncasecmp(facility_name[facility], lhs, n)))
110 break;
112 if ((facility_name[facility])) {
113 log_level[facility] = level;
114 log_level_set[facility] = 1;
116 lhs = p;
117 } while (*lhs && *lhs==',');
119 for (i=0; i<L_MAX; i++)
121 if( !log_level_set[i] )
123 log_level[i] = default_log_level;
127 else {
128 for (i=0; i<L_MAX; i++)
129 log_level[i] = default_log_level;
132 #ifdef SYSLOG
133 int openlog_option;
135 openlog_option = LOG_PID | LOG_CONS;
136 if (!fname)
137 openlog_option |= LOG_PERROR; /* also log on stderr */
138 openlog("minidlna", openlog_option, LOG_DAEMON);
140 if (fname) {
141 /* speed things up and ignore LOG_INFO and LOG_DEBUG */
142 setlogmask(LOG_UPTO(LOG_NOTICE));
144 #else
145 if (!fname) // use default i.e. stdout
146 return 0;
148 if (!(fp = fopen(fname, "a")))
149 return 1;
150 log_fp = fp;
151 #endif
152 return 0;
155 void
156 log_err(int level, enum _log_facility facility, char *fname, int lineno, char *fmt, ...)
158 //char errbuf[1024];
159 char * errbuf;
160 va_list ap;
161 #ifndef SYSLOG
162 time_t t;
163 struct tm *tm;
164 #endif
166 if (level && level>log_level[facility] && level>E_FATAL)
167 return;
169 #ifndef SYSLOG
170 if (!log_fp)
171 log_fp = stdout;
172 #endif
174 // user log
175 va_start(ap, fmt);
176 //vsnprintf(errbuf, sizeof(errbuf), fmt, ap);
177 if (vasprintf(&errbuf, fmt, ap) == -1)
179 va_end(ap);
180 return;
182 va_end(ap);
184 #ifdef SYSLOG
185 if (log_level[facility] < E_DEBUG)
186 syslog(level_prio[level], errbuf);
187 else
188 syslog(level_prio[level], "%s:%d: %s", fname, lineno, errbuf);
189 #else
190 // timestamp
191 t = time(NULL);
192 tm = localtime(&t);
193 fprintf(log_fp, "[%04d/%02d/%02d %02d:%02d:%02d] ",
194 tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday,
195 tm->tm_hour, tm->tm_min, tm->tm_sec);
197 if (level)
198 fprintf(log_fp, "%s:%d: %s: %s", fname, lineno, level_name[level], errbuf);
199 else
200 fprintf(log_fp, "%s:%d: %s", fname, lineno, errbuf);
201 fflush(log_fp);
202 #endif
203 free(errbuf);
205 if (level==E_FATAL)
206 exit(-1);
208 return;