From b1f1cb2b560aa56d485ba1e326bf111326c7aa74 Mon Sep 17 00:00:00 2001
From: Nicolas Werner <nicolas.werner@hotmail.de>
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 322875826..b5952aeb6 100644
--- a/src/Logging.cpp
+++ b/src/Logging.cpp
@@ -5,14 +5,43 @@
 #include "spdlog/sinks/stdout_color_sinks.h"
 #include <iostream>
 
+#include <QString>
+#include <QtGlobal>
+
 namespace {
 std::shared_ptr<spdlog::logger> db_logger     = nullptr;
 std::shared_ptr<spdlog::logger> net_logger    = nullptr;
 std::shared_ptr<spdlog::logger> crypto_logger = nullptr;
 std::shared_ptr<spdlog::logger> ui_logger     = nullptr;
+std::shared_ptr<spdlog::logger> 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<spdlog::logger>("db", std::begin(sinks), std::end(sinks));
         crypto_logger =
           std::make_shared<spdlog::logger>("crypto", std::begin(sinks), std::end(sinks));
+        qml_logger = std::make_shared<spdlog::logger>("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<spdlog::logger>
@@ -66,4 +98,11 @@ crypto()
 {
         return crypto_logger;
 }
+
+std::shared_ptr<spdlog::logger>
+qml()
+{
+        return qml_logger;
+}
 }
+
diff --git a/src/Logging.h b/src/Logging.h
index e54f3c3f3..f572afae0 100644
--- a/src/Logging.h
+++ b/src/Logging.h
@@ -19,5 +19,8 @@ db();
 std::shared_ptr<spdlog::logger>
 crypto();
 
+std::shared_ptr<spdlog::logger>
+qml();
+
 extern bool enable_debug_log_from_commandline;
 }
-- 
GitLab