Skip to content

Commit 219092b

Browse files
committed
Dump the last 20 lines of logs to a file when we crash
Fixes: #8467
1 parent aa2f635 commit 219092b

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

changelog/unreleased/8467

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Enhancement: Attach the last 20 log lines to a crash report
2+
3+
We now save the last 20 lines of log to a tempoary file.
4+
This file is then part of a crash report.
5+
6+
https://github.com/owncloud/client/issues/8467
7+
https://github.com/owncloud/client/pull/8469

src/libsync/logger.cpp

+18
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@
3131
#include <io.h> // for stdout
3232
#endif
3333

34+
namespace {
35+
constexpr int CrashLogSize = 20;
36+
}
3437
namespace OCC {
3538

3639
static void mirallLogCatcher(QtMsgType type, const QMessageLogContext &ctx, const QString &message)
@@ -45,6 +48,7 @@ static void mirallLogCatcher(QtMsgType type, const QMessageLogContext &ctx, cons
4548

4649
if(type == QtFatalMsg) {
4750
if (!logger->isNoop()) {
51+
logger->dumpCrashLog();
4852
logger->close();
4953
}
5054
#if defined(Q_OS_WIN)
@@ -69,6 +73,7 @@ Logger::Logger(QObject *parent)
6973
, _logDebug(false)
7074
{
7175
qSetMessagePattern(QStringLiteral("%{time MM-dd hh:mm:ss:zzz} [ %{type} %{category} ]%{if-debug}\t[ %{function} ]%{endif}:\t%{message}"));
76+
_crashLog.resize(CrashLogSize);
7277
#ifndef NO_MSG_HANDLER
7378
qInstallMessageHandler(mirallLogCatcher);
7479
#else
@@ -132,6 +137,8 @@ void Logger::doLog(const QString &msg)
132137
{
133138
{
134139
QMutexLocker lock(&_mutex);
140+
_crashLogIndex = (_crashLogIndex + 1) % CrashLogSize;
141+
_crashLog[_crashLogIndex] = msg;
135142
if (_logstream) {
136143
(*_logstream) << msg << endl;
137144
if (_doFileFlush)
@@ -246,6 +253,17 @@ void Logger::setLogRules(const QSet<QString> &rules)
246253
QLoggingCategory::setFilterRules(tmp);
247254
}
248255

256+
void Logger::dumpCrashLog()
257+
{
258+
QFile logFile(QDir::tempPath() + QStringLiteral("/" APPLICATION_NAME "-crash.log"));
259+
if (logFile.open(QFile::WriteOnly)) {
260+
QTextStream out(&logFile);
261+
for (int i = 1; i <= CrashLogSize; ++i) {
262+
out << _crashLog[(_crashLogIndex + i) % CrashLogSize] << QLatin1Char('\n');
263+
}
264+
}
265+
}
266+
249267
static bool compressLog(const QString &originalName, const QString &targetName)
250268
{
251269
#ifdef ZLIB_FOUND

src/libsync/logger.h

+4
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ class OWNCLOUDSYNC_EXPORT Logger : public QObject
8989
}
9090
void setLogRules(const QSet<QString> &rules);
9191

92+
void dumpCrashLog();
93+
9294
signals:
9395
void logWindowLog(const QString &);
9496

@@ -113,6 +115,8 @@ public slots:
113115
QString _logDirectory;
114116
bool _temporaryFolderLogDir = false;
115117
QSet<QString> _logRules;
118+
QVector<QString> _crashLog;
119+
int _crashLogIndex = 0;
116120
};
117121

118122
} // namespace OCC

0 commit comments

Comments
 (0)