From 8eacd13ce7f528adc52614ce3cc5faf963b3fc5a Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Wed, 9 Jan 2013 22:33:06 +0100 Subject: sig_handlers, log: convert to C++ --- Makefile.am | 14 +- src/CommandLine.cxx | 6 +- src/Log.cxx | 341 +++++++++++++++++++++++++++++++++++++++++++++++++ src/Log.hxx | 54 ++++++++ src/Main.cxx | 4 +- src/SignalHandlers.cxx | 80 ++++++++++++ src/SignalHandlers.hxx | 25 ++++ src/log.c | 341 ------------------------------------------------- src/log.h | 54 -------- src/sig_handlers.c | 80 ------------ src/sig_handlers.h | 25 ---- 11 files changed, 509 insertions(+), 515 deletions(-) create mode 100644 src/Log.cxx create mode 100644 src/Log.hxx create mode 100644 src/SignalHandlers.cxx create mode 100644 src/SignalHandlers.hxx delete mode 100644 src/log.c delete mode 100644 src/log.h delete mode 100644 src/sig_handlers.c delete mode 100644 src/sig_handlers.h diff --git a/Makefile.am b/Makefile.am index 5ddeba9fe..779ddc1a3 100644 --- a/Makefile.am +++ b/Makefile.am @@ -92,7 +92,6 @@ mpd_headers = \ src/icy_server.h \ src/icy_metadata.h \ src/server_socket.h \ - src/log.h \ src/ls.h \ src/mixer_api.h \ src/mixer_control.h \ @@ -129,7 +128,6 @@ mpd_headers = \ src/replay_gain_config.h \ src/replay_gain_info.h \ src/replay_gain_ape.h \ - src/sig_handlers.h \ src/TimePrint.cxx src/TimePrint.hxx \ src/song.h \ src/song_sort.c src/song_sort.h \ @@ -240,7 +238,7 @@ src_mpd_SOURCES = \ src/ClientFile.cxx src/ClientFile.hxx \ src/server_socket.c \ src/Listen.cxx src/Listen.hxx \ - src/log.c \ + src/Log.cxx src/Log.hxx \ src/ls.cxx \ src/io_error.h \ src/io_thread.c src/io_thread.h \ @@ -279,7 +277,7 @@ src_mpd_SOURCES = \ src/QueueSave.cxx src/QueueSave.hxx \ src/ReplayGainConfig.cxx \ src/ReplayGainInfo.cxx \ - src/sig_handlers.c \ + src/SignalHandlers.cxx src/SignalHandlers.hxx \ src/Song.cxx \ src/SongUpdate.cxx \ src/SongPrint.cxx src/SongPrint.hxx \ @@ -1118,7 +1116,7 @@ test_run_decoder_LDADD = \ test_run_decoder_SOURCES = test/run_decoder.c \ test/stdbin.h \ src/io_thread.c src/io_thread.h \ - src/conf.c src/tokenizer.c src/utils.c src/string_util.c src/log.c \ + src/conf.c src/tokenizer.c src/utils.c src/string_util.c \ src/Tag.cxx src/TagNames.c src/TagPool.cxx src/tag_handler.c \ src/ReplayGainInfo.cxx \ src/uri.c \ @@ -1140,7 +1138,7 @@ test_read_tags_LDADD = \ $(GLIB_LIBS) test_read_tags_SOURCES = test/read_tags.c \ src/io_thread.c src/io_thread.h \ - src/conf.c src/tokenizer.c src/utils.c src/string_util.c src/log.c \ + src/conf.c src/tokenizer.c src/utils.c src/string_util.c \ src/Tag.cxx src/TagNames.c src/TagPool.cxx src/tag_handler.c \ src/ReplayGainInfo.cxx \ src/uri.c \ @@ -1263,7 +1261,7 @@ test_run_output_LDADD = $(MPD_LIBS) \ test_run_output_SOURCES = test/run_output.cxx \ test/FakeReplayGainConfig.cxx \ test/stdbin.h \ - src/conf.c src/tokenizer.c src/utils.c src/string_util.c src/log.c \ + src/conf.c src/tokenizer.c src/utils.c src/string_util.c \ src/io_thread.c src/io_thread.h \ src/audio_check.c \ src/audio_format.c \ @@ -1292,7 +1290,7 @@ test_read_mixer_LDADD = \ $(OUTPUT_LIBS) \ $(GLIB_LIBS) test_read_mixer_SOURCES = test/read_mixer.c \ - src/conf.c src/tokenizer.c src/utils.c src/string_util.c src/log.c \ + src/conf.c src/tokenizer.c src/utils.c src/string_util.c \ src/mixer_control.c src/mixer_api.c \ src/filter_plugin.c \ src/filter/volume_filter_plugin.c \ diff --git a/src/CommandLine.cxx b/src/CommandLine.cxx index 670995214..6f9528a4e 100644 --- a/src/CommandLine.cxx +++ b/src/CommandLine.cxx @@ -21,11 +21,7 @@ #include "CommandLine.hxx" #include "path.h" #include "ls.hxx" - -extern "C" { -#include "log.h" -} - +#include "Log.hxx" #include "conf.h" #include "decoder_list.h" #include "decoder_plugin.h" diff --git a/src/Log.cxx b/src/Log.cxx new file mode 100644 index 000000000..7ca64197f --- /dev/null +++ b/src/Log.cxx @@ -0,0 +1,341 @@ +/* + * Copyright (C) 2003-2013 The Music Player Daemon Project + * 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., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "config.h" +#include "Log.hxx" +#include "conf.h" +#include "utils.h" +#include "fd_util.h" +#include "mpd_error.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#ifdef HAVE_SYSLOG +#include +#endif + +#undef G_LOG_DOMAIN +#define G_LOG_DOMAIN "log" + +#define LOG_LEVEL_SECURE G_LOG_LEVEL_INFO + +#define LOG_DATE_BUF_SIZE 16 +#define LOG_DATE_LEN (LOG_DATE_BUF_SIZE - 1) + +static GLogLevelFlags log_threshold = G_LOG_LEVEL_MESSAGE; + +static const char *log_charset; + +static bool stdout_mode = true; +static int out_fd; +static char *out_filename; + +static void redirect_logs(int fd) +{ + assert(fd >= 0); + if (dup2(fd, STDOUT_FILENO) < 0) + MPD_ERROR("problems dup2 stdout : %s\n", strerror(errno)); + if (dup2(fd, STDERR_FILENO) < 0) + MPD_ERROR("problems dup2 stderr : %s\n", strerror(errno)); +} + +static const char *log_date(void) +{ + static char buf[LOG_DATE_BUF_SIZE]; + time_t t = time(NULL); + strftime(buf, LOG_DATE_BUF_SIZE, "%b %d %H:%M : ", localtime(&t)); + return buf; +} + +/** + * 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; +} + +static void +file_log_func(const gchar *log_domain, + GLogLevelFlags log_level, + const gchar *message, G_GNUC_UNUSED gpointer user_data) +{ + char *converted; + + if (log_level > log_threshold) + return; + + 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; + + if (log_domain == NULL) + log_domain = ""; + + fprintf(stderr, "%s%s%s%.*s\n", + stdout_mode ? "" : log_date(), + log_domain, *log_domain == 0 ? "" : ": ", + chomp_length(message), message); + + g_free(converted); +} + +static void +log_init_stdout(void) +{ + g_log_set_default_handler(file_log_func, NULL); +} + +static int +open_log_file(void) +{ + assert(out_filename != NULL); + + return open_cloexec(out_filename, O_CREAT | O_WRONLY | O_APPEND, 0666); +} + +static bool +log_init_file(unsigned line, GError **error_r) +{ + assert(out_filename != NULL); + + out_fd = open_log_file(); + if (out_fd < 0) { + g_set_error(error_r, log_quark(), errno, + "failed to open log file \"%s\" (config line %u): %s", + out_filename, line, g_strerror(errno)); + return false; + } + + g_log_set_default_handler(file_log_func, NULL); + return true; +} + +#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 +syslog_log_func(const gchar *log_domain, + GLogLevelFlags log_level, const gchar *message, + G_GNUC_UNUSED gpointer user_data) +{ + if (stdout_mode) { + /* fall back to the file log function during + startup */ + file_log_func(log_domain, log_level, + message, user_data); + return; + } + + if (log_level > log_threshold) + return; + + if (log_domain == NULL) + log_domain = ""; + + syslog(glib_to_syslog_level(log_level), "%s%s%.*s", + log_domain, *log_domain == 0 ? "" : ": ", + chomp_length(message), message); +} + +static void +log_init_syslog(void) +{ + assert(out_filename == NULL); + + openlog(PACKAGE, 0, LOG_DAEMON); + g_log_set_default_handler(syslog_log_func, NULL); +} + +#endif + +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; + else { + MPD_ERROR("unknown log level \"%s\" at line %u\n", + value, line); + return G_LOG_LEVEL_MESSAGE; + } +} + +void +log_early_init(bool verbose) +{ + if (verbose) + log_threshold = G_LOG_LEVEL_DEBUG; + + log_init_stdout(); +} + +bool +log_init(bool verbose, bool use_stdout, GError **error_r) +{ + const struct config_param *param; + + g_get_charset(&log_charset); + + if (verbose) + log_threshold = G_LOG_LEVEL_DEBUG; + else if ((param = config_get_param(CONF_LOG_LEVEL)) != NULL) + log_threshold = parse_log_level(param->value, param->line); + + if (use_stdout) { + log_init_stdout(); + return true; + } else { + param = config_get_param(CONF_LOG_FILE); + if (param == NULL) { +#ifdef HAVE_SYSLOG + /* no configuration: default to syslog (if + available) */ + log_init_syslog(); + return true; +#else + g_set_error(error_r, log_quark(), 0, + "config parameter \"%s\" not found", + CONF_LOG_FILE); + return false; +#endif +#ifdef HAVE_SYSLOG + } else if (strcmp(param->value, "syslog") == 0) { + log_init_syslog(); + return true; +#endif + } else { + out_filename = config_dup_path(CONF_LOG_FILE, error_r); + return out_filename != NULL && + log_init_file(param->line, error_r); + } + } +} + +static void +close_log_files(void) +{ + if (stdout_mode) + return; + +#ifdef HAVE_SYSLOG + if (out_filename == NULL) + closelog(); +#endif +} + +void +log_deinit(void) +{ + close_log_files(); + g_free(out_filename); +} + + +void setup_log_output(bool use_stdout) +{ + fflush(NULL); + if (!use_stdout) { +#ifndef WIN32 + if (out_filename == NULL) + out_fd = open("/dev/null", O_WRONLY); +#endif + + if (out_fd >= 0) { + redirect_logs(out_fd); + close(out_fd); + } + + stdout_mode = false; + log_charset = NULL; + } +} + +int cycle_log_files(void) +{ + int fd; + + if (stdout_mode || out_filename == NULL) + return 0; + assert(out_filename); + + g_debug("Cycling log files...\n"); + close_log_files(); + + fd = open_log_file(); + if (fd < 0) { + g_warning("error re-opening log file: %s\n", out_filename); + return -1; + } + + redirect_logs(fd); + g_debug("Done cycling log files\n"); + return 0; +} diff --git a/src/Log.hxx b/src/Log.hxx new file mode 100644 index 000000000..cfcece104 --- /dev/null +++ b/src/Log.hxx @@ -0,0 +1,54 @@ +/* + * Copyright (C) 2003-2013 The Music Player Daemon Project + * 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., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef MPD_LOG_HXX +#define MPD_LOG_HXX + +#include +#include + +G_GNUC_CONST +static inline GQuark +log_quark(void) +{ + return g_quark_from_static_string("log"); +} + +/** + * Configure a logging destination for daemon startup, before the + * configuration file is read. This allows the daemon to use the + * logging library (and the command line verbose level) before it's + * daemonized. + * + * @param verbose true when the program is started with --verbose + */ +void +log_early_init(bool verbose); + +bool +log_init(bool verbose, bool use_stdout, GError **error_r); + +void +log_deinit(void); + +void setup_log_output(bool use_stdout); + +int cycle_log_files(void); + +#endif /* LOG_H */ diff --git a/src/Main.cxx b/src/Main.cxx index d05a95947..c66943070 100644 --- a/src/Main.cxx +++ b/src/Main.cxx @@ -41,15 +41,15 @@ #include "conf.h" #include "replay_gain_config.h" #include "Idle.hxx" +#include "SignalHandlers.hxx" +#include "Log.hxx" extern "C" { #include "daemon.h" #include "io_thread.h" #include "path.h" #include "stats.h" -#include "sig_handlers.h" #include "audio_config.h" -#include "log.h" #include "pcm_resample.h" #include "decoder_list.h" #include "input_init.h" diff --git a/src/SignalHandlers.cxx b/src/SignalHandlers.cxx new file mode 100644 index 000000000..dfef9bf84 --- /dev/null +++ b/src/SignalHandlers.cxx @@ -0,0 +1,80 @@ +/* + * Copyright (C) 2003-2011 The Music Player Daemon Project + * 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., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "config.h" +#include "SignalHandlers.hxx" + +#ifndef WIN32 + +#include "Log.hxx" +#include "Main.hxx" +#include "event_pipe.h" +#include "mpd_error.h" + +#include + +#include +#include +#include + +static void exit_signal_handler(G_GNUC_UNUSED int signum) +{ + g_main_loop_quit(main_loop); +} + +static void reload_signal_handler(G_GNUC_UNUSED int signum) +{ + event_pipe_emit_fast(PIPE_EVENT_RELOAD); +} + +static void +x_sigaction(int signum, const struct sigaction *act) +{ + if (sigaction(signum, act, NULL) < 0) + MPD_ERROR("sigaction() failed: %s", strerror(errno)); +} + +static void +handle_reload_event(void) +{ + g_debug("got SIGHUP, reopening log files"); + cycle_log_files(); +} + +#endif + +void initSigHandlers(void) +{ +#ifndef WIN32 + struct sigaction sa; + + sa.sa_flags = 0; + sigemptyset(&sa.sa_mask); + sa.sa_handler = SIG_IGN; + x_sigaction(SIGPIPE, &sa); + + sa.sa_handler = exit_signal_handler; + x_sigaction(SIGINT, &sa); + x_sigaction(SIGTERM, &sa); + + event_pipe_register(PIPE_EVENT_RELOAD, handle_reload_event); + sa.sa_handler = reload_signal_handler; + x_sigaction(SIGHUP, &sa); +#endif +} diff --git a/src/SignalHandlers.hxx b/src/SignalHandlers.hxx new file mode 100644 index 000000000..99f347fb0 --- /dev/null +++ b/src/SignalHandlers.hxx @@ -0,0 +1,25 @@ +/* + * Copyright (C) 2003-2013 The Music Player Daemon Project + * 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., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef MPD_SIGNAL_HANDLERS_HXX +#define MPD_SIGNAL_HANDLERS_HXX + +void initSigHandlers(void); + +#endif diff --git a/src/log.c b/src/log.c deleted file mode 100644 index 2d3c7cafd..000000000 --- a/src/log.c +++ /dev/null @@ -1,341 +0,0 @@ -/* - * Copyright (C) 2003-2011 The Music Player Daemon Project - * 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., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#include "config.h" -#include "log.h" -#include "conf.h" -#include "utils.h" -#include "fd_util.h" -#include "mpd_error.h" - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#ifdef HAVE_SYSLOG -#include -#endif - -#undef G_LOG_DOMAIN -#define G_LOG_DOMAIN "log" - -#define LOG_LEVEL_SECURE G_LOG_LEVEL_INFO - -#define LOG_DATE_BUF_SIZE 16 -#define LOG_DATE_LEN (LOG_DATE_BUF_SIZE - 1) - -static GLogLevelFlags log_threshold = G_LOG_LEVEL_MESSAGE; - -static const char *log_charset; - -static bool stdout_mode = true; -static int out_fd; -static char *out_filename; - -static void redirect_logs(int fd) -{ - assert(fd >= 0); - if (dup2(fd, STDOUT_FILENO) < 0) - MPD_ERROR("problems dup2 stdout : %s\n", strerror(errno)); - if (dup2(fd, STDERR_FILENO) < 0) - MPD_ERROR("problems dup2 stderr : %s\n", strerror(errno)); -} - -static const char *log_date(void) -{ - static char buf[LOG_DATE_BUF_SIZE]; - time_t t = time(NULL); - strftime(buf, LOG_DATE_BUF_SIZE, "%b %d %H:%M : ", localtime(&t)); - return buf; -} - -/** - * 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; -} - -static void -file_log_func(const gchar *log_domain, - GLogLevelFlags log_level, - const gchar *message, G_GNUC_UNUSED gpointer user_data) -{ - char *converted; - - if (log_level > log_threshold) - return; - - 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; - - if (log_domain == NULL) - log_domain = ""; - - fprintf(stderr, "%s%s%s%.*s\n", - stdout_mode ? "" : log_date(), - log_domain, *log_domain == 0 ? "" : ": ", - chomp_length(message), message); - - g_free(converted); -} - -static void -log_init_stdout(void) -{ - g_log_set_default_handler(file_log_func, NULL); -} - -static int -open_log_file(void) -{ - assert(out_filename != NULL); - - return open_cloexec(out_filename, O_CREAT | O_WRONLY | O_APPEND, 0666); -} - -static bool -log_init_file(unsigned line, GError **error_r) -{ - assert(out_filename != NULL); - - out_fd = open_log_file(); - if (out_fd < 0) { - g_set_error(error_r, log_quark(), errno, - "failed to open log file \"%s\" (config line %u): %s", - out_filename, line, g_strerror(errno)); - return false; - } - - g_log_set_default_handler(file_log_func, NULL); - return true; -} - -#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 -syslog_log_func(const gchar *log_domain, - GLogLevelFlags log_level, const gchar *message, - G_GNUC_UNUSED gpointer user_data) -{ - if (stdout_mode) { - /* fall back to the file log function during - startup */ - file_log_func(log_domain, log_level, - message, user_data); - return; - } - - if (log_level > log_threshold) - return; - - if (log_domain == NULL) - log_domain = ""; - - syslog(glib_to_syslog_level(log_level), "%s%s%.*s", - log_domain, *log_domain == 0 ? "" : ": ", - chomp_length(message), message); -} - -static void -log_init_syslog(void) -{ - assert(out_filename == NULL); - - openlog(PACKAGE, 0, LOG_DAEMON); - g_log_set_default_handler(syslog_log_func, NULL); -} - -#endif - -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; - else { - MPD_ERROR("unknown log level \"%s\" at line %u\n", - value, line); - return G_LOG_LEVEL_MESSAGE; - } -} - -void -log_early_init(bool verbose) -{ - if (verbose) - log_threshold = G_LOG_LEVEL_DEBUG; - - log_init_stdout(); -} - -bool -log_init(bool verbose, bool use_stdout, GError **error_r) -{ - const struct config_param *param; - - g_get_charset(&log_charset); - - if (verbose) - log_threshold = G_LOG_LEVEL_DEBUG; - else if ((param = config_get_param(CONF_LOG_LEVEL)) != NULL) - log_threshold = parse_log_level(param->value, param->line); - - if (use_stdout) { - log_init_stdout(); - return true; - } else { - param = config_get_param(CONF_LOG_FILE); - if (param == NULL) { -#ifdef HAVE_SYSLOG - /* no configuration: default to syslog (if - available) */ - log_init_syslog(); - return true; -#else - g_set_error(error_r, log_quark(), 0, - "config parameter \"%s\" not found", - CONF_LOG_FILE); - return false; -#endif -#ifdef HAVE_SYSLOG - } else if (strcmp(param->value, "syslog") == 0) { - log_init_syslog(); - return true; -#endif - } else { - out_filename = config_dup_path(CONF_LOG_FILE, error_r); - return out_filename != NULL && - log_init_file(param->line, error_r); - } - } -} - -static void -close_log_files(void) -{ - if (stdout_mode) - return; - -#ifdef HAVE_SYSLOG - if (out_filename == NULL) - closelog(); -#endif -} - -void -log_deinit(void) -{ - close_log_files(); - g_free(out_filename); -} - - -void setup_log_output(bool use_stdout) -{ - fflush(NULL); - if (!use_stdout) { -#ifndef WIN32 - if (out_filename == NULL) - out_fd = open("/dev/null", O_WRONLY); -#endif - - if (out_fd >= 0) { - redirect_logs(out_fd); - close(out_fd); - } - - stdout_mode = false; - log_charset = NULL; - } -} - -int cycle_log_files(void) -{ - int fd; - - if (stdout_mode || out_filename == NULL) - return 0; - assert(out_filename); - - g_debug("Cycling log files...\n"); - close_log_files(); - - fd = open_log_file(); - if (fd < 0) { - g_warning("error re-opening log file: %s\n", out_filename); - return -1; - } - - redirect_logs(fd); - g_debug("Done cycling log files\n"); - return 0; -} diff --git a/src/log.h b/src/log.h deleted file mode 100644 index 683ff3e9f..000000000 --- a/src/log.h +++ /dev/null @@ -1,54 +0,0 @@ -/* - * Copyright (C) 2003-2011 The Music Player Daemon Project - * 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., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#ifndef MPD_LOG_H -#define MPD_LOG_H - -#include -#include - -G_GNUC_CONST -static inline GQuark -log_quark(void) -{ - return g_quark_from_static_string("log"); -} - -/** - * Configure a logging destination for daemon startup, before the - * configuration file is read. This allows the daemon to use the - * logging library (and the command line verbose level) before it's - * daemonized. - * - * @param verbose true when the program is started with --verbose - */ -void -log_early_init(bool verbose); - -bool -log_init(bool verbose, bool use_stdout, GError **error_r); - -void -log_deinit(void); - -void setup_log_output(bool use_stdout); - -int cycle_log_files(void); - -#endif /* LOG_H */ diff --git a/src/sig_handlers.c b/src/sig_handlers.c deleted file mode 100644 index eabca1997..000000000 --- a/src/sig_handlers.c +++ /dev/null @@ -1,80 +0,0 @@ -/* - * Copyright (C) 2003-2011 The Music Player Daemon Project - * 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., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#include "config.h" -#include "sig_handlers.h" - -#ifndef WIN32 - -#include "log.h" -#include "Main.hxx" -#include "event_pipe.h" -#include "mpd_error.h" - -#include - -#include -#include -#include - -static void exit_signal_handler(G_GNUC_UNUSED int signum) -{ - g_main_loop_quit(main_loop); -} - -static void reload_signal_handler(G_GNUC_UNUSED int signum) -{ - event_pipe_emit_fast(PIPE_EVENT_RELOAD); -} - -static void -x_sigaction(int signum, const struct sigaction *act) -{ - if (sigaction(signum, act, NULL) < 0) - MPD_ERROR("sigaction() failed: %s", strerror(errno)); -} - -static void -handle_reload_event(void) -{ - g_debug("got SIGHUP, reopening log files"); - cycle_log_files(); -} - -#endif - -void initSigHandlers(void) -{ -#ifndef WIN32 - struct sigaction sa; - - sa.sa_flags = 0; - sigemptyset(&sa.sa_mask); - sa.sa_handler = SIG_IGN; - x_sigaction(SIGPIPE, &sa); - - sa.sa_handler = exit_signal_handler; - x_sigaction(SIGINT, &sa); - x_sigaction(SIGTERM, &sa); - - event_pipe_register(PIPE_EVENT_RELOAD, handle_reload_event); - sa.sa_handler = reload_signal_handler; - x_sigaction(SIGHUP, &sa); -#endif -} diff --git a/src/sig_handlers.h b/src/sig_handlers.h deleted file mode 100644 index 32e9bad95..000000000 --- a/src/sig_handlers.h +++ /dev/null @@ -1,25 +0,0 @@ -/* - * Copyright (C) 2003-2011 The Music Player Daemon Project - * 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., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#ifndef MPD_SIG_HANDLERS_H -#define MPD_SIG_HANDLERS_H - -void initSigHandlers(void); - -#endif -- cgit v1.2.3