2004-02-24 00:41:20 +01:00
|
|
|
/* the Music Player Daemon (MPD)
|
2006-07-14 21:37:45 +02:00
|
|
|
* (c)2003-2006 by Warren Dukes (warren.dukes@gmail.com)
|
2004-02-24 00:41:20 +01:00
|
|
|
* This project's homepage is: http://www.musicpd.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 "log.h"
|
|
|
|
|
|
|
|
#include "conf.h"
|
|
|
|
#include "myfprintf.h"
|
2004-06-12 04:06:16 +02:00
|
|
|
#include "utils.h"
|
2007-01-08 05:31:44 +01:00
|
|
|
#include "localization.h"
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2006-08-01 06:18:53 +02:00
|
|
|
#include <assert.h>
|
2004-06-12 04:06:16 +02:00
|
|
|
#include <stdlib.h>
|
2004-02-24 00:41:20 +01:00
|
|
|
#include <string.h>
|
2004-06-12 04:06:16 +02:00
|
|
|
#include <stdarg.h>
|
2006-08-01 06:18:53 +02:00
|
|
|
#include <time.h>
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2006-08-01 06:18:53 +02:00
|
|
|
static unsigned int logLevel = LOG_LEVEL_LOW;
|
|
|
|
static int warningFlushed = 0;
|
|
|
|
static int stdout_mode = 1;
|
2006-07-20 18:02:40 +02:00
|
|
|
static char *warningBuffer = NULL;
|
2006-08-01 06:18:53 +02:00
|
|
|
static int out_fd = -1;
|
|
|
|
static int err_fd = -1;
|
|
|
|
static const char *out_filename = NULL;
|
|
|
|
static const char *err_filename = NULL;
|
2006-07-20 18:02:40 +02:00
|
|
|
|
2006-08-01 06:18:53 +02:00
|
|
|
/* redirect stdin to /dev/null to work around a libao bug */
|
|
|
|
static void redirect_stdin(void)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2006-08-01 06:18:53 +02:00
|
|
|
int fd;
|
|
|
|
if ((fd = open("/dev/null", O_RDONLY)) < 0)
|
|
|
|
FATAL("failed to open /dev/null %s\n", strerror(errno));
|
|
|
|
if (dup2(fd, STDIN_FILENO) < 0)
|
|
|
|
FATAL("dup2 stdin: %s\n", strerror(errno));
|
|
|
|
}
|
2006-07-20 18:02:40 +02:00
|
|
|
|
2006-08-01 06:18:53 +02:00
|
|
|
static void redirect_logs(void)
|
|
|
|
{
|
|
|
|
assert(out_fd > 0);
|
|
|
|
assert(err_fd > 0);
|
|
|
|
if (dup2(out_fd, STDOUT_FILENO) < 0)
|
|
|
|
FATAL("problems dup2 stdout : %s\n", strerror(errno));
|
|
|
|
if (dup2(err_fd, STDERR_FILENO) < 0)
|
|
|
|
FATAL("problems dup2 stderr : %s\n", strerror(errno));
|
|
|
|
}
|
2006-07-20 18:02:40 +02:00
|
|
|
|
2006-08-01 06:18:53 +02:00
|
|
|
static const char *log_date(void)
|
|
|
|
{
|
|
|
|
static char buf[16] = { '\0' };
|
|
|
|
time_t t = time(NULL);
|
|
|
|
strftime(buf, 16, "%b %d %H:%M : ", localtime(&t));
|
|
|
|
return buf;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
2004-06-12 04:06:16 +02:00
|
|
|
|
|
|
|
#define BUFFER_LENGTH 4096
|
2006-08-01 06:18:53 +02:00
|
|
|
static void buffer_warning(const char *fmt, va_list args)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2006-08-01 06:18:53 +02:00
|
|
|
char buffer[BUFFER_LENGTH + 1];
|
|
|
|
char *tmp = buffer;
|
|
|
|
size_t len = BUFFER_LENGTH;
|
2004-06-12 04:06:16 +02:00
|
|
|
|
2006-08-01 06:18:53 +02:00
|
|
|
if (!stdout_mode) {
|
|
|
|
memcpy(buffer, log_date(), 15);
|
|
|
|
tmp += 15;
|
|
|
|
len -= 15;
|
|
|
|
}
|
2004-06-12 04:06:16 +02:00
|
|
|
|
2006-08-01 06:18:53 +02:00
|
|
|
vsnprintf(tmp, len, fmt, args);
|
|
|
|
warningBuffer = appendToString(warningBuffer, buffer);
|
2004-06-12 04:06:16 +02:00
|
|
|
|
2006-08-01 06:18:53 +02:00
|
|
|
va_end(args);
|
|
|
|
}
|
2004-06-12 04:06:16 +02:00
|
|
|
|
2006-08-03 05:29:02 +02:00
|
|
|
static void do_log(FILE *fp, const char *fmt, va_list args)
|
2006-08-01 06:18:53 +02:00
|
|
|
{
|
2007-01-08 05:31:44 +01:00
|
|
|
char buffer[BUFFER_LENGTH + 1];
|
|
|
|
char *localized;
|
|
|
|
|
|
|
|
if (!stdout_mode) {
|
2006-08-03 05:29:02 +02:00
|
|
|
fwrite(log_date(), 15, 1, fp);
|
2007-01-08 05:31:44 +01:00
|
|
|
vfprintf(fp, fmt, args);
|
|
|
|
} else {
|
|
|
|
vsnprintf(buffer, BUFFER_LENGTH, fmt, args);
|
|
|
|
localized = utf8ToLocaleCharset(buffer);
|
|
|
|
fputs(localized, fp);
|
|
|
|
}
|
2004-06-12 04:06:16 +02:00
|
|
|
}
|
2005-03-12 04:10:09 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
void flushWarningLog(void)
|
|
|
|
{
|
2006-10-03 04:22:37 +02:00
|
|
|
char *s = warningBuffer;
|
2004-06-12 04:06:16 +02:00
|
|
|
|
2005-03-12 04:10:09 +01:00
|
|
|
DEBUG("flushing warning messages\n");
|
|
|
|
|
2006-10-03 04:22:37 +02:00
|
|
|
if (warningBuffer != NULL)
|
|
|
|
{
|
|
|
|
while (s != NULL) {
|
2007-01-08 06:01:28 +01:00
|
|
|
char *next = strchr(s, '\n');
|
|
|
|
if (next == NULL) break;
|
|
|
|
*next = '\0';
|
|
|
|
next++;
|
2007-01-08 06:04:08 +01:00
|
|
|
if (stdout_mode)
|
|
|
|
fprintf(stderr, "%s\n", utf8ToLocaleCharset(s));
|
|
|
|
else
|
|
|
|
fprintf(stderr, "%s\n", s);
|
2006-10-03 04:22:37 +02:00
|
|
|
s = next;
|
|
|
|
}
|
|
|
|
|
|
|
|
warningBuffer = NULL;
|
2004-06-12 04:06:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
warningFlushed = 1;
|
2006-10-03 04:22:37 +02:00
|
|
|
|
|
|
|
DEBUG("done flushing warning messages\n");
|
2004-06-12 04:06:16 +02:00
|
|
|
}
|
2006-08-01 06:18:53 +02:00
|
|
|
|
|
|
|
void initLog(const int verbose)
|
|
|
|
{
|
|
|
|
ConfigParam *param;
|
|
|
|
|
2006-08-03 05:29:02 +02:00
|
|
|
/* unbuffer stdout, stderr is unbuffered by default, leave it */
|
|
|
|
setvbuf(stdout, (char *)NULL, _IONBF, 0);
|
|
|
|
|
2006-08-01 06:18:53 +02:00
|
|
|
if (verbose) {
|
|
|
|
logLevel = LOG_LEVEL_DEBUG;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!(param = getConfigParam(CONF_LOG_LEVEL)))
|
|
|
|
return;
|
|
|
|
if (0 == strcmp(param->value, "default")) {
|
|
|
|
logLevel = LOG_LEVEL_LOW;
|
|
|
|
} else if (0 == strcmp(param->value, "secure")) {
|
|
|
|
logLevel = LOG_LEVEL_SECURE;
|
|
|
|
} else if (0 == strcmp(param->value, "verbose")) {
|
|
|
|
logLevel = LOG_LEVEL_DEBUG;
|
|
|
|
} else {
|
|
|
|
FATAL("unknown log level \"%s\" at line %i\n",
|
|
|
|
param->value, param->line);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void open_log_files(const int use_stdout)
|
|
|
|
{
|
|
|
|
mode_t prev;
|
|
|
|
ConfigParam *param;
|
|
|
|
|
|
|
|
if (use_stdout) {
|
|
|
|
flushWarningLog();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
prev = umask(0066);
|
|
|
|
param = parseConfigFilePath(CONF_LOG_FILE, 1);
|
|
|
|
out_filename = param->value;
|
2006-08-22 08:36:51 +02:00
|
|
|
out_fd = open(out_filename, O_CREAT | O_WRONLY | O_APPEND, 0666);
|
2006-08-01 06:18:53 +02:00
|
|
|
if (out_fd < 0)
|
|
|
|
FATAL("problem opening log file \"%s\" (config line %i) for "
|
|
|
|
"writing\n", param->value, param->line);
|
|
|
|
|
|
|
|
param = parseConfigFilePath(CONF_ERROR_FILE, 1);
|
|
|
|
err_filename = param->value;
|
2006-08-22 08:36:51 +02:00
|
|
|
err_fd = open(err_filename, O_CREAT | O_WRONLY | O_APPEND, 0666);
|
2006-08-01 06:18:53 +02:00
|
|
|
if (err_fd < 0)
|
|
|
|
FATAL("problem opening error file \"%s\" (config line %i) for "
|
|
|
|
"writing\n", param->value, param->line);
|
|
|
|
|
|
|
|
umask(prev);
|
|
|
|
}
|
|
|
|
|
|
|
|
void setup_log_output(const int use_stdout)
|
|
|
|
{
|
|
|
|
fflush(NULL);
|
|
|
|
if (!use_stdout) {
|
|
|
|
redirect_logs();
|
|
|
|
stdout_mode = 0;
|
|
|
|
}
|
|
|
|
redirect_stdin();
|
|
|
|
}
|
|
|
|
|
2006-08-03 05:29:02 +02:00
|
|
|
#define log_func(func,level,fp) \
|
2006-08-01 06:18:53 +02:00
|
|
|
mpd_printf void func(const char *fmt, ...) \
|
|
|
|
{ \
|
|
|
|
if (logLevel >= level) { \
|
|
|
|
va_list args; \
|
|
|
|
va_start(args, fmt); \
|
2006-08-03 05:29:02 +02:00
|
|
|
do_log(fp, fmt, args); \
|
2006-08-01 06:18:53 +02:00
|
|
|
va_end(args); \
|
|
|
|
} \
|
|
|
|
}
|
|
|
|
|
2006-08-03 05:29:02 +02:00
|
|
|
log_func(ERROR, 0, stderr)
|
|
|
|
log_func(LOG, 0, stdout)
|
|
|
|
log_func(SECURE, LOG_LEVEL_SECURE, stdout)
|
|
|
|
log_func(DEBUG, LOG_LEVEL_DEBUG, stdout)
|
2006-08-01 06:18:53 +02:00
|
|
|
|
|
|
|
#undef log_func
|
|
|
|
|
|
|
|
void WARNING(const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
va_start(args, fmt);
|
|
|
|
if (warningFlushed) {
|
2006-08-03 05:29:02 +02:00
|
|
|
do_log(stderr, fmt, args);
|
2006-08-01 06:18:53 +02:00
|
|
|
} else
|
|
|
|
buffer_warning(fmt, args);
|
|
|
|
va_end(args);
|
|
|
|
}
|
|
|
|
|
|
|
|
mpd_printf mpd_noreturn void FATAL(const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
va_start(args, fmt);
|
2006-08-03 05:29:02 +02:00
|
|
|
do_log(stderr, fmt, args);
|
2006-08-01 06:18:53 +02:00
|
|
|
va_end(args);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
int cycle_log_files(void)
|
|
|
|
{
|
|
|
|
mode_t prev;
|
|
|
|
|
|
|
|
if (stdout_mode)
|
|
|
|
return 0;
|
|
|
|
assert(out_filename);
|
|
|
|
assert(err_filename);
|
|
|
|
|
|
|
|
DEBUG("Cycling log files...\n");
|
|
|
|
close_log_files();
|
|
|
|
|
|
|
|
prev = umask(0066);
|
|
|
|
|
2006-08-22 08:36:51 +02:00
|
|
|
out_fd = open(out_filename, O_CREAT | O_WRONLY | O_APPEND, 0666);
|
2006-08-01 06:18:53 +02:00
|
|
|
if (out_fd < 0) {
|
|
|
|
ERROR("error re-opening log file: %s\n", out_filename);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2006-08-22 08:36:51 +02:00
|
|
|
err_fd = open(err_filename, O_CREAT | O_WRONLY | O_APPEND, 0666);
|
2006-08-01 06:18:53 +02:00
|
|
|
if (err_fd < 0) {
|
|
|
|
ERROR("error re-opening error file: %s\n", err_filename);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
umask(prev);
|
|
|
|
|
|
|
|
redirect_logs();
|
|
|
|
DEBUG("Done cycling log files\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void close_log_files(void)
|
|
|
|
{
|
|
|
|
if (stdout_mode)
|
|
|
|
return;
|
|
|
|
assert(out_fd > 0);
|
|
|
|
assert(err_fd > 0);
|
|
|
|
xclose(out_fd);
|
|
|
|
xclose(err_fd);
|
|
|
|
}
|
|
|
|
|