2009-03-13 18:43:16 +01:00
|
|
|
/*
|
2013-01-09 22:33:06 +01:00
|
|
|
* Copyright (C) 2003-2013 The Music Player Daemon Project
|
2009-03-13 18:43:16 +01:00
|
|
|
* http://www.musicpd.org
|
2004-02-24 00:41:20 +01: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.
|
2009-03-13 18:43:16 +01:00
|
|
|
*
|
|
|
|
* 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.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2004-02-24 00:41:20 +01:00
|
|
|
*/
|
|
|
|
|
2009-11-12 09:12:38 +01:00
|
|
|
#include "config.h"
|
2013-01-09 22:33:06 +01:00
|
|
|
#include "Log.hxx"
|
2013-09-05 08:47:10 +02:00
|
|
|
#include "ConfigData.hxx"
|
|
|
|
#include "ConfigGlobal.hxx"
|
|
|
|
#include "ConfigOption.hxx"
|
2013-08-07 10:31:31 +02:00
|
|
|
#include "system/fd_util.h"
|
|
|
|
#include "system/FatalError.hxx"
|
2013-08-07 19:54:38 +02:00
|
|
|
#include "fs/Path.hxx"
|
|
|
|
#include "fs/FileSystem.hxx"
|
2010-09-25 15:00:43 +02:00
|
|
|
#include "mpd_error.h"
|
2013-08-10 18:02:44 +02:00
|
|
|
#include "util/Error.hxx"
|
|
|
|
#include "util/Domain.hxx"
|
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 <glib.h>
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2008-12-28 19:48:53 +01:00
|
|
|
#ifdef HAVE_SYSLOG
|
|
|
|
#include <syslog.h>
|
|
|
|
#endif
|
|
|
|
|
2009-02-19 09:24:59 +01:00
|
|
|
#undef G_LOG_DOMAIN
|
|
|
|
#define G_LOG_DOMAIN "log"
|
|
|
|
|
2008-12-20 17:26:49 +01:00
|
|
|
#define LOG_LEVEL_SECURE G_LOG_LEVEL_INFO
|
2008-11-05 18:38:30 +01:00
|
|
|
|
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
|
|
|
|
2013-08-10 18:02:44 +02:00
|
|
|
static constexpr Domain log_domain("log");
|
|
|
|
|
2008-12-28 19:48:53 +01:00
|
|
|
static GLogLevelFlags log_threshold = G_LOG_LEVEL_MESSAGE;
|
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;
|
2008-12-28 19:48:53 +01:00
|
|
|
static int out_fd;
|
2013-08-07 19:54:38 +02:00
|
|
|
static Path out_path = Path::Null();
|
2006-07-20 18:02:40 +02:00
|
|
|
|
2008-12-28 19:48:53 +01:00
|
|
|
static void redirect_logs(int fd)
|
2006-08-01 06:18:53 +02:00
|
|
|
{
|
2008-12-28 19:48:53 +01:00
|
|
|
assert(fd >= 0);
|
|
|
|
if (dup2(fd, STDOUT_FILENO) < 0)
|
2013-08-07 09:35:30 +02:00
|
|
|
FatalSystemError("Failed to dup2 stdout");
|
2008-12-28 19:48:53 +01:00
|
|
|
if (dup2(fd, STDERR_FILENO) < 0)
|
2013-08-07 09:35:30 +02:00
|
|
|
FatalSystemError("Failed to dup2 stderr");
|
2006-08-01 06:18:53 +02:00
|
|
|
}
|
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-12-29 17:28:34 +01:00
|
|
|
/**
|
|
|
|
* Determines the length of the string excluding trailing whitespace
|
|
|
|
* characters.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
chomp_length(const char *p)
|
|
|
|
{
|
|
|
|
size_t length = strlen(p);
|
|
|
|
|
|
|
|
while (length > 0 && g_ascii_isspace(p[length - 1]))
|
|
|
|
--length;
|
|
|
|
|
|
|
|
return (int)length;
|
|
|
|
}
|
|
|
|
|
2008-11-05 18:38:55 +01:00
|
|
|
static void
|
2013-08-10 18:02:44 +02:00
|
|
|
file_log_func(const gchar *domain,
|
2009-04-10 09:14:21 +02:00
|
|
|
GLogLevelFlags log_level,
|
2013-08-04 23:48:01 +02:00
|
|
|
const gchar *message, gcc_unused gpointer user_data)
|
2008-11-05 18:38:55 +01:00
|
|
|
{
|
2008-11-05 18:39:13 +01:00
|
|
|
char *converted;
|
|
|
|
|
2008-12-28 19:48:53 +01:00
|
|
|
if (log_level > log_threshold)
|
2008-11-14 18:27:11 +01:00
|
|
|
return;
|
|
|
|
|
2008-11-05 21:40:08 +01:00
|
|
|
if (log_charset != NULL) {
|
|
|
|
converted = g_convert_with_fallback(message, -1,
|
|
|
|
log_charset, "utf-8",
|
|
|
|
NULL, NULL, NULL, NULL);
|
|
|
|
if (converted != NULL)
|
|
|
|
message = converted;
|
|
|
|
} else
|
|
|
|
converted = NULL;
|
2008-11-05 18:38:55 +01:00
|
|
|
|
2013-08-10 18:02:44 +02:00
|
|
|
if (domain == nullptr)
|
|
|
|
domain = "";
|
2008-11-21 20:13:00 +01:00
|
|
|
|
2008-12-29 17:28:34 +01:00
|
|
|
fprintf(stderr, "%s%s%s%.*s\n",
|
2008-11-05 18:38:55 +01:00
|
|
|
stdout_mode ? "" : log_date(),
|
2013-08-10 18:02:44 +02:00
|
|
|
domain, *domain == 0 ? "" : ": ",
|
2008-12-29 17:28:34 +01:00
|
|
|
chomp_length(message), message);
|
2008-11-05 18:39:13 +01:00
|
|
|
|
|
|
|
g_free(converted);
|
2008-11-05 18:38:55 +01:00
|
|
|
}
|
|
|
|
|
2008-12-28 19:48:53 +01:00
|
|
|
static void
|
|
|
|
log_init_stdout(void)
|
|
|
|
{
|
|
|
|
g_log_set_default_handler(file_log_func, NULL);
|
|
|
|
}
|
|
|
|
|
2008-12-28 19:48:52 +01:00
|
|
|
static int
|
|
|
|
open_log_file(void)
|
|
|
|
{
|
2013-08-07 19:54:38 +02:00
|
|
|
assert(!out_path.IsNull());
|
2008-12-28 19:48:52 +01:00
|
|
|
|
2013-08-07 19:54:38 +02:00
|
|
|
return OpenFile(out_path, O_CREAT | O_WRONLY | O_APPEND, 0666);
|
2008-12-28 19:48:52 +01:00
|
|
|
}
|
|
|
|
|
2011-09-09 21:40:28 +02:00
|
|
|
static bool
|
2013-08-10 18:02:44 +02:00
|
|
|
log_init_file(unsigned line, Error &error)
|
2008-12-28 19:48:53 +01:00
|
|
|
{
|
2013-08-07 19:54:38 +02:00
|
|
|
assert(!out_path.IsNull());
|
2012-08-14 23:16:46 +02:00
|
|
|
|
2008-12-28 19:48:53 +01:00
|
|
|
out_fd = open_log_file();
|
2011-09-09 21:40:28 +02:00
|
|
|
if (out_fd < 0) {
|
2013-08-07 19:54:38 +02:00
|
|
|
const std::string out_path_utf8 = out_path.ToUTF8();
|
2013-08-10 18:02:44 +02:00
|
|
|
error.FormatErrno("failed to open log file \"%s\" (config line %u)",
|
|
|
|
out_path_utf8.c_str(), line);
|
2011-09-09 21:40:28 +02:00
|
|
|
return false;
|
|
|
|
}
|
2008-12-28 19:48:53 +01:00
|
|
|
|
|
|
|
g_log_set_default_handler(file_log_func, NULL);
|
2011-09-09 21:40:28 +02:00
|
|
|
return true;
|
2008-12-28 19:48:53 +01:00
|
|
|
}
|
|
|
|
|
2008-12-28 19:48:53 +01:00
|
|
|
#ifdef HAVE_SYSLOG
|
|
|
|
|
|
|
|
static int
|
|
|
|
glib_to_syslog_level(GLogLevelFlags log_level)
|
|
|
|
{
|
|
|
|
switch (log_level & G_LOG_LEVEL_MASK) {
|
|
|
|
case G_LOG_LEVEL_ERROR:
|
|
|
|
case G_LOG_LEVEL_CRITICAL:
|
|
|
|
return LOG_ERR;
|
|
|
|
|
|
|
|
case G_LOG_LEVEL_WARNING:
|
|
|
|
return LOG_WARNING;
|
|
|
|
|
|
|
|
case G_LOG_LEVEL_MESSAGE:
|
|
|
|
return LOG_NOTICE;
|
|
|
|
|
|
|
|
case G_LOG_LEVEL_INFO:
|
|
|
|
return LOG_INFO;
|
|
|
|
|
|
|
|
case G_LOG_LEVEL_DEBUG:
|
|
|
|
return LOG_DEBUG;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return LOG_NOTICE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2013-08-10 18:02:44 +02:00
|
|
|
syslog_log_func(const gchar *domain,
|
2008-12-28 19:48:53 +01:00
|
|
|
GLogLevelFlags log_level, const gchar *message,
|
2013-08-04 23:48:01 +02:00
|
|
|
gcc_unused gpointer user_data)
|
2008-12-28 19:48:53 +01:00
|
|
|
{
|
|
|
|
if (stdout_mode) {
|
|
|
|
/* fall back to the file log function during
|
|
|
|
startup */
|
2013-08-10 18:02:44 +02:00
|
|
|
file_log_func(domain, log_level,
|
2008-12-28 19:48:53 +01:00
|
|
|
message, user_data);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (log_level > log_threshold)
|
|
|
|
return;
|
|
|
|
|
2013-08-10 18:02:44 +02:00
|
|
|
if (domain == nullptr)
|
|
|
|
domain = "";
|
2008-12-28 19:48:53 +01:00
|
|
|
|
2008-12-29 17:28:34 +01:00
|
|
|
syslog(glib_to_syslog_level(log_level), "%s%s%.*s",
|
2013-08-10 18:02:44 +02:00
|
|
|
domain, *domain == 0 ? "" : ": ",
|
2008-12-29 17:28:34 +01:00
|
|
|
chomp_length(message), message);
|
2008-12-28 19:48:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
log_init_syslog(void)
|
|
|
|
{
|
2013-08-07 19:54:38 +02:00
|
|
|
assert(out_path.IsNull());
|
2008-12-28 19:48:53 +01:00
|
|
|
|
|
|
|
openlog(PACKAGE, 0, LOG_DAEMON);
|
|
|
|
g_log_set_default_handler(syslog_log_func, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2008-12-28 19:48:53 +01:00
|
|
|
static inline GLogLevelFlags
|
|
|
|
parse_log_level(const char *value, unsigned line)
|
|
|
|
{
|
|
|
|
if (0 == strcmp(value, "default"))
|
|
|
|
return G_LOG_LEVEL_MESSAGE;
|
|
|
|
if (0 == strcmp(value, "secure"))
|
|
|
|
return LOG_LEVEL_SECURE;
|
|
|
|
else if (0 == strcmp(value, "verbose"))
|
|
|
|
return G_LOG_LEVEL_DEBUG;
|
2009-01-15 09:17:06 +01:00
|
|
|
else {
|
2010-09-25 15:00:43 +02:00
|
|
|
MPD_ERROR("unknown log level \"%s\" at line %u\n",
|
|
|
|
value, line);
|
2009-01-15 09:17:06 +01:00
|
|
|
return G_LOG_LEVEL_MESSAGE;
|
|
|
|
}
|
2008-12-28 19:48:53 +01:00
|
|
|
}
|
|
|
|
|
2009-02-19 08:35:20 +01:00
|
|
|
void
|
|
|
|
log_early_init(bool verbose)
|
|
|
|
{
|
|
|
|
if (verbose)
|
|
|
|
log_threshold = G_LOG_LEVEL_DEBUG;
|
|
|
|
|
|
|
|
log_init_stdout();
|
|
|
|
}
|
|
|
|
|
2011-09-09 21:40:28 +02:00
|
|
|
bool
|
2013-08-10 18:02:44 +02:00
|
|
|
log_init(bool verbose, bool use_stdout, Error &error)
|
2006-08-01 06:18:53 +02:00
|
|
|
{
|
2009-01-25 16:03:49 +01:00
|
|
|
const struct config_param *param;
|
2006-08-01 06:18:53 +02:00
|
|
|
|
2008-11-05 18:39:13 +01:00
|
|
|
g_get_charset(&log_charset);
|
|
|
|
|
2008-12-28 19:48:53 +01:00
|
|
|
if (verbose)
|
2008-11-05 18:38:30 +01:00
|
|
|
log_threshold = G_LOG_LEVEL_DEBUG;
|
2009-01-17 20:23:27 +01:00
|
|
|
else if ((param = config_get_param(CONF_LOG_LEVEL)) != NULL)
|
2008-12-28 19:48:53 +01:00
|
|
|
log_threshold = parse_log_level(param->value, param->line);
|
2006-08-01 06:18:53 +02:00
|
|
|
|
2008-12-28 19:48:53 +01:00
|
|
|
if (use_stdout) {
|
|
|
|
log_init_stdout();
|
2011-09-09 21:40:28 +02:00
|
|
|
return true;
|
2008-12-28 19:48:53 +01:00
|
|
|
} else {
|
2009-01-17 20:23:27 +01:00
|
|
|
param = config_get_param(CONF_LOG_FILE);
|
2008-12-28 19:48:53 +01:00
|
|
|
if (param == NULL) {
|
|
|
|
#ifdef HAVE_SYSLOG
|
|
|
|
/* no configuration: default to syslog (if
|
|
|
|
available) */
|
|
|
|
log_init_syslog();
|
2011-09-09 21:40:28 +02:00
|
|
|
return true;
|
2008-12-28 19:48:53 +01:00
|
|
|
#else
|
2013-08-10 18:02:44 +02:00
|
|
|
error.Set(log_domain,
|
|
|
|
"config parameter 'log_file' not found");
|
2011-09-09 21:40:28 +02:00
|
|
|
return false;
|
2008-12-28 19:48:53 +01:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_SYSLOG
|
|
|
|
} else if (strcmp(param->value, "syslog") == 0) {
|
|
|
|
log_init_syslog();
|
2011-09-09 21:40:28 +02:00
|
|
|
return true;
|
2008-12-28 19:48:53 +01:00
|
|
|
#endif
|
|
|
|
} else {
|
2013-08-10 18:02:44 +02:00
|
|
|
out_path = config_get_path(CONF_LOG_FILE, error);
|
2013-08-07 19:54:38 +02:00
|
|
|
return !out_path.IsNull() &&
|
2013-08-10 18:02:44 +02:00
|
|
|
log_init_file(param->line, error);
|
2008-12-28 19:48:53 +01:00
|
|
|
}
|
2008-12-28 19:48:53 +01:00
|
|
|
}
|
2006-08-01 06:18:53 +02:00
|
|
|
}
|
|
|
|
|
2012-08-14 23:16:46 +02:00
|
|
|
static void
|
|
|
|
close_log_files(void)
|
|
|
|
{
|
|
|
|
if (stdout_mode)
|
|
|
|
return;
|
|
|
|
|
|
|
|
#ifdef HAVE_SYSLOG
|
2013-08-07 19:54:38 +02:00
|
|
|
if (out_path.IsNull())
|
2012-08-14 23:16:46 +02:00
|
|
|
closelog();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
log_deinit(void)
|
|
|
|
{
|
|
|
|
close_log_files();
|
2013-08-07 19:54:38 +02:00
|
|
|
out_path = Path::Null();
|
2012-08-14 23:16:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
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) {
|
2010-05-08 09:12:41 +02:00
|
|
|
#ifndef WIN32
|
2013-08-07 19:54:38 +02:00
|
|
|
if (out_path.IsNull())
|
2009-11-07 17:48:57 +01:00
|
|
|
out_fd = open("/dev/null", O_WRONLY);
|
2010-05-08 09:12:41 +02:00
|
|
|
#endif
|
2009-11-07 17:48:57 +01:00
|
|
|
|
|
|
|
if (out_fd >= 0) {
|
2008-12-28 19:48:53 +01:00
|
|
|
redirect_logs(out_fd);
|
|
|
|
close(out_fd);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int cycle_log_files(void)
|
|
|
|
{
|
2008-12-28 19:48:53 +01:00
|
|
|
int fd;
|
|
|
|
|
2013-08-07 19:54:38 +02:00
|
|
|
if (stdout_mode || out_path.IsNull())
|
2006-08-01 06:18:53 +02:00
|
|
|
return 0;
|
2013-08-07 19:54:38 +02:00
|
|
|
|
|
|
|
assert(!out_path.IsNull());
|
2006-08-01 06:18:53 +02:00
|
|
|
|
2008-12-28 19:48:53 +01:00
|
|
|
g_debug("Cycling log files...\n");
|
2006-08-01 06:18:53 +02:00
|
|
|
close_log_files();
|
|
|
|
|
2008-12-28 19:48:53 +01:00
|
|
|
fd = open_log_file();
|
|
|
|
if (fd < 0) {
|
2013-08-07 19:54:38 +02:00
|
|
|
const std::string out_path_utf8 = out_path.ToUTF8();
|
|
|
|
g_warning("error re-opening log file: %s",
|
|
|
|
out_path_utf8.c_str());
|
2006-08-01 06:18:53 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2008-12-28 19:48:53 +01:00
|
|
|
redirect_logs(fd);
|
2008-12-28 19:48:53 +01:00
|
|
|
g_debug("Done cycling log files\n");
|
2006-08-01 06:18:53 +02:00
|
|
|
return 0;
|
|
|
|
}
|