Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dump the last 20 lines of logs to a file when we crash #8469

Merged
merged 1 commit into from Mar 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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