Clean up logging initialization and setup

This commit is contained in:
Bill Somerville 2020-11-28 13:49:35 +00:00
parent 462ef827c9
commit 3352220715
No known key found for this signature in database
GPG Key ID: D864B06D1E81618F
4 changed files with 60 additions and 79 deletions

View File

@ -18,7 +18,6 @@
#include <boost/log/utility/setup/settings.hpp>
#include <boost/log/sinks/sync_frontend.hpp>
#include <boost/log/sinks/text_ostream_backend.hpp>
#include <boost/log/sinks/debug_output_backend.hpp>
#include <boost/log/support/date_time.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/make_shared.hpp>

View File

@ -10,7 +10,6 @@
#include <boost/log/trivial.hpp>
#include <boost/log/sinks/text_file_backend.hpp>
#include <boost/log/sinks/async_frontend.hpp>
#include <boost/log/sinks/debug_output_backend.hpp>
#include <boost/log/expressions.hpp>
#include <boost/log/expressions/formatters/date_time.hpp>
#include <boost/log/expressions/predicates/channel_severity_filter.hpp>
@ -19,6 +18,7 @@
#include <boost/date_time/gregorian/greg_day.hpp>
#include <boost/container/flat_map.hpp>
#include <QtGlobal>
#include <QDir>
#include <QFile>
#include <QTextStream>
@ -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<std::runtime_error, std::logic_error> (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<sinks::synchronous_sink<sinks::debug_output_backend>> ();
// 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 ();
}

View File

@ -1,10 +1,6 @@
#ifndef WSJTX_LOGGING_HPP__
#define WSJTX_LOGGING_HPP__
#include <QtGlobal>
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

View File

@ -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)};