Index: sources/storage/logger.cpp =================================================================== diff -u -r781e62c996e81897517fbdb1bc79fe3bbcf165c1 -r5963f00ffd2c557d3ae06a5deea05032a3a3bd68 --- sources/storage/logger.cpp (.../logger.cpp) (revision 781e62c996e81897517fbdb1bc79fe3bbcf165c1) +++ sources/storage/logger.cpp (.../logger.cpp) (revision 5963f00ffd2c557d3ae06a5deea05032a3a3bd68) @@ -13,9 +13,103 @@ */ #include "logger.h" +// Qt +#include +#include +#include +#include + +// Project +#include "filehandler.h" + using namespace Storage; -Logger::Logger(QObject *parent) : QObject(parent) +// Singleton +SINGLETON_INIT(Logger) + + +Logger::Logger(QObject *parent) : QObject(parent) { } + +bool Logger::init() { + checkLogPath(); + initConnections(); + return true; +} +void Logger::initConnections() { } + +void Logger::checkLogPath() +{ + setLogBasePath(); // try to use /media/sd_card on device + if (! setLogPath()) { // check and create log folders & if unsuccessful then + setLogBasePath(true); // try to use application folder + setLogPath ( ); // check and create log folders // Note: it may require to check for write access regarding device setup + } } + +void Logger::setLogBasePath(bool vUseApplicationDirPath) +{ + if (vUseApplicationDirPath) { + _dir.setPath(qApp->applicationDirPath()); + qDebug() << "WARNING :" << tr("Application Dir Path used for events logging (%1)").arg(_dir.path()); + } else { + _dir.setPath(_logBasePathName); + } +} + +bool Logger::setLogPath() +{ + bool ok = true; + ok = ok && setLogPath(eLogDatum); + ok = ok && setLogPath(eLogEvent); + ok = ok && setLogPath(eLogError); + return ok; +} + +bool Logger::setLogPath(Logger::LogType_Enum vLogType) +{ + _logPathNames[vLogType] = _dir.path() + "/" + _logBasePathNames[vLogType]; + if ( ! _dir.exists(_logBasePathNames[vLogType]) ) { + if ( ! _dir.mkpath(_logBasePathNames[vLogType]) ) { + qDebug() << "ERROR :" << tr("Can't create %1 log path (%2)") + .arg(_logPrefix[vLogType]) + .arg(_logPathNames[vLogType]) + ; + return false; + } + } + return true; +} + +bool Logger::log(const QString &vContent, LogType_Enum vLogType) { + QString date = QDate::currentDate().toString(_dateFormat); + QString mContent; + + switch (vLogType) { + case eLogDatum: + case eLogEvent: + case eLogError: + break; + default: + Q_ASSERT_X(false, "Logger::log", "Incorrect type of logging"); + } + mContent += _logPrefix[vLogType]; + mContent += _prefixSeparator; + mContent += QTime::currentTime().toString(_timeFormat); + mContent += _timeSeparator + vContent + "\r\n"; + QString fileName = date + _dateSeparator + _logFileName; + _FileHandler->doWrite(_logPathNames[vLogType] + fileName, mContent); + qDebug() << mContent; + return true; +} + +void Logger::doLog(const QString &vContent, LogType_Enum vLogType) +{ + emit didLog(log(vContent, vLogType)); +} + +void Logger::doExportLog() +{ + _FileHandler->doExportFolder(_logBasePathNameExport); +}