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
29 static FILE *log_fp
= NULL
;
30 static int default_log_level
= E_WARN
;
33 char *facility_name
[] = {
46 char *level_name
[] = {
57 log_init(const char *fname
, const char *debug
)
61 short int log_level_set
[L_MAX
];
68 memset(&log_level_set
, 0, sizeof(log_level_set
));
69 rhs
= lhs
= (char*) debug
;
70 while (rhs
&& (rhs
= 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
)))
79 if (!(level_name
[level
])) {
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
)))
91 if ((facility_name
[facility
])) {
92 log_level
[facility
] = level
;
93 log_level_set
[facility
] = 1;
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
;
107 for (i
=0; i
<L_MAX
; i
++)
108 log_level
[i
] = default_log_level
;
111 if (!fname
) // use default i.e. stdout
114 if (!(fp
= fopen(fname
, "a")))
121 log_err(int level
, enum _log_facility facility
, char *fname
, int lineno
, char *fmt
, ...)
129 if (level
&& level
>log_level
[facility
] && level
>E_FATAL
)
137 //vsnprintf(errbuf, sizeof(errbuf), fmt, ap);
138 vasprintf(&errbuf
, fmt
, ap
);
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
);
149 fprintf(log_fp
, "%s:%d: %s: %s", fname
, lineno
, level_name
[level
], errbuf
);
151 fprintf(log_fp
, "%s:%d: %s", fname
, lineno
, errbuf
);