/*! * * 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; // Singleton SINGLETON_INIT(Logger) Logger::Logger(QObject *parent) : QObject(parent) { } bool Logger::init() { checkLogPath(); qRegisterMetaType("LogType"); 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(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]) ) { 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 vLogType) { //qDebug() << QThread::currentThread()->objectName(); 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->onWrite(_logPathNames[vLogType] + fileName, mContent + "\r\n"); qDebug().noquote() << mContent; return true; } void Logger::doLog(const QString &vContent, LogType vLogType) { // // QMetaObject::invokeMethod(this, "log", Qt::AutoConnection, Q_ARG(QString, vContent) , Q_ARG(LogType, vLogType)); // /* QFuture future = */QtConcurrent::run(this,&Logger::log, vContent, vLogType); // // qDebug() << future.result(); log(vContent, vLogType); }