DtkCore
DTK Core module
|
Very simple but rather powerful component which may be used for logging your application activities. 更多...
Public 类型 | |
enum | LogLevel { Trace , Debug , Info , Warning , Error , Fatal } |
Describes the possible severity levels of the log records 更多... | |
Public 成员函数 | |
Logger () | |
Construct the instance of Logger. 更多... | |
Logger (const QString &defaultCategory) | |
Construct the instance of Logger and set logger default category. 更多... | |
~Logger () | |
Destroy the instance of Logger. 更多... | |
void | registerAppender (AbstractAppender *appender) |
Registers the appender to write the log records to. 更多... | |
void | registerCategoryAppender (const QString &category, AbstractAppender *appender) |
Registers the appender to write the log records to the specific category. 更多... | |
void | logToGlobalInstance (const QString &category, bool logToGlobal=false) |
Links some logging category with the global logger instance appenders. 更多... | |
void | setDefaultCategory (const QString &category) |
Sets default logging category. 更多... | |
QString | defaultCategory () const |
Returns default logging category name 更多... | |
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. 更多... | |
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. 更多... | |
静态 Public 成员函数 | |
static QString | levelToString (LogLevel logLevel) |
Converts the LogLevel enum value to its string representation. 更多... | |
static LogLevel | levelFromString (const QString &s) |
Converts the LogLevel string representation to enum value. 更多... | |
static Logger * | globalInstance () |
Returns the global instance of Logger. 更多... | |
Very simple but rather powerful component which may be used for logging your application activities.
\inmodule dtkcore
Global logger instance created on a first access to it (e.g. registering appenders, calling a dDebug() macro etc.) registers itself as a Qt default message handler and captures all the qDebug/dWarning/qCritical output.
Describes the possible severity levels of the log records
Dtk::Core::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 logger macro instead to access the logger instance
Dtk::Core::Logger::Logger | ( | const QString & | defaultCategory | ) |
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 logger macro instead to access the logger instance and call setDefaultCategory method.
Dtk::Core::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 Dtk::Core::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 logger 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 Dtk::Core::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 Dtk::Core::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 Dtk::Core::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 dCDebug(), dCWarning() 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 Dtk::Core::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 dDebug() macro will be treated as category logging, so you needn't to specify category name using dCDebug() macro.
To unset the default category, pass a null string as a parameter.
category Category name
void Dtk::Core::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
QDebug Dtk::Core::Logger::write | ( | LogLevel | logLevel, |
const char * | file, | ||
int | line, | ||
const char * | function, | ||
const char * | category | ||
) |
This is the overloaded function provided for the convenience. 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 Dtk::Core::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 convenience. It behaves similar to the above function.
This function uses the current timestamp obtained with QDateTime::currentDateTime()
.
void Dtk::Core::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.
The file parameter is the current file name. The line parameter indicates the number of lines to output. The function parameter indicates the function name to output.