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