/*! * * 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_BASIC(vCONTENT) emit Storage::Logger::I().didLog(vCONTENT, Storage::Logger::LogType::eLogBasic) #define LOG_EVENT(vCONTENT) emit Storage::Logger::I().didLog(vCONTENT, Storage::Logger::LogType::eLogEvent) #define LOG_ERROR(vCONTENT) emit Storage::Logger::I().didLog(vCONTENT, Storage::Logger::LogType::eLogError) #define LOG_DATUM(vCONTENT) emit Storage::Logger::I().didLog(vCONTENT, Storage::Logger::LogType::eLogDatum) // 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 { eLogBasic, eLogEvent, eLogError, eLogDatum, eLogType_Count, }; private: QDir _dir; QString _prefix; QHash _logPathNames; const QHash _logBasePathNames { { LogType::eLogBasic, "log/" }, { LogType::eLogEvent, "log/" }, { LogType::eLogDatum, "log/" }, { LogType::eLogError, "service/" }, }; const QHash _logPrefix { // Will be used for the logging in the file { LogType::eLogBasic, "" }, // Object itself tells what it is { LogType::eLogEvent, "E" }, { LogType::eLogDatum, "D" }, { LogType::eLogError, "" }, // it has its own file and all the content is Error }; const QHash _logTypeName { // Will be used for for information { LogType::eLogBasic, "Basic" }, // Object itself tells what it is { LogType::eLogEvent, "Event" }, { LogType::eLogDatum, "Datum" }, { LogType::eLogError, "Error" }, // it has its own file and all the content is Error }; const QHash _logFileNameExt { { LogType::eLogBasic, ".log" }, { LogType::eLogEvent, ".log" }, { LogType::eLogDatum, ".log" }, { LogType::eLogError, ".err" }, }; const char *_dateFormat = "yyyy_MM_dd"; const char *_timeFormat = "HH:mm:ss"; const char *_dateSeparator = "_"; // used in filename const char *_separator = ","; QString _logFileName = ""; QFutureWatcher _exportWatcher; QFutureWatcher _removeOldLogsWatcher; const quint8 _removeOldLogsDaysOlderThan = 3; 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(); // ----- Remove Old Logs structure private slots: bool concurrentRemoveOldLogs(); void onRemoveOldLogs(); signals: void didRemoveOldLogs(); // ----- 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); }; }