2004-02-24 00:41:20 +01:00
|
|
|
/* the Music Player Daemon (MPD)
|
2007-04-05 05:22:33 +02:00
|
|
|
* Copyright (C) 2003-2007 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"
|
2008-10-08 10:49:29 +02:00
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <fcntl.h>
|
2008-10-29 21:02:22 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <pthread.h>
|
|
|
|
#include <glib.h>
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2008-11-05 18:38:30 +01:00
|
|
|
#define LOG_LEVEL_SECURE G_LOG_LEVEL_MESSAGE
|
|
|
|
|
2007-08-28 07:01:16 +02:00
|
|
|
#define LOG_DATE_BUF_SIZE 16
|
|
|
|
#define LOG_DATE_LEN (LOG_DATE_BUF_SIZE - 1)
|
2008-11-05 18:38:23 +01:00
|
|
|
|
2008-11-05 18:38:30 +01:00
|
|
|
static unsigned int log_threshold = G_LOG_LEVEL_INFO;
|
2008-11-05 18:38:23 +01:00
|
|
|
|
2008-11-05 18:39:13 +01:00
|
|
|
static const char *log_charset;
|
|
|
|
|
2008-11-05 18:39:10 +01:00
|
|
|
static bool stdout_mode = true;
|
2006-08-01 06:18:53 +02:00
|
|
|
static int out_fd = -1;
|
|
|
|
static int err_fd = -1;
|
2007-01-14 04:07:53 +01:00
|
|
|
static const char *out_filename;
|
|
|
|
static const char *err_filename;
|
2006-07-20 18:02:40 +02:00
|
|
|
|
2006-08-01 06:18:53 +02:00
|
|
|
static void redirect_logs(void)
|
|
|
|
{
|
2008-03-26 11:37:06 +01:00
|
|
|
assert(out_fd >= 0);
|
|
|
|
assert(err_fd >= 0);
|
2006-08-01 06:18:53 +02:00
|
|
|
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)
|
|
|
|
{
|
2007-08-28 07:01:16 +02:00
|
|
|
static char buf[LOG_DATE_BUF_SIZE];
|
2006-08-01 06:18:53 +02:00
|
|
|
time_t t = time(NULL);
|
2007-08-28 07:01:16 +02:00
|
|
|
strftime(buf, LOG_DATE_BUF_SIZE, "%b %d %H:%M : ", localtime(&t));
|
2006-08-01 06:18:53 +02:00
|
|
|
return buf;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
2004-06-12 04:06:16 +02:00
|
|
|
|
2008-11-05 18:38:55 +01:00
|
|
|
static void
|
|
|
|
mpd_log_func(G_GNUC_UNUSED const gchar *log_domain,
|
|
|
|
G_GNUC_UNUSED GLogLevelFlags log_level,
|
|
|
|
const gchar *message, G_GNUC_UNUSED gpointer user_data)
|
|
|
|
{
|
|
|
|
FILE *file = log_level <= G_LOG_LEVEL_WARNING
|
|
|
|
? stderr : stdout;
|
2008-11-05 18:39:13 +01:00
|
|
|
char *converted;
|
|
|
|
|
|
|
|
converted = g_convert_with_fallback(message, -1,
|
|
|
|
log_charset, "utf-8",
|
|
|
|
NULL, NULL, NULL, NULL);
|
|
|
|
if (converted != NULL)
|
|
|
|
message = converted;
|
2008-11-05 18:38:55 +01:00
|
|
|
|
|
|
|
fprintf(file, "%s%s",
|
|
|
|
stdout_mode ? "" : log_date(),
|
|
|
|
message);
|
2008-11-05 18:39:13 +01:00
|
|
|
|
|
|
|
g_free(converted);
|
2008-11-05 18:38:55 +01:00
|
|
|
}
|
|
|
|
|
2008-11-05 18:39:10 +01:00
|
|
|
void initLog(bool verbose)
|
2006-08-01 06:18:53 +02:00
|
|
|
{
|
|
|
|
ConfigParam *param;
|
|
|
|
|
2008-11-05 18:39:13 +01:00
|
|
|
g_get_charset(&log_charset);
|
|
|
|
|
2008-11-05 18:38:55 +01:00
|
|
|
g_log_set_default_handler(mpd_log_func, NULL);
|
|
|
|
|
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) {
|
2008-11-05 18:38:30 +01:00
|
|
|
log_threshold = G_LOG_LEVEL_DEBUG;
|
2006-08-01 06:18:53 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!(param = getConfigParam(CONF_LOG_LEVEL)))
|
|
|
|
return;
|
|
|
|
if (0 == strcmp(param->value, "default")) {
|
2008-11-05 18:38:30 +01:00
|
|
|
log_threshold = G_LOG_LEVEL_INFO;
|
2006-08-01 06:18:53 +02:00
|
|
|
} else if (0 == strcmp(param->value, "secure")) {
|
2008-11-05 18:38:23 +01:00
|
|
|
log_threshold = LOG_LEVEL_SECURE;
|
2006-08-01 06:18:53 +02:00
|
|
|
} else if (0 == strcmp(param->value, "verbose")) {
|
2008-11-05 18:38:30 +01:00
|
|
|
log_threshold = G_LOG_LEVEL_DEBUG;
|
2006-08-01 06:18:53 +02:00
|
|
|
} else {
|
|
|
|
FATAL("unknown log level \"%s\" at line %i\n",
|
|
|
|
param->value, param->line);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-05 18:39:10 +01:00
|
|
|
void open_log_files(bool use_stdout)
|
2006-08-01 06:18:53 +02:00
|
|
|
{
|
|
|
|
mode_t prev;
|
|
|
|
ConfigParam *param;
|
|
|
|
|
2008-11-05 18:37:40 +01:00
|
|
|
if (use_stdout)
|
2006-08-01 06:18:53 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2008-11-05 18:39:10 +01:00
|
|
|
void setup_log_output(bool use_stdout)
|
2006-08-01 06:18:53 +02:00
|
|
|
{
|
|
|
|
fflush(NULL);
|
|
|
|
if (!use_stdout) {
|
|
|
|
redirect_logs();
|
2008-11-05 18:39:10 +01:00
|
|
|
stdout_mode = false;
|
2008-11-05 18:39:13 +01:00
|
|
|
log_charset = NULL;
|
2006-08-01 06:18:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-05 18:38:30 +01:00
|
|
|
#define log_func(func,level) \
|
2006-08-01 06:18:53 +02:00
|
|
|
mpd_printf void func(const char *fmt, ...) \
|
|
|
|
{ \
|
2008-11-05 18:38:30 +01:00
|
|
|
if ((int)log_threshold <= level) { \
|
2006-08-01 06:18:53 +02:00
|
|
|
va_list args; \
|
|
|
|
va_start(args, fmt); \
|
2008-11-05 18:38:30 +01:00
|
|
|
g_logv(NULL, level, fmt, args); \
|
2006-08-01 06:18:53 +02:00
|
|
|
va_end(args); \
|
|
|
|
} \
|
|
|
|
}
|
|
|
|
|
2008-11-05 18:38:30 +01:00
|
|
|
log_func(ERROR, G_LOG_LEVEL_WARNING)
|
|
|
|
log_func(WARNING, G_LOG_LEVEL_WARNING)
|
|
|
|
log_func(LOG, G_LOG_LEVEL_MESSAGE)
|
|
|
|
log_func(SECURE, LOG_LEVEL_SECURE)
|
|
|
|
log_func(DEBUG, G_LOG_LEVEL_DEBUG)
|
2006-08-01 06:18:53 +02:00
|
|
|
|
|
|
|
#undef log_func
|
|
|
|
|
|
|
|
mpd_printf mpd_noreturn void FATAL(const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
va_start(args, fmt);
|
2008-11-05 18:38:30 +01:00
|
|
|
g_logv(NULL, G_LOG_LEVEL_ERROR, 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;
|
2008-03-26 11:37:06 +01:00
|
|
|
assert(out_fd >= 0);
|
|
|
|
assert(err_fd >= 0);
|
2008-10-29 21:02:22 +01:00
|
|
|
close(out_fd);
|
|
|
|
close(err_fd);
|
2006-08-01 06:18:53 +02:00
|
|
|
}
|
|
|
|
|