Control logging via command line and environment variables
Nheko is very chatty in its log output, generating log noise (which
complicates diagnostics) and needless disk writes (which affect power
consumption and SSD life). This patch introduces command line options
and environment variables to control log levels and output type.
The old --debug command line option still works, at least for now.
It is overridden by the new command line options when they are used.
Partially addresses #665.
1 files changed, 24 insertions, 10 deletions
diff --git a/src/Logging.cpp b/src/Logging.cpp
index 9ae94f08..cd72e395 100644
--- a/src/Logging.cpp
+++ b/src/Logging.cpp
@@ -6,8 +6,10 @@
#include "Logging.h"
#include "config/nheko.h"
+#include "spdlog/cfg/helpers.h"
#include "spdlog/sinks/rotating_file_sink.h"
#include "spdlog/sinks/stdout_color_sinks.h"
+#include "spdlog/spdlog.h"
#include <iostream>
#include <QString>
@@ -61,19 +63,20 @@ qmlMessageHandler(QtMsgType type, const QMessageLogContext &context, const QStri
}
namespace nhlog {
-bool enable_debug_log_from_commandline = false;
void
-init(const std::string &file_path)
+init(const QString &level, const QString &path, bool to_stderr)
{
- auto file_sink = std::make_shared<spdlog::sinks::rotating_file_sink_mt>(
- file_path, MAX_FILE_SIZE, MAX_LOG_FILES);
-
- auto console_sink = std::make_shared<spdlog::sinks::stderr_color_sink_mt>();
-
std::vector<spdlog::sink_ptr> sinks;
- sinks.push_back(file_sink);
- sinks.push_back(console_sink);
+ 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);
+ }
mtx::utils::log::log()->sinks() = sinks;
net_logger = std::make_shared<spdlog::logger>("net", std::begin(sinks), std::end(sinks));
@@ -82,7 +85,7 @@ init(const std::string &file_path)
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 || enable_debug_log_from_commandline) {
+ 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);
@@ -91,6 +94,17 @@ init(const std::string &file_path)
mtx::utils::log::log()->set_level(spdlog::level::trace);
}
+ 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());
+ }
+
qInstallMessageHandler(qmlMessageHandler);
}
|