X-Git-Url: https://vaikene.ee/gitweb/highlight.css?a=blobdiff_plain;f=src%2Flibs%2FCommon%2Flogger.h;h=742ab9036a7957d48ea42cbd9170306f4418f4bd;hb=de270ece1b764b19968e14420f538321f1c06b15;hp=c47be4f409d88306728d9e52fd356b19c6e07281;hpb=4d81227da330c21c7aa0badc88bd5ad4467067fb;p=evaf diff --git a/src/libs/Common/logger.h b/src/libs/Common/logger.h index c47be4f..742ab90 100644 --- a/src/libs/Common/logger.h +++ b/src/libs/Common/logger.h @@ -3,7 +3,7 @@ * @brief iLogger interface implementation * @author Enar Vaikene * - * Copyright (c) 2011 Enar Vaikene + * Copyright (c) 2011-2019 Enar Vaikene * * This file is part of the eVaf C++ cross-platform application development framework. * @@ -23,6 +23,13 @@ #include "ilogger.h" #include +#include +#include +#include +#include +#include + +class QThread; namespace eVaf { @@ -30,7 +37,7 @@ namespace Common { namespace Internal { /// Default fatal error message handler -void defFatalMsgHandler(QString const & msg, QString const & source, QString const & where); +[[noreturn]] void defFatalMsgHandler(QString const & msg, QString const & source, QString const & where); /** * Logger source. @@ -48,8 +55,6 @@ public: /** * Initializes the source * @param source Name of the source - * @param logDir Full path to the log directory - * @param etcDir Full path to the configuration files directory * * This function initializes the newly created logger source and sets initial * parameters for the source. @@ -67,25 +72,28 @@ public: * Example logger.ini file: * @code * [.default] - * severity = Fatal - * maxSize = 100 - * maxCount = 3 + * log_level = Fatal + * log_size = 100 + * log_count = 3 * * [my-source] - * severity = Warning - * maxSize = 1000 - * maxCount = 10 + * log_level = Warning + * log_size = 1000 + * log_count = 10 * @endcode */ - void init(QString const & source, QString const & logDir, QString const & etcDir); + void init(QString const & source); public: // Members (we don't bother adding getter/setter functions) + /// Name of the source + QString name; + /// Current severity level iLogger::Severity severity; - /// Current log file name + /// File name QString fileName; /// Current maximum size of log files @@ -96,6 +104,30 @@ public: // Members (we don't bother adding getter/setter functions) }; +/** + * Worker class for the logger. + * + * This class separates potentially expensive I/O from the iLogger interface making sure + * that writing to the log file does not block the main thread. + */ +class LoggerWorker : public QObject +{ + Q_OBJECT + +public slots: + + /** + * Writes a message to the log file + * @param src The logger source + * @param msg The message + * + * This function writes the message to the log file. It also controls the size and + * number of log files. + */ + void writeToLogFile(LoggerSource const & src, QString const & msg); + +}; + /** * iLogger interface implementation. * @@ -107,12 +139,17 @@ class Logger : public iLogger public: + /** + * Destroys the iLogger interface instance + */ + static void destroyInstance(); + Logger(); virtual ~Logger(); /** - * Initializes the interface implementation + * Initializes the iLogger interface implementation * @return True if ok; false if initialization failed */ bool init(); @@ -121,52 +158,68 @@ public: iLogger interface */ - virtual QString defaultSource() const { return mDefaultSource; } + virtual QString defaultSource() const; virtual void setDefaultSource(QString const & source); - virtual Severity severity(QString const & source = 0) const; + virtual iLogger::Severity severity(QString const & source = QString()); - virtual void setSeverity(Severity severity, QString const & source = 0); + virtual void setSeverity(iLogger::Severity severity, QString const & source = QString()); - virtual uint maxSize(QString const & source = 0) const; + virtual uint maxSize(QString const & source = QString()); - virtual void setMaxSize(uint maxSize, QString const & source = 0); + virtual void setMaxSize(uint maxSize, QString const & source = QString()); - virtual uint maxCount(QString const & source = 0) const; + virtual uint maxCount(QString const & source = QString()); - virtual void setMaxCount(uint maxCount, QString const & source = 0); + virtual void setMaxCount(uint maxCount, QString const & source = QString()); - virtual Severity consoleSeverity() const { return mConsoleSeverity; } + virtual iLogger::Severity consoleSeverity() const { return mConsoleSeverity; } - virtual void setConsoleSeverity(Severity severity); + virtual void setConsoleSeverity(iLogger::Severity severity); - virtual void write(Severity severity, QString const & msg, QString const & source = 0, QString const & where = 0); + virtual void write(Severity severity, QString const & msg, QString const & source = QString(), QString const & where = QString()); virtual QString printf(char const * const fmt, ...) const; + virtual QString printable(QByteArray const & msg) const; + virtual FatalMsgHandler installFatalMsgHandler(FatalMsgHandler newHandler); +signals: + + void writeToLogFile(LoggerSource const & src, QString const & msg); + + private: // Members + /// Flag indicating that logger is fully initialized + bool mReady; + /// Current fatal error message handler FatalMsgHandler mFatalMsgHandler; - /// Current default source (defaults to "evaf") - QString mDefaultSource; + /// Console output severity level + iLogger::Severity mConsoleSeverity; + + /// Default logger source + QExplicitlySharedDataPointer mDefaultSource; - /// Logger sources + /// Other logger sources QHash > mSources; + /// Worker thread + QScopedPointer mThread; + + /// Worker object + QScopedPointer mWorker; -private: // Methods - /// Returns the source by the name - LoggerSource * getSource(QString const & name) const; +private: // Methods - /// Creates a new source - LoggerSource * addSource(QString const & name); + /// Returns the source by the name. The source is created if it does not exist yet. + LoggerSource * getSource(QString const & name = QString()); #ifdef Q_OS_WIN32 /// Changes text colors on the Windows console