2021-03-05 02:35:15 +03:00
|
|
|
// SPDX-FileCopyrightText: 2021 Nheko Contributors
|
2022-01-01 06:57:53 +03:00
|
|
|
// SPDX-FileCopyrightText: 2022 Nheko Contributors
|
2021-03-05 02:35:15 +03:00
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
2018-07-17 16:37:25 +03:00
|
|
|
#include "Logging.h"
|
2018-09-30 14:33:54 +03:00
|
|
|
#include "config/nheko.h"
|
2018-06-09 16:03:14 +03:00
|
|
|
|
2022-08-23 22:47:52 +03:00
|
|
|
#include "spdlog/cfg/helpers.h"
|
2018-08-08 14:15:14 +03:00
|
|
|
#include "spdlog/sinks/rotating_file_sink.h"
|
2018-09-04 13:17:21 +03:00
|
|
|
#include "spdlog/sinks/stdout_color_sinks.h"
|
2022-08-23 22:47:52 +03:00
|
|
|
#include "spdlog/spdlog.h"
|
2018-06-09 16:03:14 +03:00
|
|
|
#include <iostream>
|
|
|
|
|
2019-10-28 00:49:49 +03:00
|
|
|
#include <QString>
|
|
|
|
#include <QtGlobal>
|
|
|
|
|
2022-05-08 02:35:05 +03:00
|
|
|
#include <mtx/log.hpp>
|
|
|
|
|
2018-06-09 16:03:14 +03:00
|
|
|
namespace {
|
2018-06-10 20:03:45 +03:00
|
|
|
std::shared_ptr<spdlog::logger> db_logger = nullptr;
|
|
|
|
std::shared_ptr<spdlog::logger> net_logger = nullptr;
|
|
|
|
std::shared_ptr<spdlog::logger> crypto_logger = nullptr;
|
2018-06-14 02:28:35 +03:00
|
|
|
std::shared_ptr<spdlog::logger> ui_logger = nullptr;
|
2019-10-28 00:49:49 +03:00
|
|
|
std::shared_ptr<spdlog::logger> qml_logger = nullptr;
|
2018-06-09 16:03:14 +03:00
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
std::string localMsg = msg.toStdString();
|
|
|
|
const char *file = context.file ? context.file : "";
|
|
|
|
const char *function = context.function ? context.function : "";
|
|
|
|
|
|
|
|
if (
|
|
|
|
// The default style has the point size set. If you use pixel size anywhere, you get
|
|
|
|
// that warning, which is useless, since sometimes you need the pixel size to match the
|
|
|
|
// text to the size of the outer element for example. This is done in the avatar and
|
|
|
|
// without that you get one warning for every Avatar displayed, which is stupid!
|
|
|
|
msg.endsWith(QStringLiteral("Both point size and pixel size set. Using pixel size.")))
|
|
|
|
return;
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2019-10-28 00:49:49 +03:00
|
|
|
}
|
2018-06-09 16:03:14 +03:00
|
|
|
}
|
|
|
|
|
2018-06-14 02:28:35 +03:00
|
|
|
namespace nhlog {
|
2019-08-26 02:24:56 +03:00
|
|
|
|
2018-06-09 16:03:14 +03:00
|
|
|
void
|
2022-08-23 22:47:52 +03:00
|
|
|
init(const QString &level, const QString &path, bool to_stderr)
|
2018-06-09 16:03:14 +03:00
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
std::vector<spdlog::sink_ptr> sinks;
|
2022-08-23 22:47:52 +03:00
|
|
|
if (!path.isEmpty()) {
|
|
|
|
auto file_sink = std::make_shared<spdlog::sinks::rotating_file_sink_mt>(
|
|
|
|
path.toStdString(), MAX_FILE_SIZE, MAX_LOG_FILES);
|
|
|
|
sinks.push_back(file_sink);
|
|
|
|
}
|
|
|
|
if (to_stderr) {
|
|
|
|
auto console_sink = std::make_shared<spdlog::sinks::stderr_color_sink_mt>();
|
|
|
|
sinks.push_back(console_sink);
|
|
|
|
}
|
2021-09-18 01:22:33 +03:00
|
|
|
|
2022-05-08 02:35:05 +03:00
|
|
|
mtx::utils::log::log()->sinks() = sinks;
|
2021-09-18 01:22:33 +03:00
|
|
|
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));
|
|
|
|
qml_logger = std::make_shared<spdlog::logger>("qml", std::begin(sinks), std::end(sinks));
|
|
|
|
|
2022-08-23 22:47:52 +03:00
|
|
|
if (nheko::enable_debug_log) {
|
2021-09-18 01:22:33 +03:00
|
|
|
db_logger->set_level(spdlog::level::trace);
|
|
|
|
ui_logger->set_level(spdlog::level::trace);
|
|
|
|
crypto_logger->set_level(spdlog::level::trace);
|
|
|
|
net_logger->set_level(spdlog::level::trace);
|
|
|
|
qml_logger->set_level(spdlog::level::trace);
|
2022-05-08 02:35:05 +03:00
|
|
|
mtx::utils::log::log()->set_level(spdlog::level::trace);
|
2021-09-18 01:22:33 +03:00
|
|
|
}
|
|
|
|
|
2022-08-23 22:47:52 +03:00
|
|
|
spdlog::register_logger(net_logger);
|
|
|
|
spdlog::register_logger(ui_logger);
|
|
|
|
spdlog::register_logger(db_logger);
|
|
|
|
spdlog::register_logger(crypto_logger);
|
|
|
|
spdlog::register_logger(qml_logger);
|
|
|
|
// We assume the mtxclient library will register its own logger.
|
|
|
|
|
|
|
|
if (!level.isEmpty()) {
|
|
|
|
spdlog::cfg::helpers::load_levels(level.toStdString());
|
|
|
|
}
|
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
qInstallMessageHandler(qmlMessageHandler);
|
2018-06-09 16:03:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<spdlog::logger>
|
2018-06-14 02:28:35 +03:00
|
|
|
ui()
|
2018-06-09 16:03:14 +03:00
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
return ui_logger;
|
2018-06-09 16:03:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<spdlog::logger>
|
|
|
|
net()
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
return net_logger;
|
2018-06-09 16:03:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<spdlog::logger>
|
|
|
|
db()
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
return db_logger;
|
2018-06-09 16:03:14 +03:00
|
|
|
}
|
2018-06-10 20:03:45 +03:00
|
|
|
|
|
|
|
std::shared_ptr<spdlog::logger>
|
|
|
|
crypto()
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
return crypto_logger;
|
2018-06-10 20:03:45 +03:00
|
|
|
}
|
2019-10-28 00:49:49 +03:00
|
|
|
|
|
|
|
std::shared_ptr<spdlog::logger>
|
|
|
|
qml()
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
return qml_logger;
|
2019-10-28 00:49:49 +03:00
|
|
|
}
|
2018-06-09 16:03:14 +03:00
|
|
|
}
|