CuteLogger
Fast and simple logging solution for Qt based applications
Logger.h
Go to the documentation of this file.
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 class AbstractAppender;
25 
26 
27 class Logger;
28 CUTELOGGERSHARED_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 
88 class LoggerPrivate;
89 class 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 
99  enum LogLevel
100  {
106  Fatal
107  };
108 
111  {
113  TimingMs
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  QDebug write(LogLevel logLevel, const char* file, int line, const char* function, const char* category);
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 
146 class CUTELOGGERSHARED_EXPORT CuteMessageLogger
147 {
148  Q_DISABLE_COPY(CuteMessageLogger)
149 
150  public:
151  Q_DECL_CONSTEXPR 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  Q_DECL_CONSTEXPR 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  void write(const char* msg, ...) const
170 #if defined(Q_CC_GNU) && !defined(__INSURE__)
171 # if defined(Q_CC_MINGW) && !defined(Q_CC_CLANG)
172  __attribute__ ((format (gnu_printf, 2, 3)))
173 # else
174  __attribute__ ((format (printf, 2, 3)))
175 # endif
176 #endif
177  ;
178 
179  void write(const QString& msg) const;
180 
181  QDebug write() const;
182 
183  private:
184  Logger* m_l;
185  Logger::LogLevel m_level;
186  const char* m_file;
187  int m_line;
188  const char* m_function;
189  const char* m_category;
190 };
191 
192 
193 class CUTELOGGERSHARED_EXPORT LoggerTimingHelper
194 {
195  Q_DISABLE_COPY(LoggerTimingHelper)
196 
197  public:
198  inline explicit LoggerTimingHelper(Logger* l, Logger::LogLevel logLevel, const char* file, int line,
199  const char* function)
200  : m_logger(l),
201  m_logLevel(logLevel),
202  m_timingMode(Logger::TimingAuto),
203  m_file(file),
204  m_line(line),
205  m_function(function)
206  {}
207 
208  void start(const char* msg, ...)
209 #if defined(Q_CC_GNU) && !defined(__INSURE__)
210  # if defined(Q_CC_MINGW) && !defined(Q_CC_CLANG)
211  __attribute__ ((format (gnu_printf, 2, 3)))
212  # else
213  __attribute__ ((format (printf, 2, 3)))
214  # endif
215 #endif
216  ;
217 
218  void start(const QString& msg = QString());
219  void start(Logger::TimingMode mode, const QString& msg);
220 
221  ~LoggerTimingHelper();
222 
223  private:
224  Logger* m_logger;
225  QTime m_time;
226  Logger::LogLevel m_logLevel;
227  Logger::TimingMode m_timingMode;
228  const char* m_file;
229  int m_line;
230  const char* m_function;
231  QString m_block;
232 };
233 
234 
235 #endif // LOGGER_H
Very simple but rather powerful component which may be used for logging your application activities...
Definition: Logger.h:89
Trace level. Can be used for mostly unneeded records used for internal code tracing.
Definition: Logger.h:101
Warning. May be used to log some non-fatal warnings detected by your application. ...
Definition: Logger.h:104
The AbstractAppender class provides an abstract base class for writing a log entries.
Definition: AbstractAppender.h:25
LogLevel
Describes the possible severity levels of the log records.
Definition: Logger.h:99
Info level. Can be used for informational records, which may be interesting for not only developers...
Definition: Logger.h:103
Debug level. Useful for non-necessary records used for the debugging of the software.
Definition: Logger.h:102
Error. May be used for a big problems making your application work wrong but not crashing.
Definition: Logger.h:105
TimingMode
Sets the timing display mode for the LOG_TRACE_TIME, LOG_DEBUG_TIME and LOG_INFO_TIME macros...
Definition: Logger.h:110
Show time in seconds, if it exceeds 10s (default)
Definition: Logger.h:112