Index: sources/storage/Logger.cpp =================================================================== diff -u -r265ce7409a0ea99a4ae059f5ce7978c9cdb10631 -r142f2ddb8ce284c52c0add2acf3ac81f471b78de --- sources/storage/Logger.cpp (.../Logger.cpp) (revision 265ce7409a0ea99a4ae059f5ce7978c9cdb10631) +++ sources/storage/Logger.cpp (.../Logger.cpp) (revision 142f2ddb8ce284c52c0add2acf3ac81f471b78de) @@ -86,10 +86,11 @@ void Logger::postInit() { if ( ! checkThread() ) return; - ADD_EVENT_HEADER; + ADD_APPED_HEADER; + ADD_DEBUG_HEADER; LOG_DEBUG(tr("%1 Initialized").arg(metaObject()->className())); LOG_DEBUG(tr("Application %1 Started").arg(qApp->applicationName())); - LOG_EVENT_UI(qApp-> applicationVersion()); + LOG_APPED_UI(qApp-> applicationVersion()); } /*! @@ -240,14 +241,11 @@ bool Logger::setLogPath() { bool ok = true; - // disabled coco begin validated: Has been tested manually, this needs user interaction to check the file system - if ( ok && ! setLogPath(LogType::eLogEvent) ) ok = false; - if ( ok && ! setLogPath(LogType::eLogDatum) ) ok = false; + if ( ok && ! setLogPath(LogType::eLogAppED) ) ok = false; if ( ok && ! setLogPath(LogType::eLogDebug) ) ok = false; if ( ok && ! setLogPath(LogType::eLogTrtmt) ) ok = false; return ok; } -// disabled coco end /*! * \brief Logger::setLogPath @@ -259,15 +257,7 @@ bool Logger::setLogPath(LogType vLogType) { bool ok = false; - switch (vLogType) { - case LogType::eLogDebug: - _logPathNames[vLogType] = _dir.path() + "/" + _logBasePathNames[vLogType]; - break; - - default: - _logPathNames[vLogType] = _dir.path() + "/" + _logBasePathNames[vLogType]; - break; - } + _logPathNames[vLogType] = _dir.path() + "/" + _logBasePathNames[vLogType]; ok = FileHandler::makeFolder(_logPathNames[vLogType]); if ( ok ) emit didLogPathSet(vLogType, _logPathNames[vLogType]); return ok; @@ -287,19 +277,26 @@ static QString date; QString mContent; + + // - Add header QString currentDate = QDate::currentDate().toString(_dateFormat); - if (date != currentDate) { - if (!date.isEmpty()) { - mContent = _headerE; - mContent += "\r\n" ; + if ( date != currentDate ) { + if ( ! date.isEmpty() ) { + switch ( vLogType ) { + case eLogAppED : mContent = _headerA; break; + case eLogDebug : mContent = _headerD; break; + // case LogType::eLogTrtmt: // this type of log will never happen here. Only put here to make sure it is intentional. + default : mContent = _headerD; break; + } + mContent += "\r\n"; } date = currentDate; } + // - Make log file name QString fileName = date + _dateSeparator + _logFileNamePrefix; switch (vLogType) { - case LogType::eLogEvent: - case LogType::eLogDatum: + case LogType::eLogAppED: case LogType::eLogDebug: // case LogType::eLogTrtmt: // this type of log will never happen here. Only put here to make sure it is intentional. fileName += _logFileNameExt[vLogType]; @@ -310,28 +307,26 @@ LOG_DEBUG(QString("Incorrect type of logging %1").arg(vLogType)); } + // - Add timestamp if ( vTimestamp ) mContent = QTime::currentTime().toString(_timeFormat) + _separator; - QString logPrefix = _logPrefix[vLogType]; - if (vTimestamp && ! logPrefix.isEmpty()) { - mContent += logPrefix; - mContent += _separator; - } + // - Add the content mContent += vContent; // some messages like the version having the U08(uchar) parameters which converts to '\0' and causes problems in reading the log file. mContent.replace('\0', "0"); + // - Make the log path and write to log. QString logPathName = _logPathNames[vLogType]; if (logPathName.isEmpty()) logPathName = _logPathNames[eLogDebug]; _logFileName = logPathName + fileName; FileHandler::write(_logFileName, mContent + "\r\n", true); - // disabled coco begin validated: This code is only for debugging purposes and had been tested manually. + + // console out the log if enabled. if (_enableConsoleOut) { qDebug().noquote() << mContent; } - // disabled coco end } /*! @@ -340,23 +335,9 @@ * into USB drive folder (Storage::USB_Mount_Point) * \return true if at least one file has been exported */ -bool Logger::exportLogs() +bool Logger::exportLogs(const Gui::GuiStringIndexMap &vExportList) { - // qDebug() << " ~~~~~~~~~~ " << QThread::currentThread()->objectName(); - int result = 0; - static QString mOSource; - QString mDestination = USB_Mount_Point; - for ( const auto &iType : { eLogEvent, eLogDatum } ) { - QString mCSource = _logPathNames[iType]; - // if the event and datum are mixed (mOSource == mCSource) in one file then no need to go over the same files in same folder and do it again. - if (mOSource != mCSource) { - mOSource = mCSource; - // Copy Folder - result = FileHandler::copyFolder( mCSource, mDestination); - } - } - mOSource = ""; - return result >= 0; // refer to QProcess::execute(hit F1 on execute) doc. + return exportList(vExportList, eLogAppED); } /*! @@ -365,15 +346,9 @@ * into USB drive folder (Storage::USB_Mount_Point) * \return true if at least one file has been exported */ -bool Logger::exportErrs() +bool Logger::exportErrs(const Gui::GuiStringIndexMap &vExportList) { - // qDebug() << " ~~~~~~~~~~ " << QThread::currentThread()->objectName(); - int result = 0; - QString mDestination = USB_Mount_Point; - QString mSource = _logPathNames[eLogDebug]; - // Copy Folder - result = FileHandler::copyFolder( mSource, mDestination); - return result >= 0; // refer to QProcess::execute(hit F1 on execute) doc. + return exportList(vExportList, eLogDebug); } /*! @@ -382,15 +357,9 @@ * into USB drive folder (Storage::USB_Mount_Point) * \return true if at least one file has been exported */ -bool Logger::exportTrts() +bool Logger::exportTrts(const Gui::GuiStringIndexMap &vExportList) { - // qDebug() << " ~~~~~~~~~~ " << QThread::currentThread()->objectName(); - int result = 0; - QString mDestination = USB_Mount_Point; - QString mSource = _logPathNames[eLogTrtmt]; - // Copy Folder - result = FileHandler::copyFolder( mSource, mDestination); - return result >= 0; // refer to QProcess::execute(hit F1 on execute) doc. + return exportList(vExportList, eLogTrtmt); } /*! @@ -413,13 +382,13 @@ * \return always returns true for now. * \note This method uses QtConcurrent run to execute the FileHandler copyFolder method. */ -bool Logger::concurrentExportLogs() +bool Logger::concurrentExportLogs(const Gui::GuiStringIndexMap &vExportList) { if ( ! concurrentExportIsOk() ) return false; - _exportLogsType = eLogEvent; + _exportLogsType = eLogAppED; LOG_DEBUG(QString("Export %1 start").arg(_logNames[_exportLogsType])); - QFuture future = QtConcurrent::run(this, &Logger::exportLogs); + QFuture future = QtConcurrent::run(this, &Logger::exportLogs, vExportList); _exportLogsWatcher.setFuture(future); return true; } @@ -430,13 +399,13 @@ * \return always returns true for now. * \note This method uses QtConcurrent run to execute the FileHandler copyFolder method. */ -bool Logger::concurrentExportErrs() +bool Logger::concurrentExportErrs(const Gui::GuiStringIndexMap &vExportList) { if ( ! concurrentExportIsOk() ) return false; _exportLogsType = eLogDebug; LOG_DEBUG(QString("Export %1 start").arg(_logNames[_exportLogsType])); - QFuture future = QtConcurrent::run(this, &Logger::exportErrs); + QFuture future = QtConcurrent::run(this, &Logger::exportErrs, vExportList); _exportLogsWatcher.setFuture(future); return true; } @@ -447,13 +416,13 @@ * \return always returns true for now. * \note This method uses QtConcurrent run to execute the FileHandler copyFolder method. */ -bool Logger::concurrentExportTrts() +bool Logger::concurrentExportTrts(const Gui::GuiStringIndexMap &vExportList) { if ( ! concurrentExportIsOk() ) return false; _exportLogsType = eLogTrtmt; LOG_DEBUG(QString("Export %1 start").arg(_logNames[_exportLogsType])); - QFuture future = QtConcurrent::run(this, &Logger::exportTrts); + QFuture future = QtConcurrent::run(this, &Logger::exportTrts, vExportList); _exportLogsWatcher.setFuture(future); return true; } @@ -478,64 +447,45 @@ */ int Logger::removeLogs() { - // disabled coco begin validated: This needs user interaction to check the old files deleted // Storage::FileHandler::find("/media/denali/0CAA-40C1/log/", {"*.err"}, 15); return 0; // has been tested manually LOG_DEBUG(tr("Initializing log clean up")); - static QString mOSource; - static QString mOExtension; int removeCount = 0; - QString mLogFileFilter; - for ( const auto &iType : { eLogEvent , eLogDatum , eLogDebug , eLogTrtmt } ) { + for ( const auto &iType : { eLogAppED , eLogDebug , eLogTrtmt } ) { QString mCSource = _logPathNames [iType]; QString mCExtension = _logFileNameExt[iType]; - // if the event and datum are mixed (mOSource == mCSource && mCExtension == mOExtension) in one file then no need to go over the same files in same folder and do it again. - if (mOSource != mCSource || mCExtension != mOExtension) { - mOSource = mCSource; - mOExtension = mCExtension; - // Remove Logs - // TODO: It appeared to me that the filter on extension may not be necessary, since the folder of each type is different. - // NOTE: The filter on the extension comes from the idea that in the same folder(log) we may want to separate the event(log) from datum(dat) with different usage percentage. -#ifdef MIXED_EVENT_DATUM - mLogFileFilter = QString("*%1").arg(".*"); -#else - mLogFileFilter = QString("*%1").arg(mCExtension); -#endif - // DEBUG: qDebug() << "@" << mCSource << mLogFileFilter << mCExtension << iType << _logTypeMaxUsageLimit[iType]; - QFileInfoList fileInfoList = FileHandler::find(mCSource, {mLogFileFilter}, _logTypeMaxUsageLimit[iType]); - removeCount = fileInfoList.count(); - // qDebug() << "@" << removeCount << fileInfoList; - if (removeCount) { - LOG_DEBUG(QString("Removing %1 logs of type (%2) more than %3% limit from folder %4") - .arg(removeCount) - .arg(mCExtension) - .arg(_logTypeMaxUsageLimit[iType]) - .arg(mCSource)); - for (const auto &info: fileInfoList) { - if (info.lastModified().date() == QDate().currentDate()) { - LOG_DEBUG(QString("Current day log %1 cannot be deleted").arg(info.fileName())); + // DEBUG: qDebug() << "@" << mCSource << mLogFileFilter << mCExtension << iType << _logTypeMaxUsageLimit[iType]; + QFileInfoList fileInfoList = FileHandler::find(mCSource, {"*.*"}, _logTypeMaxUsageLimit[iType]); + removeCount = fileInfoList.count(); + // qDebug() << "@" << removeCount << fileInfoList; + if (removeCount) { + LOG_DEBUG(QString("Removing %1 logs of type (%2) more than %3% limit from folder %4") + .arg(removeCount) + .arg(mCExtension) + .arg(_logTypeMaxUsageLimit[iType]) + .arg(mCSource)); + for (const auto &info: fileInfoList) { + if (info.lastModified().date() == QDate().currentDate()) { + LOG_DEBUG(QString("Current day log %1 cannot be deleted").arg(info.fileName())); + } + else { + QString mFileName = mCSource + info.fileName(); + // DEBUG: qDebug() << "#" << mFileName; + bool ok = QFile::remove(mFileName); + if (ok) { + LOG_DEBUG(QString("Removing %1 succeeded").arg(mFileName)); + } else { + LOG_DEBUG(QString("Removing %1 failed" ).arg(mFileName)); } - else { - QString mFileName = mCSource + info.fileName(); - // DEBUG: qDebug() << "#" << mFileName; - bool ok = QFile::remove(mFileName); - if (ok) { - LOG_DEBUG(QString("Removing %1 succeeded").arg(mFileName)); - } else { - LOG_DEBUG(QString("Removing %1 failed" ).arg(mFileName)); - } - } } } - else { - LOG_DEBUG("No log file is deleted for " + mCExtension); - } } + else { + LOG_DEBUG("No log file is deleted for " + mCExtension); + } } - mOSource = ""; return removeCount; } -// disabled coco end /*! * \brief Logger::concurrentRemoveLogs @@ -633,3 +583,40 @@ { return _logPathNames[vLogType]; } + +/*! + * \brief Logger::exportList + * \details Exports files from the list if the vExportList has any item, otherwise exports all the log files of type vLogType. + * \param vExportList - List of files to export + * \param vLogType - type of the log files to get the correct location for export. + * \return true if the export is successful. + */ +bool Logger::exportList(const Gui::GuiStringIndexMap &vExportList, Logger::LogType vLogType) +{ + // DEBUG: qDebug() << __FUNCTION__ << vExportList; + // qDebug() << " ~~~~~~~~~~ " << QThread::currentThread()->objectName(); + + auto notifier = [this] (quint32 vIndex, const QString &vFileName, quint8 vPercent) { + emit didExportStat(vIndex, vFileName, vPercent); + // qDebug() << "0" << vIndex << vFileName << vPercent; + }; + + int result = 0; + QString mSource = _logPathNames[vLogType]; + QString mDestination = USB_Mount_Point; + if ( vExportList.isEmpty() ) { + // Copy Folder + result = FileHandler::copyFolder( mSource, mDestination); + } + else { + Gui::GuiStringIndexMapIterator it(vExportList); + while ( it.hasNext() ) { + it.next(); + // qDebug() << it.key() << it.value() << mSource << mDestination; + auto index = it.key (); + auto filename = it.value(); + result = FileHandler::copyFile(mSource, mDestination + _logBasePathNames[vLogType], filename, ¬ifier, index); + } + } + return result >= 0; // refer to QProcess::execute(hit F1 on execute) doc. +}