/********************************************************************************** * *author:JCW *Date:5/13/2015 *Description:Log functions based on text file * **********************************************************************************/ #ifndef CORELOGDEAMON_H #define CORELOGDEAMON_H #include "corelogdeamon_global.h" #include #include #include #include #include namespace CoreLogDeamon { /** * @brief The LogLevel enum desfines the level of the log message. */ enum CORELOGDEAMONSHARED_EXPORT LogLevel { TraceLevel = 0, DebugLevel, InfoLevel, WarnLevel, ErrorLevel, FatalLevel, UnkownLevel}; /** * @brief Here is done the call to write the message in the module. First of all is confirmed * that the log level we want to write is less or equal to the level defined when we create the * destination. * * @param module The module that the message references. * @param level The level of the message. * @param message The message. */ void CORELOGDEAMONSHARED_EXPORT QLog_(const QString &module, LogLevel level, const QString &message); /** * @brief Used to store Trace level messages. * @param module The module that the message references. * @param message The message. */ void CORELOGDEAMONSHARED_EXPORT QLog_Trace(const QString &module, const QString &message); /** * @brief Used to store Debug level messages. * @param module The module that the message references. * @param message The message. */ void CORELOGDEAMONSHARED_EXPORT QLog_Debug(const QString &module, const QString &message); /** * @brief Used to store Info level messages. * @param module The module that the message references. * @param message The message. */ void CORELOGDEAMONSHARED_EXPORT QLog_Info(const QString &module, const QString &message); /** * @brief Used to store Warning level messages. * @param module The module that the message references. * @param message The message. */ void CORELOGDEAMONSHARED_EXPORT QLog_Warning(const QString &module, const QString &message); /** * @brief Used to store Error level messages. * @param module The module that the message references. * @param message The message. */ void CORELOGDEAMONSHARED_EXPORT QLog_Error(const QString &module, const QString &message); /** * @brief Used to store Fatal level messages. * @param module The module that the message references. * @param message The message. */ void CORELOGDEAMONSHARED_EXPORT QLog_Fatal(const QString &module, const QString &message); void CORELOGDEAMONSHARED_EXPORT QLog_(const QString &module, LogLevel level, const QString &message); /** * @brief The QLoggerWriter class writes the message and manages the file where it is printed. */ class CORELOGDEAMONSHARED_EXPORT QLogger : public QObject { Q_OBJECT public: /** * @brief Constructor that gets the complete path and filename to create the file. It also * configures the level of this file to filter the logs depending on the level. * @param fileDestination The complete path. * @param level The maximum level that is allowed. */ // explicit QLogger(const QString &fileDestination, LogLevel level,QObject *parent=0); QLogger(const QString &fileDestination, LogLevel level,QObject *parent=0); explicit QLogger(LogLevel level,QObject *parent=0); QLogger(); /** * @brief Gets the current maximum level. * @return The LogLevel. */ LogLevel getLevel() const { return m_level; } /** * @brief Within this method the message is written in the log file. If it would exceed * from 1 MByte, another file will be created and the log message will be stored in the * new one. The older file will be renamed with the date and time of this message to know * where it is updated. * * @param module The module that corresponds to the message. * @param message The message log. */ bool write(const QString &module, const QString &message); bool write(const QString &i_module,const LogLevel i_level, const QString &i_message); /*************************************************************************** *Input:null *Output:o_message *Return:void *Description: get logged messages in module log file ***************************************************************************/ bool read(QString &o_message); bool read(QStringList &o_message); private: /** * @brief Path and name of the file that will store the logs. */ QString m_fileDestination; /** * @brief Maximum log level allowed for the file. */ LogLevel m_level; }; /** * @brief The QLoggerManager class manages the different destination files that we would like to have. */ class CORELOGDEAMONSHARED_EXPORT QLoggerManager : public QThread { public: /** * @brief Gets an instance to the QLoggerManager. * @return A pointer to the instance. */ static QLoggerManager * getInstance(); /** * @brief Converts the given level in a QString. * @param level The log level in LogLevel format. * @return The string with the name of the log level. */ static QString levelToText(const LogLevel &level); /** * @brief This method creates a QLoogerWriter that stores the name of the file and the log * level assigned to it. Here is added to the map the different modules assigned to each * log file. The method returns false if a module is configured to be stored in * more than one file. * * @param fileDest The file name and path to print logs. * @param modules The modules that will be stored in the file. * @param level The maximum level allowed. * @return Returns true if any error have been done. */ bool addLogInstance(const QString &fileDest, const QStringList &modules, LogLevel level); bool addLogInstance(const QString &fileDest, const QString &module, LogLevel level); bool addLogInstance(const QString &module,LogLevel level); /** * @brief Gets the QLoggerWriter instance corresponding to the module module. * @param module The module we look for. * @return Retrns a pointer to the object. */ QLogger * getLogInstance(const QString &module) { return moduleDest.value(module); } /** * @brief This method closes the logger and the thread it represents. */ void closeLogger(); /** * @brief Mutex to make the method thread-safe. */ QMutex mutex; private: /** * @brief Instance of the class. */ static QLoggerManager *INSTANCE; /** * @brief Map that stores the module and the file it is assigned. */ QMap moduleDest; /** * @brief Default builder of the class. It starts the thread. */ QLoggerManager(); }; } //class CORELOGDEAMONSHARED_EXPORT CoreLogDeamon //{ //public: // CoreLogDeamon(); //}; #endif // CORELOGDEAMON_H