Redirect Qt and mbgl logs to spdlog

This commit is contained in:
Dan Paulat 2024-05-21 22:40:54 -05:00
parent a76da1caac
commit 4938b7c112
2 changed files with 44 additions and 1 deletions

@ -1 +1 @@
Subproject commit 237928075a8391e1c1469f79d74458d566075012
Subproject commit 23e73b2b99088ab88801df92520fb397bc58d601

View file

@ -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<std::string>& 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<std::string>& 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<QtMsgType, spdlog::level::level_enum>
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());
}
}