QLoggingCategory 类表示日志基础设施类别或区域。 更多...
头: | #include <QLoggingCategory> |
CMake: |
find_package(Qt6 COMPONENTS Core REQUIRED)
target_link_libraries(mytarget PRIVATE Qt6::Core) |
qmake: | QT += core |
Since: | Qt 5.2 |
注意: 此类的所有函数 thread-safe .
CategoryFilter |
QLoggingCategory (const char * category , QtMsgType enableForLevel = QtDebugMsg) | |
~QLoggingCategory () | |
const char * | categoryName () const |
bool | isCriticalEnabled () const |
bool | isDebugEnabled () const |
bool | isEnabled (QtMsgType msgtype ) const |
bool | isInfoEnabled () const |
bool | isWarningEnabled () const |
void | setEnabled (QtMsgType type , bool enable ) |
QLoggingCategory & | operator() () |
const QLoggingCategory & | operator() () const |
QLoggingCategory * | defaultCategory () |
QLoggingCategory::CategoryFilter | installFilter (QLoggingCategory::CategoryFilter filter ) |
void | setFilterRules (const QString & rules ) |
Q_DECLARE_LOGGING_CATEGORY ( name ) | |
Q_LOGGING_CATEGORY ( name , string , msgType ) | |
Q_LOGGING_CATEGORY ( name , string ) | |
qCCritical ( category , const char * message , ...) | |
qCCritical ( category ) | |
qCDebug ( category , const char * message , ...) | |
qCDebug ( category ) | |
qCInfo ( category , const char * message , ...) | |
qCInfo ( category ) | |
qCWarning ( category , const char * message , ...) | |
qCWarning ( category ) |
QLoggingCategory represents a certain logging category - identified by a string - at runtime. A category can be configured to enable or disable logging of messages per message type.
To check whether a message type is enabled or not, use one of these methods: isDebugEnabled (), isInfoEnabled (), isWarningEnabled (),和 isCriticalEnabled ().
All objects are meant to be configured by a common registry, as described in 配置类别 . Different objects can also represent the same category. Therefore, it's not recommended to export objects across module boundaries, to manipulate the objects directly, or to inherit from QLoggingCategory.
Q_DECLARE_LOGGING_CATEGORY () 和 Q_LOGGING_CATEGORY () macros conveniently declare and create QLoggingCategory objects:
// in a header Q_DECLARE_LOGGING_CATEGORY(driverUsb) // in one source file Q_LOGGING_CATEGORY(driverUsb, "driver.usb")
Category names are free text; to configure categories using 日志规则 , their names should follow this convention:
debug
,
info
,
warning
,和
critical
.
qt
prefix are solely reserved for Qt modules.
QLoggingCategory objects that are implicitly defined by Q_LOGGING_CATEGORY () are created on first use, in a thread-safe manner.
QLoggingCategory provides isDebugEnabled (), isInfoEnabled (), isWarningEnabled (), isCriticalEnabled (), as well as isEnabled () to check whether messages for the given message type should be logged.
qCDebug (), qCWarning (),和 qCCritical () macros prevent arguments from being evaluated if the respective message types are not enabled for the category, so explicit checking is not needed:
// usbEntries() will only be called if driverUsb category is enabled qCDebug(driverUsb) << "devices: " << usbEntries();
Both the QLoggingCategory constructor and the Q_LOGGING_CATEGORY () macro accept an optional QtMsgType argument, which disables all message types with a lower severity. That is, a category declared with
Q_LOGGING_CATEGORY(driverUsbEvents, "driver.usb.events", QtWarningMsg)
logs messages of type
QtWarningMsg
,
QtCriticalMsg
,
QtFatalMsg
, but ignores messages of type
QtDebugMsg
and
QtInfoMsg
.
If no argument is passed, all messages are logged. Only Qt internal categories which start with
qt
are handled differently: For these, only messages of type
QtInfoMsg
,
QtWarningMsg
,和
QtCriticalMsg
are logged by default.
注意: Logging categories are not affected by your C++ build configuration. That is, whether messages are printed does not change depending on whether the code is compiled with debug symbols ('Debug Build'), optimizations ('Release Build'), or some other combination.
You can override the default configuration for categories either by setting logging rules, or by installing a custom filter.
Logging rules let you enable or disable logging for categories in a flexible way. Rules are specified in text, where every line must have the format:
<category>[.<type>] = true|false
<category>
is the name of the category, potentially with
*
as a wildcard symbol for the first or last character; or at both positions. The optional
<type>
必须为
debug
,
info
,
warning
,或
critical
. Lines that don't fit this scheme are ignored.
Rules are evaluated in text order, from first to last. That is, if two rules apply to a category/type, the rule that comes later is applied.
Rules can be set via setFilterRules ():
QLoggingCategory::setFilterRules("*.debug=false\n" "driver.usb.debug=true");
Logging rules are automatically loaded from the
[Rules]
section in a logging configuration file. These configuration files are looked up in the QtProject configuration directory, or explicitly set in a
QT_LOGGING_CONF
environment variable:
[Rules] *.debug=false driver.usb.debug=true
Logging rules can also be specified in a
QT_LOGGING_RULES
environment variable; multiple rules can also be separated by semicolons:
QT_LOGGING_RULES="*.debug=false;driver.usb.debug=true"
Rules set by
setFilterRules
() take precedence over rules specified in the QtProject configuration directory. In turn, these rules can be overwritten by those from the configuration file specified by
QT_LOGGING_CONF
, and those set by
QT_LOGGING_RULES
.
The order of evaluation is as follows:
QT_LOGGING_CONF
QT_LOGGING_RULES
QtProject/qtlogging.ini
file is looked up in all directories returned by
QStandardPaths::GenericConfigLocation
.
设置
QT_LOGGING_DEBUG
environment variable to find out where your logging rules are loaded from.
As a lower-level alternative to the text rules, you can also implement a custom filter via installFilter (). All filter rules are ignored in this case.
使用
%{category}
placeholder to print the category in the default message handler:
qSetMessagePattern("%{category} %{message}");
这是采用以下签名的函数指针的 typedef:
void myCategoryFilter(QLoggingCategory *);
A function with this signature can be installed with installFilter ().
[since 5.4]
QLoggingCategory::
QLoggingCategory
(const
char
*
category
,
QtMsgType
enableForLevel
= QtDebugMsg)
Constructs a QLoggingCategory object with the provided category name, and enables all messages with types at least as verbose as enableForLevel ,其默认为 QtDebugMsg (which enables all categories).
若
category
is
nullptr
, the category name
"default"
被使用。
注意: category must be kept valid during the lifetime of this object. Using a string literal for it is the usual way to achieve this.
该函数在 Qt 5.4 引入。
销毁 QLoggingCategory 对象。
Returns the name of the category.
[static]
QLoggingCategory
*QLoggingCategory::
defaultCategory
()
Returns a pointer to the global category
"default"
that is used, for example, by
qDebug
(),
qInfo
(),
qWarning
(),
qCritical
(),或
qFatal
().
注意:
The pointer returned may be null during destruction of static objects. Also, don't
delete
this pointer, as ownership of the category isn't transferred.
[static]
QLoggingCategory::CategoryFilter
QLoggingCategory::
installFilter
(
QLoggingCategory::CategoryFilter
filter
)
Installs a function filter that is used to determine which categories and message types should be enabled. Returns a pointer to the previous installed filter.
Every QLoggingCategory object created is passed to the filter, and the filter is free to change the respective category configuration with setEnabled ().
When you define your filter, note that it can be called from different threads; but never concurrently. This filter cannot call any static functions from QLoggingCategory .
范例:
QLoggingCategory::CategoryFilter oldCategoryFilter; void myCategoryFilter(QLoggingCategory *category) { // configure driver.usb category here, otherwise forward to to default filter. if (qstrcmp(category->categoryName(), "driver.usb") == 0) category->setEnabled(QtDebugMsg, true); else oldCategoryFilter(category); }
Alternatively, you can configure the default filter via setFilterRules ().
返回
true
if critical messages should be shown for this category;
false
否则。
注意: qCCritical () macro already does this check before executing any code. However, calling this method may be useful to avoid the expensive generation of data for debug output only.
返回
true
if debug messages should be shown for this category;
false
否则。
注意: qCDebug () macro already does this check before running any code. However, calling this method may be useful to avoid the expensive generation of data for debug output only.
返回
true
if a message of type
msgtype
for the category should be shown;
false
否则。
[since 5.5]
bool
QLoggingCategory::
isInfoEnabled
() const
返回
true
if informational messages should be shown for this category;
false
否则。
注意: qCInfo () macro already does this check before executing any code. However, calling this method may be useful to avoid the expensive generation of data for debug output only.
该函数在 Qt 5.5 引入。
返回
true
if warning messages should be shown for this category;
false
否则。
注意: qCWarning () macro already does this check before executing any code. However, calling this method may be useful to avoid the expensive generation of data for debug output only.
Changes the message type type for the category to enable .
This method is meant for use only from inside a filter installed with installFilter (). For an overview on how to configure categories globally, see 配置类别 .
注意:
QtFatalMsg
cannot be changed; it will always remain
true
.
另请参阅 isEnabled ().
[static]
void
QLoggingCategory::
setFilterRules
(const
QString
&
rules
)
Configures which categories and message types should be enabled through a set of rules .
范例:
QLoggingCategory::setFilterRules(QStringLiteral("driver.usb.debug=true"));
注意:
The rules might be ignored if a custom category filter is installed with
installFilter
(), or if the user has defined the
QT_LOGGING_CONF
或
QT_LOGGING_RULES
环境变量。
Returns the object itself. This allows for both: a QLoggingCategory variable, and a factory method that returns a QLoggingCategory , to be used in qCDebug (), qCWarning (),或 qCCritical () 宏。
Returns the object itself. This allows for both: a QLoggingCategory variable, and a factory method that returns a QLoggingCategory , to be used in qCDebug (), qCWarning (),或 qCCritical () 宏。
[since 5.2]
Q_DECLARE_LOGGING_CATEGORY
(
name
)
Declares a logging category name . The macro can be used to declare a common logging category shared in different parts of the program.
This macro must be used outside of a class or method.
该函数在 Qt 5.2 引入。
另请参阅 Q_LOGGING_CATEGORY ().
[since 5.4]
Q_LOGGING_CATEGORY
(
name
,
string
,
msgType
)
Defines a logging category name , and makes it configurable under the string identifier. By default, messages of QtMsgType msgType and more severe are enabled, types with a lower severity are disabled.
Only one translation unit in a library or executable can define a category with a specific name. The implicitly-defined QLoggingCategory object is created on first use, in a thread-safe manner.
This macro must be used outside of a class or method. It is only defined if variadic macros are supported.
该函数在 Qt 5.4 引入。
另请参阅 Q_DECLARE_LOGGING_CATEGORY ().
[since 5.2]
Q_LOGGING_CATEGORY
(
name
,
string
)
Defines a logging category name , and makes it configurable under the string identifier. By default, all message types are enabled.
Only one translation unit in a library or executable can define a category with a specific name. The implicitly-defined QLoggingCategory object is created on first use, in a thread-safe manner.
This macro must be used outside of a class or method.
该函数在 Qt 5.2 引入。
另请参阅 Q_DECLARE_LOGGING_CATEGORY ().
[since 5.3]
qCCritical
(
category
, const
char
*
message
, ...)
Logs a critical message, message , in the logging category, category . message may contain place holders to be replaced by additional arguments, similar to the C printf() function.
范例:
QLoggingCategory category("driver.usb"); qCCritical(category, "a critical message logged into category %s", category.categoryName());
注意: If the critical output for a particular category isn't enabled, arguments won't be processed, so don't rely on any side effects.
该函数在 Qt 5.3 引入。
另请参阅 qCritical ().
[since 5.2]
qCCritical
(
category
)
Returns an output stream for critical messages in the logging category, category .
The macro expands to code that checks whether
QLoggingCategory::isCriticalEnabled
() evaluates to
true
. If so, the stream arguments are processed and sent to the message handler.
范例:
QLoggingCategory category("driver.usb"); qCCritical(category) << "a critical message";
注意: If the critical output for a particular category isn't enabled, arguments won't be processed, so don't rely on any side effects.
该函数在 Qt 5.2 引入。
另请参阅 qCritical ().
[since 5.3]
qCDebug
(
category
, const
char
*
message
, ...)
Logs a debug message, message , in the logging category, category . message may contain place holders to be replaced by additional arguments, similar to the C printf() function.
范例:
QLoggingCategory category("driver.usb"); qCDebug(category, "a debug message logged into category %s", category.categoryName());
注意: Arguments aren't processed if the debug output for that category is not enabled, so don't rely on any side effects.
该函数在 Qt 5.3 引入。
另请参阅 qDebug ().
[since 5.2]
qCDebug
(
category
)
Returns an output stream for debug messages in the logging category, category .
The macro expands to code that checks whether
QLoggingCategory::isDebugEnabled
() evaluates to
true
. If so, the stream arguments are processed and sent to the message handler.
范例:
QLoggingCategory category("driver.usb"); qCDebug(category) << "a debug message";
注意: Arguments aren't processed if the debug output for that category is not enabled, so don't rely on any side effects.
该函数在 Qt 5.2 引入。
另请参阅 qDebug ().
[since 5.5]
qCInfo
(
category
, const
char
*
message
, ...)
Logs an informational message, message , in the logging category, category . message may contain place holders to be replaced by additional arguments, similar to the C printf() function.
范例:
QLoggingCategory category("driver.usb"); qCInfo(category, "an informational message logged into category %s", category.categoryName());
注意: If the debug output for a particular category isn't enabled, arguments won't be processed, so don't rely on any side effects.
该函数在 Qt 5.5 引入。
另请参阅 qInfo ().
[since 5.5]
qCInfo
(
category
)
Returns an output stream for informational messages in the logging category, category .
The macro expands to code that checks whether
QLoggingCategory::isInfoEnabled
() evaluates to
true
. If so, the stream arguments are processed and sent to the message handler.
范例:
QLoggingCategory category("driver.usb"); qCInfo(category) << "an informational message";
注意: If the debug output for a particular category isn't enabled, arguments won't be processed, so don't rely on any side effects.
该函数在 Qt 5.5 引入。
另请参阅 qInfo ().
[since 5.3]
qCWarning
(
category
, const
char
*
message
, ...)
Logs a warning message, message , in the logging category, category . message may contain place holders to be replaced by additional arguments, similar to the C printf() function.
范例:
QLoggingCategory category("driver.usb"); qCWarning(category, "a warning message logged into category %s", category.categoryName());
注意: If the warning output for a particular category isn't enabled, arguments won't be processed, so don't rely on any side effects.
该函数在 Qt 5.3 引入。
另请参阅 qWarning ().
[since 5.2]
qCWarning
(
category
)
Returns an output stream for warning messages in the logging category, category .
The macro expands to code that checks whether
QLoggingCategory::isWarningEnabled
() evaluates to
true
. If so, the stream arguments are processed and sent to the message handler.
范例:
QLoggingCategory category("driver.usb"); qCWarning(category) << "a warning message";
注意: If the warning output for a particular category isn't enabled, arguments won't be processed, so don't rely on any side effects.
该函数在 Qt 5.2 引入。
另请参阅 qWarning ().