/*! * * 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 #include #include // Project #include "storageglobals.h" #include "filehandler.h" #include "threads.h" using namespace Storage; Logger::Logger(QObject *parent) : QObject(parent) { } /*! * \brief Logger::init * \details Initializes the Class. * \return False if it has been called before. */ bool Logger::init() { if ( _init ) return false; _init = true; // runs in main thread Q_ASSERT_X(QThread::currentThread() == qApp->thread() , __func__, "The Class initialization must be done in Main Thread" ); // runs in thread checkLogPath(); initConnections(); return true; } /*! * \brief Logger::quit * \details Does nothing for now */ void Logger::quit() { } /*! * \brief Logger::initConnections * \details Initializes the required signal/slot connection between this class and other objects * to be able to communicate. * \note No connection has been defined yet. */ void Logger::initConnections() { } /*! * \brief Logger::checkLogPath * \details Sets the log paths and creates them if didn't exist. */ 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 } } /*! * \brief Logger::setLogBasePath * \details Tries to the set the log path to the default log path (Log_Base_Path_Name) * Will set the application folder as the base log path if can't set the log path to the default. * Will log the event in that case. * \param vUseApplicationDirPath */ void Logger::setLogBasePath(bool vUseApplicationDirPath) { if (vUseApplicationDirPath) { _dir.setPath(qApp->applicationDirPath()); LOG_EVENT(tr("Application Dir Path used for events logging (%1)").arg(_dir.path())); } else { _dir.setPath(Log_Base_Path_Name); } } /*! * \brief Logger::setLogPath * \details set the log path for each of the Datum, Event, Error log types * \return False if can not st the log paths. */ bool Logger::setLogPath() { bool ok = true; ok = ok && setLogPath(LogType::eLogDatum); ok = ok && setLogPath(LogType::eLogEvent); ok = ok && setLogPath(LogType::eLogError); return ok; } /*! * \brief Logger::setLogPath * \details Sets the log path for the log type vLogType * Creates the folder if not exists. * \param vLogType - log type * \return returns false if the path doesn't exist and folder can't be created. */ 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(_logPrefix[vLogType]) .arg(_logPathNames[vLogType]) ); return false; } } return true; } /*! * \brief Logger::log * \details Logs the content vContent in log type of vLogType. * \param vContent - Log content * \param vLogType - Log type * \note This method is not thread-safe so is private and needs to be called by concurrentLog * Which uses QtConcurrent::run to run in thread and thread-safe. */ void Logger::log(const QString &vContent, Logger::LogType 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; QString fileName = date + _dateSeparator + Log_File_Name; FileHandler::write(_logPathNames[vLogType] + fileName, mContent + "\r\n", true); if (vLogType == eLogError) { qDebug().noquote() << mContent; } } /*! * \brief Logger::concurrentLog * \details The thread safe of calling the log method in a separate thread. * This method uses the QtConcurrent::run to call the log in separate thread and thread-safe. * \param vContent - the content to be logged * \param vLogType - The log type */ void Logger::concurrentLog(const QString &vContent, LogType vLogType) { // QFuture future = QtConcurrent::run(this,&Logger::log, vContent, vLogType); // qDebug() << future.result(); }