]> vaikene.ee Git - evaf/blobdiff - src/libs/Common/logger.h
More work on the common library and the main GUI application.
[evaf] / src / libs / Common / logger.h
diff --git a/src/libs/Common/logger.h b/src/libs/Common/logger.h
new file mode 100644 (file)
index 0000000..c47be4f
--- /dev/null
@@ -0,0 +1,182 @@
+/**
+ * @file Common/logger.h
+ * @brief iLogger interface implementation
+ * @author Enar Vaikene
+ *
+ * Copyright (c) 2011 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 <QObject>
+
+
+namespace eVaf {
+namespace Common {
+namespace Internal {
+
+/// Default fatal error message handler
+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
+     * @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.
+     *
+     * 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]
+     * severity = Fatal
+     * maxSize = 100
+     * maxCount = 3
+     *
+     * [my-source]
+     * severity = Warning
+     * maxSize = 1000
+     * maxCount = 10
+     * @endcode
+     */
+    void init(QString const & source, QString const & logDir, QString const & etcDir);
+
+
+public: // Members (we don't bother adding getter/setter functions)
+
+    /// Current severity level
+    iLogger::Severity severity;
+
+    /// Current log file name
+    QString fileName;
+
+    /// Current maximum size of log files
+    uint maxSize;
+
+    /// Current maximum number of log files
+    uint maxCount;
+
+};
+
+/**
+ * iLogger interface implementation.
+ *
+ * This class implements the iLogger interface.
+ */
+class Logger : public iLogger
+{
+    Q_OBJECT
+
+public:
+
+    Logger();
+
+    virtual ~Logger();
+
+    /**
+     * Initializes the interface implementation
+     * @return True if ok; false if initialization failed
+     */
+    bool init();
+
+    /*
+        iLogger interface
+    */
+
+    virtual QString defaultSource() const { return mDefaultSource; }
+
+    virtual void setDefaultSource(QString const & source);
+
+    virtual Severity severity(QString const & source = 0) const;
+
+    virtual void setSeverity(Severity severity, QString const & source = 0);
+
+    virtual uint maxSize(QString const & source = 0) const;
+
+    virtual void setMaxSize(uint maxSize, QString const & source = 0);
+
+    virtual uint maxCount(QString const & source = 0) const;
+
+    virtual void setMaxCount(uint maxCount, QString const & source = 0);
+
+    virtual Severity consoleSeverity() const { return mConsoleSeverity; }
+
+    virtual void setConsoleSeverity(Severity severity);
+
+    virtual void write(Severity severity, QString const & msg, QString const & source = 0, QString const & where = 0);
+
+    virtual QString printf(char const * const fmt, ...) const;
+
+    virtual FatalMsgHandler installFatalMsgHandler(FatalMsgHandler newHandler);
+
+
+private: // Members
+
+    /// Current fatal error message handler
+    FatalMsgHandler mFatalMsgHandler;
+
+    /// Current default source (defaults to "evaf")
+    QString mDefaultSource;
+
+    /// Logger sources
+    QHash<QString, QExplicitlySharedDataPointer<LoggerSource> > mSources;
+
+
+private: // Methods
+
+    /// Returns the source by the name
+    LoggerSource * getSource(QString const & name) const;
+
+    /// Creates a new source
+    LoggerSource * addSource(QString const & name);
+
+#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