C++记录每一行代码的宏

发布于 2024-12-05 01:59:06 字数 327 浏览 3 评论 0原文

在我最近与经理的一次讨论中,他提到他的一位前客户使用 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 技术交流群。

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

发布评论

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

评论(2

你如我软肋 2024-12-12 01:59:06

我不知道是否每行/变量都可以这样扩展,但可以记录函数调用。我已使用 gcc 的 -finstrument-functions 选项记录了所有函数调用。它将调用:

  void __cyg_profile_func_enter (void *this_fn, void *call_site);

   void __cyg_profile_func_exit  (void *this_fn, void *call_site);

函数进入和退出。

文档解释了如何使用它。我不知道其他编译器是否提供类似的东西。

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:

  void __cyg_profile_func_enter (void *this_fn, void *call_site);

and

   void __cyg_profile_func_exit  (void *this_fn, void *call_site);

for function enter and exit.

The docs explain how to use it. I don't know if other compilers offer something similar.

怪我入戏太深 2024-12-12 01:59:06

您可以检查 Boost.Test 的 BOOST_CHECKA 是如何实现的。它在内部使用表达式模板。

对于测试:

#define BOOST_TEST_MAIN

#include <boost/test/included/unit_test.hpp>
#include <boost/test/test_tools.hpp>

BOOST_AUTO_TEST_CASE(test1)
{
    int a=0;
    int b=1;
    int c=2;
    BOOST_CHECKA( a+b == c );
}

输出为:

Running 1 test case...
main.cpp(11): error: in "test1": check a+b == c failed [0+1!=2]

*** 1 failure detected in test suite "Master Test Suite"

注意方括号中的值:[0+1!=2]

它有一些限制。

对于测试:

BOOST_CHECKA( (a+b) == c );

输出为:

check (a+b) == c failed [1!=2]

You may check how BOOST_CHECKA from Boost.Test is implemented. Internally it uses expression templates.

For test:

#define BOOST_TEST_MAIN

#include <boost/test/included/unit_test.hpp>
#include <boost/test/test_tools.hpp>

BOOST_AUTO_TEST_CASE(test1)
{
    int a=0;
    int b=1;
    int c=2;
    BOOST_CHECKA( a+b == c );
}

Output is:

Running 1 test case...
main.cpp(11): error: in "test1": check a+b == c failed [0+1!=2]

*** 1 failure detected in test suite "Master Test Suite"

Note values in square brackets: [0+1!=2]

It has some limitations.

For test:

BOOST_CHECKA( (a+b) == c );

output is:

check (a+b) == c failed [1!=2]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文