DtkCore
DTK Core module
Logger.h
1/*
2 Copyright (c) 2012 Boris Moiseev (cyberbobs at gmail dot com)
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License version 2.1
6 as published by the Free Software Foundation and appearing in the file
7 LICENSE.LGPL included in the packaging of this file.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
13*/
14#ifndef LOGGER_H
15#define LOGGER_H
16
17// Qt
18#include <QString>
19#include <QDebug>
20#include <QDateTime>
21
22// Local
23#include "CuteLogger_global.h"
24
25DCORE_BEGIN_NAMESPACE
26
27class AbstractAppender;
28class Logger;
29CUTELOGGERSHARED_EXPORT Logger *loggerInstance();
30#define logger loggerInstance()
31
32
33#define dTrace CuteMessageLogger(loggerInstance(), Logger::Trace, __FILE__, __LINE__, Q_FUNC_INFO).write
34#define dDebug CuteMessageLogger(loggerInstance(), Logger::Debug, __FILE__, __LINE__, Q_FUNC_INFO).write
35#define dInfo CuteMessageLogger(loggerInstance(), Logger::Info, __FILE__, __LINE__, Q_FUNC_INFO).write
36#define dWarning CuteMessageLogger(loggerInstance(), Logger::Warning, __FILE__, __LINE__, Q_FUNC_INFO).write
37#define dError CuteMessageLogger(loggerInstance(), Logger::Error, __FILE__, __LINE__, Q_FUNC_INFO).write
38#define dFatal CuteMessageLogger(loggerInstance(), Logger::Fatal, __FILE__, __LINE__, Q_FUNC_INFO).write
39
40#define dCTrace(category) CuteMessageLogger(loggerInstance(), Logger::Trace, __FILE__, __LINE__, Q_FUNC_INFO, category).write()
41#define dCDebug(category) CuteMessageLogger(loggerInstance(), Logger::Debug, __FILE__, __LINE__, Q_FUNC_INFO, category).write()
42#define dCInfo(category) CuteMessageLogger(loggerInstance(), Logger::Info, __FILE__, __LINE__, Q_FUNC_INFO, category).write()
43#define dCWarning(category) CuteMessageLogger(loggerInstance(), Logger::Warning, __FILE__, __LINE__, Q_FUNC_INFO, category).write()
44#define dCError(category) CuteMessageLogger(loggerInstance(), Logger::Error, __FILE__, __LINE__, Q_FUNC_INFO, category).write()
45#define dCFatal(category) CuteMessageLogger(loggerInstance(), Logger::Fatal, __FILE__, __LINE__, Q_FUNC_INFO, category).write()
46
47#define dTraceTime LoggerTimingHelper loggerTimingHelper(loggerInstance(), Logger::Trace, __FILE__, __LINE__, Q_FUNC_INFO); loggerTimingHelper.start
48#define dDebugTime LoggerTimingHelper loggerTimingHelper(loggerInstance(), Logger::Debug, __FILE__, __LINE__, Q_FUNC_INFO); loggerTimingHelper.start
49#define dInfoTime LoggerTimingHelper loggerTimingHelper(loggerInstance(), Logger::Info, __FILE__, __LINE__, Q_FUNC_INFO); loggerTimingHelper.start
50
51#define dAssert(cond) ((!(cond)) ? loggerInstance()->writeAssert(__FILE__, __LINE__, Q_FUNC_INFO, #cond) : qt_noop())
52#define dAssertX(cond, msg) ((!(cond)) ? loggerInstance()->writeAssert(__FILE__, __LINE__, Q_FUNC_INFO, msg) : qt_noop())
53
54#define dCategory(category) \
55 private:\
56 Logger* loggerInstance()\
57 {\
58 static Logger customLoggerInstance(category);\
59 return &customLoggerInstance;\
60 }\
61
62#define dGlobalCategory(category) \
63 private:\
64 Logger* loggerInstance()\
65 {\
66 static Logger customLoggerInstance(category);\
67 customLoggerInstance.logToGlobalInstance(category, true);\
68 return &customLoggerInstance;\
69 }\
70
71
72 class LoggerPrivate;
73 class CUTELOGGERSHARED_EXPORT Logger
74 {
75 Q_DISABLE_COPY(Logger)
76
77 public:
78 Logger();
79 Logger(const QString &defaultCategory);
80 ~Logger();
81
83 enum LogLevel {
89 Fatal
90 };
91
92 static QString levelToString(LogLevel logLevel);
93 static LogLevel levelFromString(const QString &s);
94
95 static Logger *globalInstance();
96
97 void registerAppender(AbstractAppender *appender);
98 void registerCategoryAppender(const QString &category, AbstractAppender *appender);
99
100 void logToGlobalInstance(const QString &category, bool logToGlobal = false);
101
102 void setDefaultCategory(const QString &category);
103 QString defaultCategory() const;
104
105 void write(const QDateTime &timeStamp, LogLevel logLevel, const char *file, int line, const char *function, const char *category,
106 const QString &message);
107 void write(LogLevel logLevel, const char *file, int line, const char *function, const char *category, const QString &message);
108 QDebug write(LogLevel logLevel, const char *file, int line, const char *function, const char *category);
109
110 void writeAssert(const char *file, int line, const char *function, const char *condition);
111
112 private:
113 void write(const QDateTime &timeStamp, LogLevel logLevel, const char *file, int line, const char *function, const char *category,
114 const QString &message, bool fromLocalInstance);
115 Q_DECLARE_PRIVATE(Logger)
116 LoggerPrivate *d_ptr;
117 };
118
119
120 class CUTELOGGERSHARED_EXPORT CuteMessageLogger
121 {
122 Q_DISABLE_COPY(CuteMessageLogger)
123
124 public:
125 Q_DECL_CONSTEXPR CuteMessageLogger(Logger *l, Logger::LogLevel level, const char *file, int line, const char *function)
126 : m_l(l),
127 m_level(level),
128 m_file(file),
129 m_line(line),
130 m_function(function),
131 m_category(0)
132 {}
133
134 Q_DECL_CONSTEXPR CuteMessageLogger(Logger *l, Logger::LogLevel level, const char *file, int line, const char *function, const char *category)
135 : m_l(l),
136 m_level(level),
137 m_file(file),
138 m_line(line),
139 m_function(function),
140 m_category(category)
141 {}
142
143 void write(const char *msg, ...) const
144#if defined(Q_CC_GNU) && !defined(__INSURE__)
145# if defined(Q_CC_MINGW) && !defined(Q_CC_CLANG)
146 __attribute__((format(gnu_printf, 2, 3)))
147# else
148 __attribute__((format(printf, 2, 3)))
149# endif
150#endif
151 ;
152
153 void write(const QString &msg) const;
154
155 QDebug write() const;
156
157 private:
158 Logger *m_l;
159 Logger::LogLevel m_level;
160 const char *m_file;
161 int m_line;
162 const char *m_function;
163 const char *m_category;
164 };
165
166
167 class CUTELOGGERSHARED_EXPORT LoggerTimingHelper
168 {
169 Q_DISABLE_COPY(LoggerTimingHelper)
170
171 public:
172 inline explicit LoggerTimingHelper(Logger *l, Logger::LogLevel logLevel, const char *file, int line,
173 const char *function)
174 : m_logger(l),
175 m_logLevel(logLevel),
176 m_file(file),
177 m_line(line),
178 m_function(function)
179 {}
180
181 void start(const char *msg, ...)
182#if defined(Q_CC_GNU) && !defined(__INSURE__)
183# if defined(Q_CC_MINGW) && !defined(Q_CC_CLANG)
184 __attribute__((format(gnu_printf, 2, 3)))
185# else
186 __attribute__((format(printf, 2, 3)))
187# endif
188#endif
189 ;
190
191 void start(const QString &msg = QString());
192
194
195 private:
196 Logger *m_logger;
197 QTime m_time;
198 Logger::LogLevel m_logLevel;
199 const char *m_file;
200 int m_line;
201 const char *m_function;
202 QString m_block;
203 };
204
205 DCORE_END_NAMESPACE
206
207#endif // LOGGER_H
The AbstractAppender class provides an abstract base class for writing a log entries.
Definition: AbstractAppender.h:27
Definition: Logger.h:121
Definition: Logger.cpp:513
Definition: Logger.h:168
Very simple but rather powerful component which may be used for logging your application activities.
Definition: Logger.h:74
LogLevel
Describes the possible severity levels of the log records
Definition: Logger.h:83
@ Debug
Debug level. Useful for non-necessary records used for the debugging of the software.
Definition: Logger.h:85
@ Warning
Warning. May be used to log some non-fatal warnings detected by your application.
Definition: Logger.h:87
@ Error
Error. May be used for a big problems making your application work wrong but not crashing.
Definition: Logger.h:88
@ Trace
Trace level. Can be used for mostly unneeded records used for internal code tracing.
Definition: Logger.h:84
@ Info
Info level. Can be used for informational records, which may be interesting for not only developers.
Definition: Logger.h:86