MiniDLNA cvs 2010-11-11
[tomato.git] / release / src / router / minidlna / log.c
blobfb9f75f51bb691d66d8c5bf1a182694ef942b21e
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
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <stdarg.h>
24 #include <string.h>
25 #include <time.h>
27 #include "log.h"
29 static FILE *log_fp = NULL;
30 static int default_log_level = E_WARN;
31 int log_level[L_MAX];
33 char *facility_name[] = {
34 "general",
35 "artwork",
36 "database",
37 "inotify",
38 "scanner",
39 "metadata",
40 "http",
41 "ssdp",
42 "tivo",
46 char *level_name[] = {
47 "off", // E_OFF
48 "fatal", // E_FATAL
49 "error", // E_ERROR
50 "warn", // E_WARN
51 "info", // E_INFO
52 "debug", // E_DEBUG
56 int
57 log_init(const char *fname, const char *debug)
59 int i;
60 FILE *fp;
61 short int log_level_set[L_MAX];
63 if (debug)
65 char *rhs, *lhs, *p;
66 int n;
67 int level, facility;
68 memset(&log_level_set, 0, sizeof(log_level_set));
69 rhs = lhs = (char*) debug;
70 while (rhs && (rhs = strchr(rhs, '='))) {
71 rhs++;
72 p = strchr(rhs, ',');
73 n = p ? p - rhs : strlen(rhs);
74 for (level=0; level_name[level]; level++) {
75 if (!(strncasecmp(level_name[level], rhs, n)))
76 break;
78 rhs = p;
79 if (!(level_name[level])) {
80 // unknown level
81 continue;
83 do {
84 if (*lhs==',') lhs++;
85 p = strpbrk(lhs, ",=");
86 n = p ? p - lhs : strlen(lhs);
87 for (facility=0; facility_name[facility]; facility++) {
88 if (!(strncasecmp(facility_name[facility], lhs, n)))
89 break;
91 if ((facility_name[facility])) {
92 log_level[facility] = level;
93 log_level_set[facility] = 1;
95 lhs = p;
96 } while (*lhs && *lhs==',');
98 for (i=0; i<L_MAX; i++)
100 if( !log_level_set[i] )
102 log_level[i] = default_log_level;
106 else {
107 for (i=0; i<L_MAX; i++)
108 log_level[i] = default_log_level;
111 if (!fname) // use default i.e. stdout
112 return 0;
114 if (!(fp = fopen(fname, "a")))
115 return 1;
116 log_fp = fp;
117 return 0;
120 void
121 log_err(int level, enum _log_facility facility, char *fname, int lineno, char *fmt, ...)
123 //char errbuf[1024];
124 char * errbuf;
125 va_list ap;
126 time_t t;
127 struct tm *tm;
129 if (level && level>log_level[facility] && level>E_FATAL)
130 return;
132 if (!log_fp)
133 log_fp = stdout;
135 // user log
136 va_start(ap, fmt);
137 //vsnprintf(errbuf, sizeof(errbuf), fmt, ap);
138 vasprintf(&errbuf, fmt, ap);
139 va_end(ap);
141 // timestamp
142 t = time(NULL);
143 tm = localtime(&t);
144 fprintf(log_fp, "[%04d/%02d/%02d %02d:%02d:%02d] ",
145 tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday,
146 tm->tm_hour, tm->tm_min, tm->tm_sec);
148 if (level)
149 fprintf(log_fp, "%s:%d: %s: %s", fname, lineno, level_name[level], errbuf);
150 else
151 fprintf(log_fp, "%s:%d: %s", fname, lineno, errbuf);
152 fflush(log_fp);
153 free(errbuf);
155 if (level==E_FATAL)
156 exit(-1);
158 return;