mirror of
https://github.com/Gnucash/gnucash.git
synced 2025-02-25 18:55:30 -06:00
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
This commit is contained in:
parent
f9863e3ba9
commit
8ebec0de11
@ -39,6 +39,9 @@
|
||||
#include <string.h>
|
||||
#include <sys/time.h>
|
||||
|
||||
#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)
|
||||
{
|
||||
|
@ -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);
|
||||
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user