From c6cb611065ab1260ff07b939a9fafa4027fcc5c8 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Sun, 28 Dec 2008 19:48:53 +0100 Subject: log: moved code to log_init_file() Added log_init_file() and log_init_stdout(), preparing for other logging targets. --- src/log.c | 42 +++++++++++++++++++++++++++--------------- 1 file changed, 27 insertions(+), 15 deletions(-) (limited to 'src/log.c') diff --git a/src/log.c b/src/log.c index 38e0a8f10..fdb470447 100644 --- a/src/log.c +++ b/src/log.c @@ -64,9 +64,9 @@ static const char *log_date(void) } static void -mpd_log_func(const gchar *log_domain, - G_GNUC_UNUSED GLogLevelFlags log_level, - const gchar *message, G_GNUC_UNUSED gpointer user_data) +file_log_func(const gchar *log_domain, + G_GNUC_UNUSED GLogLevelFlags log_level, + const gchar *message, G_GNUC_UNUSED gpointer user_data) { char *converted; @@ -93,6 +93,12 @@ mpd_log_func(const gchar *log_domain, g_free(converted); } +static void +log_init_stdout(void) +{ + g_log_set_default_handler(file_log_func, NULL); +} + static int open_log_file(void) { @@ -101,6 +107,18 @@ open_log_file(void) return open(out_filename, O_CREAT | O_WRONLY | O_APPEND, 0666); } +static void +log_init_file(const char *path, unsigned line) +{ + out_filename = path; + out_fd = open_log_file(); + if (out_fd < 0) + FATAL("problem opening log file \"%s\" (config line %u) for " + "writing\n", path, line); + + g_log_set_default_handler(file_log_func, NULL); +} + static inline GLogLevelFlags parse_log_level(const char *value, unsigned line) { @@ -121,23 +139,17 @@ void log_init(bool verbose, bool use_stdout) g_get_charset(&log_charset); - g_log_set_default_handler(mpd_log_func, NULL); - if (verbose) log_threshold = G_LOG_LEVEL_DEBUG; else if ((param = getConfigParam(CONF_LOG_LEVEL)) != NULL) log_threshold = parse_log_level(param->value, param->line); - if (use_stdout) - return; - - param = parseConfigFilePath(CONF_LOG_FILE, 1); - out_filename = param->value; - - out_fd = open_log_file(); - if (out_fd < 0) - FATAL("problem opening log file \"%s\" (config line %i) for " - "writing\n", param->value, param->line); + if (use_stdout) { + log_init_stdout(); + } else { + param = parseConfigFilePath(CONF_LOG_FILE, 1); + log_init_file(param->value, param->line); + } } void setup_log_output(bool use_stdout) -- cgit v1.2.3