C++ macro to log every line of code

前端 未结 2 1207
小鲜肉
小鲜肉 2021-02-02 15:05

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 en

相关标签:
2条回答
  • 2021-02-02 15:16

    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.

    0 讨论(0)
  • 2021-02-02 15:35

    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]
    
    0 讨论(0)
提交回复
热议问题