From 8ebec0de111519ad8bf034b53a881d67832094a6 Mon Sep 17 00:00:00 2001 From: Joshua Sled Date: Sat, 10 Feb 2007 01:29:29 +0000 Subject: [PATCH] Support for ~/.gnucash/log.conf, a key-value file of logging settings; see comment-doc for qof_log_parse_log_config(...) for the file format. git-svn-id: svn+ssh://svn.gnucash.org/repo/gnucash/trunk@15545 57a11ea4-9604-0410-9ed3-97b8803252fd --- lib/libqof/qof/qoflog.c | 89 +++++++++++++++++++++++++++++++++++++++++ lib/libqof/qof/qoflog.h | 20 +++++++++ src/bin/gnucash-bin.c | 21 +++++----- 3 files changed, 118 insertions(+), 12 deletions(-) diff --git a/lib/libqof/qof/qoflog.c b/lib/libqof/qof/qoflog.c index 8311198258..73aac9e249 100644 --- a/lib/libqof/qof/qoflog.c +++ b/lib/libqof/qof/qoflog.c @@ -39,6 +39,9 @@ #include #include +#undef G_LOG_DOMAIN +#define G_LOG_DOMAIN "qof.log" + #ifndef HAVE_LOCALTIME_R #include "localtime_r.h" #endif @@ -223,6 +226,92 @@ qof_log_prettify (const char *name) return function_buffer; } +void +qof_log_init_filename_special(const char *log_to_filename) +{ + if (g_ascii_strcasecmp("stderr", log_to_filename) == 0) + { + qof_log_set_file(stderr); + } + else if (g_ascii_strcasecmp("stdout", log_to_filename) == 0) + { + qof_log_set_file(stdout); + } + else + { + qof_log_init_filename(log_to_filename); + } +} + +void +qof_log_parse_log_config(const char *filename) +{ + const gchar *levels_group = "levels", *output_group = "output"; + GError *err; + GKeyFile *conf = g_key_file_new(); + + if (!g_key_file_load_from_file(conf, filename, G_KEY_FILE_NONE, &err)) + { + g_warning("unable to parse [%s]: %s", filename, err->message); + g_error_free(err); + return; + } + + g_debug("parsing log config from [%s]", filename); + if (g_key_file_has_group(conf, levels_group)) + { + int num_levels; + int key_idx; + gchar **levels; + + levels = g_key_file_get_keys(conf, levels_group, &num_levels, NULL); + + for (key_idx = 0; key_idx < num_levels && levels[key_idx] != NULL; key_idx++) + { + QofLogLevel level; + gchar *logger_name = NULL, *level_str = NULL; + + logger_name = g_strdup(levels[key_idx]); + level_str = g_key_file_get_string(conf, levels_group, logger_name, NULL); + level = qof_log_level_from_string(level_str); + + g_debug("setting log [%s] to level [%s=%d]", logger_name, level_str, level); + qof_log_set_level(logger_name, level); + + g_free(level_str); + } + g_strfreev(levels); + } + + if (g_key_file_has_group(conf, levels_group)) + { + int num_outputs; + int output_idx; + gchar **outputs; + + outputs = g_key_file_get_keys(conf, output_group, &num_outputs, NULL); + for (output_idx = 0; output_idx < num_outputs && outputs[output_idx] != NULL; output_idx++) + { + gchar *key = outputs[output_idx]; + gchar *value; + + if (g_ascii_strcasecmp("to", key) != 0) + { + g_warning("unknown key [%s] in [outputs], skipping", key); + continue; + } + + value = g_key_file_get_string(conf, output_group, key, NULL); + g_debug("setting [output].to=[%s]", value); + qof_log_init_filename_special(value); + g_free(value); + } + g_strfreev(outputs); + } + + g_key_file_free(conf); +} + gboolean qof_log_check(QofLogModule log_domain, QofLogLevel log_level) { diff --git a/lib/libqof/qof/qoflog.h b/lib/libqof/qof/qoflog.h index 436cce260a..6b1097100e 100644 --- a/lib/libqof/qof/qoflog.h +++ b/lib/libqof/qof/qoflog.h @@ -81,6 +81,26 @@ void qof_log_set_file (FILE *outfile); **/ void qof_log_init_filename (const gchar* logfilename); +/** + * If {@param log_to_filename} is "stderr" or "stdout" (exactly, + * case-insensitive), then those special files are used; otherwise, the + * literal filename as given, as {@link qof_log_init_filename}. + **/ +void qof_log_init_filename_special(const char *log_to_filename); + +/** Parse a log-configuration file. A GKeyFile-format file of the schema:: + * + * [levels] + * # log.ger.path=level + * gnc.engine.sx=debug + * gnc.gui.sx=debug + * gnc.gui.freqspec=debug + * [output] + * # to=["stderr"|"stdout"|filename] + * to=stderr + **/ +void qof_log_parse_log_config(const char *filename); + /** Be nice, close the logfile if possible. */ void qof_log_shutdown (void); diff --git a/src/bin/gnucash-bin.c b/src/bin/gnucash-bin.c index e0bc63e5b8..69e12e23ac 100644 --- a/src/bin/gnucash-bin.c +++ b/src/bin/gnucash-bin.c @@ -461,18 +461,7 @@ gnc_log_init() { if (log_to_filename != NULL) { - if (g_ascii_strcasecmp("stderr", log_to_filename) == 0) - { - qof_log_set_file(stderr); - } - else if (g_ascii_strcasecmp("stdout", log_to_filename) == 0) - { - qof_log_set_file(stdout); - } - else - { - qof_log_init_filename(log_to_filename); - } + qof_log_init_filename_special(log_to_filename); } else { @@ -513,6 +502,14 @@ gnc_log_init() g_strfreev(parts); } } + + { + gchar *log_config_filename; + log_config_filename = gnc_build_dotgnucash_path("log.conf"); + if (g_file_test(log_config_filename, G_FILE_TEST_EXISTS)) + qof_log_parse_log_config(log_config_filename); + g_free(log_config_filename); + } } int