/*! * * Copyright (c) 2019-2020 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.h * \date 2019/09/30 * \author Behrouz NematiPour * */ #pragma once // Qt #include #include #include // Project #include "main.h" #include "storageglobals.h" // Define #define _Logger Storage::Logger::I() #define LOG_EXPORT _Logger.concurrentExport() #define LOG_EVENT(vCONTENT) emit Storage::Logger::I().didLog(vCONTENT, Storage::Logger::LogType::eLogEvent) #define LOG_EVENT_ONCE_LOGGED \ bLOG_EVENT_ONCE_LOGGED #define LOG_EVENT_ONCE(vCONTENT) \ static bool LOG_EVENT_ONCE_LOGGED = false; \ if ( ! LOG_EVENT_ONCE_LOGGED ) { \ LOG_EVENT_ONCE_LOGGED = true; \ LOG_EVENT(vCONTENT); \ } #define LOG_ERROR(vCONTENT) emit Storage::Logger::I().didLog(vCONTENT, Storage::Logger::LogType::eLogError) #define LOG_ERROR_ONCE_LOGGED \ bLOG_ERROR_ONCE_LOGGED #define LOG_ERROR_ONCE(vCONTENT) \ static bool LOG_ERROR_ONCE_LOGGED = false; \ if ( ! LOG_ERROR_ONCE_LOGGED ) { \ LOG_ERROR_ONCE_LOGGED = true; \ LOG_ERROR(vCONTENT); \ } #define LOG_DATUM(vCONTENT) emit Storage::Logger::I().didLog(vCONTENT, Storage::Logger::LogType::eLogDatum) #define LOG_DATUM_ONCE_LOGGED \ bLOG_DATUM_ONCE_LOGGED #define LOG_DATUM_ONCE(vCONTENT) \ static bool LOG_DATUM_ONCE_LOGGED = false; \ if ( ! LOG_DATUM_ONCE_LOGGED ) { \ LOG_DATUM_ONCE_LOGGED = true; \ LOG_DATUM(vCONTENT); \ } // forward declarations class tst_logging; namespace Storage { /*! * \brief The Logger class * \details Main logger class that has all the required implementation for logging. * The provided interface is the LOG_DATUM, LOG_EVENT, LOG_ERROR, LOG_EXPORT defines * and no other methods. * This should have its own thread. * \note * PLEASE BE CAREFUL THIS CLASS IS USING QtConcurrent::run FOR THE EXPORT LOG FILES. * AND ONLY PRIVATE VOID LOG (,) IS CALLING IN POOLED THREAD * PLEASE BE VERY CAREFUL. * ALL THE OTHER CLASSES TO USE THIS CLASS SHOULD ONLY USE LOG_DATUM, LOG_EVENT, LOG_ERROR * TO DO THE LOGGING */ class Logger : public QObject { Q_OBJECT // friends friend class ::tst_logging; public: enum LogType { eLogEvent, eLogError, eLogDatum, eLogType_Count, }; private: QDir _dir; QHash _logPathNames; QHash _logBasePathNames { { eLogEvent, "log/event/" }, { eLogError, "log/error/" }, { eLogDatum, "log/event/" }, // "log/datum/" }; QHash _logPrefix { { eLogEvent, "Event" }, { eLogError, "Error" }, { eLogDatum, "Datum" }, }; const char *_dateFormat = "yyyy_MM_dd"; const char *_timeFormat = "HH:mm:ss"; const char *_prefixSeparator = ": " ; const char *_dateSeparator = "_" ; const char *_timeSeparator = " , "; QString _logFileName = ""; QFutureWatcher _exportWatcher; QThread *_thread = nullptr; bool _init = false; // Singleton SINGLETON(Logger) public slots: bool init(); bool init(QThread &vThread); private slots: void quit(); private: void initConnections(); void initThread(QThread &vThread); void quitThread(); private: // ----- setting up void checkLogPath (); void setLogBasePath (bool vUseApplicationDirPath = false); bool setLogPath (); bool setLogPath (LogType vLogType); // ----- Export structure public slots: bool concurrentExport(); private slots: void onExport(); signals: /*! * \brief didExport * \details notifies the UI when the export is done. */ void didExport(); // ----- logging structure private slots: void onLog (const QString &vContent, LogType vLogType); private: void log (const QString &vContent, LogType vLogType); signals: /*! * \brief didLog * \details Notifies the logger on a request for log * \param vContent - content as type of string to be logged * \param vLogType - the type of logging of type Storage::Logger::LogType */ void didLog (const QString &vContent, LogType vLogType); }; }