Index: sources/storage/logger.cpp =================================================================== diff -u -rd04653f0fbf1ed98178b6c7094beb4ec226a777f -r3aab84456cfbdc4c4f495975ba9b8968eb844309 --- sources/storage/logger.cpp (.../logger.cpp) (revision d04653f0fbf1ed98178b6c7094beb4ec226a777f) +++ sources/storage/logger.cpp (.../logger.cpp) (revision 3aab84456cfbdc4c4f495975ba9b8968eb844309) @@ -22,6 +22,7 @@ #include // Project +#include "DriveWatcher.h" #include "threads.h" #include "storageglobals.h" #include "maintimer.h" @@ -37,7 +38,7 @@ * Qt handles the children destruction by their parent objects life-cycle. */ Logger::Logger(QObject *parent) : QObject(parent) { - _prefix = QFileInfo(qApp->applicationFilePath()).baseName(); + _logFileNamePrefix = QFileInfo(qApp->applicationFilePath()).baseName(); } /*! @@ -90,11 +91,6 @@ } // coco end -void Logger::onLog(const QString &vContent, LogType vLogType) -{ - log(vContent,vLogType); -} - /*! * \brief Logger::initConnections * \details Initializes the required signal/slot connection between this class and other objects @@ -106,7 +102,6 @@ connect(&_exportWatcher, SIGNAL(finished()), this , SLOT(onExport())); - connect(this, SIGNAL(didLog(QString,LogType)), this, SLOT( onLog(QString,LogType))); @@ -116,6 +111,9 @@ connect(&_removeOldLogsWatcher, SIGNAL(finished ()), this , SLOT(onRemoveOldLogs())); + + connect(&_DriveWatcher, SIGNAL( didSDCardSpaceChange(bool, qint64, qint64, quint8)), + this , SLOT( onSDCardSpaceChange(bool, qint64, qint64, quint8))); } /*! @@ -154,6 +152,11 @@ } // coco end +void Logger::onLog(const QString &vContent, LogType vLogType) +{ + log(vContent,vLogType); +} + /*! * \brief Logger::checkLogPath * \details Sets the log paths and creates them if didn't exist. @@ -181,10 +184,12 @@ { if (vUseApplicationDirPath) { _dir.setPath(qApp->applicationDirPath()); - LOG_EVENT(tr("Application path used for events logging (%1)").arg(_dir.path())); - } else { - _dir.setPath(Log_Base_Path_Name); + // Don't use LOG_XXXXX, At this moment Logger has not been initialized yet + qDebug() << QString("Application path used for events logging (%1)").arg(_dir.path()); } + else { + _dir.setPath(SDCard_Base_Path_Name); + } } /*! @@ -195,10 +200,9 @@ bool Logger::setLogPath() { bool ok = true; - if ( ! setLogPath(LogType::eLogBasic) ) ok = false; - if ( ! setLogPath(LogType::eLogEvent) ) ok = false; - if ( ! setLogPath(LogType::eLogError) ) ok = false; - if ( ! setLogPath(LogType::eLogDatum) ) ok = false; + if ( ok && ! setLogPath(LogType::eLogDebug) ) ok = false; + if ( ok && ! setLogPath(LogType::eLogEvent) ) ok = false; + if ( ok && ! setLogPath(LogType::eLogDatum) ) ok = false; return ok; } @@ -211,17 +215,18 @@ */ bool Logger::setLogPath(LogType vLogType) { - _logPathNames[vLogType] = _dir.path() + "/" + _logBasePathNames[vLogType]; - if ( ! _dir.exists(_logBasePathNames[vLogType]) ) { - if ( ! _dir.mkpath(_logBasePathNames[vLogType]) ) { - LOG_ERROR(tr("Can't create %1 log path (%2)") - .arg(_logTypeName [vLogType]) - .arg(_logPathNames[vLogType]) - ); - return false; - } + bool ok = false; + switch (vLogType) { + case LogType::eLogDebug: + _logPathNames[vLogType] = qApp->applicationDirPath() + "/" + _logBasePathNames[vLogType]; + break; + + default: + _logPathNames[vLogType] = _dir.path() + "/" + _logBasePathNames[vLogType]; + break; } - return true; + ok = FileHandler::makeFolder(_logPathNames[vLogType]); + return ok; } /*! @@ -235,18 +240,17 @@ void Logger::log(const QString &vContent, LogType vLogType) { QString date = QDate::currentDate().toString(_dateFormat); - QString fileName = date + _dateSeparator + _prefix; + QString fileName = date + _dateSeparator + _logFileNamePrefix; switch (vLogType) { - case LogType::eLogBasic: case LogType::eLogEvent: case LogType::eLogDatum: - case LogType::eLogError: + case LogType::eLogDebug: fileName += _logFileNameExt[vLogType]; break; default: - fileName += _logFileNameExt[LogType::eLogError]; - LOG_ERROR(tr("Incorrect type of logging %1").arg(vLogType)); + fileName += _logFileNameExt[eLogDebug]; + LOG_DEBUG(QString("Incorrect type of logging %1").arg(vLogType)); } QString mContent = QTime::currentTime().toString(_timeFormat) + _separator; @@ -260,11 +264,10 @@ QString logPathName = _logPathNames[vLogType]; if (logPathName.isEmpty()) - logPathName = _logPathNames[LogType::eLogError]; + logPathName = _logPathNames[eLogDebug]; _logFileName = logPathName + fileName; - FileHandler::write(_logFileName, mContent + "\r\n", true); - if (vLogType == LogType::eLogError) { + if (vLogType == eLogDebug) { #ifdef QT_DEBUG //mContent.prepend("\033[1;31m --- @ --- \033[0m"); mContent.prepend(" @ "); @@ -284,8 +287,8 @@ { // coco begin validated: This needs user interaction to export to USB device // has been tested manually - QString mSource = Storage::Log_Base_Path_Name_Location; - QString mDestination = Storage::USB_Mount_Point; + QString mSource = SDCard_Base_Path_Name; + QString mDestination = USB_Mount_Point; QFuture future = QtConcurrent::run(&FileHandler::copyFolder, mSource, mDestination); _exportWatcher.setFuture(future); return true; @@ -301,6 +304,28 @@ // coco end /*! + * \brief Logger::removeOldLogs + * \details Remove old logs by iterating in the log/service folders and look for expired logs. + * \return count file(s) have been removed. + */ +int Logger::removeOldLogs() +{ + // coco begin validated: This needs user interaction to check the old files deleted + // has been tested manually + QStringList mLogFileFilter ; + QDate mOlderThan ; + for (int iType = 0; iType < eLogType_Count; iType++) { + LogType logType = static_cast(iType); + mLogFileFilter = Format::toStringList(_logFileNameExt .values(logType), true, "*"); + mOlderThan = QDate().currentDate().addDays( _logTypeExpiryDay.value(logType) * -1 ); + FileHandler::removeFiles({_logPathNames[logType]}, mLogFileFilter, mOlderThan); + } + + return true; +} +// coco end + +/*! * \brief Logger::concurrentRemoveOldLogs * \details * @@ -311,14 +336,7 @@ { // coco begin validated: This needs user interaction to check the old files deleted // has been tested manually - QString mLogsLocation = "./"; //Storage::Log_Base_Path_Name; - QStringList mLogFileFilter ; - QStringList mLogFolders ; - - mLogFolders = Format::toStringList(_logBasePathNames.values(), true, mLogsLocation); - mLogFileFilter = Format::toStringList(_logFileNameExt .values(), true, "*"); - QDate mOlderThan = QDate().currentDate().addDays(-1 * _removeOldLogsDaysOlderThan); - QFuture mFuture = QtConcurrent::run(&FileHandler::removeFiles, mLogFolders, mLogFileFilter, mOlderThan); + QFuture mFuture = QtConcurrent::run(this,&Logger::removeOldLogs); _removeOldLogsWatcher.setFuture(mFuture); return true; } @@ -330,4 +348,9 @@ // has been tested manually emit didRemoveOldLogs(); } + +void Logger::onSDCardSpaceChange(bool vReady, qint64 vTotal, qint64 vAvailable, quint8 vPercent) +{ + qDebug() << vReady << vTotal << vAvailable << vPercent; +} // coco end