Logger added
[situare] / src / logger.h
1 #ifndef LOGGER_H
2 #define LOGGER_H
3
4 #include <QDebug>
5 #include <QTime>
6 #include <QSettings>
7 #include "common.h"
8
9 class Logger
10 {
11 public:
12     /** Logging level
13       * Exists as "LogLevel" entry in Situare.conf
14       * Valid values:
15       * QtDebugMsg -> 0
16       * QtWarningMsg -> 1
17       * QtCriticalMsg -> 2
18       * QtFatalMsg -> 3
19       * QtSystemMsg -> QtCriticalMsg -> 2
20       * @see QtMsgType
21       * Default: QtSystemMsg
22       */
23     static QtMsgType LOG_LEVEL;
24
25 public:
26     Logger(QtMsgType level, const char* msg) :
27             level(level),
28             msg(msg)
29     {
30         static bool initialized = false;
31         if (!initialized) {
32             const QSettings settings(SETTINGS_ORGANIZATION_NAME, SETTINGS_APPLICATION_NAME);
33             LOG_LEVEL = static_cast<QtMsgType>(settings.value("LogLevel", (int)LOG_LEVEL).toInt());
34             initialized = true;
35         }
36         if (LOG_LEVEL <= level) {
37             time.start();
38             QDebug(QtDebugMsg) << msg << "enter";
39         }
40     }
41     ~Logger()
42     {
43         if (LOG_LEVEL <= level) {
44             QDebug(level) << msg << "exit, elapsed:" << time.elapsed() << "ms";
45         }
46     }
47
48 private:
49     QTime time;
50     QtMsgType level;
51     const char* msg;
52 };
53
54 QtMsgType Logger::LOG_LEVEL = QtSystemMsg;
55
56 /** @brief Log function time
57   */
58 #define DEBUG_FUNCTION_TIME Logger function_time_logger(QtDebugMsg, __PRETTY_FUNCTION__)
59
60 #endif // LOGGER_H