Skip to content

Commit

Permalink
flush logs every 5 lines written
Browse files Browse the repository at this point in the history
should help not loosing too much logs in case of a crash or similar not
nominal stop of the client

Signed-off-by: Matthieu Gallien <[email protected]>
  • Loading branch information
mgallien committed Nov 21, 2024
1 parent ea94e44 commit fac4ced
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/libsync/logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ namespace {

constexpr int CrashLogSize = 20;
constexpr auto MaxLogLinesCount = 50000;
constexpr auto MaxLogLinesBeforeFlush = 10;

static bool compressLog(const QString &originalName, const QString &targetName)
{
Expand Down Expand Up @@ -145,8 +146,13 @@ void Logger::doLog(QtMsgType type, const QMessageLogContext &ctx, const QString

if (_logstream) {
(*_logstream) << msg << "\n";
if (_doFileFlush)
++_linesCounter;
if (_doFileFlush ||
_linesCounter >= MaxLogLinesBeforeFlush ||
type == QtMsgType::QtWarningMsg || type == QtMsgType::QtCriticalMsg || type == QtMsgType::QtFatalMsg) {
_logstream->flush();
_linesCounter = 0;
}
}
if (_permanentDeleteLogStream && ctx.category && strcmp(ctx.category, lcPermanentLog().categoryName()) == 0) {
(*_permanentDeleteLogStream) << msg << "\n";
Expand Down
1 change: 1 addition & 0 deletions src/libsync/logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ public slots:

QFile _logFile;
bool _doFileFlush = false;
int _linesCounter = 0;
int _logExpire = 0;
bool _logDebug = false;
QScopedPointer<QTextStream> _logstream;
Expand Down

0 comments on commit fac4ced

Please sign in to comment.