CuteLogger
Fast and simple logging solution for Qt based applications
|
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 Logger * | globalInstance () |
Returns the global instance of Logger. More... | |
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.
enum Logger::LogLevel |
Describes the possible severity levels of the log records.
enum Logger::TimingMode |
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::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.
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
QString Logger::defaultCategory | ( | ) | const |
Returns default logging category name.
|
static |
Returns the global instance of Logger.
In a most cases you shouldn't use this function directly. Consider using cuteLogger macro instead.
|
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.
s | String to be decoded |
|
static |
Converts the LogLevel enum value to its string representation.
logLevel | Log level to convert |
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()).
category | Category name |
logToGlobal | Link or onlink the category from global logger instance appender |
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.
appender | Appender to register in the Logger |
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.
category | Category name |
appender | Appender to register in the Logger |
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.
appender | Pointer to appender to remove from logger |
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.
category | Category name |
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.
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 |
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()
.
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:
instead of writing
Please consider reading the Qt Reference Documentation for the description of the QDebug class usage syntax.
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.