Redirect qt logger

This commit is contained in:
Nicolas Werner 2019-10-27 22:49:49 +01:00
parent 2055c75f8b
commit b1f1cb2b56
2 changed files with 42 additions and 0 deletions

View file

@ -5,14 +5,43 @@
#include "spdlog/sinks/stdout_color_sinks.h" #include "spdlog/sinks/stdout_color_sinks.h"
#include <iostream> #include <iostream>
#include <QString>
#include <QtGlobal>
namespace { namespace {
std::shared_ptr<spdlog::logger> db_logger = nullptr; std::shared_ptr<spdlog::logger> db_logger = nullptr;
std::shared_ptr<spdlog::logger> net_logger = nullptr; std::shared_ptr<spdlog::logger> net_logger = nullptr;
std::shared_ptr<spdlog::logger> crypto_logger = nullptr; std::shared_ptr<spdlog::logger> crypto_logger = nullptr;
std::shared_ptr<spdlog::logger> ui_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_FILE_SIZE = 1024 * 1024 * 6;
constexpr auto MAX_LOG_FILES = 3; 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 { 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)); db_logger = std::make_shared<spdlog::logger>("db", std::begin(sinks), std::end(sinks));
crypto_logger = crypto_logger =
std::make_shared<spdlog::logger>("crypto", std::begin(sinks), std::end(sinks)); 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) { if (nheko::enable_debug_log) {
db_logger->set_level(spdlog::level::trace); db_logger->set_level(spdlog::level::trace);
ui_logger->set_level(spdlog::level::trace); ui_logger->set_level(spdlog::level::trace);
crypto_logger->set_level(spdlog::level::trace); crypto_logger->set_level(spdlog::level::trace);
} }
qInstallMessageHandler(qmlMessageHandler);
} }
std::shared_ptr<spdlog::logger> std::shared_ptr<spdlog::logger>
@ -66,4 +98,11 @@ crypto()
{ {
return crypto_logger; return crypto_logger;
} }
std::shared_ptr<spdlog::logger>
qml()
{
return qml_logger;
} }
}

View file

@ -19,5 +19,8 @@ db();
std::shared_ptr<spdlog::logger> std::shared_ptr<spdlog::logger>
crypto(); crypto();
std::shared_ptr<spdlog::logger>
qml();
extern bool enable_debug_log_from_commandline; extern bool enable_debug_log_from_commandline;
} }