CuteLogger
Fast and simple logging solution for Qt based applications
Public Types | Public Member Functions | Static Public Member Functions | List of all members
Logger Class Reference

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

Public Types

enum  LogLevel {
  Trace, Debug, Info, Warning,
  Error, Fatal
}
 Describes the possible severity levels of the log records. More...
 
enum  TimingMode { TimingAuto, TimingMs }
 Sets the timing display mode for the LOG_TRACE_TIME, LOG_DEBUG_TIME and LOG_INFO_TIME macros. More...
 

Public Member Functions

 Logger ()
 Construct the instance of Logger. More...
 
 Logger (const QString &defaultCategory, bool writeToGlobalInstance=false)
 Construct the instance of Logger and set logger default category. More...
 
 ~Logger ()
 Destroy the instance of Logger. More...
 
void registerAppender (AbstractAppender *appender)
 Registers the appender to write the log records to. More...
 
void registerCategoryAppender (const QString &category, AbstractAppender *appender)
 Registers the appender to write the log records to the specific category. More...
 
void removeAppender (AbstractAppender *appender)
 Removes the registered appender from logger. More...
 
void logToGlobalInstance (const QString &category, bool logToGlobal=false)
 Links some logging category with the global logger instance appenders. More...
 
void setDefaultCategory (const QString &category)
 Sets default logging category. More...
 
QString defaultCategory () const
 Returns default logging category name. More...
 
void write (const QDateTime &timeStamp, LogLevel logLevel, const char *file, int line, const char *function, const char *category, const QString &message)
 Writes the log record. More...
 
void write (LogLevel logLevel, const char *file, int line, const char *function, const char *category, const QString &message)
 
QDebug write (LogLevel logLevel, const char *file, int line, const char *function, const char *category)
 
void writeAssert (const char *file, int line, const char *function, const char *condition)
 Writes the assertion. More...
 

Static Public Member Functions

static QString levelToString (LogLevel logLevel)
 Converts the LogLevel enum value to its string representation. More...
 
static LogLevel levelFromString (const QString &s)
 Converts the LogLevel string representation to enum value. More...
 
static LoggerglobalInstance ()
 Returns the global instance of Logger. More...
 

Detailed Description

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

Global logger instance created on a first access to it (e.g. registering appenders, calling a LOG_DEBUG() macro etc.) registers itself as a Qt default message handler and captures all the qDebug/qWarning/qCritical output.

Note
Qt 4 qDebug set of macro doesn't support capturing source function name, file name or line number so we recommend to use LOG_DEBUG() and other Logger macros instead.
See also
cuteLogger
CuteLogger Documentation

Member Enumeration Documentation

◆ LogLevel

Describes the possible severity levels of the log records.

Enumerator
Trace 

Trace level. Can be used for mostly unneeded records used for internal code tracing.

Debug 

Debug level. Useful for non-necessary records used for the debugging of the software.

Info 

Info level. Can be used for informational records, which may be interesting for not only developers.

Warning 

Warning. May be used to log some non-fatal warnings detected by your application.

Error 

Error. May be used for a big problems making your application work wrong but not crashing.

Fatal 

Fatal. Used for unrecoverable errors, crashes the application right after the log record is written.

◆ TimingMode

Sets the timing display mode for the LOG_TRACE_TIME, LOG_DEBUG_TIME and LOG_INFO_TIME macros.

Enumerator
TimingAuto 

Show time in seconds, if it exceeds 10s (default)

TimingMs 

Always use milliseconds to display.

Constructor & Destructor Documentation

◆ Logger() [1/2]

Logger::Logger ( )

Construct the instance of Logger.

If you're only using one global instance of logger you wouldn't probably need to use this constructor manually. Consider using cuteLogger macro instead to access the logger instance

◆ Logger() [2/2]

Logger::Logger ( const QString &  defaultCategory,
bool  writeToGlobalInstance = false 
)

Construct the instance of Logger and set logger default category.

If you're only using one global instance of logger you wouldn't probably need to use this constructor manually. Consider using cuteLogger macro instead to access the logger instance and call setDefaultCategory method.

See also
Logger()
setDefaultCategory()

◆ ~Logger()

Logger::~Logger ( )

Destroy the instance of Logger.

You probably wouldn't need to use this function directly. Global instance of logger will be destroyed automatically at the end of your QCoreApplication execution

Member Function Documentation

◆ defaultCategory()

QString Logger::defaultCategory ( ) const

Returns default logging category name.

See also
setDefaultCategory

◆ globalInstance()

Logger * Logger::globalInstance ( )
static

Returns the global instance of Logger.

In a most cases you shouldn't use this function directly. Consider using cuteLogger macro instead.

See also
cuteLogger

◆ levelFromString()

Logger::LogLevel Logger::levelFromString ( const QString &  s)
static

Converts the LogLevel string representation to enum value.

Comparation of the strings is case independent. If the log level string provided cannot be understood Logger::Debug is returned.

Parameters
sString to be decoded
See also
LogLevel
levelToString()

◆ levelToString()

QString Logger::levelToString ( Logger::LogLevel  logLevel)
static

Converts the LogLevel enum value to its string representation.

Parameters
logLevelLog level to convert
See also
LogLevel
levelFromString()

◆ logToGlobalInstance()

void Logger::logToGlobalInstance ( const QString &  category,
bool  logToGlobal = false 
)

Links some logging category with the global logger instance appenders.

If set to true, all log messages to the specified category appenders will also be written to the global logger instance appenders, registered using registerAppender().

By default, all messages to the specific category are written only to the specific category appenders (registered using registerCategoryAppender()).

