GemaCoreLib
The GeMA Core library
Loading...
Searching...
No Matches
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
32class QStringList;
33
36{
45 // ATTENTION: Do not add values below this line
46 // Also remember to adjust the GmLogCategory constructor + GmLogCategory::str2Level() if adding a new category
48};
49
58{
59public:
62
64 bool isEnabled(GmLogLevel level) const { return _categories[level]->isDebugEnabled(); }
65
67 void setEnabled(GmLogLevel level, bool mode) { _categories[level]->setEnabled(QtDebugMsg, mode); }
68
70 QLoggingCategory& qtCategory(GmLogLevel level) const { return *_categories[level]; }
71
72 static GmLogLevel str2Level(const char* levelStr);
73
74private:
75 Q_DISABLE_COPY(GmLogCategory);
76
78 QLoggingCategory* _categories[GM_COUNT];
79};
80
81#define gmLog(category, level, ...) qCDebug((category).qtCategory(level), __VA_ARGS__)
82
83#define gmTrace(category, ...) qCDebug((category).qtCategory(GM_TRACE), __VA_ARGS__)
84#define gmExtInfo(category, ...) qCDebug((category).qtCategory(GM_EXT_INFO), __VA_ARGS__)
85#define gmInfo(category, ...) qCDebug((category).qtCategory(GM_INFO), __VA_ARGS__)
86#define gmWarn(category, ...) qCDebug((category).qtCategory(GM_WARNING), __VA_ARGS__)
87#define gmError(category, ...) qCDebug((category).qtCategory(GM_ERROR), __VA_ARGS__)
88#define gmFatal(category, ...) qCDebug((category).qtCategory(GM_FATAL), __VA_ARGS__)
89#define gmTime(category, ...) qCDebug((category).qtCategory(GM_TIME), __VA_ARGS__)
90#define gmMemory(category, ...) qCDebug((category).qtCategory(GM_MEMORY), __VA_ARGS__)
91
92#define gmLogMsg(category, level, msg) qCDebug((category).qtCategory(level), qPrintable(msg))
93
94#define gmTraceMsg(category, msg) qCDebug((category).qtCategory(GM_TRACE), qPrintable(msg))
95#define gmExtInfoMsg(category, msg) qCDebug((category).qtCategory(GM_EXT_INFO), qPrintable(msg))
96#define gmInfoMsg(category, msg) qCDebug((category).qtCategory(GM_INFO), qPrintable(msg))
97#define gmWarnMsg(category, msg) qCDebug((category).qtCategory(GM_WARNING), qPrintable(msg))
98#define gmErrorMsg(category, msg) qCDebug((category).qtCategory(GM_ERROR), qPrintable(msg))
99#define gmFatalMsg(category, msg) qCDebug((category).qtCategory(GM_FATAL), qPrintable(msg))
100#define gmTimeMsg(category, msg) qCDebug((category).qtCategory(GM_TIME), qPrintable(msg))
101#define gmMemoryMsg(category, msg) qCDebug((category).qtCategory(GM_MEMORY), qPrintable(msg))
102
105{
107
108signals:
109 void logMessage(int level, QString msg);
110};
111
112
113class LuaTable;
114
116GMC_API_EXPORT bool GmSetConsoleLogging(bool mode);
117GMC_API_EXPORT void GmSetLogFile(QString logfile, bool flush, const GmLogCategory& logger);
120GMC_API_EXPORT bool GmSetShowLoggerName(bool mode);
121GMC_API_EXPORT bool GmSetShowThreadId(bool mode);
124GMC_API_EXPORT void GmSetLogLevels(LuaTable& tab, const QList<QPair<QString, bool> >& extraRules, const GmLogCategory& logger);
125
128
129GMC_API_EXPORT bool GmSetStringListLogging(bool mode, QStringList* targetList = NULL);
131
132GMC_API_EXPORT const QObject* GmSetLoggingSignal(bool enable);
133
134GMC_API_EXPORT void GmLogWrappedMsg(const GmLogCategory& logger, GmLogLevel level, const QString& msg,
135 int maxSize = 120, int maxBreak = 30);
136
137GMC_API_EXPORT void GmLogIdent(int offset);
139
140GMC_API_EXPORT void GmLogMemoryUsage(const GmLogCategory& logger, const QString& msg);
142
144
149{
150public:
151 GmLogIdenter(int l = 1) : _l(l) { GmLogIdent(_l); }
152 ~GmLogIdenter() { GmLogIdent(-_l); }
153private:
154 int _l; // Number of ident levels
155};
156
161{
162public:
163 GmLogIdentClearer() { _old = GmLogIdentClear(); }
164 ~GmLogIdentClearer() { GmLogIdent(_old); }
165private:
166 int _old; // Old number of ident levels
167};
168
169
170#endif
171
Class representing a category with multiple logging levels.
Definition gmLog.h:58
QLoggingCategory & qtCategory(GmLogLevel level) const
Returns the "Qt category" that represents the requested level.
Definition gmLog.h:70
void setEnabled(GmLogLevel level, bool mode)
Enables or disables log messages for the requested level.
Definition gmLog.h:67
bool isEnabled(GmLogLevel level) const
Returns true if messages for this log level are enabled, false otherwise.
Definition gmLog.h:64
RAII object to clear the identation level to log messages, restoring to the previous identation when ...
Definition gmLog.h:161
RAII object to add an identation level to log messages, reverting the settings when the object is des...
Definition gmLog.h:149
Aux class whose only purpose is to allow loggers to emit a logMessage signal, if configured to do so.
Definition gmLog.h:105
Declaration of useful configuration definitions for the Core library.
#define GMC_API_EXPORT
Macro for controlling if the class is being exported (GEMA_CORE_LIB defined) or imported (GEMA_CORE_L...
Definition gmCoreConfig.h:35
int GmLogIdentClear()
Clears the current identation level, returning the old one.
Definition gmLog.cpp:572
void GmLogIdent(int offset)
Changes the current identation level.
Definition gmLog.cpp:563
GMC_API_EXPORT void GmSetLogFile(QString logfile, bool flush, const GmLogCategory &logger)
Enables and configures file logging.
Definition gmLog.cpp:319
GMC_API_EXPORT bool GmSetLoggerDecorations(bool mode)
Updates the current logger decoration mode. Returns the previous mode.
Definition gmLog.cpp:517
GMC_API_EXPORT bool GmSetShowThreadId(bool mode)
Enables or disables showing the thread id before each message. Returns the previous mode.
Definition gmLog.cpp:376
GmLogLevel
Available log levels list.
Definition gmLog.h:36
@ GM_COUNT
Number of log levels.
Definition gmLog.h:47
@ GM_TRACE
Trace information to help system debugging. Much more info than you want.
Definition gmLog.h:37
@ GM_MEMORY
Reporting memory information. New category so as to be easy to disable all memory messages for tests ...
Definition gmLog.h:44
@ GM_EXT_INFO
Extended information. Probably more than you wanted.
Definition gmLog.h:38
@ GM_ERROR
Reporting an error from which the system can continue (maybe not with its total capacity)
Definition gmLog.h:41
@ GM_FATAL
Reporting a fatal error. The system cannot continue.
Definition gmLog.h:42
@ GM_TIME
Reporting timing information. New category so as to be easy to disable all timing messages for tests ...
Definition gmLog.h:43
@ GM_INFO
Information regarding system evolution and config. Should be the "right" level of messages.
Definition gmLog.h:39
@ GM_WARNING
A warning message reporting something strange but not necessarilly an error.
Definition gmLog.h:40
GMC_API_EXPORT void GmCloseLogFile()
Closes the log file if enabled.
Definition gmLog.cpp:343
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:531
GMC_API_EXPORT const QStringList & GmLogStringList()
Returns the logging string list (has contents only if GmSetStringListLogging(true) was called before)
Definition gmLog.cpp:428
GMC_API_EXPORT void GmLogInit()
Initializes the logging facilities.
Definition gmLog.cpp:276
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:511
GMC_API_EXPORT void GmLogIdent(int offset)
Changes the current identation level.
Definition gmLog.cpp:563
GMC_API_EXPORT bool GmSetShowLoggerName(bool mode)
Enables or disables showing the logger name before each message. Returns the previous mode.
Definition gmLog.cpp:365
GMC_API_EXPORT bool GmSetConsoleLogging(bool mode)
Enables or disables logging to the console. Returns the previous mode.
Definition gmLog.cpp:302
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:437
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:585
GMC_API_EXPORT bool GmSetStringListLogging(bool mode, QStringList *targetList=NULL)
Enables or disables logging to a string list. This is useful for capturing messages for testing purpo...
Definition gmLog.cpp:407
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:647
GMC_API_EXPORT void GmFlushLogFile()
Flushes the log file if enabled.
Definition gmLog.cpp:356
GMC_API_EXPORT int GmLogIdentClear()
Clears the current identation level, returning the old one.
Definition gmLog.cpp:572
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:447
GMC_API_EXPORT bool GmSetHideRepeatedWarnings(bool mode)
Enables or disables skipping repeated warning messages. Returns the previous mode.
Definition gmLog.cpp:387
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:630
Q_OBJECTQ_OBJECT