diff --git a/Logger.cpp b/Logger.cpp index 6c92ca407..00e5597b1 100644 --- a/Logger.cpp +++ b/Logger.cpp @@ -18,7 +18,6 @@ #include #include #include -#include #include #include #include diff --git a/WSJTXLogging.cpp b/WSJTXLogging.cpp index ffea213c8..bb6171609 100644 --- a/WSJTXLogging.cpp +++ b/WSJTXLogging.cpp @@ -10,7 +10,6 @@ #include #include #include -#include #include #include #include @@ -19,6 +18,7 @@ #include #include +#include #include #include #include @@ -60,12 +60,62 @@ namespace //throw; } }; + + // Reroute Qt messages to the system logger + void qt_log_handler (QtMsgType type, QMessageLogContext const& context, QString const& msg) + { + // Convert Qt message types to logger severities + auto severity = trivial::trace; + switch (type) + { + case QtDebugMsg: severity = trivial::debug; break; + case QtInfoMsg: severity = trivial::info; break; + case QtWarningMsg: severity = trivial::warning; break; + case QtCriticalMsg: severity = trivial::error; break; + case QtFatalMsg: severity = trivial::fatal; break; + } + // Map non-default Qt categories to logger channels, Qt logger + // context is mapped to the appropriate logger attributes. + auto log = sys::get (); + std::string file; + std::string function; + if (context.file) + { + file = context.file; + } + if (context.function) + { + function = context.function; + } + if (!context.category || !qstrcmp (context.category, "default")) + { + BOOST_LOG_SEV (log, severity) + << boost::log::add_value ("Line", context.line) + << boost::log::add_value ("File", file) + << boost::log::add_value ("Function", function) + << msg.toStdString (); + } + else + { + BOOST_LOG_CHANNEL_SEV (log, std::string {context.category}, severity) + << boost::log::add_value ("Line", context.line) + << boost::log::add_value ("File", file) + << boost::log::add_value ("Function", function) + << msg.toStdString (); + } + if (QtFatalMsg == type) + { + // bail out + throw std::runtime_error {"Fatal Qt Error"}; + } + } } WSJTXLogging::WSJTXLogging () { + auto core = logging::core::get (); // Catch relevant exceptions from logging. - logging::core::get ()->set_exception_handler + core->set_exception_handler ( logging::make_exception_handler (exception_handler {}) ); @@ -120,7 +170,6 @@ WSJTXLogging::WSJTXLogging () // Default log file location. QDir app_data {QStandardPaths::writableLocation (QStandardPaths::AppLocalDataLocation)}; Logger::init (); // Basic setup of attributes - auto core = logging::core::get (); // // Sink intended for general use that passes everything above @@ -175,70 +224,17 @@ WSJTXLogging::WSJTXLogging () ); core->add_sink (sys_sink); - -#if !defined (NDEBUG) && defined (Q_OS_WIN) - // auto windbg_sink = boost::make_shared> (); - // windbg_sink->set_filter (trivial::severity >= trivial::trace && expr::is_debugger_present ()); - // core->add_sink (windbg_sink); -#endif } + // Indicate start of logging LOG_INFO ("Log Start"); + ::qInstallMessageHandler (&qt_log_handler); } WSJTXLogging::~WSJTXLogging () { LOG_INFO ("Log Finish"); - auto lg = logging::core::get (); - lg->flush (); - lg->remove_all_sinks (); -} - -// Reroute Qt messages to the system logger -void WSJTXLogging::qt_log_handler (QtMsgType type, QMessageLogContext const& context, QString const& msg) -{ - // Convert Qt message types to logger severities - auto severity = trivial::trace; - switch (type) - { - case QtDebugMsg: severity = trivial::debug; break; - case QtInfoMsg: severity = trivial::info; break; - case QtWarningMsg: severity = trivial::warning; break; - case QtCriticalMsg: severity = trivial::error; break; - case QtFatalMsg: severity = trivial::fatal; break; - } - // Map non-default Qt categories to logger channels, Qt logger - // context is mapped to the appropriate logger attributes. - auto log = sys::get (); - std::string file; - std::string function; - if (context.file) - { - file = context.file; - } - if (context.function) - { - function = context.function; - } - if (!context.category || !qstrcmp (context.category, "default")) - { - BOOST_LOG_SEV (log, severity) - << boost::log::add_value ("Line", context.line) - << boost::log::add_value ("File", file) - << boost::log::add_value ("Function", function) - << msg.toStdString (); - } - else - { - BOOST_LOG_CHANNEL_SEV (log, std::string {context.category}, severity) - << boost::log::add_value ("Line", context.line) - << boost::log::add_value ("File", file) - << boost::log::add_value ("Function", function) - << msg.toStdString (); - } - if (QtFatalMsg == type) - { - // bail out - throw std::runtime_error {"Fatal Qt Error"}; - } + auto core = logging::core::get (); + core->flush (); + core->remove_all_sinks (); } diff --git a/WSJTXLogging.hpp b/WSJTXLogging.hpp index d36e9afd2..4ecedbf78 100644 --- a/WSJTXLogging.hpp +++ b/WSJTXLogging.hpp @@ -1,10 +1,6 @@ #ifndef WSJTX_LOGGING_HPP__ #define WSJTX_LOGGING_HPP__ -#include - -class QString; - // // Class WSJTXLogging - wraps application specific logging // @@ -13,14 +9,6 @@ class WSJTXLogging final public: explicit WSJTXLogging (); ~WSJTXLogging (); - - // - // Install this as the Qt message handler (qInstallMessageHandler) - // to integrate Qt messages. This handler can be installed at any - // time, it does not rely on an instance of WSJTXLogging existing, - // so logging occurring before the logging sinks, filters, and - // formatters, etc, are established can take place. - static void qt_log_handler (QtMsgType type, QMessageLogContext const& context, QString const&); }; #endif diff --git a/main.cpp b/main.cpp index 9e2403de1..c6dd0ccbf 100644 --- a/main.cpp +++ b/main.cpp @@ -104,7 +104,6 @@ namespace int main(int argc, char *argv[]) { - ::qInstallMessageHandler (&WSJTXLogging::qt_log_handler); init_random_seed (); // make the Qt type magic happen @@ -211,7 +210,9 @@ int main(int argc, char *argv[]) multiple = true; } - // now we have the application name we can open the settings + // now we have the application name we can open the logging and settings + WSJTXLogging lg; + LOG_INFO (program_title (revision ()) << " - Program startup"); MultiSettings multi_settings {parser.value (cfg_option)}; // find the temporary files path @@ -247,9 +248,6 @@ int main(int argc, char *argv[]) } } - WSJTXLogging lg; - LOG_INFO (program_title (revision ()) << " - Program startup"); - // load UI translations L10nLoader l10n {&a, locale, parser.value (lang_option)};