From b1f1cb2b560aa56d485ba1e326bf111326c7aa74 Mon Sep 17 00:00:00 2001 From: Nicolas Werner Date: Sun, 27 Oct 2019 22:49:49 +0100 Subject: [PATCH] Redirect qt logger --- src/Logging.cpp | 39 +++++++++++++++++++++++++++++++++++++++ src/Logging.h | 3 +++ 2 files changed, 42 insertions(+) diff --git a/src/Logging.cpp b/src/Logging.cpp index 32287582..b5952aeb 100644 --- a/src/Logging.cpp +++ b/src/Logging.cpp @@ -5,14 +5,43 @@ #include "spdlog/sinks/stdout_color_sinks.h" #include +#include +#include + namespace { std::shared_ptr db_logger = nullptr; std::shared_ptr net_logger = nullptr; std::shared_ptr crypto_logger = nullptr; std::shared_ptr ui_logger = nullptr; +std::shared_ptr qml_logger = nullptr; constexpr auto MAX_FILE_SIZE = 1024 * 1024 * 6; constexpr auto MAX_LOG_FILES = 3; + +void +qmlMessageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg) +{ + std::string localMsg = msg.toStdString(); + const char *file = context.file ? context.file : ""; + const char *function = context.function ? context.function : ""; + switch (type) { + case QtDebugMsg: + nhlog::qml()->debug("{} ({}:{}, {})", localMsg, file, context.line, function); + break; + case QtInfoMsg: + nhlog::qml()->info("{} ({}:{}, {})", localMsg, file, context.line, function); + break; + case QtWarningMsg: + nhlog::qml()->warn("{} ({}:{}, {})", localMsg, file, context.line, function); + break; + case QtCriticalMsg: + nhlog::qml()->critical("{} ({}:{}, {})", localMsg, file, context.line, function); + break; + case QtFatalMsg: + nhlog::qml()->critical("{} ({}:{}, {})", localMsg, file, context.line, function); + break; + } +} } namespace nhlog { @@ -35,12 +64,15 @@ init(const std::string &file_path) db_logger = std::make_shared("db", std::begin(sinks), std::end(sinks)); crypto_logger = std::make_shared("crypto", std::begin(sinks), std::end(sinks)); + qml_logger = std::make_shared("qml", std::begin(sinks), std::end(sinks)); if (nheko::enable_debug_log) { db_logger->set_level(spdlog::level::trace); ui_logger->set_level(spdlog::level::trace); crypto_logger->set_level(spdlog::level::trace); } + + qInstallMessageHandler(qmlMessageHandler); } std::shared_ptr @@ -66,4 +98,11 @@ crypto() { return crypto_logger; } + +std::shared_ptr +qml() +{ + return qml_logger; +} } + diff --git a/src/Logging.h b/src/Logging.h index e54f3c3f..f572afae 100644 --- a/src/Logging.h +++ b/src/Logging.h @@ -19,5 +19,8 @@ db(); std::shared_ptr crypto(); +std::shared_ptr +qml(); + extern bool enable_debug_log_from_commandline; }