/*! * * 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" using namespace Storage; Logger::Logger(QObject *parent) : QObject(parent) { } bool Logger::init() { // runs in main thread Q_ASSERT_X(QThread::currentThread() == qApp->thread() , "_Logger::init", "The Class initialization must be done in Main Thread" ); // runs in Logger thread checkLogPath(); qRegisterMetaType("LogType"); initConnections(); return true; } void Logger::quit() { } 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()); LOG_EVENT(tr("Application Dir Path used for events logging (%1)").arg(_dir.path())); } else { _dir.setPath(Log_Base_Path_Name); } } bool Logger::setLogPath() { bool ok = true; ok = ok && setLogPath(LogType::eLogDatum); ok = ok && setLogPath(LogType::eLogEvent); ok = ok && setLogPath(LogType::eLogError); return ok; } 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; } void Logger::log(const QString &vContent, Logger::LogType vLogType) { PRINT_THREAD_NAME; 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; } } void Logger::concurrentLog(const QString &vContent, LogType vLogType) { PRINT_THREAD_NAME; // QFuture future = QtConcurrent::run(this,&Logger::log, vContent, vLogType); // qDebug() << future.result(); }