CuteLogger
Fast and simple logging solution for Qt based applications
Classes | Macros
Logger.h File Reference

A file containing the description of Logger class and and additional useful macros for logging. More...

#include <QString>
#include <QDebug>
#include <QDateTime>
#include "CuteLogger_global.h"

Go to the source code of this file.

Classes

class  Logger
 Very simple but rather powerful component which may be used for logging your application activities. More...
 

Macros

#define cuteLogger   cuteLoggerInstance()
 Macro returning the current instance of Logger object. More...
 
#define LOG_TRACE   CuteMessageLogger(cuteLoggerInstance(), Logger::Trace, __FILE__, __LINE__, Q_FUNC_INFO).write
 Writes the trace log record. More...
 
#define LOG_DEBUG   CuteMessageLogger(cuteLoggerInstance(), Logger::Debug, __FILE__, __LINE__, Q_FUNC_INFO).write
 Writes the debug log record. More...
 
#define LOG_INFO   CuteMessageLogger(cuteLoggerInstance(), Logger::Info, __FILE__, __LINE__, Q_FUNC_INFO).write
 Writes the info log record. More...
 
#define LOG_WARNING   CuteMessageLogger(cuteLoggerInstance(), Logger::Warning, __FILE__, __LINE__, Q_FUNC_INFO).write
 Write the warning log record. More...
 
#define LOG_ERROR   CuteMessageLogger(cuteLoggerInstance(), Logger::Error, __FILE__, __LINE__, Q_FUNC_INFO).write
 Write the error log record This macro records the error log record using the Logger::write() function. It works similar to the LOG_TRACE() macro. More...
 
#define LOG_FATAL   CuteMessageLogger(cuteLoggerInstance(), Logger::Fatal, __FILE__, __LINE__, Q_FUNC_INFO).write
 Write the fatal log record. More...
 
#define LOG_CTRACE(category)   CuteMessageLogger(cuteLoggerInstance(), Logger::Trace, __FILE__, __LINE__, Q_FUNC_INFO, category).write()
 Writes the trace log record to the specific category. More...
 
#define LOG_CDEBUG(category)   CuteMessageLogger(cuteLoggerInstance(), Logger::Debug, __FILE__, __LINE__, Q_FUNC_INFO, category).write()
 Writes the debug log record to the specific category. More...
 
#define LOG_CINFO(category)   CuteMessageLogger(cuteLoggerInstance(), Logger::Info, __FILE__, __LINE__, Q_FUNC_INFO, category).write()
 Writes the info log record to the specific category. More...
 
#define LOG_CWARNING(category)   CuteMessageLogger(cuteLoggerInstance(), Logger::Warning, __FILE__, __LINE__, Q_FUNC_INFO, category).write()
 Writes the warning log record to the specific category. More...
 
#define LOG_CERROR(category)   CuteMessageLogger(cuteLoggerInstance(), Logger::Error, __FILE__, __LINE__, Q_FUNC_INFO, category).write()
 Writes the error log record to the specific category. More...
 
#define LOG_CFATAL(category)   CuteMessageLogger(cuteLoggerInstance(), Logger::Fatal, __FILE__, __LINE__, Q_FUNC_INFO, category).write()
 Write the fatal log record to the specific category. More...
 
#define LOG_TRACE_TIME   LoggerTimingHelper loggerTimingHelper(cuteLoggerInstance(), Logger::Trace, __FILE__, __LINE__, Q_FUNC_INFO); loggerTimingHelper.start
 Logs the processing time of current function / code block. More...
 
#define LOG_DEBUG_TIME   LoggerTimingHelper loggerTimingHelper(cuteLoggerInstance(), Logger::Debug, __FILE__, __LINE__, Q_FUNC_INFO); loggerTimingHelper.start
 Logs the processing time of current function / code block. More...
 
#define LOG_INFO_TIME   LoggerTimingHelper loggerTimingHelper(cuteLoggerInstance(), Logger::Info, __FILE__, __LINE__, Q_FUNC_INFO); loggerTimingHelper.start
 Logs the processing time of current function / code block. More...
 
