Index: sources/storage/logger.cpp =================================================================== diff -u -r56d00a82669a7a2c00ab90109a89dbec8db27527 -rb9c5b0b3afc3b34d4980ecc4f023f498f80dafbc --- sources/storage/logger.cpp (.../logger.cpp) (revision 56d00a82669a7a2c00ab90109a89dbec8db27527) +++ sources/storage/logger.cpp (.../logger.cpp) (revision b9c5b0b3afc3b34d4980ecc4f023f498f80dafbc) @@ -40,23 +40,45 @@ 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(); + + LOG_EVENT(QObject::tr("%1 Initialized").arg(metaObject()->className())); + return true; } /*! - * \brief Logger::quit - * \details Does nothing for now + * \brief Logger::init + * \details Initialized the Class by calling the init() method first + * And initializes the thread vThread by calling initThread + * on success init(). + * \param vThread - the thread + * \return returns the return value of the init() method */ +bool Logger::init(QThread &vThread) +{ + if ( ! init() ) return false; + initThread(vThread); + return true; +} + +/*! + * \brief Logger quit + * \details quits the class + * Calls quitThread + */ void Logger::quit() { + quitThread(); } +void Logger::onLog(const QString &vContent, LogType vLogType) +{ + log(vContent,vLogType); +} + /*! * \brief Logger::initConnections * \details Initializes the required signal/slot connection between this class and other objects @@ -65,9 +87,45 @@ */ void Logger::initConnections() { + connect(&_exportWatcher, SIGNAL(finished()), + this , SLOT(onExport())); + + connect(this, SIGNAL(didLog(QString,LogType)), + this, SLOT( onLog(QString,LogType))); } /*! + * \brief Logger::initThread + * \details Moves this object into the thread vThread. + * And checks that this method is called from main thread. + * Also connects quitThread to application aboutToQuit. + * \param vThread - the thread + */ +void Logger::initThread(QThread &vThread) +{ + // runs in main thread + Q_ASSERT_X(QThread::currentThread() == qApp->thread() , __func__, "The Class initialization must be done in Main Thread" ); + _thread = &vThread; + _thread->setObjectName(QString("%1_Thread").arg(metaObject()->className())); + connect(qApp, SIGNAL(aboutToQuit()), this, SLOT(quit())); + _thread->start(); + moveToThread(_thread); +} + +/*! + * \brief Logger::quitThread + * \details Moves this object to main thread to be handled by QApplicaiton + * And to be destroyed there. + */ +void Logger::quitThread() +{ + if (! _thread) return; + + // runs in thread + moveToThread(qApp->thread()); +} + +/*! * \brief Logger::checkLogPath * \details Sets the log paths and creates them if didn't exist. */ @@ -105,9 +163,9 @@ bool Logger::setLogPath() { bool ok = true; - ok = ok && setLogPath(LogType::eLogDatum); ok = ok && setLogPath(LogType::eLogEvent); ok = ok && setLogPath(LogType::eLogError); + ok = ok && setLogPath(LogType::eLogDatum); return ok; } @@ -147,34 +205,42 @@ QString mContent; switch (vLogType) { - case eLogDatum: case eLogEvent: case eLogError: + case eLogDatum: break; default: - Q_ASSERT_X(false, "Logger::log", "Incorrect type of logging"); + LOG_ERROR(tr("Incorrect type of logging").arg(vLogType)); } 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); + _logFileName = _logPathNames[vLogType] + fileName; + FileHandler::write(_logFileName, 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 + * \brief Logger::concurrentExport + * \details Exports the log files from log folder (Storage::Log_Base_Path_Name_Location) + * into USB drive folder (Storage::USB_Mount_Point) + * \return always returns true for now. + * \note This method uses QtConcurrent run to execute the FileHandler copyFolder method. */ -void Logger::concurrentLog(const QString &vContent, LogType vLogType) +bool Logger::concurrentExport() { - // QFuture future = - QtConcurrent::run(this,&Logger::log, vContent, vLogType); - // qDebug() << future.result(); + QString mSource = Storage::Log_Base_Path_Name_Location; + QString mDestination = Storage::USB_Mount_Point; + QFuture future = QtConcurrent::run(&FileHandler::copyFolder, mSource, mDestination); + _exportWatcher.setFuture(future); + return true; } + +void Logger::onExport() +{ + emit didExport(); +}