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.

Dec 19, 2013

How to set a X window visible or invisible using Xlib


bool m_visible;
Display* m_display;
Window m_window;

void setVisible(bool visible)
{
    if (visible == m_visible)
        return;

    if (visible)
        XMapWindow(m_display, m_window);
    else
        XUnmapWindow(m_display, m_window);

    m_visible = visible;
}