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.
28 #include "upnpglobalvars.h"
31 static FILE *log_fp
= NULL
;
32 static const int _default_log_level
= E_WARN
;
35 const char *facility_name
[] = {
48 const char *level_name
[] = {
55 "maxdebug", // E_MAXDEBUG
69 if (log_path
[0] && log_fp
)
72 snprintf(logfile
, sizeof(logfile
), "%s/" LOGFILE_NAME
, log_path
);
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
[])
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
))
97 log_init(const char *debug
)
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
;
110 const char *rhs
, *lhs
, *nlhs
;
114 while (rhs
&& (rhs
= strchr(rhs
, '='))) {
116 level
= find_matching_name(rhs
, level_name
);
118 DPRINTF(E_WARN
, L_GENERAL
, "unknown level in debug string: %s", debug
);
123 rhs
= nlhs
= strchr(rhs
, ',');
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
);
130 log_level
[facility
] = level
;
133 lhs
= strpbrk(lhs
, ",=");
134 } while (*lhs
&& *lhs
==',');
141 snprintf(logfile
, sizeof(logfile
), "%s/" LOGFILE_NAME
, log_path
);
142 if (!(fp
= fopen(logfile
, "a")))
151 log_err(int level
, enum _log_facility facility
, char *fname
, int lineno
, char *fmt
, ...)
155 if (level
&& level
>log_level
[facility
] && level
>E_FATAL
)
162 if (!GETFLAG(SYSTEMD_MASK
))
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
);
174 fprintf(log_fp
, "%s:%d: %s: ", fname
, lineno
, level_name
[level
]);
176 fprintf(log_fp
, "%s:%d: ", fname
, lineno
);
180 if (vfprintf(log_fp
, fmt
, ap
) == -1)