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
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
];
89 memset(&log_level_set
, 0, sizeof(log_level_set
));
90 rhs
= lhs
= (char*) debug
;
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
)))
100 if (!(level_name
[level
])) {
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
)))
112 if ((facility_name
[facility
])) {
113 log_level
[facility
] = level
;
114 log_level_set
[facility
] = 1;
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
;
128 for (i
=0; i
<L_MAX
; i
++)
129 log_level
[i
] = default_log_level
;
135 openlog_option
= LOG_PID
| LOG_CONS
;
137 openlog_option
|= LOG_PERROR
; /* also log on stderr */
138 openlog("minidlna", openlog_option
, LOG_DAEMON
);
141 /* speed things up and ignore LOG_INFO and LOG_DEBUG */
142 setlogmask(LOG_UPTO(LOG_NOTICE
));
145 if (!fname
) // use default i.e. stdout
148 if (!(fp
= fopen(fname
, "a")))
156 log_err(int level
, enum _log_facility facility
, char *fname
, int lineno
, char *fmt
, ...)
166 if (level
&& level
>log_level
[facility
] && level
>E_FATAL
)
176 //vsnprintf(errbuf, sizeof(errbuf), fmt, ap);
177 if (vasprintf(&errbuf
, fmt
, ap
) == -1)
185 if (log_level
[facility
] < E_DEBUG
)
186 syslog(level_prio
[level
], errbuf
);
188 syslog(level_prio
[level
], "%s:%d: %s", fname
, lineno
, errbuf
);
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
);
198 fprintf(log_fp
, "%s:%d: %s: %s", fname
, lineno
, level_name
[level
], errbuf
);
200 fprintf(log_fp
, "%s:%d: %s", fname
, lineno
, errbuf
);