Skip to content

Commit 6cb10cf

Browse files
Enable file logging by default (#119)
* Initial plan * feat: enable file logging by default on first launch Agent-Logs-Url: https://github.com/MaximumTrainer/MaximumTrainer_Redux/sessions/2161e8d7-50ea-41f9-82ba-49730ec00d02 Co-authored-by: MaximumTrainer <1376575+MaximumTrainer@users.noreply.github.com> * test: make testConfig_fileLoggingEnabledByDefault exercise loadConfig() with RAII guards Agent-Logs-Url: https://github.com/MaximumTrainer/MaximumTrainer_Redux/sessions/be36561c-c943-47c3-9bd6-fba36b896431 Co-authored-by: MaximumTrainer <1376575+MaximumTrainer@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: MaximumTrainer <1376575+MaximumTrainer@users.noreply.github.com>
1 parent a316008 commit 6cb10cf

4 files changed

Lines changed: 48 additions & 3 deletions

File tree

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,8 @@ You need Qt Linguist to open and generate a new language file (`.qm` file).
300300
## Log Files
301301

302302
MaximumTrainer writes diagnostic messages (network errors, BLE events, OAuth login steps) to a log
303-
file when file logging is enabled. Enable it in **Preferences → Preferences & Profile → Logging**.
303+
file. File logging is **enabled by default** on first launch; you can adjust the level or disable it
304+
in **Preferences → Preferences & Profile → Logging**.
304305

305306
| Platform | Default log file path |
306307
|----------|-----------------------|

src/app/logger.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ void Logger::loadConfig()
141141
const int level = settings.value(QStringLiteral("level"),
142142
static_cast<int>(LogLevel::Info)).toInt();
143143
const bool fileOn = settings.value(QStringLiteral("file_enabled"),
144-
false).toBool();
144+
true).toBool();
145145
const QString fp = settings.value(QStringLiteral("file_path"),
146146
QString()).toString();
147147
settings.endGroup();

src/app/logger.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*
1919
* Configuration is persisted via QSettings (group "logging"):
2020
* logging/level (int, default 2 = Info)
21-
* logging/file_enabled (bool, default false)
21+
* logging/file_enabled (bool, default true)
2222
* logging/file_path (string, default <AppData>/MaximumTrainer.log)
2323
*
2424
* Quick-start:

tests/logger/tst_logger.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <QSettings>
1818
#include <QRegularExpression>
1919
#include <QDir>
20+
#include <QScopeGuard>
2021

2122
#include "../../src/app/logger.h"
2223

@@ -106,6 +107,7 @@ private slots:
106107

107108
// ── Config round-trip ─────────────────────────────────────────────────────
108109
void testConfig_saveAndLoad();
110+
void testConfig_fileLoggingEnabledByDefault();
109111
};
110112

111113
// ─────────────────────────────────────────────────────────────────────────────
@@ -356,6 +358,48 @@ void TstLogger::testConfig_saveAndLoad()
356358
Logger::instance().setLogLevel(saved);
357359
}
358360

361+
void TstLogger::testConfig_fileLoggingEnabledByDefault()
362+
{
363+
// RAII guard: restore Logger's file-logging state unconditionally so that
364+
// a QVERIFY failure in this test cannot leave the Logger in a broken state
365+
// and cause subsequent tests to behave unexpectedly.
366+
const bool savedEnabled = Logger::instance().isFileLoggingEnabled();
367+
const QString savedPath = Logger::instance().logFilePath();
368+
auto loggerGuard = qScopeGuard([&] {
369+
Logger::instance().setFileLogging(savedEnabled, savedPath);
370+
});
371+
372+
// RAII guard: remove (or restore) the logging/file_enabled QSettings key.
373+
// This simulates a fresh install where no user preference has been saved.
374+
QSettings settings;
375+
settings.beginGroup(QStringLiteral("logging"));
376+
const QVariant prevValue = settings.value(QStringLiteral("file_enabled"));
377+
settings.remove(QStringLiteral("file_enabled"));
378+
settings.endGroup();
379+
settings.sync();
380+
381+
auto settingsGuard = qScopeGuard([&] {
382+
QSettings s;
383+
s.beginGroup(QStringLiteral("logging"));
384+
if (prevValue.isValid())
385+
s.setValue(QStringLiteral("file_enabled"), prevValue);
386+
else
387+
s.remove(QStringLiteral("file_enabled"));
388+
s.endGroup();
389+
s.sync();
390+
});
391+
392+
// Start from a known-disabled state so we can detect whether
393+
// loadConfig() actually enables file logging.
394+
Logger::instance().setFileLogging(false);
395+
396+
// Exercise the real loadConfig() path — it must default to enabled.
397+
Logger::instance().loadConfig();
398+
399+
QVERIFY2(Logger::instance().isFileLoggingEnabled(),
400+
"loadConfig() must enable file logging when file_enabled is absent from settings");
401+
}
402+
359403
// ─────────────────────────────────────────────────────────────────────────────
360404
// Test runner
361405
// ─────────────────────────────────────────────────────────────────────────────

0 commit comments

Comments
 (0)