2009-05-08 11:04:25 -04:00
|
|
|
/*
|
2011-06-27 12:29:21 -04:00
|
|
|
* Copyright (C) 2009-2011 Julien BLACHE <jb@jblache.org>
|
2009-05-08 11:04:25 -04:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
2010-02-02 15:00:47 -05:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include <config.h>
|
|
|
|
#endif
|
|
|
|
|
2017-01-13 17:32:59 -05:00
|
|
|
#include "logger.h"
|
|
|
|
|
2009-05-08 11:04:25 -04:00
|
|
|
#include <stdio.h>
|
2010-07-14 07:06:32 -04:00
|
|
|
#include <unistd.h>
|
2009-05-08 11:04:25 -04:00
|
|
|
#include <stdarg.h>
|
2017-01-23 02:41:23 -05:00
|
|
|
#include <stdlib.h>
|
2009-05-08 11:04:25 -04:00
|
|
|
#include <string.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <errno.h>
|
2011-06-27 12:29:21 -04:00
|
|
|
#include <sys/stat.h>
|
2009-05-08 11:04:25 -04:00
|
|
|
|
2015-10-19 15:15:29 -04:00
|
|
|
#include <event2/event.h>
|
2009-05-08 12:17:01 -04:00
|
|
|
|
2010-03-07 04:45:15 -05:00
|
|
|
#include <libavutil/log.h>
|
|
|
|
|
2011-06-27 12:29:21 -04:00
|
|
|
#include "conffile.h"
|
2017-01-13 17:32:59 -05:00
|
|
|
#include "misc.h"
|
2009-05-08 11:04:25 -04:00
|
|
|
|
2017-01-21 10:11:20 -05:00
|
|
|
static pthread_mutex_t logger_lck;
|
|
|
|
static int logger_initialized;
|
2009-05-08 11:04:25 -04:00
|
|
|
static int logdomains;
|
|
|
|
static int threshold;
|
2017-01-21 10:11:20 -05:00
|
|
|
static int console = 1;
|
2009-05-08 11:04:25 -04:00
|
|
|
static char *logfilename;
|
|
|
|
static FILE *logfile;
|
2017-08-12 14:00:12 -04:00
|
|
|
static char *labels[] = { "config", "daap", "db", "httpd", "http", "main", "mdns", "misc", "rsp", "scan", "xcode", "event", "remote", "dacp", "ffmpeg", "artwork", "player", "raop", "laudio", "dmap", "dbperf", "spotify", "lastfm", "cache", "mpd", "stream", "cast", "fifo", "lib", "web" };
|
2015-01-07 15:50:24 -05:00
|
|
|
static char *severities[] = { "FATAL", "LOG", "WARN", "INFO", "DEBUG", "SPAM" };
|
2009-05-08 11:04:25 -04:00
|
|
|
|
2017-01-21 10:11:20 -05:00
|
|
|
/* We need our own check to avoid nested locking or recursive calls */
|
|
|
|
#define LOGGER_CHECK_ERR(f) \
|
|
|
|
do { int lerr; lerr = f; if (lerr != 0) { \
|
|
|
|
vlogger_fatal("%s failed at line %d, err %d (%s)\n", #f, __LINE__, \
|
|
|
|
lerr, strerror(lerr)); \
|
|
|
|
abort(); \
|
|
|
|
} } while(0)
|
2009-05-08 11:04:25 -04:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2010-05-03 12:19:40 -04:00
|
|
|
static void
|
2017-01-21 10:11:20 -05:00
|
|
|
vlogger_writer(int severity, int domain, const char *fmt, va_list args)
|
2009-05-08 11:04:25 -04:00
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
char stamp[32];
|
|
|
|
time_t t;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (logfile)
|
|
|
|
{
|
|
|
|
t = time(NULL);
|
|
|
|
ret = strftime(stamp, sizeof(stamp), "%Y-%m-%d %H:%M:%S", localtime(&t));
|
|
|
|
if (ret == 0)
|
|
|
|
stamp[0] = '\0';
|
|
|
|
|
2015-01-03 02:07:03 -05:00
|
|
|
fprintf(logfile, "[%s] [%5s] %8s: ", stamp, severities[severity], labels[domain]);
|
2009-05-08 11:04:25 -04:00
|
|
|
|
2009-06-01 10:01:47 -04:00
|
|
|
va_copy(ap, args);
|
2009-05-08 11:04:25 -04:00
|
|
|
vfprintf(logfile, fmt, ap);
|
|
|
|
va_end(ap);
|
2009-10-04 06:08:46 -04:00
|
|
|
|
|
|
|
fflush(logfile);
|
2009-05-08 11:04:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (console)
|
|
|
|
{
|
2015-01-03 02:07:03 -05:00
|
|
|
fprintf(stderr, "[%5s] %8s: ", severities[severity], labels[domain]);
|
2009-05-08 11:04:25 -04:00
|
|
|
|
2009-06-01 10:01:47 -04:00
|
|
|
va_copy(ap, args);
|
2009-05-08 11:04:25 -04:00
|
|
|
vfprintf(stderr, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
}
|
2017-01-21 10:11:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
vlogger_fatal(const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
vlogger_writer(E_FATAL, L_MISC, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
vlogger(int severity, int domain, const char *fmt, va_list args)
|
|
|
|
{
|
|
|
|
|
|
|
|
if(! logger_initialized)
|
|
|
|
{
|
|
|
|
/* lock not initialized, use stderr */
|
|
|
|
vlogger_writer(severity, domain, fmt, args);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!((1 << domain) & logdomains) || (severity > threshold))
|
|
|
|
return;
|
|
|
|
|
|
|
|
LOGGER_CHECK_ERR(pthread_mutex_lock(&logger_lck));
|
|
|
|
|
|
|
|
if (!logfile && !console)
|
|
|
|
{
|
|
|
|
LOGGER_CHECK_ERR(pthread_mutex_unlock(&logger_lck));
|
|
|
|
return;
|
|
|
|
}
|
2009-05-08 11:04:25 -04:00
|
|
|
|
2017-01-21 10:11:20 -05:00
|
|
|
vlogger_writer(severity, domain, fmt, args);
|
|
|
|
|
|
|
|
LOGGER_CHECK_ERR(pthread_mutex_unlock(&logger_lck));
|
2009-05-08 11:04:25 -04:00
|
|
|
}
|
|
|
|
|
2009-06-01 10:01:47 -04:00
|
|
|
void
|
2010-03-07 04:36:33 -05:00
|
|
|
DPRINTF(int severity, int domain, const char *fmt, ...)
|
2009-06-01 10:01:47 -04:00
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
vlogger(severity, domain, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
2010-03-07 04:45:15 -05:00
|
|
|
void
|
|
|
|
logger_ffmpeg(void *ptr, int level, const char *fmt, va_list ap)
|
|
|
|
{
|
|
|
|
int severity;
|
|
|
|
|
2014-01-13 16:01:54 -05:00
|
|
|
if (level <= AV_LOG_FATAL)
|
2010-03-07 04:45:15 -05:00
|
|
|
severity = E_LOG;
|
2013-12-26 17:06:34 -05:00
|
|
|
else if (level <= AV_LOG_WARNING)
|
2010-03-07 04:45:15 -05:00
|
|
|
severity = E_WARN;
|
2013-12-26 17:06:34 -05:00
|
|
|
else if (level <= AV_LOG_VERBOSE)
|
2010-03-07 04:45:15 -05:00
|
|
|
severity = E_DBG;
|
2017-02-28 17:06:01 -05:00
|
|
|
else if (level <= AV_LOG_DEBUG)
|
|
|
|
severity = E_SPAM;
|
2010-03-07 04:45:15 -05:00
|
|
|
else
|
2013-12-26 17:06:34 -05:00
|
|
|
severity = E_SPAM;
|
2010-03-07 04:45:15 -05:00
|
|
|
|
|
|
|
vlogger(severity, L_FFMPEG, fmt, ap);
|
|
|
|
}
|
|
|
|
|
2009-05-08 12:17:01 -04:00
|
|
|
void
|
|
|
|
logger_libevent(int severity, const char *msg)
|
|
|
|
{
|
|
|
|
switch (severity)
|
|
|
|
{
|
2015-10-19 15:15:29 -04:00
|
|
|
case EVENT_LOG_DEBUG:
|
2009-05-08 12:17:01 -04:00
|
|
|
severity = E_DBG;
|
|
|
|
break;
|
|
|
|
|
2015-10-19 15:15:29 -04:00
|
|
|
case EVENT_LOG_ERR:
|
2009-05-08 12:17:01 -04:00
|
|
|
severity = E_LOG;
|
|
|
|
break;
|
|
|
|
|
2015-10-19 15:15:29 -04:00
|
|
|
case EVENT_LOG_WARN:
|
2009-05-08 12:17:01 -04:00
|
|
|
severity = E_WARN;
|
|
|
|
break;
|
|
|
|
|
2015-10-19 15:15:29 -04:00
|
|
|
case EVENT_LOG_MSG:
|
2009-05-08 12:17:01 -04:00
|
|
|
severity = E_INFO;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
severity = E_LOG;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
DPRINTF(severity, L_EVENT, "%s\n", msg);
|
|
|
|
}
|
|
|
|
|
2017-01-06 03:44:18 -05:00
|
|
|
#ifdef HAVE_ALSA
|
2010-05-02 04:50:52 -04:00
|
|
|
void
|
|
|
|
logger_alsa(const char *file, int line, const char *function, int err, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
vlogger(E_LOG, L_LAUDIO, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
}
|
2017-01-06 03:44:18 -05:00
|
|
|
#endif /* HAVE_ALSA */
|
2010-05-02 04:50:52 -04:00
|
|
|
|
2009-05-08 11:04:25 -04:00
|
|
|
void
|
|
|
|
logger_reinit(void)
|
|
|
|
{
|
|
|
|
FILE *fp;
|
2010-07-14 07:06:32 -04:00
|
|
|
|
|
|
|
if (!logfile)
|
|
|
|
return;
|
2009-05-08 11:04:25 -04:00
|
|
|
|
2017-01-21 10:11:20 -05:00
|
|
|
LOGGER_CHECK_ERR(pthread_mutex_lock(&logger_lck));
|
2009-05-08 11:04:25 -04:00
|
|
|
|
2010-07-14 07:06:32 -04:00
|
|
|
fp = fopen(logfilename, "a");
|
|
|
|
if (!fp)
|
|
|
|
{
|
|
|
|
fprintf(logfile, "Could not reopen logfile: %s\n", strerror(errno));
|
|
|
|
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(logfile);
|
|
|
|
logfile = fp;
|
|
|
|
|
|
|
|
out:
|
2017-01-21 10:11:20 -05:00
|
|
|
LOGGER_CHECK_ERR(pthread_mutex_unlock(&logger_lck));
|
2009-05-08 11:04:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
|
2011-07-09 05:51:48 -04:00
|
|
|
ret = fchown(fileno(logfile), runas_uid, 0);
|
2011-06-27 12:29:21 -04:00
|
|
|
if (ret < 0)
|
|
|
|
fprintf(stderr, "Failed to set ownership on logfile: %s\n", strerror(errno));
|
|
|
|
|
|
|
|
ret = fchmod(fileno(logfile), 0644);
|
|
|
|
if (ret < 0)
|
|
|
|
fprintf(stderr, "Failed to set permissions on logfile: %s\n", strerror(errno));
|
|
|
|
|
2009-05-08 11:04:25 -04:00
|
|
|
logfilename = file;
|
|
|
|
|
2017-01-21 10:11:20 -05:00
|
|
|
/* logging w/o locks before initialized complete */
|
|
|
|
CHECK_ERR(L_MISC, mutex_init(&logger_lck));
|
|
|
|
|
|
|
|
logger_initialized = 1;
|
|
|
|
|
2009-05-08 11:04:25 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
logger_deinit(void)
|
|
|
|
{
|
|
|
|
if (logfile)
|
2017-01-21 10:11:20 -05:00
|
|
|
{
|
|
|
|
fclose(logfile);
|
|
|
|
logfile = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(logger_initialized)
|
|
|
|
{
|
|
|
|
/* logging w/o locks to stderr now */
|
|
|
|
logger_initialized = 0;
|
|
|
|
console = 1;
|
|
|
|
CHECK_ERR(L_MISC, pthread_mutex_destroy(&logger_lck));
|
|
|
|
}
|
2009-05-08 11:04:25 -04:00
|
|
|
}
|