DtkCore
DTK Core module
Dtk::Core::Logger类 参考

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 LoggerglobalInstance ()
 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.

注解
Qt 4 qDebug set of macro doesn't support capturing source function name, file name or line number so we recommend to use dDebug() and other Logger macros instead.
参见
Dtk::Core::logger

成员枚举类型说明

◆ LogLevel

Describes the possible severity levels of the log records

枚举值
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.

构造及析构函数说明

◆ Logger() [1/2]

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

◆ Logger() [2/2]

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.

参见
Logger()
setDefaultCategory()

◆ ~Logger()

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

成员函数说明

◆ defaultCategory()

QString Dtk::Core::Logger::defaultCategory ( ) const

Returns default logging category name

参见
setDefaultCategory

◆ globalInstance()

Logger * Dtk::Core::Logger::globalInstance ( )
static

Returns the global instance of Logger.

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

参见
Dtk::Core::logger

◆ levelFromString()

Logger::LogLevel Dtk::Core::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.

s String to be decoded

参见
LogLevel
levelToString()

◆ levelToString()

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

Converts the LogLevel enum value to its string representation.

logLevel Log level to convert

参见
LogLevel
levelFromString()

◆ logToGlobalInstance()

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

参见
globalInstance
registerAppender
registerCategoryAppender

◆ registerAppender()

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

注解
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.
参见
registerCategoryAppender
AbstractAppender

◆ registerCategoryAppender()

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

注解
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.
参见
registerAppender
Dtk::Core::dCTrace(), Dtk::Core::dCDebug(), Dtk::Core::dCInfo(), Dtk::Core::dCWarning(), Dtk::Core::dCError(), Dtk::Core::dCFatal()
Dtk::Core::dCategory(), Dtk::Core::dGlobalCategory()
logToGlobalInstance()
setDefaultCategory()

◆ setDefaultCategory()

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

注解
"category" format marker will be set to the category name for all of these messages (see AbstractStringAppender::setFormat).
参见
defaultCategory()
registerCategoryAppender()
logToGlobalInstance()

◆ write() [1/3]

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.

注解
It is not recommended to call this function directly. Instead of this you can just call one of the macros (dTrace, dTrac, dInfo, dWarning, dError, dFatal) that will supply all the needed information to this function.

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

注解
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.
参见
LogLevel
Dtk::Core::dTrace(), Dtk::Core::dDebug(), Dtk::Core::dInfo(), Dtk::Core::dWarning(), Dtk::Core::dError(), Dtk::Core::dFatal()
AbstractAppender

◆ write() [2/3]

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:

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

instead of writing

dDebug(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.

注解
This overload is definitely more pleasant to use than the first write() overload, but it behaves definitely slower than all the above overloads.
参见
write()

◆ write() [3/3]

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().

参见
write()

◆ writeAssert()

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.

注解
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.
参见
write()

该类的文档由以下文件生成: