Fixed LOG_LEVEL from Logger
[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 namespace Situare
10 {
11     class Logger
12     {
13     public:
14
15     public:
16         Logger(QtMsgType level, const char* msg) :
17                 level(level),
18                 msg(msg)
19         {
20             if (matchLogLevel(level)) {
21                 time.start();
22                 QDebug(QtDebugMsg) << msg << "enter";
23             }
24         }
25         ~Logger()
26         {
27             if (matchLogLevel(level)) {
28                 QDebug(level) << msg << "exit, elapsed:" << time.elapsed() << "ms";
29             }
30         }
31
32         /** Test if given logging level matches with application logging level
33           * Application logging level exists as "LogLevel" entry in Situare.conf
34           * Valid values:
35           * QtDebugMsg -> 0
36           * QtWarningMsg -> 1
37           * QtCriticalMsg -> 2
38           * QtFatalMsg -> 3
39           * QtSystemMsg -> QtCriticalMsg -> 2
40           * @see QtMsgType
41           */
42         static bool matchLogLevel(QtMsgType level)
43         {
44             static const QSettings settings(SETTINGS_ORGANIZATION_NAME, SETTINGS_APPLICATION_NAME);
45             static QtMsgType LOG_LEVEL = static_cast<QtMsgType>(settings.value("LogLevel", (int)QtSystemMsg).toInt());
46             return LOG_LEVEL <= level;
47         }
48
49     private:
50         QTime time;
51         QtMsgType level;
52         const char* msg;
53     };
54 }
55
56 /** @brief Log function time
57   */
58 #define DEBUG_FUNCTION_TIME Situare::Logger function_time_logger(QtDebugMsg, __PRETTY_FUNCTION__)
59
60 #endif // LOGGER_H