mirror of
https://github.com/ciphervance/supercell-wx.git
synced 2025-10-30 21:00:05 +00:00
Redirect Qt and mbgl logs to spdlog
This commit is contained in:
parent
a76da1caac
commit
4938b7c112
2 changed files with 44 additions and 1 deletions
|
|
@ -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());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue