在全局函数中获取对象地址或 NULL 的统一方法
我正在为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以更改宏定义:
用法:
You can change your macro definition:
Usage:
我认为如果不修改要记录的类中的代码,这是不可能的。但是,如果您愿意这样做,则可以从类似这样的模板派生您想要记录功能的所有类:
例如:
函数 get_this() 的附加全局定义将在非成员函数中使用
:日志宏看起来像:
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:
For example:
An additional global definition of the function get_this() will be used in non-member functions:
The logging macro would look like: