matrixion/src/Logging.cpp

116 lines
3.6 KiB
C++
Raw Normal View History

2018-07-17 16:37:25 +03:00
#include "Logging.h"
2018-09-30 14:33:54 +03:00
#include "config/nheko.h"
#include "spdlog/sinks/rotating_file_sink.h"
2018-09-04 13:17:21 +03:00
#include "spdlog/sinks/stdout_color_sinks.h"
#include <iostream>
2019-10-28 00:49:49 +03:00
#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;
2019-10-28 00:49:49 +03:00
std::shared_ptr<spdlog::logger> qml_logger = nullptr;
constexpr auto MAX_FILE_SIZE = 1024 * 1024 * 6;
constexpr auto MAX_LOG_FILES = 3;
2019-10-28 00:49:49 +03:00
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 : "";
// Surpress binding wrning for now, as we can't set restore mode to keep compat with qt 5.10
if (msg.endsWith(
"QML Binding: Not restoring previous value because restoreMode has not been set.This "
"behavior is deprecated.In Qt < 6.0 the default is Binding.RestoreBinding.In Qt >= "
"6.0 the default is Binding.RestoreBindingOrValue.") || msg.endsWith("Both point size and pixel size set. Using pixel size."))
return;
2019-10-28 00:49:49 +03:00
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 {
bool enable_debug_log_from_commandline = false;
void
init(const std::string &file_path)
{
auto file_sink = std::make_shared<spdlog::sinks::rotating_file_sink_mt>(
file_path, MAX_FILE_SIZE, MAX_LOG_FILES);
2018-09-04 13:17:21 +03:00
auto console_sink = std::make_shared<spdlog::sinks::stdout_color_sink_mt>();
std::vector<spdlog::sink_ptr> sinks;
sinks.push_back(file_sink);
sinks.push_back(console_sink);
net_logger = std::make_shared<spdlog::logger>("net", std::begin(sinks), std::end(sinks));
ui_logger = std::make_shared<spdlog::logger>("ui", std::begin(sinks), std::end(sinks));
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));
2019-10-28 00:49:49 +03:00
qml_logger = std::make_shared<spdlog::logger>("qml", std::begin(sinks), std::end(sinks));
2018-09-30 14:33:54 +03:00
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);
}
2019-10-28 00:49:49 +03:00
qInstallMessageHandler(qmlMessageHandler);
}
std::shared_ptr<spdlog::logger>
ui()
{
return ui_logger;
}
std::shared_ptr<spdlog::logger>
net()
{
return net_logger;
}
std::shared_ptr<spdlog::logger>
db()
{
return db_logger;
}
std::shared_ptr<spdlog::logger>
crypto()
{
return crypto_logger;
}
2019-10-28 00:49:49 +03:00
std::shared_ptr<spdlog::logger>
qml()
{
return qml_logger;
}
}