Dec 27, 2013

Improve your lazy debugging in C++

Are you too lazy to learn your debugger? When debugging, I usually want to know if some code section is reached.

I can do this:


void foobar() {
    printf("foobar()\n");
    // ... do foobar
}

But I should use cout instead printf in C++ . printf is faster, but in this case it should be irrelevant. So I have:


void foobar() {
    std::cout << "foobar()" << std::endl;
    // ... do foobar
}

I could also use GCC's function name macro __PRETTY_FUNCTION__ and __LINE__ macro to identify the reached code section easier.


void foobar() {
    std::cout <<  __PRETTY_FUNCTION__ << " at line " <<  __LINE__ << std::endl;
    // ... do foobar
}

Add that on a macro to reuse it:


#if DEBUG
#include 
#define REACH std::cout <<  __PRETTY_FUNCTION__ << " at line " <<  __LINE__ << std::endl;
#endif

void foobar() {
    REACH
    // ... do foobar
}

And happy debugging!

Not.

1 comment:

  1. And should always reserve a space for the gamb_counter variable. Without it the universe could create a time paradox, the results of which could cause a chain reaction that would unravel the very fabric of the space time continuum, and destroy the entire universe! Better keep the gamb variables close, just in case... =P

    ReplyDelete