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 #include <QElapsedTimer>
22 
23 // Local
24 #include "CuteLogger_global.h"
25 class AbstractAppender;
26 
27 
28 class Logger;
29 CUTELOGGERSHARED_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 #define LOG_CATEGORY(category) \
55  private:\
56  Logger* cuteLoggerInstance()\
57  {\
58  static Logger customCuteLoggerInstance(category);\
59  return &customCuteLoggerInstance;\
60  }\
61 
62 #define LOG_GLOBAL_CATEGORY(category) \
63  private:\
64  Logger* cuteLoggerInstance()\
65  {\
66  static Logger customCuteLoggerInstance(category);\
67  customCuteLoggerInstance.logToGlobalInstance(category, true);\
68  return &customCuteLoggerInstance;\
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
84  {
87  Info,
90  Fatal
91  };
92 
95  {
97  TimingMs
98  };
99 
100  static QString levelToString(LogLevel logLevel);
101  static LogLevel levelFromString(const QString& s);
102 
103  static Logger* globalInstance();
104 
105  void registerAppender(AbstractAppender* appender);
106  void registerCategoryAppender(const QString& category, AbstractAppender* appender);
107 
108  void removeAppender(AbstractAppender* appender);
109 
110  void logToGlobalInstance(const QString& category, bool logToGlobal = false);
111 
112  void setDefaultCategory(const QString& category);
113  QString defaultCategory() const;
114 
115  void write(const QDateTime& timeStamp, LogLevel logLevel, const char* file, int line, const char* function, const char* category,
116  const QString& message);
117  void write(LogLevel logLevel, const char* file, int line, const char* function, const char* category, const QString& message);
118  QDebug write(LogLevel logLevel, const char* file, int line, const char* function, const char* category);
119 
120  void writeAssert(const char* file, int line, const char* function, const char* condition);
121 
122  private:
123  void write(const QDateTime& timeStamp, LogLevel logLevel, const char* file, int line, const char* function, const char* category,
124  const QString& message, bool fromLocalInstance);
125  Q_DECLARE_PRIVATE(Logger)
126  LoggerPrivate* d_ptr;
127 };
128 
129 
130 class CUTELOGGERSHARED_EXPORT CuteMessageLogger
131 {
132  Q_DISABLE_COPY(CuteMessageLogger)
133 
134  public:
135  Q_DECL_CONSTEXPR CuteMessageLogger(Logger* l, Logger::LogLevel level, const char* file, int line, const char* function)
136  : m_l(l),
137  m_level(level),
138  m_file(file),
139  m_line(line),
140  m_function(function),
141  m_category(0)
142  {}
143 
144  Q_DECL_CONSTEXPR CuteMessageLogger(Logger* l, Logger::LogLevel level, const char* file, int line, const char* function, const char* category)
145  : m_l(l),
146  m_level(level),
147  m_file(file),
148  m_line(line),
149  m_function(function),
150  m_category(category)
151  {}
152 
153  void write(const char* msg, ...) const
154 #if defined(Q_CC_GNU) && !defined(__INSURE__)
155 # if defined(Q_CC_MINGW) && !defined(Q_CC_CLANG)
156  __attribute__ ((format (gnu_printf, 2, 3)))
157 # else
158  __attribute__ ((format (printf, 2, 3)))
159 # endif
160 #endif
161  ;
162 
163  void write(const QString& msg) const;
164 
165  QDebug write() const;
166 
167  private:
168  Logger* m_l;
169  Logger::LogLevel m_level;
170  const char* m_file;
171  int m_line;
172  const char* m_function;
173  const char* m_category;
174 };
175 
176 
177 class CUTELOGGERSHARED_EXPORT LoggerTimingHelper
178 {
179  Q_DISABLE_COPY(LoggerTimingHelper)
180 
181  public:
182  inline explicit LoggerTimingHelper(Logger* l, Logger::LogLevel logLevel, const char* file, int line,
183  const char* function)
184  : m_logger(l),
185  m_logLevel(logLevel),
186  m_timingMode(Logger::TimingAuto),
187  m_file(file),
188  m_line(line),
189  m_function(function)
190  {}
191 
192  void start(const char* msg, ...)
193 #if defined(Q_CC_GNU) && !defined(__INSURE__)
194  # if defined(Q_CC_MINGW) && !defined(Q_CC_CLANG)
195  __attribute__ ((format (gnu_printf, 2, 3)))
196  # else
197  __attribute__ ((format (printf, 2, 3)))
198  # endif
199 #endif
200  ;
201 
202  void start(const QString& msg = QString());
203  void start(Logger::TimingMode mode, const QString& msg);
204 
205  ~LoggerTimingHelper();
206 
207  private:
208  Logger* m_logger;
209  QElapsedTimer m_time;
210  Logger::LogLevel m_logLevel;
211  Logger::TimingMode m_timingMode;
212  const char* m_file;
213  int m_line;
214  const char* m_function;
215  QString m_block;
216 };
217 
218 
219 #endif // LOGGER_H
The AbstractAppender class provides an abstract base class for writing a log entries.
Definition: AbstractAppender.h:26
Very simple but rather powerful component which may be used for logging your application activities.
Definition: Logger.h:74
TimingMode
Sets the timing display mode for the LOG_TRACE_TIME, LOG_DEBUG_TIME and LOG_INFO_TIME macros.
Definition: Logger.h:95
@ TimingAuto
Show time in seconds, if it exceeds 10s (default)
Definition: Logger.h:96
LogLevel
Describes the possible severity levels of the log records.
Definition: Logger.h:84
@ Debug
Debug level. Useful for non-necessary records used for the debugging of the software.
Definition: Logger.h:86
@ Warning
Warning. May be used to log some non-fatal warnings detected by your application.
Definition: Logger.h:88
@ Trace
Trace level. Can be used for mostly unneeded records used for internal code tracing.
Definition: Logger.h:85
@ Info
Info level. Can be used for informational records, which may be interesting for not only developers.
Definition: Logger.h:87
@ Error
Error. May be used for a big problems making your application work wrong but not crashing.
Definition: Logger.h:89