#define LOG_ASSERT(cond)   ((!(cond)) ? cuteLoggerInstance()->writeAssert(__FILE__, __LINE__, Q_FUNC_INFO, #cond) : qt_noop())
 Check the assertion. More...
 
#define LOG_CATEGORY(category)
 Create logger instance inside your custom class to log all messages to the specified category. More...
 
#define LOG_GLOBAL_CATEGORY(category)
 Create logger instance inside your custom class to log all messages both to the specified category and to the global logger instance. More...
 

Detailed Description

A file containing the description of Logger class and and additional useful macros for logging.

Macro Definition Documentation

◆ cuteLogger

#define cuteLogger   cuteLoggerInstance()

Macro returning the current instance of Logger object.

If you haven't created a local Logger object it returns the same value as the Logger::globalInstance() functions. This macro is a recommended way to get an access to the Logger instance used in current class.

Example:

ConsoleAppender* consoleAppender = new ConsoleAppender;
cuteLogger->registerAppender(consoleAppender);
See also
Logger::globalInstance()

◆ LOG_ASSERT

#define LOG_ASSERT (   cond)    ((!(cond)) ? cuteLoggerInstance()->writeAssert(__FILE__, __LINE__, Q_FUNC_INFO, #cond) : qt_noop())

Check the assertion.

This macro is a convinient and recommended to use way to call Logger::writeAssert() function. It uses the preprocessor macros (as the LOG_DEBUG() does) to fill the necessary arguments of the Logger::writeAssert() call. It also uses undocumented but rather mature and stable qt_noop() function (which does nothing) when the assertion is true.

Example:

bool b = checkSomething();
...
LOG_ASSERT(b == true);
See also
Logger::writeAssert()

◆ LOG_CATEGORY

#define LOG_CATEGORY (   category)
Value:
Logger* cuteLoggerInstance()\
{\
static Logger customCuteLoggerInstance(category);\
return &customCuteLoggerInstance;\
}\
Very simple but rather powerful component which may be used for logging your application activities...
Definition: Logger.h:89

Create logger instance inside your custom class to log all messages to the specified category.

This macro is used to pass all log messages inside your custom class to the specific category. You must include this macro inside your class declaration (similarly to the Q_OBJECT macro). Internally, this macro redefines cuteLoggerInstance() function, creates the local Logger object inside your class and sets the default category to the specified parameter.

Thus, any call to cuteLoggerInstance() (for example, inside LOG_TRACE() macro) will return the local Logger object, so any logging message will be directed to the default category.

Note
This macro does not register any appender to the newly created logger instance. You should register logger appenders manually, inside your class.

Usage example:

class CustomClass : public QObject
{
Q_OBJECT
LOG_CATEGORY("custom_category")
...
};
CustomClass::CustomClass(QObject* parent) : QObject(parent)
{
cuteLogger->registerAppender(new FileAppender("custom_category_log"));
LOG_TRACE() << "Trace to the custom category log";
}

If used compiler supports C++11 standard, LOG_CATEGORY and LOG_GLOBAL_CATEGORY macros would also work when added inside of any scope. It could be useful, for example, to log every single run of a method to a different file.

void foo()
{
QString categoryName = QDateTime::currentDateTime().toString("yyyy-MM-ddThh-mm-ss-zzz");
LOG_CATEGORY(categoryName);
cuteLogger->registerAppender(new FileAppender(categoryName + ".log"));
...
}
See also
Logger::write()
LOG_TRACE
Logger::registerCategoryAppender()
Logger::setDefaultCategory()
LOG_GLOBAL_CATEGORY

◆ LOG_CDEBUG

#define LOG_CDEBUG (   category)    CuteMessageLogger(cuteLoggerInstance(), Logger::Debug, __FILE__, __LINE__, Q_FUNC_INFO, category).write()

Writes the debug log record to the specific category.

This macro records the debug log record using the Logger::write() function. It works similar to the LOG_CTRACE() macro.

See also
LOG_CTRACE()

