summaryrefslogtreecommitdiff
path: root/rbutil/rbutilqt/logger/include
diff options
context:
space:
mode:
Diffstat (limited to 'rbutil/rbutilqt/logger/include')
-rw-r--r--rbutil/rbutilqt/logger/include/AbstractAppender.h49
-rw-r--r--rbutil/rbutilqt/logger/include/AbstractStringAppender.h46
-rw-r--r--rbutil/rbutilqt/logger/include/ConsoleAppender.h36
-rw-r--r--rbutil/rbutilqt/logger/include/CuteLogger_global.h16
-rw-r--r--rbutil/rbutilqt/logger/include/FileAppender.h49
-rw-r--r--rbutil/rbutilqt/logger/include/Logger.h237
-rw-r--r--rbutil/rbutilqt/logger/include/OutputDebugAppender.h29
7 files changed, 462 insertions, 0 deletions
diff --git a/rbutil/rbutilqt/logger/include/AbstractAppender.h b/rbutil/rbutilqt/logger/include/AbstractAppender.h
new file mode 100644
index 0000000000..e029b045aa
--- /dev/null
+++ b/rbutil/rbutilqt/logger/include/AbstractAppender.h
@@ -0,0 +1,49 @@
1/*
2 Copyright (c) 2010 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 ABSTRACTAPPENDER_H
15#define ABSTRACTAPPENDER_H
16
17// Local
18#include "CuteLogger_global.h"
19#include <Logger.h>
20
21// Qt
22#include <QMutex>
23
24
25class CUTELOGGERSHARED_EXPORT AbstractAppender
26{
27 public:
28 AbstractAppender();
29 virtual ~AbstractAppender();
30
31 Logger::LogLevel detailsLevel() const;
32 void setDetailsLevel(Logger::LogLevel level);
33 void setDetailsLevel(const QString& level);
34
35 void write(const QDateTime& timeStamp, Logger::LogLevel logLevel, const char* file, int line, const char* function,
36 const QString& category, const QString& message);
37
38 protected:
39 virtual void append(const QDateTime& timeStamp, Logger::LogLevel logLevel, const char* file, int line,
40 const char* function, const QString& category, const QString& message) = 0;
41
42 private:
43 QMutex m_writeMutex;
44
45 Logger::LogLevel m_detailsLevel;
46 mutable QMutex m_detailsLevelMutex;
47};
48
49#endif // ABSTRACTAPPENDER_H
diff --git a/rbutil/rbutilqt/logger/include/AbstractStringAppender.h b/rbutil/rbutilqt/logger/include/AbstractStringAppender.h
new file mode 100644
index 0000000000..78df9e6176
--- /dev/null
+++ b/rbutil/rbutilqt/logger/include/AbstractStringAppender.h
@@ -0,0 +1,46 @@
1/*
2 Copyright (c) 2010 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 ABSTRACTSTRINGAPPENDER_H
15#define ABSTRACTSTRINGAPPENDER_H
16
17// Local
18#include "CuteLogger_global.h"
19#include <AbstractAppender.h>
20
21// Qt
22#include <QReadWriteLock>
23
24
25class CUTELOGGERSHARED_EXPORT AbstractStringAppender : public AbstractAppender
26{
27 public:
28 AbstractStringAppender();
29
30 virtual QString format() const;
31 void setFormat(const QString&);
32
33 static QString stripFunctionName(const char*);
34
35 protected:
36 QString formattedString(const QDateTime& timeStamp, Logger::LogLevel logLevel, const char* file, int line,
37 const char* function, const QString& category, const QString& message) const;
38
39 private:
40 static QByteArray qCleanupFuncinfo(const char*);
41
42 QString m_format;
43 mutable QReadWriteLock m_formatLock;
44};
45
46#endif // ABSTRACTSTRINGAPPENDER_H
diff --git a/rbutil/rbutilqt/logger/include/ConsoleAppender.h b/rbutil/rbutilqt/logger/include/ConsoleAppender.h
new file mode 100644
index 0000000000..64ef2e7a19
--- /dev/null
+++ b/rbutil/rbutilqt/logger/include/ConsoleAppender.h
@@ -0,0 +1,36 @@
1/*
2 Copyright (c) 2010 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 CONSOLEAPPENDER_H
15#define CONSOLEAPPENDER_H
16
17#include "CuteLogger_global.h"
18#include <AbstractStringAppender.h>
19
20
21class CUTELOGGERSHARED_EXPORT ConsoleAppender : public AbstractStringAppender
22{
23 public:
24 ConsoleAppender();
25 virtual QString format() const;
26 void ignoreEnvironmentPattern(bool ignore);
27
28 protected:
29 virtual void append(const QDateTime& timeStamp, Logger::LogLevel logLevel, const char* file, int line,
30 const char* function, const QString& category, const QString& message);
31
32 private:
33 bool m_ignoreEnvPattern;
34};
35
36#endif // CONSOLEAPPENDER_H
diff --git a/rbutil/rbutilqt/logger/include/CuteLogger_global.h b/rbutil/rbutilqt/logger/include/CuteLogger_global.h
new file mode 100644
index 0000000000..c5e7680845
--- /dev/null
+++ b/rbutil/rbutilqt/logger/include/CuteLogger_global.h
@@ -0,0 +1,16 @@
1#ifndef CUTELOGGER_GLOBAL_H
2#define CUTELOGGER_GLOBAL_H
3
4#include <QtCore/qglobal.h>
5
6#if !defined(CUTELOGGER_STATIC)
7#if defined(CUTELOGGER_LIBRARY)
8# define CUTELOGGERSHARED_EXPORT Q_DECL_EXPORT
9#else
10# define CUTELOGGERSHARED_EXPORT Q_DECL_IMPORT
11#endif
12#else
13#define CUTELOGGERSHARED_EXPORT
14#endif
15
16#endif // CUTELOGGER_GLOBAL_H
diff --git a/rbutil/rbutilqt/logger/include/FileAppender.h b/rbutil/rbutilqt/logger/include/FileAppender.h
new file mode 100644
index 0000000000..ab9e12a91d
--- /dev/null
+++ b/rbutil/rbutilqt/logger/include/FileAppender.h
@@ -0,0 +1,49 @@
1/*
2 Copyright (c) 2010 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 FILEAPPENDER_H
15#define FILEAPPENDER_H
16
17// Logger
18#include "CuteLogger_global.h"
19#include <AbstractStringAppender.h>
20
21// Qt
22#include <QFile>
23#include <QTextStream>
24
25
26class CUTELOGGERSHARED_EXPORT FileAppender : public AbstractStringAppender
27{
28 public:
29 FileAppender(const QString& fileName = QString());
30 ~FileAppender();
31
32 QString fileName() const;
33 void setFileName(const QString&);
34
35 bool reopenFile();
36
37 protected:
38 virtual void append(const QDateTime& timeStamp, Logger::LogLevel logLevel, const char* file, int line,
39 const char* function, const QString& category, const QString& message);
40 bool openFile();
41 void closeFile();
42
43 private:
44 QFile m_logFile;
45 QTextStream m_logStream;
46 mutable QMutex m_logFileMutex;
47};
48
49#endif // FILEAPPENDER_H
diff --git a/rbutil/rbutilqt/logger/include/Logger.h b/rbutil/rbutilqt/logger/include/Logger.h
new file mode 100644
index 0000000000..509bc5f435
--- /dev/null
+++ b/rbutil/rbutilqt/logger/include/Logger.h
@@ -0,0 +1,237 @@
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"
24class AbstractAppender;
25
26
27class Logger;
28CUTELOGGERSHARED_EXPORT Logger* cuteLoggerInstance();
29#define cuteLogger cuteLoggerInstance()
30
31
32#define LOG_TRACE CuteMessageLogger(cuteLoggerInstance(), Logger::Trace, __FILE__, __LINE__, Q_FUNC_INFO).write
33#define LOG_DEBUG CuteMessageLogger(cuteLoggerInstance(), Logger::Debug, __FILE__, __LINE__, Q_FUNC_INFO).write
34#define LOG_INFO CuteMessageLogger(cuteLoggerInstance(), Logger::Info, __FILE__, __LINE__, Q_FUNC_INFO).write
35#define LOG_WARNING CuteMessageLogger(cuteLoggerInstance(), Logger::Warning, __FILE__, __LINE__, Q_FUNC_INFO).write
36#define LOG_ERROR CuteMessageLogger(cuteLoggerInstance(), Logger::Error, __FILE__, __LINE__, Q_FUNC_INFO).write
37#define LOG_FATAL CuteMessageLogger(cuteLoggerInstance(), Logger::Fatal, __FILE__, __LINE__, Q_FUNC_INFO).write
38
39#define LOG_CTRACE(category) CuteMessageLogger(cuteLoggerInstance(), Logger::Trace, __FILE__, __LINE__, Q_FUNC_INFO, category).write()
40#define LOG_CDEBUG(category) CuteMessageLogger(cuteLoggerInstance(), Logger::Debug, __FILE__, __LINE__, Q_FUNC_INFO, category).write()
41#define LOG_CINFO(category) CuteMessageLogger(cuteLoggerInstance(), Logger::Info, __FILE__, __LINE__, Q_FUNC_INFO, category).write()
42#define LOG_CWARNING(category) CuteMessageLogger(cuteLoggerInstance(), Logger::Warning, __FILE__, __LINE__, Q_FUNC_INFO, category).write()
43#define LOG_CERROR(category) CuteMessageLogger(cuteLoggerInstance(), Logger::Error, __FILE__, __LINE__, Q_FUNC_INFO, category).write()
44#define LOG_CFATAL(category) CuteMessageLogger(cuteLoggerInstance(), Logger::Fatal, __FILE__, __LINE__, Q_FUNC_INFO, category).write()
45
46#define LOG_TRACE_TIME LoggerTimingHelper loggerTimingHelper(cuteLoggerInstance(), Logger::Trace, __FILE__, __LINE__, Q_FUNC_INFO); loggerTimingHelper.start
47#define LOG_DEBUG_TIME LoggerTimingHelper loggerTimingHelper(cuteLoggerInstance(), Logger::Debug, __FILE__, __LINE__, Q_FUNC_INFO); loggerTimingHelper.start
48#define LOG_INFO_TIME LoggerTimingHelper loggerTimingHelper(cuteLoggerInstance(), Logger::Info, __FILE__, __LINE__, Q_FUNC_INFO); loggerTimingHelper.start
49
50#define LOG_ASSERT(cond) ((!(cond)) ? cuteLoggerInstance()->writeAssert(__FILE__, __LINE__, Q_FUNC_INFO, #cond) : qt_noop())
51#define LOG_ASSERT_X(cond, msg) ((!(cond)) ? cuteLoggerInstance()->writeAssert(__FILE__, __LINE__, Q_FUNC_INFO, msg) : qt_noop())
52
53#if (__cplusplus >= 201103L)
54#include <functional>
55
56#define LOG_CATEGORY(category) \
57 Logger customCuteLoggerInstance{category};\
58 std::function<Logger*()> cuteLoggerInstance = [&customCuteLoggerInstance]() {\
59 return &customCuteLoggerInstance;\
60 };\
61
62#define LOG_GLOBAL_CATEGORY(category) \
63 Logger customCuteLoggerInstance{category, true};\
64 std::function<Logger*()> cuteLoggerInstance = [&customCuteLoggerInstance]() {\
65 return &customCuteLoggerInstance;\
66 };\
67
68#else
69
70#define LOG_CATEGORY(category) \
71 Logger* cuteLoggerInstance()\
72 {\
73 static Logger customCuteLoggerInstance(category);\
74 return &customCuteLoggerInstance;\
75 }\
76
77#define LOG_GLOBAL_CATEGORY(category) \
78 Logger* cuteLoggerInstance()\
79 {\
80 static Logger customCuteLoggerInstance(category);\
81 customCuteLoggerInstance.logToGlobalInstance(category, true);\
82 return &customCuteLoggerInstance;\
83 }\
84
85#endif
86
87
88class LoggerPrivate;
89class CUTELOGGERSHARED_EXPORT Logger
90{
91 Q_DISABLE_COPY(Logger)
92
93 public:
94 Logger();
95 Logger(const QString& defaultCategory, bool writeToGlobalInstance = false);
96 ~Logger();
97
98 //! Describes the possible severity levels of the log records
99 enum LogLevel
100 {
101 Trace, //!< Trace level. Can be used for mostly unneeded records used for internal code tracing.
102 Debug, //!< Debug level. Useful for non-necessary records used for the debugging of the software.
103 Info, //!< Info level. Can be used for informational records, which may be interesting for not only developers.
104 Warning, //!< Warning. May be used to log some non-fatal warnings detected by your application.
105 Error, //!< Error. May be used for a big problems making your application work wrong but not crashing.
106 Fatal //!< Fatal. Used for unrecoverable errors, crashes the application right after the log record is written.
107 };
108
109 //! Sets the timing display mode for the LOG_TRACE_TIME, LOG_DEBUG_TIME and LOG_INFO_TIME macros
110 enum TimingMode
111 {
112 TimingAuto, //!< Show time in seconds, if it exceeds 10s (default)
113 TimingMs //!< Always use milliseconds to display
114 };
115
116 static QString levelToString(LogLevel logLevel);
117 static LogLevel levelFromString(const QString& s);
118
119 static Logger* globalInstance();
120
121 void registerAppender(AbstractAppender* appender);
122 void registerCategoryAppender(const QString& category, AbstractAppender* appender);
123
124 void removeAppender(AbstractAppender* appender);
125
126 void logToGlobalInstance(const QString& category, bool logToGlobal = false);
127
128 void setDefaultCategory(const QString& category);
129 QString defaultCategory() const;
130
131 void write(const QDateTime& timeStamp, LogLevel logLevel, const char* file, int line, const char* function, const char* category,
132 const QString& message);
133 void write(LogLevel logLevel, const char* file, int line, const char* function, const char* category, const QString& message);
134
135 void writeAssert(const char* file, int line, const char* function, const char* condition);
136
137 private:
138 void write(const QDateTime& timeStamp, LogLevel logLevel, const char* file, int line, const char* function, const char* category,
139 const QString& message, bool fromLocalInstance);
140 Q_DECLARE_PRIVATE(Logger)
141 LoggerPrivate* d_ptr;
142};
143
144
145class CUTELOGGERSHARED_EXPORT CuteMessageLogger
146{
147 Q_DISABLE_COPY(CuteMessageLogger)
148
149 public:
150 CuteMessageLogger(Logger* l, Logger::LogLevel level, const char* file, int line, const char* function)
151 : m_l(l),
152 m_level(level),
153 m_file(file),
154 m_line(line),
155 m_function(function),
156 m_category(nullptr)
157 {}
158
159 CuteMessageLogger(Logger* l, Logger::LogLevel level, const char* file, int line, const char* function, const char* category)
160 : m_l(l),
161 m_level(level),
162 m_file(file),
163 m_line(line),
164 m_function(function),
165 m_category(category)
166 {}
167
168 ~CuteMessageLogger();
169
170 void write(const char* msg, ...)
171#if defined(Q_CC_GNU) && !defined(__INSURE__)
172# if defined(Q_CC_MINGW) && !defined(Q_CC_CLANG)
173 __attribute__ ((format (gnu_printf, 2, 3)))
174# else
175 __attribute__ ((format (printf, 2, 3)))
176# endif
177#endif
178 ;
179
180 void write(const QString& msg);
181
182 QDebug write();
183
184 private:
185 Logger* m_l;
186 Logger::LogLevel m_level;
187 const char* m_file;
188 int m_line;
189 const char* m_function;
190 const char* m_category;
191 QString m_message;
192};
193
194
195class CUTELOGGERSHARED_EXPORT LoggerTimingHelper
196{
197 Q_DISABLE_COPY(LoggerTimingHelper)
198
199 public:
200 inline explicit LoggerTimingHelper(Logger* l, Logger::LogLevel logLevel, const char* file, int line,
201 const char* function)
202 : m_logger(l),
203 m_logLevel(logLevel),
204 m_timingMode(Logger::TimingAuto),
205 m_file(file),
206 m_line(line),
207 m_function(function)
208 {}
209
210 void start(const char* msg, ...)
211#if defined(Q_CC_GNU) && !defined(__INSURE__)
212 # if defined(Q_CC_MINGW) && !defined(Q_CC_CLANG)
213 __attribute__ ((format (gnu_printf, 2, 3)))
214 # else
215 __attribute__ ((format (printf, 2, 3)))
216 # endif
217#endif
218 ;
219
220 void start(const QString& msg = QString());
221 void start(Logger::TimingMode mode, const QString& msg);
222
223 ~LoggerTimingHelper();
224
225 private:
226 Logger* m_logger;
227 QTime m_time;
228 Logger::LogLevel m_logLevel;
229 Logger::TimingMode m_timingMode;
230 const char* m_file;
231 int m_line;
232 const char* m_function;
233 QString m_block;
234};
235
236
237#endif // LOGGER_H
diff --git a/rbutil/rbutilqt/logger/include/OutputDebugAppender.h b/rbutil/rbutilqt/logger/include/OutputDebugAppender.h
new file mode 100644
index 0000000000..dd7ad4deb7
--- /dev/null
+++ b/rbutil/rbutilqt/logger/include/OutputDebugAppender.h
@@ -0,0 +1,29 @@
1/*
2 Copyright (c) 2010 Karl-Heinz Reichel (khreichel at googlemail 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
15#ifndef OUTPUTDEBUGAPPENDER_H
16#define OUTPUTDEBUGAPPENDER_H
17
18#include "CuteLogger_global.h"
19#include <AbstractStringAppender.h>
20
21
22class CUTELOGGERSHARED_EXPORT OutputDebugAppender : public AbstractStringAppender
23{
24 protected:
25 virtual void append(const QDateTime& timeStamp, Logger::LogLevel logLevel, const char* file, int line,
26 const char* function, const QString& category, const QString& message);
27};
28
29#endif // OUTPUTDEBUGAPPENDER_H