声明用于日志记录的类的模块名称

发布于 2024-08-26 10:59:26 字数 540 浏览 12 评论 0原文

我目前正在向我们的日志库添加一些功能。其中之一是可以为一个类声明一个模块名称,该名称会自动添加到从该类中写入的任何日志消息之前。但是,如果未提供模块名称,则不会在前面添加任何内容。目前我正在使用一个具有返回名称的静态函数的特征类。

template< class T >
struct ModuleNameTrait {
    static std::string Value() { return ""; }
};

template< >
struct ModuleNameTrait< Foo > {
    static std::string Value() { return "Foo"; }
};

可以使用辅助宏来定义此类。缺点是,模块名称必须在类之外声明。我希望这在课堂上成为可能。另外,我希望能够使用预处理器指令删除所有日志记录代码。我知道使用 SFINAE 可以检查模板参数是否具有某个成员,但由于其他人对模板不像我那么友好,因此必须维护代码,所以我正在寻找一种更简单的解决方案。如果没有,我将坚持使用特质方法。

提前致谢!

I currently am adding some features to our logging-library. One of these is the possibility to declare a module-name for a class that automatically gets preprended to any log-messages writing from within that class. However, if no module-name is provided, nothing is prepended. Currently I am using a trait-class that has a static function that returns the name.

template< class T >
struct ModuleNameTrait {
    static std::string Value() { return ""; }
};

template< >
struct ModuleNameTrait< Foo > {
    static std::string Value() { return "Foo"; }
};

This class can be defined using a helper-macro. The drawback is, that the module-name has to be declared outside of the class. I would like this to be possible within the class. Also, I want to be able to remove all logging-code using a preprocessor directive. I know that using SFINAE one can check if a template argument has a certain member, but since other people, that are not as friendly with templates as I am, will have to maintain the code, I am looking for a much simpler solution. If there is none, I will stick with the traits approach.

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无声静候 2024-09-02 10:59:26

我希望这可以在课堂上实现。

这对于您的方法来说是不可能的,必须在模板所属的命名空间中声明显式专业化。

您没有说明实际使用代码的样子,但您应该能够让名称和重载解析为您工作(例如,从日志记录宏):

template<class T> const char* const name(const T&) { return ""; }

class X;
const char* const name(const X&) { return "X"; }

struct X {
    // prints "X"
    void f() { std::cout << name(*this) <<  std::endl; }
};

struct Y {
    static const char* const name(const Y&) { return "Y"; }    
    // prints "Y"
    void f() { std::cout << name(*this) << std::endl; }
};

struct Z {
    // prints ""
    void f() { std::cout << name(*this) << std::endl; }
};

如果您想定义 name()仅在类中而不是在外部,当然不需要模板或重载:

const char* const name() { return ""; }

struct X {
    static const char* const name() { return "X"; }    
    // prints "X"
    void f() { std::cout << name() << std::endl; }
};

struct Y {
    // prints ""
    void f() { std::cout << name() <<  std::endl; }
};

I would like this to be possible within the class.

This is not possible with your approach, explicit specializations have to be declared in the namespace of which the template is a member.

You don't say how the actual using code looks like, but you should be able to let name and overload resolution work for you (e.g. from a logging macro):

template<class T> const char* const name(const T&) { return ""; }

class X;
const char* const name(const X&) { return "X"; }

struct X {
    // prints "X"
    void f() { std::cout << name(*this) <<  std::endl; }
};

struct Y {
    static const char* const name(const Y&) { return "Y"; }    
    // prints "Y"
    void f() { std::cout << name(*this) << std::endl; }
};

struct Z {
    // prints ""
    void f() { std::cout << name(*this) << std::endl; }
};

If you want to define name() only in classes and not outside, there is of course no need for templates or overloads:

const char* const name() { return ""; }

struct X {
    static const char* const name() { return "X"; }    
    // prints "X"
    void f() { std::cout << name() << std::endl; }
};

struct Y {
    // prints ""
    void f() { std::cout << name() <<  std::endl; }
};
红焚 2024-09-02 10:59:26

我不确定解决方案应该有多简单,这是我使用过几次的非常简单的解决方案。

有一个基类 ClassName ,其内容类似于:

class ClassName
{
    string name;
public:
    ClassName( string strName = "" ) : name(strName)
    {
         if( strName.length() )
               strName += ": ";
    }
    string getName()
    {
        return name;
    }
};
#ifdef _DEBUG
    #define LOG cout << getName() 
#else
    #define LOG cout
#endif

其他类将继承它,并给出其名称:

class Session : virtual public ClassName
{
public:
    Session() : ClassName("Session")
    {
    }

    void func()
    {
         LOG << "Some log here" << endl;
    }
};

I'm not sure how simple the solution should be, here's a very simple one I used a few times.

There is a base class ClassName with something like:

class ClassName
{
    string name;
public:
    ClassName( string strName = "" ) : name(strName)
    {
         if( strName.length() )
               strName += ": ";
    }
    string getName()
    {
        return name;
    }
};
#ifdef _DEBUG
    #define LOG cout << getName() 
#else
    #define LOG cout
#endif

And other class would inherit it, giving its name:

class Session : virtual public ClassName
{
public:
    Session() : ClassName("Session")
    {
    }

    void func()
    {
         LOG << "Some log here" << endl;
    }
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文