Introduce new logger

This commit is contained in:
Julien BLACHE 2009-05-08 17:04:25 +02:00
parent 2127aff906
commit 3a2a218a0f
3 changed files with 260 additions and 0 deletions

View File

@ -13,6 +13,7 @@ mt_daapd_CPPFLAGS = -D_GNU_SOURCE @AVAHI_CFLAGS@ @SQLITE3_CFLAGS@ @FFMPEG_CFLAGS
mt_daapd_LDADD = @AVAHI_LIBS@ @SQLITE3_LIBS@ @FFMPEG_LIBS@ @CONFUSE_LIBS@ @FLAC_LIBS@ @TAGLIB_LIBS@ @LIBEVENT_LIBS@ @LIBAVL_LIBS@ @MINIXML_LIBS@
mt_daapd_SOURCES = main.c \
err.c err.h \
logger.c logger.h \
conffile.c conffile.h \
filescanner.c filescanner.h \
filescanner_ffmpeg.c filescanner_urlfile.c filescanner_m3u.c \

205
src/logger.c Normal file
View File

@ -0,0 +1,205 @@
/*
* Copyright (C) 2009 Julien BLACHE <jb@jblache.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <time.h>
#include <errno.h>
#include <pthread.h>
#include "logger.h"
static pthread_mutex_t logger_lck = PTHREAD_MUTEX_INITIALIZER;
static int logdomains;
static int threshold;
static int console;
static char *logfilename;
static FILE *logfile;
static char *labels[] = { "config", "daap", "db", "httpd", "main", "mdns", "misc", "parse", "rsp", "scan", "xcode", "lock" };
static int
set_logdomains(char *domains)
{
char *ptr;
char *d;
int i;
logdomains = 0;
while ((d = strtok_r(domains, " ,", &ptr)))
{
domains = NULL;
for (i = 0; i < N_LOGDOMAINS; i++)
{
if (strcmp(d, labels[i]) == 0)
{
logdomains |= (1 << i);
break;
}
}
if (i == N_LOGDOMAINS)
{
fprintf(stderr, "Error: unknown log domain '%s'\n", d);
return -1;
}
}
return 0;
}
void
DPRINTF(int severity, int domain, char *fmt, ...)
{
va_list ap;
char stamp[32];
time_t t;
int ret;
if (!((1 << domain) & logdomains) || (severity > threshold))
return;
pthread_mutex_lock(&logger_lck);
if (!logfile && !console)
{
pthread_mutex_unlock(&logger_lck);
return;
}
if (logfile)
{
t = time(NULL);
ret = strftime(stamp, sizeof(stamp), "%Y-%m-%d %H:%M:%S", localtime(&t));
if (ret == 0)
stamp[0] = '\0';
fprintf(logfile, "[%s] %6s: ", stamp, labels[domain]);
va_start(ap, fmt);
vfprintf(logfile, fmt, ap);
va_end(ap);
}
if (console)
{
fprintf(stderr, "%6s: ", labels[domain]);
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
}
pthread_mutex_unlock(&logger_lck);
}
void
logger_reinit(void)
{
FILE *fp;
pthread_mutex_lock(&logger_lck);
if (logfile)
{
fclose(logfile);
fp = fopen(logfilename, "a");
if (!fp)
{
fprintf(logfile, "WARNING: Could not reopen logfile, logging will stop now\n");
}
fclose(logfile);
logfile = fp;
}
pthread_mutex_unlock(&logger_lck);
}
/* The functions below are used at init time with a single thread running */
void
logger_domains(void)
{
int i;
fprintf(stdout, "%s", labels[0]);
for (i = 1; i < N_LOGDOMAINS; i++)
fprintf(stdout, ", %s", labels[i]);
fprintf(stdout, "\n");
}
void
logger_detach(void)
{
console = 0;
}
int
logger_init(char *file, char *domains, int severity)
{
int ret;
if ((sizeof(labels) / sizeof(labels[0])) != N_LOGDOMAINS)
{
fprintf(stderr, "WARNING: log domains do not match\n");
return -1;
}
console = 1;
threshold = severity;
if (domains)
{
ret = set_logdomains(domains);
if (ret < 0)
return ret;
}
else
logdomains = ~0;
if (!file)
return 0;
logfile = fopen(file, "a");
if (!logfile)
{
fprintf(stderr, "Could not open logfile %s: %s\n", file, strerror(errno));
return -1;
}
logfilename = file;
return 0;
}
void
logger_deinit(void)
{
if (logfile)
fclose(logfile);
}

54
src/logger.h Normal file
View File

@ -0,0 +1,54 @@
#ifndef __LOGGER_H__
#define __LOGGER_H__
/* Log domains */
#define L_CONF 0
#define L_DAAP 1
#define L_DB 2
#define L_HTTPD 3
#define L_MAIN 4
#define L_MDNS 5
#define L_MISC 6
#define L_PARSE 7
#define L_RSP 8
#define L_SCAN 9
#define L_XCODE 10
/* Will go away */
#define L_REND L_MDNS
#define L_LOCK 11
#define N_LOGDOMAINS 12
/* Severities */
#define E_FATAL 0
#define E_LOG 1
#define E_WARN 2
#define E_INFO 3
#define E_DBG 4
#define E_SPAM 5
/* Will go away */
#define E_INF E_INFO
void
DPRINTF(int severity, int domain, char *fmt, ...);
void
logger_reinit(void);
void
logger_domains(void);
void
logger_detach(void);
int
logger_init(char *file, char *domains, int severity);
void
logger_deinit(void);
#endif /* !__LOGGER_H__ */