# HG changeset patch # User Myhailo Danylenko # Date 1263829770 -7200 # Node ID f02e7076ccecf37c35fbe7dc9ec47bdb24e0319b # Parent 552da310b83e441e17ef7d19789b7a2ba4f8ebdf Use option guards for tracelog diff -r 552da310b83e -r f02e7076ccec mcabber/mcabber/main.c --- a/mcabber/mcabber/main.c Mon Jan 18 16:32:02 2010 +0200 +++ b/mcabber/mcabber/main.c Mon Jan 18 17:49:30 2010 +0200 @@ -358,6 +358,7 @@ caps_init(); /* Initialize charset */ scr_InitLocaleCharSet(); + ut_InitDebug(); /* Parsing config file... */ ret = cfg_read_file(configFile, TRUE); @@ -367,10 +368,6 @@ if (ret == -2) exit(EXIT_FAILURE); - optstring = settings_opt_get("tracelog_file"); - if (optstring) - ut_InitDebug(settings_opt_get_int("tracelog_level"), optstring); - /* If no password is stored, we ask for it before entering ncurses mode -- unless the username is unknown. */ if (settings_opt_get("jid") && !settings_opt_get("password")) { diff -r 552da310b83e -r f02e7076ccec mcabber/mcabber/utils.c --- a/mcabber/mcabber/utils.c Mon Jan 18 16:32:02 2010 +0200 +++ b/mcabber/mcabber/utils.c Mon Jan 18 17:49:30 2010 +0200 @@ -27,6 +27,7 @@ #include #include #include +#include #ifdef HAVE_LIBIDN #include @@ -50,6 +51,7 @@ #include "utils.h" #include "logprint.h" +#include "settings.h" static int DebugEnabled; static char *FName; @@ -153,53 +155,77 @@ return TRUE; } -void ut_InitDebug(int level, const char *filename) +static gboolean tracelog_create(void) { FILE *fp; struct stat buf; int err; - if (level < 1) { - DebugEnabled = 0; - FName = NULL; - return; - } - - if (filename) - FName = expand_filename(filename); - else { - FName = getenv("HOME"); - if (!FName) - FName = g_strdup("/tmp/mcabberlog"); - else { - FName = g_strdup_printf("%s/mcabberlog", FName); - } - } - - DebugEnabled = level; - fp = fopen(FName, "a"); if (!fp) { - fprintf(stderr, "ERROR: Cannot open tracelog file\n"); - return; + scr_LogPrint(LPRINT_NORMAL, "ERROR: Cannot open tracelog file: %s!", + strerror(errno)); + return FALSE; } err = fstat(fileno(fp), &buf); if (err || buf.st_uid != geteuid()) { fclose(fp); - DebugEnabled = 0; - FName = NULL; - if (err) { - fprintf(stderr, "ERROR: cannot stat the tracelog file!\n"); - } else { - fprintf(stderr, "ERROR: tracelog file does not belong to you!\n"); - } - return; + if (err) + scr_LogPrint(LPRINT_NORMAL, "ERROR: cannot stat the tracelog file: %s!", + strerror(errno)); + else + scr_LogPrint(LPRINT_NORMAL, "ERROR: tracelog file does not belong to you!"); + return FALSE; } fchmod(fileno(fp), S_IRUSR|S_IWUSR); - fprintf(fp, "New trace log started.\n----------------------\n"); + fputs("New trace log started.\n----------------------\n", fp); fclose(fp); + + return TRUE; +} + +static gchar *tracelog_level_guard(const gchar *key, const gchar *new_value) +{ + int new_level = 0; + if (new_value) + new_level = atoi(new_value); + if (DebugEnabled < 1 && new_level > 0 && FName && !tracelog_create()) + DebugEnabled = 0; + else + DebugEnabled = new_level; + return g_strdup(new_value); +} + +static gchar *tracelog_file_guard(const gchar *key, const gchar *new_value) +{ + gchar *new_fname = NULL; + + if (new_value) + new_fname = expand_filename(new_value); + + if (g_strcmp0(FName, new_fname)) { + g_free(FName); + FName = new_fname; + if (DebugEnabled > 0 && !tracelog_create()) { + g_free(FName); + FName = NULL; + } + } else + g_free(new_fname); + + return g_strdup(new_value); +} + +// ut_InitDebug() +// Installs otpion guards before initial config file parsing. +void ut_InitDebug(void) +{ + DebugEnabled = 0; + FName = NULL; + settings_set_guard("tracelog_level", tracelog_level_guard); + settings_set_guard("tracelog_file", tracelog_file_guard); } void ut_WriteLog(unsigned int flag, const char *data) @@ -210,11 +236,12 @@ ((DebugEnabled == 1) && (flag & LPRINT_LOG))) { FILE *fp = fopen(FName, "a+"); if (!fp) { - scr_LogPrint(LPRINT_NORMAL, "ERROR: Cannot open tracelog file"); + scr_LogPrint(LPRINT_NORMAL, "ERROR: Cannot open tracelog file: %s.", + strerror(errno)); return; } if (fputs(data, fp) == EOF) - scr_LogPrint(LPRINT_NORMAL, "ERROR: Cannot write to tracelog file"); + scr_LogPrint(LPRINT_NORMAL, "ERROR: Cannot write to tracelog file."); fclose(fp); } } diff -r 552da310b83e -r f02e7076ccec mcabber/mcabber/utils.h --- a/mcabber/mcabber/utils.h Mon Jan 18 16:32:02 2010 +0200 +++ b/mcabber/mcabber/utils.h Mon Jan 18 17:49:30 2010 +0200 @@ -23,7 +23,7 @@ void fingerprint_to_hex(const unsigned char *fpr, char hex[49]); gboolean hex_to_fingerprint(const char * hex, char fpr[16]); -void ut_InitDebug(int level, const char *file); +void ut_InitDebug(void); void ut_WriteLog(unsigned int flag, const char *data); char *expand_filename(const char *fname);