C++记录每一行代码的宏
在我最近与经理的一次讨论中,他提到他的一位前客户使用 C++ 宏来记录每行代码的信息。他们所要做的就是在开始运行之前启用环境变量。 (当然,环境变量是在测试台中单独启用的。
日志也提到了使用的变量及其对应的值。 例如,对于以下行:
a = a + b;
日志会这样写:
"a = a + b; (a = 5 + 3)"
就我个人而言,我不确定这是否可能,但他非常确定它已经存在,尽管他不记得代码的具体细节。
所以,这是一个(明显的)问题:这可能吗?你能提供这个的代码吗?
During one of my recent discussions with my manager, he mentioned that one of his former clients used a C++ macro to log info about every line of code. All they had to do was enable an environment variable before starting the run. (Of course the environment variable was enabled in the test-bed alone.
The log mentioned the variables used and their corresponding values too.
For example, for the line:
a = a + b;
The log would say something like:
"a = a + b; (a = 5 + 3)"
Personally, I was not sure if this was possible, but he was very sure of this having existed, though he did not remember the specifics of the code.
So, here is the (obvious) question: Is this possible? Can you provide the code for this one?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不知道是否每行/变量都可以这样扩展,但可以记录函数调用。我已使用 gcc 的 -finstrument-functions 选项记录了所有函数调用。它将调用:
和
函数进入和退出。
文档解释了如何使用它。我不知道其他编译器是否提供类似的东西。
I don't know if every line/variable can be expanded like that, but function calls can be logged. I have logged all function calls using the
-finstrument-functions
option of gcc. It will call:and
for function enter and exit.
The docs explain how to use it. I don't know if other compilers offer something similar.
您可以检查 Boost.Test 的 BOOST_CHECKA 是如何实现的。它在内部使用表达式模板。
对于测试:
输出为:
注意方括号中的值:[0+1!=2]
它有一些限制。
对于测试:
输出为:
You may check how BOOST_CHECKA from Boost.Test is implemented. Internally it uses expression templates.
For test:
Output is:
Note values in square brackets: [0+1!=2]
It has some limitations.
For test:
output is: