Merge PR #551: Keep more logs

This commit is contained in:
Jenkins nymea 2021-03-11 23:24:57 +01:00
commit ab93ee59f0
2 changed files with 18 additions and 2 deletions

View File

@ -64,6 +64,7 @@ AppLogController::AppLogController(QObject *parent) : QAbstractListModel(parent)
QString fileName = path + "/nymea-app.log";
m_logFile.setFileName(fileName);
QByteArray oldContent;
if (QFile::exists(fileName)) {
if (QFile::exists(fileName + ".old")) {
QFile::remove(fileName + ".old");
@ -71,13 +72,18 @@ AppLogController::AppLogController(QObject *parent) : QAbstractListModel(parent)
QFile::rename(fileName, fileName + ".old");
QFile oldFile(fileName + ".old");
if (oldFile.open(QFile::ReadOnly)) {
m_buffer.append(QString(oldFile.readAll()).split('\n'));
oldFile.seek(qMax((long long)0, oldFile.size() - 1024 * 1024));
oldContent = oldFile.readAll();
oldFile.close();
m_buffer.append(QString(oldContent).split('\n'));
for (int i = 0; i < m_buffer.count(); i++) {
m_types.append(TypeInfo);
}
m_types.append(TypeWarning);
m_buffer.append("**** App restart ****");
oldFile.close();
oldContent.append("\n\n**** App restart ****\n\n");
}
}
QDir dir(path);
@ -93,6 +99,8 @@ AppLogController::AppLogController(QObject *parent) : QAbstractListModel(parent)
return;
}
qDebug() << "App log opened at" << fileName;
m_logFile.write(oldContent);
if (enabled()) {
activate();
@ -164,6 +172,11 @@ void AppLogController::toClipboard()
QGuiApplication::clipboard()->setText(completeLog);
}
QString AppLogController::logFile() const
{
return m_logFile.fileName();
}
void AppLogController::logMessageHandler(QtMsgType type, const QMessageLogContext &context, const QString &message)
{
s_oldLogMessageHandler(type, context, message);

View File

@ -42,6 +42,7 @@ class AppLogController : public QAbstractListModel
Q_OBJECT
Q_PROPERTY(bool canWriteLogs READ canWriteLogs CONSTANT)
Q_PROPERTY(bool enabled READ enabled WRITE setEnabled NOTIFY enabledChanged)
Q_PROPERTY(QString logFile READ logFile CONSTANT)
public:
enum Type {
@ -71,6 +72,8 @@ public:
Q_INVOKABLE void toClipboard();
QString logFile() const;
signals:
void enabledChanged();