/*! * * Copyright (c) 2019-2019 Diality Inc. - All Rights Reserved. * \copyright \n * THIS CODE MAY NOT BE COPIED OR REPRODUCED IN ANY FORM, \n * IN PART OR IN WHOLE, \n * WITHOUT THE EXPLICIT PERMISSION OF THE COPYRIGHT OWNER. \n * * \file logger.cpp * \date 2019/09/30 * \author Behrouz NematiPour * */ #include "logger.h" // Qt #include #include #include #include // Project #include "filehandler.h" using namespace Storage; // 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); }