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 const char *rhs, *lhs, *nlhs, *p; 66 int n; 67 int level, facility; 68 memset(&log_level_set, 0, sizeof(log_level_set)); 69 rhs = nlhs = 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 lhs = nlhs; 79 rhs = nlhs = p; 80 if (!(level_name[level])) { 81 // unknown level 82 continue; 83 } 84 do { 85 if (*lhs==',') lhs++; 86 p = strpbrk(lhs, ",="); 87 n = p ? p - lhs : strlen(lhs); 88 for (facility=0; facility_name[facility]; facility++) { 89 if (!(strncasecmp(facility_name[facility], lhs, n))) 90 break; 91 } 92 if ((facility_name[facility])) { 93 log_level[facility] = level; 94 log_level_set[facility] = 1; 95 } 96 lhs = p; 97 } while (*lhs && *lhs==','); 98 } 99 for (i=0; i<L_MAX; i++) 100 { 101 if( !log_level_set[i] ) 102 { 103 log_level[i] = default_log_level; 104 } 105 } 106 } 107 else { 108 for (i=0; i<L_MAX; i++) 109 log_level[i] = default_log_level; 110 } 111 112 if (!fname) // use default i.e. stdout 113 return 0; 114 115 if (!(fp = fopen(fname, "a"))) 116 return 1; 117 log_fp = fp; 118 return 0; 119} 120 121void 122log_err(int level, enum _log_facility facility, char *fname, int lineno, char *fmt, ...) 123{ 124 //char errbuf[1024]; 125 char * errbuf; 126 va_list ap; 127 time_t t; 128 struct tm *tm; 129 130 if (level && level>log_level[facility] && level>E_FATAL) 131 return; 132 133 if (!log_fp) 134 log_fp = stdout; 135 136 // user log 137 va_start(ap, fmt); 138 //vsnprintf(errbuf, sizeof(errbuf), fmt, ap); 139 if (vasprintf(&errbuf, fmt, ap) == -1) 140 { 141 va_end(ap); 142 return; 143 } 144 va_end(ap); 145 146 // timestamp 147 t = time(NULL); 148 tm = localtime(&t); 149 fprintf(log_fp, "[%04d/%02d/%02d %02d:%02d:%02d] ", 150 tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday, 151 tm->tm_hour, tm->tm_min, tm->tm_sec); 152 153 if (level) 154 fprintf(log_fp, "%s:%d: %s: %s", fname, lineno, level_name[level], errbuf); 155 else 156 fprintf(log_fp, "%s:%d: %s", fname, lineno, errbuf); 157 fflush(log_fp); 158 free(errbuf); 159 160 if (level==E_FATAL) 161 exit(-1); 162 163 return; 164} 165