◆ LOG_CERROR

#define LOG_CERROR (   category)    CuteMessageLogger(cuteLoggerInstance(), Logger::Error, __FILE__, __LINE__, Q_FUNC_INFO, category).write()

Writes the error log record to the specific category.

This macro records the error log record using the Logger::write() function. It works similar to the LOG_CTRACE() macro.

See also
LOG_CTRACE()

◆ LOG_CFATAL

#define LOG_CFATAL (   category)    CuteMessageLogger(cuteLoggerInstance(), Logger::Fatal, __FILE__, __LINE__, Q_FUNC_INFO, category).write()

Write the fatal log record to the specific category.

This macro records the fatal log record using the Logger::write() function. It works similar to the LOG_CTRACE() macro.

Note
Recording of the log record using the Logger::Fatal log level will lead to calling the STL abort() function, which will interrupt the running of your software and begin the writing of the core dump.
See also
LOG_CTRACE()

◆ LOG_CINFO

#define LOG_CINFO (   category)    CuteMessageLogger(cuteLoggerInstance(), Logger::Info, __FILE__, __LINE__, Q_FUNC_INFO, category).write()

Writes the info log record to the specific category.

This macro records the info log record using the Logger::write() function. It works similar to the LOG_CTRACE() macro.

See also
LOG_CTRACE()

◆ LOG_CTRACE

#define LOG_CTRACE (   category)    CuteMessageLogger(cuteLoggerInstance(), Logger::Trace, __FILE__, __LINE__, Q_FUNC_INFO, category).write()

Writes the trace log record to the specific category.

This macro is the similar to the LOG_TRACE() macro, but has a category parameter to write only to the category appenders (registered using Logger::registerCategoryAppender() method).

Parameters
categorycategory name string
See also
LOG_TRACE()
Logger::LogLevel
Logger::registerCategoryAppender()
Logger::write()
LOG_CATEGORY(), LOG_GLOBAL_CATEGORY()

◆ LOG_CWARNING

#define LOG_CWARNING (   category)    CuteMessageLogger(cuteLoggerInstance(), Logger::Warning, __FILE__, __LINE__, Q_FUNC_INFO, category).write()

Writes the warning log record to the specific category.

This macro records the warning log record using the Logger::write() function. It works similar to the LOG_CTRACE() macro.

See also
LOG_CTRACE()

◆ LOG_DEBUG

#define LOG_DEBUG   CuteMessageLogger(cuteLoggerInstance(), Logger::Debug, __FILE__, __LINE__, Q_FUNC_INFO).write

Writes the debug log record.

This macro records the debug log record using the Logger::write() function. It works similar to the LOG_TRACE() macro.

See also
LOG_TRACE()
Logger::LogLevel
Logger::write()

◆ LOG_DEBUG_TIME

#define LOG_DEBUG_TIME   LoggerTimingHelper loggerTimingHelper(cuteLoggerInstance(), Logger::Debug, __FILE__, __LINE__, Q_FUNC_INFO); loggerTimingHelper.start

Logs the processing time of current function / code block.

This macro automagically measures the function or code of block execution time and outputs it as a Logger::Debug level log record. It works similar to LOG_TRACE_TIME() macro.

See also
LOG_TRACE_TIME

◆ LOG_ERROR

#define LOG_ERROR   CuteMessageLogger(cuteLoggerInstance(), Logger::Error, __FILE__, __LINE__, Q_FUNC_INFO).write

Write the error log record This macro records the error log record using the Logger::write() function. It works similar to the LOG_TRACE() macro.

See also
LOG_TRACE()
Logger::LogLevel
Logger::write()

◆ LOG_FATAL

#define LOG_FATAL   CuteMessageLogger(cuteLoggerInstance(), Logger::Fatal, __FILE__, __LINE__, Q_FUNC_INFO).write

Write the fatal log record.

This macro records the fatal log record using the Logger::write() function. It works similar to the LOG_TRACE() macro.

