Skip to content
Snippets Groups Projects
Commit b1f1cb2b authored by Nicolas Werner's avatar Nicolas Werner
Browse files

Redirect qt logger

parent 2055c75f
No related branches found
No related tags found
No related merge requests found
......@@ -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;
}
}
......@@ -19,5 +19,8 @@ db();
std::shared_ptr<spdlog::logger>
crypto();
std::shared_ptr<spdlog::logger>
qml();
extern bool enable_debug_log_from_commandline;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment