From 4938b7c112d8a77545e993c962de651aee81d9b4 Mon Sep 17 00:00:00 2001 From: Dan Paulat Date: Tue, 21 May 2024 22:40:54 -0500 Subject: [PATCH] Redirect Qt and mbgl logs to spdlog --- external/maplibre-native | 2 +- scwx-qt/source/scwx/qt/main/main.cpp | 43 ++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/external/maplibre-native b/external/maplibre-native index 23792807..23e73b2b 160000 --- a/external/maplibre-native +++ b/external/maplibre-native @@ -1 +1 @@ -Subproject commit 237928075a8391e1c1469f79d74458d566075012 +Subproject commit 23e73b2b99088ab88801df92520fb397bc58d601 diff --git a/scwx-qt/source/scwx/qt/main/main.cpp b/scwx-qt/source/scwx/qt/main/main.cpp index b6dcf5f0..0b4a4f07 100644 --- a/scwx-qt/source/scwx/qt/main/main.cpp +++ b/scwx-qt/source/scwx/qt/main/main.cpp @@ -31,6 +31,9 @@ static const std::string logPrefix_ = "scwx::main"; static const auto logger_ = scwx::util::Logger::Create(logPrefix_); static void OverrideDefaultStyle(const std::vector& args); +static void QtLogMessageHandler(QtMsgType messageType, + const QMessageLogContext& context, + const QString& message); int main(int argc, char* argv[]) { @@ -45,6 +48,9 @@ int main(int argc, char* argv[]) scwx::util::Logger::Initialize(); spdlog::set_level(spdlog::level::debug); + // Install Qt Message Handler + qInstallMessageHandler(&QtLogMessageHandler); + QCoreApplication::setAttribute(Qt::AA_ShareOpenGLContexts, true); QApplication a(argc, argv); @@ -170,3 +176,40 @@ OverrideDefaultStyle([[maybe_unused]] const std::vector& args) } #endif } + +void QtLogMessageHandler(QtMsgType messageType, + const QMessageLogContext& context, + const QString& message) +{ + static const auto qtLogger_ = scwx::util::Logger::Create("qt"); + + static const std::unordered_map + levelMap_ {{QtMsgType::QtDebugMsg, spdlog::level::level_enum::debug}, + {QtMsgType::QtInfoMsg, spdlog::level::level_enum::info}, + {QtMsgType::QtWarningMsg, spdlog::level::level_enum::warn}, + {QtMsgType::QtCriticalMsg, spdlog::level::level_enum::err}, + {QtMsgType::QtFatalMsg, spdlog::level::level_enum::critical}}; + + spdlog::level::level_enum level = spdlog::level::level_enum::info; + auto it = levelMap_.find(messageType); + if (it != levelMap_.cend()) + { + level = it->second; + } + + spdlog::source_loc location {}; + if (context.file != nullptr && context.function != nullptr) + { + location = {context.file, context.line, context.function}; + } + + if (context.category != nullptr) + { + qtLogger_->log( + location, level, "[{}] {}", context.category, message.toStdString()); + } + else + { + qtLogger_->log(location, level, message.toStdString()); + } +}