GemaCoreLib
The GeMA Core library
gmLog.h
Go to the documentation of this file.
1 /************************************************************************
2 **
3 ** Copyright (C) 2014 by Carlos Augusto Teixera Mendes
4 ** All rights reserved.
5 **
6 ** This file is part of the "GeMA" software. It's use should respect
7 ** the terms in the license agreement that can be found together
8 ** with this source code.
9 ** It is provided AS IS, with NO WARRANTY OF ANY KIND,
10 ** INCLUDING THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR
11 ** A PARTICULAR PURPOSE.
12 **
13 ************************************************************************/
14 
25 #ifndef _GEMA_LOG_H_
26 #define _GEMA_LOG_H_
27 
28 #include "gmCoreConfig.h"
29 
30 #include <QLoggingCategory>
31 #include <QElapsedTimer>
32 
33 class QStringList;
34 
37 {
46  // ATTENTION: Do not add values bellow this line
47  // Also remember to adjust the GmLogCategory constructor + GmLogCategory::str2Level() if adding a new category
49 };
50 
59 {
60 public:
61  GmLogCategory(QString name);
62  ~GmLogCategory();
63 
65  bool isEnabled(GmLogLevel level) const { return _categories[level]->isDebugEnabled(); }
66 
68  void setEnabled(GmLogLevel level, bool mode) { _categories[level]->setEnabled(QtDebugMsg, mode); }
69 
71  QLoggingCategory& qtCategory(GmLogLevel level) const { return *_categories[level]; }
72 
73  static GmLogLevel str2Level(const char* levelStr);
74 
75 private:
76  Q_DISABLE_COPY(GmLogCategory);
77 
78  QByteArray _names[GM_COUNT];
79  QLoggingCategory* _categories[GM_COUNT];
80 };
81 
82 #define gmLog(category, level, ...) qCDebug((category).qtCategory(level), __VA_ARGS__)
83 
84 #define gmTrace(category, ...) qCDebug((category).qtCategory(GM_TRACE), __VA_ARGS__)
85 #define gmExtInfo(category, ...) qCDebug((category).qtCategory(GM_EXT_INFO), __VA_ARGS__)
86 #define gmInfo(category, ...) qCDebug((category).qtCategory(GM_INFO), __VA_ARGS__)
87 #define gmWarn(category, ...) qCDebug((category).qtCategory(GM_WARNING), __VA_ARGS__)
88 #define gmError(category, ...) qCDebug((category).qtCategory(GM_ERROR), __VA_ARGS__)
89 #define gmFatal(category, ...) qCDebug((category).qtCategory(GM_FATAL), __VA_ARGS__)
90 #define gmTime(category, ...) qCDebug((category).qtCategory(GM_TIME), __VA_ARGS__)
91 #define gmMemory(category, ...) qCDebug((category).qtCategory(GM_MEMORY), __VA_ARGS__)
92 
93 #define gmLogMsg(category, level, msg) qCDebug((category).qtCategory(level), qPrintable(msg))
94 
95 #define gmTraceMsg(category, msg) qCDebug((category).qtCategory(GM_TRACE), qPrintable(msg))
96 #define gmExtInfoMsg(category, msg) qCDebug((category).qtCategory(GM_EXT_INFO), qPrintable(msg))
97 #define gmInfoMsg(category, msg) qCDebug((category).qtCategory(GM_INFO), qPrintable(msg))
98 #define gmWarnMsg(category, msg) qCDebug((category).qtCategory(GM_WARNING), qPrintable(msg))
99 #define gmErrorMsg(category, msg) qCDebug((category).qtCategory(GM_ERROR), qPrintable(msg))
100 #define gmFatalMsg(category, msg) qCDebug((category).qtCategory(GM_FATAL), qPrintable(msg))
101 #define gmTimeMsg(category, msg) qCDebug((category).qtCategory(GM_TIME), qPrintable(msg))
102 #define gmMemoryMsg(category, msg) qCDebug((category).qtCategory(GM_MEMORY), qPrintable(msg))
103 
106 {
107  Q_OBJECT
108 
109 signals:
110  void logMessage(int level, QString msg);
111 };
112 
113 
114 class LuaTable;
115 
117 GMC_API_EXPORT bool GmSetConsoleLogging(bool mode);
118 GMC_API_EXPORT void GmSetLogFile(QString logfile, bool flush, const GmLogCategory& logger);
121 GMC_API_EXPORT bool GmSetShowLoggerName(bool mode);
122 GMC_API_EXPORT bool GmSetShowThreadId(bool mode);
125 GMC_API_EXPORT void GmSetLogLevels(LuaTable& tab, const QList<QPair<QString, bool> >& extraRules, const GmLogCategory& logger);
126 
128 GMC_API_EXPORT bool GmSetLoggerDecorations(bool mode);
129 
130 GMC_API_EXPORT bool GmSetStringListLogging(bool mode, QStringList* targetList = NULL);
132 
134 GMC_API_EXPORT void GmAddToTimeStatistics(const QString& msg, qint64 elapsedTime);
136 
137 GMC_API_EXPORT const QObject* GmSetLoggingSignal(bool enable);
138 
139 GMC_API_EXPORT void GmLogWrappedMsg(const GmLogCategory& logger, GmLogLevel level, const QString& msg,
140  int maxSize = 120, int maxBreak = 30);
141 
142 GMC_API_EXPORT void GmLogIdent(int offset);
144 
145 GMC_API_EXPORT void GmLogMemoryUsage(const GmLogCategory& logger, const QString& msg);
147 
149 
154 {
155 public:
156  GmLogIdenter(int l = 1) : _l(l) { GmLogIdent(_l); }
157  ~GmLogIdenter() { GmLogIdent(-_l); }
158 private:
159  int _l; // Number of ident levels
160 };
161 
166 {
167 public:
168  GmLogIdentClearer() { _old = GmLogIdentClear(); }
169  ~GmLogIdentClearer() { GmLogIdent(_old); }
170 private:
171  int _old; // Old number of ident levels
172 };
173 
180 {
181 public:
182  GmTimeMsg(const GmLogCategory& logger, QString msg) : _logger(logger), _msg(msg)
183  {
184  if(logger.isEnabled(GM_TIME))
185  {
186  _timer.start();
187  _nestLevel++;
188  }
189  }
190  ~GmTimeMsg()
191  {
192  if(_logger.isEnabled(GM_TIME))
193  {
194  qint64 t = _timer.elapsed();
195  gmTimeMsg(_logger, QObject::tr("%1 [%2] finished in %3 s").arg(_msg).arg(_nestLevel).arg(t/1000.0));
196  _nestLevel--;
197  if(_collectTimeStatistics)
198  GmAddToTimeStatistics(_msg, t);
199  }
200  }
201 
202 private:
203  friend GMC_API_EXPORT bool GmSetCollectTimeStatistics(bool);
205 
207  static int _nestLevel;
208 
212 };
213 
216 {
217 public:
218  GmTimeAdder(qint64* counter) : _counter(counter)
219  {
220  _timer.start();
221  }
222  ~GmTimeAdder()
223  {
225  }
226 
227 private:
229  qint64* _counter;
230 };
231 
232 #endif
233 
void setEnabled(GmLogLevel level, bool mode)
Enables or disables log messges for the requested level.
Definition: gmLog.h:68
GMC_API_EXPORT const QStringList & GmLogStringList()
Returns the logging string list (has contents only if GmSetStringListLogging(true) was called before)
Definition: gmLog.cpp:437
static bool _collectTimeStatistics
A global flag controlling whether we should collect per message statistics. Controled by GmSetCollect...
Definition: gmLog.h:206
GMC_API_EXPORT bool GmSetCollectTimeStatistics(bool mode)
Enables or disables the collection of time statistics from messages emmitted with GmTimeMsg.
Definition: gmLog.cpp:446
Reporting a fatal error. The system cannot continue.
Definition: gmLog.h:43
Declaration of usefull configuration definitions for the Core library.
GMC_API_EXPORT int GmLogIdentClear()
Clears the current identation level, returning the old one.
Definition: gmLog.cpp:625
GMC_API_EXPORT bool GmSetLoggerDecorations(bool mode)
Updates the current logger decoration mode. Returns the previous mode.
Definition: gmLog.cpp:570
QString tr(const char *sourceText, const char *disambiguation, int n)
GMC_API_EXPORT void GmFlushLogFile()
Flushes the log file if enabled.
Definition: gmLog.cpp:365
GMC_API_EXPORT void GmLogIdent(int offset)
Changes the current identation level.
Definition: gmLog.cpp:616
Extended information. Probably more than you wanted.
Definition: gmLog.h:39
GMC_API_EXPORT const GmLogCategory & GmPanicLogger()
Returns the global "panic" logger, usually used to report memory allocation fails deep inside class s...
Definition: gmLog.cpp:700
GMC_API_EXPORT bool GmSetShowThreadId(bool mode)
Enables or disables showing the thread id before each message. Returns the previous mode.
Definition: gmLog.cpp:385
QElapsedTimer _timer
Timer used to measure elapsed time.
Definition: gmLog.h:228
bool isEnabled(GmLogLevel level) const
Returns true if messages for this log level are enabled, false otherwise.
Definition: gmLog.h:65
RAII object used to measure the elapsed time of an operation.
Definition: gmLog.h:179
GMC_API_EXPORT void GmLogTimeStatistics(const GmLogCategory &logger)
Logs the current contents of the time statistics map using the given logger. Expects to be called fro...
Definition: gmLog.cpp:469
GMC_API_EXPORT void GmLogInit()
Initializes the logging facilities.
Definition: gmLog.cpp:285
GMC_API_EXPORT void GmSetLogLevels(const QList< QPair< QString, bool > > &rules)
Load a new set of logging rules from a rules list. Adds the no qt rules option on top of the given on...
Definition: gmLog.cpp:500
GMC_API_EXPORT void GmLogWrappedMsg(const GmLogCategory &logger, GmLogLevel level, const QString &msg, int maxSize=120, int maxBreak=30)
Logs the given message breaking lines bigger than maxSize. Message breaking is preferentially done on...
Definition: gmLog.cpp:584
RAII object to clear the identation level to log messages, restoring to the previous identation when ...
Definition: gmLog.h:165
A warning message reporting something strange but not necessarilly an error.
Definition: gmLog.h:41
Aux class whose only purpose is to allow loggers to emit a logMessage signal, if configured to do so.
Definition: gmLog.h:105
RAII class used to add the elapsed time to a counter received by reference. Counter is incremented in...
Definition: gmLog.h:215
Reporting timing information. New category so as to be easy to disable all timing messages for tests ...
Definition: gmLog.h:44
GMC_API_EXPORT void GmLogMemoryUsage(const GmLogCategory &logger, const QString &msg)
Log the current memory usage and the difference from the last call to GmLogMemoryUsage that printed s...
Definition: gmLog.cpp:638
Reporting an error from which the system can continue (maybe not with its total capacity)
Definition: gmLog.h:42
const GmLogCategory & _logger
The logger used to emmit messages.
Definition: gmLog.h:210
QElapsedTimer _timer
Timer used to measure elapsed time.
Definition: gmLog.h:209
GMC_API_EXPORT bool GmLoggerDecorations(void)
Returns the current logger decoration mode. When set to true, code logging value tables should add de...
Definition: gmLog.cpp:564
GMC_API_EXPORT void GmCloseLogFile()
Closes the log file if enabled.
Definition: gmLog.cpp:352
#define GMC_API_EXPORT
Macro for controling if the class is being exported (GEMA_CORE_LIB defined) or imported (GEMA_CORE_LI...
Definition: gmCoreConfig.h:35
static int _nestLevel
A global value storing the current nesting level (GmTimeMsg objects created in the scope of another o...
Definition: gmLog.h:207
GMC_API_EXPORT const QObject * GmSetLoggingSignal(bool enable)
Enables or disables logging to the logMessage signal. Returns the object that will be used to emit th...
Definition: gmLog.cpp:490
GmLogLevel
Available log levels list.
Definition: gmLog.h:36
Class representing a category with multiple logging levels.
Definition: gmLog.h:58
GMC_API_EXPORT bool GmSetHideRepeatedWarnings(bool mode)
Enables or disables skipping repeated warning messages. Returns the previous mode.
Definition: gmLog.cpp:396
GMC_API_EXPORT bool GmSetShowLoggerName(bool mode)
Enables or disables showing the logger name before each message. Returns the previous mode.
Definition: gmLog.cpp:374
qint64 nsecsElapsed() const const
QLoggingCategory & qtCategory(GmLogLevel level) const
Returns the "Qt category" that represents the requested level.
Definition: gmLog.h:71
QString _msg
Message title.
Definition: gmLog.h:211
Number of log levels.
Definition: gmLog.h:48
GMC_API_EXPORT void GmSetLogFile(QString logfile, bool flush, const GmLogCategory &logger)
Enables and configures file logging.
Definition: gmLog.cpp:328
GMC_API_EXPORT bool GmSetStringListLogging(bool mode, QStringList *targetList=NULL)
Enables or disables logging to a string list. This is usefull for capturing messages for testing purp...
Definition: gmLog.cpp:416
GMC_API_EXPORT QString GmLogFormatMemory(size_t s)
Return a string with the memory size converted to kb, Mb or Gb, adding the unit to the converted (if ...
Definition: gmLog.cpp:683
Trace information to help system debugging. Much more info than you want.
Definition: gmLog.h:38
Information regarding system evolution and config. Should be the "right" level of messages.
Definition: gmLog.h:40
RAII object to add an identation level to log messages, reverting the settings when the object is des...
Definition: gmLog.h:153
GMC_API_EXPORT bool GmSetConsoleLogging(bool mode)
Enables or disables logging to the console. Returns the previous mode.
Definition: gmLog.cpp:311
Reporting memory information. New category so as to be easy to disable all memory messages for tests ...
Definition: gmLog.h:45
qint64 * _counter
The counter where the elapsed time will be added to.
Definition: gmLog.h:229
GMC_API_EXPORT void GmAddToTimeStatistics(const QString &msg, qint64 elapsedTime)
Adds the given time to the global statistics for the giiven message used as identifir in calls to GmT...
Definition: gmLog.cpp:457