Skip to content

Commit

Permalink
Merge pull request #13684 from daschuer/log-max-file-size
Browse files Browse the repository at this point in the history
Limit mixxx.log size
  • Loading branch information
JoergAtGithub authored Dec 15, 2024
2 parents ac5b48c + 89e955b commit 42d509e
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/util/cmdlineargs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ CmdlineArgs::CmdlineArgs()
m_parseForUserFeedbackRequired(false),
m_logLevel(mixxx::kLogLevelDefault),
m_logFlushLevel(mixxx::kLogFlushLevelDefault),
m_logMaxFileSize(mixxx::kLogMaxFileSizeDefault),
// We are not ready to switch to XDG folders under Linux, so keeping $HOME/.mixxx as preferences folder. see #8090
#ifdef MIXXX_SETTINGS_PATH
m_settingsPath(QDir::homePath().append("/").append(MIXXX_SETTINGS_PATH))
Expand Down Expand Up @@ -326,6 +327,18 @@ bool CmdlineArgs::parse(const QStringList& arguments, CmdlineArgs::ParseMode mod
parser.addOption(logFlushLevel);
parser.addOption(logFlushLevelDeprecated);

const QCommandLineOption logMaxFileSize(QStringLiteral("log-max-file-size"),
forUserFeedback ? QCoreApplication::translate("CmdlineArgs",
"Sets the maximum file size of the "
"mixxx.log file in bytes. "
"Use -1 for unlimited. The default is "
"100 MB as 1e5 or 100000000.")
: QString(),
QStringLiteral("bytes"));
logFlushLevelDeprecated.setFlags(QCommandLineOption::HiddenFromHelp);
logFlushLevelDeprecated.setValueName(logFlushLevel.valueName());
parser.addOption(logMaxFileSize);

QCommandLineOption debugAssertBreak(QStringLiteral("debug-assert-break"),
forUserFeedback ? QCoreApplication::translate("CmdlineArgs",
"Breaks (SIGINT) Mixxx, if a DEBUG_ASSERT evaluates to "
Expand Down Expand Up @@ -456,6 +469,17 @@ bool CmdlineArgs::parse(const QStringList& arguments, CmdlineArgs::ParseMode mod
}
}

if (parser.isSet(logMaxFileSize)) {
QString strLogMaxFileSize = parser.value(logMaxFileSize);
bool ok = false;
// We parse it as double to also support exponential notation
m_logMaxFileSize = static_cast<qint64>(strLogMaxFileSize.toDouble(&ok));
if (!ok) {
fputs("\nFailed to parse log-max-file-size.\n", stdout);
return false;
}
}

// set colors
if (parser.value(color).compare(QLatin1String("always"), Qt::CaseInsensitive) == 0) {
m_useColors = true;
Expand Down
4 changes: 4 additions & 0 deletions src/util/cmdlineargs.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ class CmdlineArgs final {
bool getSettingsPathSet() const { return m_settingsPathSet; }
mixxx::LogLevel getLogLevel() const { return m_logLevel; }
mixxx::LogLevel getLogFlushLevel() const { return m_logFlushLevel; }
qint64 getLogMaxFileSize() const {
return m_logMaxFileSize;
}
bool getTimelineEnabled() const { return !m_timelinePath.isEmpty(); }
const QString& getLocale() const { return m_locale; }
const QString& getSettingsPath() const { return m_settingsPath; }
Expand Down Expand Up @@ -102,6 +105,7 @@ class CmdlineArgs final {
bool m_parseForUserFeedbackRequired;
mixxx::LogLevel m_logLevel; // Level of stderr logging message verbosity
mixxx::LogLevel m_logFlushLevel; // Level of mixx.log file flushing
qint64 m_logMaxFileSize;
QString m_locale;
QString m_settingsPath;
QString m_resourcePath;
Expand Down
16 changes: 16 additions & 0 deletions src/util/logging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <QString>
#include <QTextStream>
#include <QThread>
#include <atomic>
#include <string_view>

#include "util/assert.h"
Expand All @@ -29,6 +30,8 @@ QMutex s_mutexStdErr;

// The file handle for Mixxx's log file.
QFile s_logfile;
qint64 s_logMaxFileSize = mixxx::kLogMaxFileSizeDefault;
std::atomic<bool> s_logMaxFileSizeReached = false;

QLoggingCategory::CategoryFilter oldCategoryFilter = nullptr;

Expand Down Expand Up @@ -152,6 +155,10 @@ inline void writeToFile(
const QString& message,
const QString& threadName,
bool flush) {
if (s_logMaxFileSizeReached.load(std::memory_order_relaxed)) {
return;
}

QString formattedMessageStr =
formatLogFileMessage(type, message, threadName) +
QChar('\n');
Expand All @@ -161,6 +168,13 @@ inline void writeToFile(
// Writing to a closed QFile could cause an infinite recursive loop
// by logging to qWarning!
if (s_logfile.isOpen()) {
if (s_logMaxFileSize >= 0 && s_logfile.pos() >= s_logMaxFileSize) {
formattedMessage =
"Maximum log file size reached. It can be adjusted via: "
"--log-max-file-size <bytes>";
s_logMaxFileSizeReached.store(true, std::memory_order_relaxed);
flush = true;
}
const int written = s_logfile.write(formattedMessage);
Q_UNUSED(written);
DEBUG_ASSERT(written == formattedMessage.size());
Expand Down Expand Up @@ -437,6 +451,8 @@ void Logging::initialize(
oldCategoryFilter = QLoggingCategory::installFilter(nullptr);
QLoggingCategory::installFilter(controllerDebugCategoryFilter);
}

s_logMaxFileSize = CmdlineArgs::Instance().getLogMaxFileSize();
}

// static
Expand Down
1 change: 1 addition & 0 deletions src/util/logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Q_DECLARE_OPERATORS_FOR_FLAGS(LogFlags);

/// Default log level for (console) logs.
constexpr LogLevel kLogLevelDefault = LogLevel::Warning;
constexpr qint64 kLogMaxFileSizeDefault = 100'000'000; // 100 MB

/// Default log level for flushing the buffered log stream.
/// This is required to ensure that all buffered messages have
Expand Down

0 comments on commit 42d509e

Please sign in to comment.