在全局函数中获取对象地址或 NULL 的统一方法

发布于 2024-11-25 09:33:42 字数 506 浏览 2 评论 0原文

我正在为 C++ 构建一个基于树的调试/日志系统。

它的“用户界面”是一个宏,它将用户定义的消息和调用站点信息(文件、行、对象地址)传递给特殊函数,然后执行日志记录。

该函数使用对象地址按对象实例对消息进行分组。

目前它看起来像这样:

// in logging system header
#define msg (event_level, message) \
    do_logging_ (event_level, __FILE__, __LINE__, this, message)

...

// in code
msg (MSG_WARNING, "some text");

我想问,是否有一些统一的方法(可在 msg 宏中使用)来获取 NULL 而不是 this where this< /code> 未定义(全局/静态函数)?

I'm building a tree-based debug/logging system for C++.

Its "user interface" is a macro which passes user-defined message and call site information (file, line, object address) to special function which then performs logging.

The function uses the object address to group messages by object instance.

Currently it looks like this:

// in logging system header
#define msg (event_level, message) \
    do_logging_ (event_level, __FILE__, __LINE__, this, message)

...

// in code
msg (MSG_WARNING, "some text");

I want to ask, is there some uniform way (usable in the msg macro) to get NULL instead of this where this is not defined (global/static functions)?

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

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

发布评论

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

评论(2

街道布景 2024-12-02 09:33:42

您可以更改宏定义:

#define msg (event_level, message, THIS) \
    do_logging_ (event_level, __FILE__, __LINE__, THIS, message)

用法:

msg (MSG_WARNING, "some text", this); // in member methods
msg (MSG_WARNING, "some text", NULL); // otherwise

You can change your macro definition:

#define msg (event_level, message, THIS) \
    do_logging_ (event_level, __FILE__, __LINE__, THIS, message)

Usage:

msg (MSG_WARNING, "some text", this); // in member methods
msg (MSG_WARNING, "some text", NULL); // otherwise
勿忘初心 2024-12-02 09:33:42

我认为如果不修改要记录的类中的代码,这是不可能的。但是,如果您愿意这样做,则可以从类似这样的模板派生您想要记录功能的所有类:

    template <typename T>
    class loggable_class
    {
    protected:
        T* get_this() { return static_cast <T*> (this); }
    };

例如:

    class A : public loggable_class<A>
    {
        ...
    };

函数 get_this() 的附加全局定义将在非成员函数中使用

    inline void* get_this()
    {
         return NULL;
    }

:日志宏看起来像:

    #define msg (event_level, message) \
        do_logging_ (event_level, __FILE__, __LINE__, get_this(), message)

I think this is not possible without modifying the code in your class you want to log. If you are willing to do that, however, you could derive all classes where you want logging functionality from a template like this one:

    template <typename T>
    class loggable_class
    {
    protected:
        T* get_this() { return static_cast <T*> (this); }
    };

For example:

    class A : public loggable_class<A>
    {
        ...
    };

An additional global definition of the function get_this() will be used in non-member functions:

    inline void* get_this()
    {
         return NULL;
    }

The logging macro would look like:

    #define msg (event_level, message) \
        do_logging_ (event_level, __FILE__, __LINE__, get_this(), message)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文