diff options
author | Forest <forestix@sonic.net> | 2022-08-23 12:47:52 -0700 |
---|---|---|
committer | Forest <forestix@sonic.net> | 2022-09-10 18:28:44 -0700 |
commit | 80f7683a577357e59ec9ca557b79bbfb03b489e2 (patch) | |
tree | 40bb5e3ea1470fbe1827cdbc305b58ff1105439a /src/main.cpp | |
parent | Bump mtxclient to released version (diff) | |
download | nheko-80f7683a577357e59ec9ca557b79bbfb03b489e2.tar.xz |
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.
Diffstat (limited to '')
-rw-r--r-- | src/main.cpp | 53 |
1 files changed, 45 insertions, 8 deletions
diff --git a/src/main.cpp b/src/main.cpp index 47ebba27..f76d8ca6 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -202,8 +202,24 @@ main(int argc, char *argv[]) QCommandLineParser parser; parser.addHelpOption(); parser.addVersionOption(); - QCommandLineOption debugOption(QStringLiteral("debug"), QStringLiteral("Enable debug output")); + QCommandLineOption debugOption(QStringLiteral("debug"), + QObject::tr("Alias for '--log-level trace'.")); parser.addOption(debugOption); + QCommandLineOption logLevel( + QStringList() << QStringLiteral("l") << QStringLiteral("log-level"), + QObject::tr("Set the global log level, or a comma-separated list of <component>=<level> " + "pairs, or both. For example, to set the default log level to 'warn' but " + "disable logging for the 'ui' component, pass 'warn,ui=off'. " + "levels:{trace,debug,info,warning,error,critical,off} " + "components:{crypto,db,mtx,net,qml,ui}"), + QObject::tr("level")); + parser.addOption(logLevel); + QCommandLineOption logType( + QStringList() << QStringLiteral("L") << QStringLiteral("log-type"), + QObject::tr("Set the log output type. A comma-separated list is allowed. " + "The default is 'file,stderr'. types:{file,stderr,none}"), + QObject::tr("type")); + parser.addOption(logType); // This option is not actually parsed via Qt due to the need to parse it before the app // name is set. It only exists to keep Qt from complaining about the --profile/-p @@ -254,15 +270,36 @@ main(int argc, char *argv[]) } #endif - if (parser.isSet(debugOption)) - nhlog::enable_debug_log_from_commandline = true; - try { - nhlog::init(QStringLiteral("%1/nheko.log") - .arg(QStandardPaths::writableLocation(QStandardPaths::CacheLocation)) - .toStdString()); + QString level; + if (parser.isSet(logLevel)) { + level = parser.value(logLevel); + } else if (parser.isSet(debugOption)) { + level = "trace"; + } else { + level = qEnvironmentVariable("NHEKO_LOG_LEVEL"); + } + + QStringList targets = + (parser.isSet(logType) ? parser.value(logType) + : qEnvironmentVariable("NHEKO_LOG_TYPE", "file,stderr")) + .split(',', Qt::SkipEmptyParts); + targets.removeAll("none"); + bool to_stderr = bool(targets.removeAll("stderr")); + QString path = targets.removeAll("file") + ? QDir(QStandardPaths::writableLocation(QStandardPaths::CacheLocation)) + .filePath("nheko.log") + : QLatin1String(""); + if (!targets.isEmpty()) { + std::cerr << "Invalid log type '" << targets.first().toStdString().c_str() << "'" + << std::endl; + std::exit(1); + } + + nhlog::init(level, path, to_stderr); + } catch (const spdlog::spdlog_ex &ex) { - std::cout << "Log initialization failed: " << ex.what() << std::endl; + std::cerr << "Log initialization failed: " << ex.what() << std::endl; std::exit(1); } |