1
0
mirror of https://github.com/chylex/Nextcloud-Desktop.git synced 2025-04-26 13:15:47 +02:00

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

Fixes: 
This commit is contained in:
Hannah von Reth 2021-03-03 16:47:35 +01:00 committed by Matthieu Gallien
parent 48f4c1e9e1
commit 990ce6ed05
2 changed files with 23 additions and 1 deletions

View File

@ -32,6 +32,9 @@
#include <io.h> // for stdout
#endif
namespace {
constexpr int CrashLogSize = 20;
}
namespace OCC {
QtMessageHandler s_originalMessageHandler = nullptr;
@ -52,6 +55,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)
@ -71,7 +75,8 @@ Logger *Logger::instance()
Logger::Logger(QObject *parent)
: QObject(parent)
{
qSetMessagePattern("%{time yyyy-MM-dd hh:mm:ss:zzz} [ %{type} %{category} ]%{if-debug}\t[ %{function} ]%{endif}:\t%{message}");
qSetMessagePattern(QStringLiteral("%{time yyyy-MM-dd hh:mm:ss:zzz} [ %{type} %{category} ]%{if-debug}\t[ %{function} ]%{endif}:\t%{message}"));
_crashLog.resize(CrashLogSize);
#ifndef NO_MSG_HANDLER
s_originalMessageHandler = qInstallMessageHandler(mirallLogCatcher);
#else
@ -135,6 +140,8 @@ void Logger::doLog(const QString &msg)
{
{
QMutexLocker lock(&_mutex);
_crashLogIndex = (_crashLogIndex + 1) % CrashLogSize;
_crashLog[_crashLogIndex] = msg;
if (_logstream) {
(*_logstream) << msg << endl;
if (_doFileFlush)
@ -267,6 +274,17 @@ void Logger::setLogRules(const QSet<QString> &rules)
QLoggingCategory::setFilterRules(rules.toList().join(QLatin1Char('\n')));
}
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

View File

@ -95,6 +95,8 @@ public:
}
void setLogRules(const QSet<QString> &rules);
void dumpCrashLog();
signals:
void logWindowLog(const QString &);
@ -119,6 +121,8 @@ private:
QString _logDirectory;
bool _temporaryFolderLogDir = false;
QSet<QString> _logRules;
QVector<QString> _crashLog;
int _crashLogIndex = 0;
};
} // namespace OCC