source: titan/minidlna-1.0.22/log.c @ 13567

Last change on this file since 13567 was 13567, checked in by obi, 11 years ago

[titan] add minidlna-1.0.22 first step

File size: 3.4 KB
Line 
1/* MiniDLNA media server
2 * Copyright (C) 2008-2010 NETGEAR, Inc. All Rights Reserved.
3 *
4 * This file is part of MiniDLNA.
5 *
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.
10 *
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.
15 *
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.
19 */
20
21#include <stdlib.h>
22#include <stdio.h>
23#include <stdarg.h>
24#include <string.h>
25#include <time.h>
26
27#include "log.h"
28
29static FILE *log_fp = NULL;
30static int default_log_level = E_WARN;
31int log_level[L_MAX];
32
33char *facility_name[] = {
34        "general",
35        "artwork",
36        "database",
37        "inotify",
38        "scanner",
39        "metadata",
40        "http",
41        "ssdp",
42        "tivo",
43        0
44};
45
46char *level_name[] = {
47        "off",                                  // E_OFF
48        "fatal",                                // E_FATAL
49        "error",                                // E_ERROR
50        "warn",                                 // E_WARN
51        "info",                                 // E_INFO
52        "debug",                                // E_DEBUG
53        0
54};
55
56int
57log_init(const char *fname, const char *debug)
58{
59        int i;
60        FILE *fp;
61        short int log_level_set[L_MAX];
62
63        if (debug)
64        {
65                char *rhs, *lhs, *p;
66                int n;
67                int level, facility;
68                memset(&log_level_set, 0, sizeof(log_level_set));
69                rhs = lhs = (char*) debug;
70                while (rhs && (rhs = strchr(rhs, '='))) {
71                        rhs++;
72                        p = 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)))
76                                        break;
77                        }
78                        rhs = p;
79                        if (!(level_name[level])) {
80                                // unknown level
81                                continue;
82                        }
83                        do {
84                                if (*lhs==',') lhs++;
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)))
89                                                break;
90                                }
91                                if ((facility_name[facility])) {
92                                        log_level[facility] = level;
93                                        log_level_set[facility] = 1;
94                                }
95                                lhs = p;
96                        } while (*lhs && *lhs==',');
97                }
98                for (i=0; i<L_MAX; i++)
99                {
100                        if( !log_level_set[i] )
101                        {
102                                log_level[i] = default_log_level;
103                        }
104                }
105        }
106        else {
107                for (i=0; i<L_MAX; i++)
108                        log_level[i] = default_log_level;
109        }
110
111        if (!fname)                                     // use default i.e. stdout
112                return 0;
113
114        if (!(fp = fopen(fname, "a")))
115                return 1;
116        log_fp = fp;
117        return 0;
118}
119
120void
121log_err(int level, enum _log_facility facility, char *fname, int lineno, char *fmt, ...)
122{
123        //char errbuf[1024];
124        char * errbuf;
125        va_list ap;
126        time_t t;
127        struct tm *tm;
128
129        if (level && level>log_level[facility] && level>E_FATAL)
130                return;
131
132        if (!log_fp)
133                log_fp = stdout;
134
135        // user log
136        va_start(ap, fmt);
137        //vsnprintf(errbuf, sizeof(errbuf), fmt, ap);
138        if (vasprintf(&errbuf, fmt, ap) == -1)
139        {
140                va_end(ap);
141                return;
142        }
143        va_end(ap);
144
145        // timestamp
146        t = time(NULL);
147        tm = localtime(&t);
148        fprintf(log_fp, "[%04d/%02d/%02d %02d:%02d:%02d] ",
149                tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday,
150                tm->tm_hour, tm->tm_min, tm->tm_sec);
151
152        if (level)
153                fprintf(log_fp, "%s:%d: %s: %s", fname, lineno, level_name[level], errbuf);
154        else
155                fprintf(log_fp, "%s:%d: %s", fname, lineno, errbuf);
156        fflush(log_fp);
157        free(errbuf);
158
159        if (level==E_FATAL)
160                exit(-1);
161
162        return;
163}
Note: See TracBrowser for help on using the repository browser.