如何使用 Q_LOGGING_CATEGORY 并在模板化函数中引用类别而不会出现多个实现错误?

dar*_*der 2 c++ qt templates

我在 QT 项目中遇到了一个头文件的问题。

通常我使用以下结构:

全局文件

#ifndef global
#define global
#include <QLoggingCategory>
Q_DECLARE_LOGGING_CATEGORY(cat)
#endif
Run Code Online (Sandbox Code Playgroud)

foo.h

#ifndef foo
#define foo
#include "global.h"
class foo{
 public:
   void bar();
};
#endif
Run Code Online (Sandbox Code Playgroud)

文件

#include "foo.h"
Q_LOGGING_CATEGORY(cat, "awesomecategory")
void foo::bar(){
qCDebug(cat) << "helloworld";
}
Run Code Online (Sandbox Code Playgroud)

这很好用。现在我有一个头文件,它从内部实现了一个模板化函数,我想登录到我的日志记录类别:

foo.h

#ifndef foo
#define foo

#include "global.h"

template <typename T>
bool bar(T, int val);

// Template definitions outside of header:
#include "foo.tpp"

#endif // foo
Run Code Online (Sandbox Code Playgroud)

文件

#include <QLoggingCategory>
Q_LOGGING_CATEGORY(cat, "awesomecategory")
template <typename T>
bool bar(T, int val)
{
        T baz;
        // do stuff with baz
        qCDebug(cat) << "helloworld";
}
Run Code Online (Sandbox Code Playgroud)

如果我在另一个 cpp 文件中调用模板化函数(foo.h当然,我在其中包含),但只要我包含foo.h在另一个 cpp 文件中我想调用该函数,我就会收到一个多重定义错误,如下所示:

In function `ZN11QStringListD1Ev':
C:\path\to\foo.h:{n}: multiple definition of `cat()'
C:\path\to\foo.h:{n}: first defined here
collect2.exe: error: ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud)

我必须把我的 Q_LOGGING_CATEGORY 宏放在哪里,以便函数可以使用它并且我仍然可以在不同的地方使用标题?

Max*_*rno 6

您只能使用Q_LOGGING_CATEGORY(cat, "awesomecategory")一次,因为这实际上从调用它的任何地方创建了一个“全局”函数(见下文)。此外,当您#include "foo.tpp"只是将该文件的内容放入标头时(例如,它不是像 .cpp 源文件那样的“独立单元”)。

如果你想要一个名为 的全局日志类别cat,你可以在你的global.h. 我认为Q_LOGGING_CATEGORY(cat, "awesomecategory")不会直接在标题中工作,但这很容易解决。

这是这些宏的作用......实际上非常简单,并且更清楚地看到发生了什么:

#define Q_DECLARE_LOGGING_CATEGORY(name) \
    extern const QLoggingCategory &name();

#define Q_LOGGING_CATEGORY(name, ...) \
    const QLoggingCategory &name() \
    { \
        static const QLoggingCategory category(__VA_ARGS__); \
        return category; \
    }
Run Code Online (Sandbox Code Playgroud)

所以DECLARE实际上只是一个导出函数的前向声明。您也可以将其放入global.h(并Q完全跳过所有宏):

static const QLoggingCategory &cat()
{
    static const QLoggingCategory category("awesomecategory");
    return category;
}
Run Code Online (Sandbox Code Playgroud)

它需要是全局的,或者至少在同一个默认命名空间中,或者只在一个单元中声明,因为...

#define qCDebug(category, ...) \
    for (bool qt_category_enabled = category().isDebugEnabled(); qt_category_enabled; qt_category_enabled = false) \
        QMessageLogger(QT_MESSAGELOG_FILE, QT_MESSAGELOG_LINE, QT_MESSAGELOG_FUNC, category().categoryName()).debug(__VA_ARGS__)
Run Code Online (Sandbox Code Playgroud)

或者换句话说,cat()之前创建的函数是在单元(或在我们的示例中为全局)上下文中调用的。

当然,如果仅从一个类的成员中调用类别样式日志例程,则该cat()方法可能只属于该类。

或者cat()函数本身可以在命名空间甚至它自己的类中......

class MyLoggers
{
  public:
    static const QLoggingCategory &cat()
    {
        static const QLoggingCategory category("awesomecategory");
        return category;
    }
}

// used somewhere else:
qCDebug(MyLoggers::cat)  << "Hello cats!";
Run Code Online (Sandbox Code Playgroud)

它比人们认为的那些简单的宏要灵活得多。

HTH

添加:下面的具体示例似乎没有错误。

全局文件

#define Q_DECLARE_LOGGING_CATEGORY(name) \
    extern const QLoggingCategory &name();

#define Q_LOGGING_CATEGORY(name, ...) \
    const QLoggingCategory &name() \
    { \
        static const QLoggingCategory category(__VA_ARGS__); \
        return category; \
    }
Run Code Online (Sandbox Code Playgroud)

foo.h

static const QLoggingCategory &cat()
{
    static const QLoggingCategory category("awesomecategory");
    return category;
}
Run Code Online (Sandbox Code Playgroud)

baz.h

#define qCDebug(category, ...) \
    for (bool qt_category_enabled = category().isDebugEnabled(); qt_category_enabled; qt_category_enabled = false) \
        QMessageLogger(QT_MESSAGELOG_FILE, QT_MESSAGELOG_LINE, QT_MESSAGELOG_FUNC, category().categoryName()).debug(__VA_ARGS__)
Run Code Online (Sandbox Code Playgroud)

主程序

class MyLoggers
{
  public:
    static const QLoggingCategory &cat()
    {
        static const QLoggingCategory category("awesomecategory");
        return category;
    }
}

// used somewhere else:
qCDebug(MyLoggers::cat)  << "Hello cats!";
Run Code Online (Sandbox Code Playgroud)

印刷:

awesomecategory: helloworld
awesomecategory: baz() says meow.
Run Code Online (Sandbox Code Playgroud)