Wrap up version 1.3.3.
[minidlna.git] / log.c
bloba989904a24d938a84e249c1f85431797aff8096c
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.
20 #include "config.h"
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <stdarg.h>
25 #include <string.h>
26 #include <time.h>
28 #include "upnpglobalvars.h"
29 #include "log.h"
31 static FILE *log_fp = NULL;
32 static const int _default_log_level = E_WARN;
33 int log_level[L_MAX];
35 const char *facility_name[] = {
36 "general",
37 "artwork",
38 "database",
39 "inotify",
40 "scanner",
41 "metadata",
42 "http",
43 "ssdp",
44 "tivo",
48 const char *level_name[] = {
49 "off", // E_OFF
50 "fatal", // E_FATAL
51 "error", // E_ERROR
52 "warn", // E_WARN
53 "info", // E_INFO
54 "debug", // E_DEBUG
55 "maxdebug", // E_MAXDEBUG
59 void
60 log_close(void)
62 if (log_fp)
63 fclose(log_fp);
66 void
67 log_reopen(void)
69 if (log_path[0] && log_fp)
71 char logfile[1048];
72 snprintf(logfile, sizeof(logfile), "%s/" LOGFILE_NAME, log_path);
73 fclose(log_fp);
74 log_fp = fopen(logfile, "a");
75 DPRINTF(E_INFO, L_GENERAL, "Reopened log file\n");
79 int find_matching_name(const char* str, const char* names[])
81 const char *start;
82 int level, c;
84 if (!str)
85 return -1;
87 start = strpbrk(str, ",=");
88 c = start ? start - str : strlen(str);
89 for (level = 0; names[level] != 0; level++) {
90 if (!strncasecmp(names[level], str, c))
91 return level;
93 return -1;
96 int
97 log_init(const char *debug)
99 int i;
100 FILE *fp = NULL;
102 int level = find_matching_name(debug, level_name);
103 int default_log_level = (level == -1) ? _default_log_level : level;
105 for (i = 0; i < L_MAX; i++)
106 log_level[i] = default_log_level;
108 if (debug)
110 const char *rhs, *lhs, *nlhs;
111 int level, facility;
113 rhs = nlhs = debug;
114 while (rhs && (rhs = strchr(rhs, '='))) {
115 rhs++;
116 level = find_matching_name(rhs, level_name);
117 if (level == -1) {
118 DPRINTF(E_WARN, L_GENERAL, "unknown level in debug string: %s", debug);
119 continue;
122 lhs = nlhs;
123 rhs = nlhs = strchr(rhs, ',');
124 do {
125 if (*lhs==',') lhs++;
126 facility = find_matching_name(lhs, facility_name);
127 if (facility == -1) {
128 DPRINTF(E_WARN, L_GENERAL, "unknown debug facility in debug string: %s", debug);
129 } else {
130 log_level[facility] = level;
133 lhs = strpbrk(lhs, ",=");
134 } while (*lhs && *lhs==',');
138 if (log_path[0])
140 char logfile[1048];
141 snprintf(logfile, sizeof(logfile), "%s/" LOGFILE_NAME, log_path);
142 if (!(fp = fopen(logfile, "a")))
143 return -1;
145 log_fp = fp;
147 return 0;
150 void
151 log_err(int level, enum _log_facility facility, char *fname, int lineno, char *fmt, ...)
153 va_list ap;
155 if (level && level>log_level[facility] && level>E_FATAL)
156 return;
158 if (!log_fp)
159 log_fp = stdout;
161 // timestamp
162 if (!GETFLAG(SYSTEMD_MASK))
164 time_t t;
165 struct tm *tm;
166 t = time(NULL);
167 tm = localtime(&t);
168 fprintf(log_fp, "[%04d/%02d/%02d %02d:%02d:%02d] ",
169 tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday,
170 tm->tm_hour, tm->tm_min, tm->tm_sec);
173 if (level)
174 fprintf(log_fp, "%s:%d: %s: ", fname, lineno, level_name[level]);
175 else
176 fprintf(log_fp, "%s:%d: ", fname, lineno);
178 // user log
179 va_start(ap, fmt);
180 if (vfprintf(log_fp, fmt, ap) == -1)
182 va_end(ap);
183 return;
185 va_end(ap);
187 fflush(log_fp);
189 if (level==E_FATAL)
190 exit(-1);
192 return;