Skip to content

Commit

Permalink
Dump the last 20 lines of logs to a file when we crash
Browse files Browse the repository at this point in the history
Fixes: #8467
  • Loading branch information
TheOneRing committed Mar 3, 2021
1 parent f6e4978 commit a3c492d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
7 changes: 7 additions & 0 deletions changelog/unreleased/8467
@@ -0,0 +1,7 @@
Enhancement: Attach the last 20 log lines to a crash report

We now save the last 20 lines of log to a tempoary file.
This file is then part of a crash report.

https://github.com/owncloud/client/issues/8467
https://github.com/owncloud/client/pull/8469
18 changes: 18 additions & 0 deletions src/libsync/logger.cpp
Expand Up @@ -31,6 +31,9 @@
#include <io.h> // for stdout
#endif

namespace {
constexpr int CrashLogSize = 20;
}
namespace OCC {

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

if(type == QtFatalMsg) {
if (!logger->isNoop()) {
logger->dumpCrashLog();
logger->close();
}
#if defined(Q_OS_WIN)
Expand All @@ -69,6 +73,7 @@ Logger::Logger(QObject *parent)
, _logDebug(false)
{
qSetMessagePattern(QStringLiteral("%{time MM-dd hh:mm:ss:zzz} [ %{type} %{category} ]%{if-debug}\t[ %{function} ]%{endif}:\t%{message}"));
_crashLog.resize(CrashLogSize);
#ifndef NO_MSG_HANDLER
qInstallMessageHandler(mirallLogCatcher);
#else
Expand Down Expand Up @@ -132,6 +137,8 @@ void Logger::doLog(const QString &msg)
{
{
QMutexLocker lock(&_mutex);
_crashLogIndex = (_crashLogIndex + 1) % CrashLogSize;
_crashLog[_crashLogIndex] = msg;
if (_logstream) {
(*_logstream) << msg << endl;
if (_doFileFlush)
Expand Down Expand Up @@ -246,6 +253,17 @@ void Logger::setLogRules(const QSet<QString> &rules)
QLoggingCategory::setFilterRules(tmp);
}

void Logger::dumpCrashLog()
{
QFile logFile(QDir::tempPath() + QStringLiteral("/" APPLICATION_NAME "-crash.log"));
if (logFile.open(QFile::WriteOnly)) {
QTextStream out(&logFile);
for (int i = 1; i <= CrashLogSize; ++i) {
out << _crashLog[(_crashLogIndex + i) % CrashLogSize] << QLatin1Char('\n');
}
}
}

static bool compressLog(const QString &originalName, const QString &targetName)
{
#ifdef ZLIB_FOUND
Expand Down
4 changes: 4 additions & 0 deletions src/libsync/logger.h
Expand Up @@ -89,6 +89,8 @@ class OWNCLOUDSYNC_EXPORT Logger : public QObject
}
void setLogRules(const QSet<QString> &rules);

void dumpCrashLog();

signals:
void logWindowLog(const QString &);

Expand All @@ -113,6 +115,8 @@ public slots:
QString _logDirectory;
bool _temporaryFolderLogDir = false;
QSet<QString> _logRules;
QVector<QString> _crashLog;
int _crashLogIndex = 0;
};

} // namespace OCC
Expand Down

0 comments on commit a3c492d

Please sign in to comment.