Parameters
categoryCategory name
logToGlobalLink or onlink the category from global logger instance appender
See also
globalInstance
registerAppender
registerCategoryAppender

◆ registerAppender()

void Logger::registerAppender ( AbstractAppender appender)

Registers the appender to write the log records to.

On the log writing call (using one of the macros or the write() function) Logger traverses through the list of the appenders and writes a log records to the each of them. Please, look through the AbstractAppender documentation to understand the concept of appenders.

If no appenders was added to Logger, it falls back to logging into the std::cerr STL stream.

Parameters
appenderAppender to register in the Logger
Note
Logger takes ownership on the appender and it will delete it on the application exit. According to this, appenders must be created on heap to prevent double destruction of the appender.
See also
registerCategoryAppender
AbstractAppender

◆ registerCategoryAppender()

void Logger::registerCategoryAppender ( const QString &  category,
AbstractAppender appender 
)

Registers the appender to write the log records to the specific category.

Calling this method, you can link some appender with the named category. On the log writing call to the specific category (calling write() with category parameter directly, writing to the default category, or using special LOG_CDEBUG(), LOG_CWARNING() etc. macros), Logger writes the log message only to the list of registered category appenders.

You can call logToGlobalInstance() to pass all category log messages to the global logger instance appenders (registered using registerAppender()). If no category appenders with specific name was registered to the Logger, it falls back to logging into the std::cerr STL stream, both with simple warning message.

Parameters
categoryCategory name
appenderAppender to register in the Logger
Note
Logger takes ownership on the appender and it will delete it on the application exit. According to this, appenders must be created on heap to prevent double destruction of the appender.
See also
registerAppender
LOG_CTRACE(), LOG_CDEBUG(), LOG_CINFO(), LOG_CWARNING(), LOG_CERROR(), LOG_CFATAL()
LOG_CATEGORY(), LOG_GLOBAL_CATEGORY()
logToGlobalInstance
setDefaultCategory

◆ removeAppender()

void Logger::removeAppender ( AbstractAppender appender)

Removes the registered appender from logger.

After calling this function logger stops writing any of the records to the appender.

Parameters
appenderPointer to appender to remove from logger
Note
Removed appender will not be deleted on the application shutdown and you will need to destroy the object yourself.
See also
registerAppender

◆ setDefaultCategory()

void Logger::setDefaultCategory ( const QString &  category)

Sets default logging category.

All log messages to this category appenders will also be written to general logger instance appenders (registered using registerAppender method), and vice versa. In particular, any calls to the LOG_DEBUG() macro will be treated as category logging, so you needn't to specify category name using LOG_CDEBUG() macro.

To unset the default category, pass a null string as a parameter.

Parameters
categoryCategory name
Note
"category" format marker will be set to the category name for all of these messages (see AbstractStringAppender::setFormat).
See also
defaultCategory()
registerCategoryAppender()
logToGlobalInstance()

◆ write() [1/3]

void Logger::write ( const QDateTime &  timeStamp,
LogLevel  logLevel,
const char *  file,
int  line,
const char *  function,
const char *  category,
const QString &  message 
)

Writes the log record.

Writes the log records with the supplied arguments to all the registered appenders.

Note
It is not recommended to call this function directly. Instead of this you can just call one of the macros (LOG_TRACE, LOG_DEBUG, LOG_INFO, LOG_WARNING, LOG_ERROR, LOG_FATAL) that will supply all the needed information to this function.
Parameters
timeStamp- the time stamp of the record
logLevel- the log level of the record
file- the name of the source file that requested the log record
line- the line of the code of source file that requested the log record
function- name of the function that requested the log record
category- logging category (0 for default category)
message- log message
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
LogLevel
LOG_TRACE, LOG_DEBUG, LOG_INFO, LOG_WARNING, LOG_ERROR, LOG_FATAL
AbstractAppender

◆ write() [2/3]

void Logger::write ( LogLevel  logLevel,
const char *  file,
int  line,
const char *  function,
const char *  category,
const QString &  message 
)

This is the overloaded function provided for the convinience. It behaves similar to the above function.

This function uses the current timestamp obtained with QDateTime::currentDateTime().

See also
write()

◆ write() [3/3]

QDebug Logger::write ( LogLevel  logLevel,
const char *  file,
int  line,
const char *  function,
const char *  category 
)

This is the overloaded function provided for the convinience. It behaves similar to the above function.

This function doesn't accept any log message as argument. It returns the QDebug object that can be written using the stream functions. For example, you may like to write:

LOG_DEBUG() << "This is the size" << size << "of the element" << elementName;

instead of writing

LOG_DEBUG(QString(QLatin1String("This is the size %1x%2 of the element %3"))
.arg(size.x()).arg(size.y()).arg(elementName));

Please consider reading the Qt Reference Documentation for the description of the QDebug class usage syntax.

Note
This overload is definitely more pleasant to use than the first write() overload, but it behaves definitely slower than all the above overloads.
See also
write()

◆ writeAssert()

void Logger::writeAssert ( const char *  file,
int  line,
const char *  function,
const char *  condition 
)

Writes the assertion.

This function writes the assertion record using the write() function.

The assertion record is always written using the Logger::Fatal log level which leads to the abortation of the program and generation of the core dump (if supported).

The message written to the appenders will be identical to the condition argument prefixed with the ASSERT: notification.

Note
It is not recommended to call this function directly. Instead of this you can just call the LOG_ASSERT macro that will supply all the needed information to this function.
See also
LOG_ASSERT
write()

The documentation for this class was generated from the following files: