#include <QSettings>
#include "common.h"
-class Logger
+namespace Situare
{
-public:
- /** Logging level
- * Exists as "LogLevel" entry in Situare.conf
- * Valid values:
- * QtDebugMsg -> 0
- * QtWarningMsg -> 1
- * QtCriticalMsg -> 2
- * QtFatalMsg -> 3
- * QtSystemMsg -> QtCriticalMsg -> 2
- * @see QtMsgType
- * Default: QtSystemMsg
- */
- static QtMsgType LOG_LEVEL;
-
-public:
- Logger(QtMsgType level, const char* msg) :
- level(level),
- msg(msg)
+ class Logger
{
- static bool initialized = false;
- if (!initialized) {
- const QSettings settings(SETTINGS_ORGANIZATION_NAME, SETTINGS_APPLICATION_NAME);
- LOG_LEVEL = static_cast<QtMsgType>(settings.value("LogLevel", (int)LOG_LEVEL).toInt());
- initialized = true;
+ public:
+
+ public:
+ Logger(QtMsgType level, const char* msg) :
+ level(level),
+ msg(msg)
+ {
+ if (matchLogLevel(level)) {
+ time.start();
+ QDebug(QtDebugMsg) << msg << "enter";
+ }
}
- if (LOG_LEVEL <= level) {
- time.start();
- QDebug(QtDebugMsg) << msg << "enter";
+ ~Logger()
+ {
+ if (matchLogLevel(level)) {
+ QDebug(level) << msg << "exit, elapsed:" << time.elapsed() << "ms";
+ }
}
- }
- ~Logger()
- {
- if (LOG_LEVEL <= level) {
- QDebug(level) << msg << "exit, elapsed:" << time.elapsed() << "ms";
- }
- }
-private:
- QTime time;
- QtMsgType level;
- const char* msg;
-};
+ /** Test if given logging level matches with application logging level
+ * Application logging level exists as "LogLevel" entry in Situare.conf
+ * Valid values:
+ * QtDebugMsg -> 0
+ * QtWarningMsg -> 1
+ * QtCriticalMsg -> 2
+ * QtFatalMsg -> 3
+ * QtSystemMsg -> QtCriticalMsg -> 2
+ * @see QtMsgType
+ */
+ static bool matchLogLevel(QtMsgType level)
+ {
+ static const QSettings settings(SETTINGS_ORGANIZATION_NAME, SETTINGS_APPLICATION_NAME);
+ static QtMsgType LOG_LEVEL = static_cast<QtMsgType>(settings.value("LogLevel", (int)QtSystemMsg).toInt());
+ return LOG_LEVEL <= level;
+ }
-QtMsgType Logger::LOG_LEVEL = QtSystemMsg;
+ private:
+ QTime time;
+ QtMsgType level;
+ const char* msg;
+ };
+}
/** @brief Log function time
*/
-#define DEBUG_FUNCTION_TIME Logger function_time_logger(QtDebugMsg, __PRETTY_FUNCTION__)
+#define DEBUG_FUNCTION_TIME Situare::Logger function_time_logger(QtDebugMsg, __PRETTY_FUNCTION__)
#endif // LOGGER_H