Note
Recording of the log record using the Logger::Fatal log level will lead to calling the STL abort() function, which will interrupt the running of your software and begin the writing of the core dump.
See also
LOG_TRACE()
Logger::LogLevel
Logger::write()

◆ LOG_GLOBAL_CATEGORY

#define LOG_GLOBAL_CATEGORY (   category)
Value:
Logger* cuteLoggerInstance()\
{\
static Logger customCuteLoggerInstance(category);\
customCuteLoggerInstance.logToGlobalInstance(category, true);\
return &customCuteLoggerInstance;\
}\
Very simple but rather powerful component which may be used for logging your application activities...
Definition: Logger.h:89

Create logger instance inside your custom class to log all messages both to the specified category and to the global logger instance.

This macro is similar to LOG_CATEGORY(), but also passes all log messages to the global logger instance appenders. It is equal to defining the local category logger using LOG_CATEGORY macro and calling:

cuteLogger->logToGlobalInstance(cuteLogger->defaultCategory(), true);
See also
LOG_CATEGORY
Logger::logToGlobalInstance()
Logger::defaultCategory()
Logger::registerCategoryAppender()
Logger::write()

◆ LOG_INFO

#define LOG_INFO   CuteMessageLogger(cuteLoggerInstance(), Logger::Info, __FILE__, __LINE__, Q_FUNC_INFO).write

Writes the info log record.

This macro records the info log record using the Logger::write() function. It works similar to the LOG_TRACE() macro.

See also
LOG_TRACE()
Logger::LogLevel
Logger::write()

◆ LOG_INFO_TIME

#define LOG_INFO_TIME   LoggerTimingHelper loggerTimingHelper(cuteLoggerInstance(), Logger::Info, __FILE__, __LINE__, Q_FUNC_INFO); loggerTimingHelper.start

Logs the processing time of current function / code block.

This macro automagically measures the function or code of block execution time and outputs it as a Logger::Info level log record. It works similar to LOG_TRACE_TIME() macro.

See also
LOG_TRACE_TIME

◆ LOG_TRACE

#define LOG_TRACE   CuteMessageLogger(cuteLoggerInstance(), Logger::Trace, __FILE__, __LINE__, Q_FUNC_INFO).write

Writes the trace log record.

This macro is the convinient way to call Logger::write(). It uses the common preprocessor macros FILE, LINE and the standart Qt Q_FUNC_INFO macros to automatically determine the needed parameters to call Logger::write().

Note
This and other (LOG_INFO() etc...) macros uses the variadic macro arguments to give convinient usage form for the different versions of Logger::write() (using the QString or const char* argument or returning the QDebug class instance). Not all compilers will support this. Please, consider reviewing your compiler documentation to ensure it support VA_ARGS macro.
See also
Logger::LogLevel
Logger::write()

◆ LOG_TRACE_TIME

#define LOG_TRACE_TIME   LoggerTimingHelper loggerTimingHelper(cuteLoggerInstance(), Logger::Trace, __FILE__, __LINE__, Q_FUNC_INFO); loggerTimingHelper.start

Logs the processing time of current function / code block.

This macro automagically measures the function or code of block execution time and outputs it as a Logger::Trace level log record.

Example:

int foo()
{
... // Do some long operations
return 0;
} // Outputs: Function foo finished in <time> ms.

If you are measuring a code of block execution time you may also add a name of block to the macro:

int bar(bool doFoo)
{
if (doFoo)
{
...
}
...
}
// Outputs:
// "Foo" finished in <time1> ms.
// Function bar finished in <time2> ms.
Note
Macro switches to logging the seconds instead of milliseconds when the execution time reaches 10000 ms.
See also
LOG_DEBUG_TIME, LOG_INFO_TIME

◆ LOG_WARNING

#define LOG_WARNING   CuteMessageLogger(cuteLoggerInstance(), Logger::Warning, __FILE__, __LINE__, Q_FUNC_INFO).write

Write the warning log record.

This macro records the warning log record using the Logger::write() function. It works similar to the LOG_TRACE() macro.

See also
LOG_TRACE()
Logger::LogLevel
Logger::write()