From 4af7ea0f0a0f06d384abdf321c804e059ddc992a Mon Sep 17 00:00:00 2001 From: Geert Janssens Date: Fri, 29 May 2020 14:54:05 +0200 Subject: [PATCH] Fix log initialization - Log related options weren't parsed properly yet - Rewrite gnc_log_init to use c++ features --- gnucash/gnucash-core-app.cpp | 103 ++++++++++++++++------------------- gnucash/gnucash-core-app.hpp | 5 +- 2 files changed, 51 insertions(+), 57 deletions(-) diff --git a/gnucash/gnucash-core-app.cpp b/gnucash/gnucash-core-app.cpp index 20bf8b08a4..3e6f415c96 100644 --- a/gnucash/gnucash-core-app.cpp +++ b/gnucash/gnucash-core-app.cpp @@ -47,6 +47,7 @@ extern "C" { #include } +#include #include #include #include @@ -348,60 +349,65 @@ load_user_config(void) static void -gnc_log_init (gchar **log_flags, gchar *log_to_filename) +gnc_log_init (std::vector &log_flags, std::string &log_to_filename) { - if (log_to_filename != NULL) - qof_log_init_filename_special(log_to_filename); + if (!log_to_filename.empty()) + { +#ifdef __MINGW64__ + auto *utf8_filename = g_utf16_to_utf8 (log_to_filename, -1, NULL, NULL, NULL); +#else + auto utf8_filename = log_to_filename.c_str(); +#endif + + qof_log_init_filename_special (utf8_filename); + +#ifdef __MINGW64__ + g_free (utf8_filename); +#endif + } else { /* initialize logging to our file. */ - gchar *tracefilename; - tracefilename = g_build_filename(g_get_tmp_dir(), "gnucash.trace", - (gchar *)NULL); - qof_log_init_filename(tracefilename); - g_free(tracefilename); + auto tracefilename = g_build_filename (g_get_tmp_dir(), "gnucash.trace", + (gchar *)NULL); + qof_log_init_filename (tracefilename); + g_free (tracefilename); } // set a reasonable default. qof_log_set_default(QOF_LOG_WARNING); - gnc_log_default(); if (gnc_prefs_is_debugging_enabled()) { - qof_log_set_level("", QOF_LOG_INFO); - qof_log_set_level("qof", QOF_LOG_INFO); - qof_log_set_level("gnc", QOF_LOG_INFO); + qof_log_set_level ("", QOF_LOG_INFO); + qof_log_set_level ("qof", QOF_LOG_INFO); + qof_log_set_level ("gnc", QOF_LOG_INFO); } - { - gchar *log_config_filename; - log_config_filename = g_build_filename (gnc_userconfig_dir (), - "log.conf", (char *)NULL); - if (g_file_test(log_config_filename, G_FILE_TEST_EXISTS)) - qof_log_parse_log_config(log_config_filename); - g_free(log_config_filename); - } + auto log_config_filename = g_build_filename (gnc_userconfig_dir (), + "log.conf", (char *)NULL); + if (g_file_test (log_config_filename, G_FILE_TEST_EXISTS)) + qof_log_parse_log_config (log_config_filename); + g_free (log_config_filename); - if (log_flags != NULL) + if (!log_flags.empty()) { - int i = 0; - for (; log_flags[i] != NULL; i++) + for (auto log_flag : log_flags) { - QofLogLevel level; - gchar **parts = NULL; - - gchar *log_opt = log_flags[i]; - parts = g_strsplit(log_opt, "=", 2); - if (parts == NULL || parts[0] == NULL || parts[1] == NULL) + if (log_flag.empty () || + log_flag[0] == '=' || + log_flag[log_flag.length () - 1] == '=') { - g_warning("string [%s] not parseable", log_opt); + g_warning ("string [%s] not parseable", log_flag.c_str()); continue; } - level = qof_log_level_from_string(parts[1]); - qof_log_set_level(parts[0], level); - g_strfreev(parts); + std::vector split_flag; + boost::split (split_flag, log_flag, [](char c){return c == '=';}); + + auto level = qof_log_level_from_string (split_flag[1].c_str()); + qof_log_set_level (split_flag[0].c_str(), level); } } } @@ -567,11 +573,6 @@ Gnucash::CoreApp::CoreApp (const char* app_name) void Gnucash::CoreApp::parse_command_line (int argc, char **argv) { -#ifdef __MINGW64__ - wchar_t *tmp_log_to_filename = NULL; -#else - char *tmp_log_to_filename = NULL; -#endif try { bpo::store (bpo::command_line_parser (argc, argv). @@ -586,16 +587,6 @@ Gnucash::CoreApp::parse_command_line (int argc, char **argv) exit(1); } - if (tmp_log_to_filename != NULL) - { -#ifdef __MINGW64__ - log_to_filename = g_utf16_to_utf8(tmp_log_to_filename, -1, NULL, NULL, NULL); - g_free (tmp_log_to_filename); -#else - log_to_filename = tmp_log_to_filename; -#endif - } - if (m_opt_map["version"].as()) { bl::format rel_fmt (bl::translate ("GnuCash {1}")); @@ -623,6 +614,14 @@ Gnucash::CoreApp::parse_command_line (int argc, char **argv) gnc_gsettings_set_prefix (m_opt_map["gsettings-prefix"]. as().c_str()); + if (m_opt_map.count ("log")) + m_log_flags = m_opt_map["log"]. + as>(); + + if (m_opt_map.count ("logto")) + m_log_to_filename = m_opt_map["logto"]. + as().c_str(); + if (args_remaining) file_to_load = args_remaining[0]; } @@ -631,12 +630,6 @@ Gnucash::CoreApp::parse_command_line (int argc, char **argv) void Gnucash::CoreApp::add_common_program_options (void) { - #ifdef __MINGW64__ - wchar_t *tmp_log_to_filename = NULL; - #else - char *tmp_log_to_filename = NULL; - #endif - bpo::options_description common_options(_("Common Options")); common_options.add_options() ("help,h", bpo::bool_switch(), @@ -669,7 +662,7 @@ Gnucash::CoreApp::start (void) if (userdata_migration_msg) g_print("\n\n%s\n", userdata_migration_msg); - gnc_log_init (log_flags, log_to_filename); + gnc_log_init (m_log_flags, m_log_to_filename); gnc_engine_init (0, NULL); /* Write some locale details to the log to simplify debugging */ diff --git a/gnucash/gnucash-core-app.hpp b/gnucash/gnucash-core-app.hpp index 57b610fcba..8c65443ba6 100644 --- a/gnucash/gnucash-core-app.hpp +++ b/gnucash/gnucash-core-app.hpp @@ -54,11 +54,12 @@ private: void add_common_program_options (void); /* Command-line option variables */ + std::string m_log_to_filename; + std::vector m_log_flags; + int gnucash_show_version = 0; int debugging = 0; int extra = 0; - char **log_flags; - char *log_to_filename = nullptr; int nofile = 0; const char *gsettings_prefix = nullptr; const char *add_quotes_file = nullptr;