/** * @file Common/logger.h * @brief iLogger interface implementation * @author Enar Vaikene * * Copyright (c) 2011-2019 Enar Vaikene * * This file is part of the eVaf C++ cross-platform application development framework. * * This file can be used under the terms of the GNU General Public License * version 3.0 as published by the Free Software Foundation and appearing in * the file LICENSE included in the packaging of this file. Please review the * the following information to ensure the GNU General Public License version * 3.0 requirements will be met: http://www.gnu.org/copyleft/gpl.html. * * Alternatively, this file may be used in accordance with the Commercial License * Agreement provided with the Software. */ #ifndef __COMMON_LOGGER_H #define __COMMON_LOGGER_H #include "ilogger.h" #include #include #include #include #include #include class QThread; namespace eVaf { namespace Common { namespace Internal { /// Default fatal error message handler [[noreturn]] void defFatalMsgHandler(QString const & msg, QString const & source, QString const & where); /** * Logger source. * * This class stores information about known sources. */ class LoggerSource : public QSharedData { public: LoggerSource(); LoggerSource(LoggerSource const & o); /** * Initializes the source * @param source Name of the source * * This function initializes the newly created logger source and sets initial * parameters for the source. * * Default parameters: * @li severity is set to Fatal * @li maximum size of the log file is set to 100 KiB * @li maximum number of log files is set to 3 * * Default parameters can be overwritten with values read from the logger.ini file. * This file should have the [.default] section with new default values for all the * sources. Individual sources can have their parameters changed in sections with the * name of the source. * * Example logger.ini file: * @code * [.default] * log_level = Fatal * log_size = 100 * log_count = 3 * * [my-source] * log_level = Warning * log_size = 1000 * log_count = 10 * @endcode */ 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; /// File name QString fileName; /// Current maximum size of log files uint maxSize; /// Current maximum number of log files uint maxCount; }; /** * 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. * * This class implements the iLogger interface. */ class Logger : public iLogger { Q_OBJECT public: /** * Destroys the iLogger interface instance */ static void destroyInstance(); Logger(); virtual ~Logger(); /** * Initializes the iLogger interface implementation * @return True if ok; false if initialization failed */ bool init(); /* iLogger interface */ virtual QString defaultSource() const; virtual void setDefaultSource(QString const & source); virtual iLogger::Severity severity(QString const & source = QString()); virtual void setSeverity(iLogger::Severity severity, QString const & source = QString()); virtual uint maxSize(QString const & source = QString()); virtual void setMaxSize(uint maxSize, QString const & source = QString()); virtual uint maxCount(QString const & source = QString()); virtual void setMaxCount(uint maxCount, QString const & source = QString()); virtual iLogger::Severity consoleSeverity() const { return mConsoleSeverity; } virtual void setConsoleSeverity(iLogger::Severity severity); 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; /// Console output severity level iLogger::Severity mConsoleSeverity; /// Default logger source QExplicitlySharedDataPointer mDefaultSource; /// Other logger sources QHash > mSources; /// Worker thread QScopedPointer mThread; /// Worker object QScopedPointer mWorker; private: // Methods /// 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 void setColor(short int c); #endif }; } // namespace eVaf::Common::Internal } // namespace eVaf::Common } // namespace eVaf #endif // logger.h