From 8dae128d07a4d0e78a9c3573456adabf285eeab9 Mon Sep 17 00:00:00 2001 From: Michael Zanetti Date: Tue, 18 Jun 2019 16:55:54 +0200 Subject: [PATCH] Fix app logging not working when the cache dir does not exist. This may happen on the very first startup on a pristine system. --- nymea-app/applogcontroller.cpp | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/nymea-app/applogcontroller.cpp b/nymea-app/applogcontroller.cpp index 223d59fb..5d7ceb64 100644 --- a/nymea-app/applogcontroller.cpp +++ b/nymea-app/applogcontroller.cpp @@ -5,6 +5,7 @@ #include #include #include +#include QtMessageHandler AppLogController::s_oldLogMessageHandler = nullptr; @@ -28,7 +29,8 @@ AppLogController *AppLogController::instance() AppLogController::AppLogController(QObject *parent) : QAbstractListModel(parent) { - QString fileName = QStandardPaths::writableLocation(QStandardPaths::CacheLocation) + "/nymea-app.log"; + QString path = QStandardPaths::writableLocation(QStandardPaths::CacheLocation); + QString fileName = path + "/nymea-app.log"; m_logFile.setFileName(fileName); if (QFile::exists(fileName)) { @@ -37,11 +39,19 @@ AppLogController::AppLogController(QObject *parent) : QAbstractListModel(parent) } QFile::rename(fileName, fileName + ".old"); } + QDir dir(path); + if (!dir.exists()) { + if (!dir.mkpath(path)) { + qWarning() << "Cannot create cache location. Logging will not work."; + return; + } + } if (!m_logFile.open(QFile::ReadWrite | QFile::Truncate)) { - qDebug() << "Cannot open logfile for writing"; + qWarning() << "Cannot open logfile for writing."; return; } + qDebug() << "App log opened at" << fileName; if (enabled()) { activate();