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.
35 static FILE *log_fp
= NULL
;
37 static int default_log_level
= E_WARN
;
40 char *facility_name
[] = {
53 char *level_name
[] = {
68 LOG_WARNING
, // E_WARN
76 log_init(const char *fname
, const char *debug
)
82 short int log_level_set
[L_MAX
];
86 const char *rhs
, *lhs
, *nlhs
, *p
;
89 memset(&log_level_set
, 0, sizeof(log_level_set
));
91 while (rhs
&& (rhs
= 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
)))
101 if (!(level_name
[level
])) {
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
)))
113 if ((facility_name
[facility
])) {
114 log_level
[facility
] = level
;
115 log_level_set
[facility
] = 1;
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
;
129 for (i
=0; i
<L_MAX
; i
++)
130 log_level
[i
] = default_log_level
;
136 openlog_option
= LOG_PID
| LOG_CONS
;
138 openlog_option
|= LOG_PERROR
; /* also log on stderr */
139 openlog("minidlna", openlog_option
, LOG_DAEMON
);
142 /* speed things up and ignore LOG_INFO and LOG_DEBUG */
143 setlogmask(LOG_UPTO(LOG_NOTICE
));
146 if (!fname
) // use default i.e. stdout
149 if (!(fp
= fopen(fname
, "a")))
157 log_err(int level
, enum _log_facility facility
, char *fname
, int lineno
, char *fmt
, ...)
167 if (level
&& level
>log_level
[facility
] && level
>E_FATAL
)
177 //vsnprintf(errbuf, sizeof(errbuf), fmt, ap);
178 if (vasprintf(&errbuf
, fmt
, ap
) == -1)
186 if (log_level
[facility
] < E_DEBUG
)
187 syslog(level_prio
[level
], errbuf
);
189 syslog(level_prio
[level
], "%s:%d: %s", fname
, lineno
, errbuf
);
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
);
199 fprintf(log_fp
, "%s:%d: %s: %s", fname
, lineno
, level_name
[level
], errbuf
);
201 fprintf(log_fp
, "%s:%d: %s", fname
, lineno
, errbuf
);