07 November 2015

A C++ debugging helper

When working with C++ and you're in debug mode, you might be accustomed to these:
  • Using a lot of std::cout statements.
  • Seeing a clutter of outputs.
  • Introducing a lot of temporary code which you don't want to be part of the final build
  • Commenting out useful code which you don't want others to delete later because it's useful

and more...

I needed a way of color coding various outputs, so I used this:

/* Include this hpp file in any file where you need to output
 * cerr or cout.
 * 
 * Usage: In your code, instead of typing std::cout<<"Hi";
 * Just type cout("Hi");
 * Same way, instead of std::cerr<<"Error"; just type cerr("Error");
 */

#ifndef DEBUGHELPER_HPP
#define DEBUGHELPER_HPP

#define DEBUG_MODE

#define USEFUL_CODE_TEMPORARILY_COMMENTED_OUT_DONT_DELETE_IT
#define CODE_FOR_TESTING_FEEL_FREE_TO_DELETE_IT

#ifdef DEBUG_MODE
    //Colour code by Vaughan Schmidt. http://www.codebuilder.me/2014/01/color-terminal-text-in-c/
    #define RESET   "\033[0m"
    #define BLACK   "\033[30m"      // Black
    #define RED     "\033[31m"      // Red
    #define GREEN   "\033[32m"      // Green
    #define YELLOW  "\033[33m"      // Yellow
    #define BLUE    "\033[34m"      // Blue
    #define MAGENTA "\033[35m"      // Magenta
    #define CYAN    "\033[36m"      // Cyan
    #define WHITE   "\033[37m"      // White
    #define BOLDBLACK   "\033[1m\033[30m"      // Bold Black
    #define BOLDRED     "\033[1m\033[31m"      // Bold Red
    #define BOLDGREEN   "\033[1m\033[32m"      // Bold Green
    #define BOLDYELLOW  "\033[1m\033[33m"      // Bold Yellow
    #define BOLDBLUE    "\033[1m\033[34m"      // Bold Blue
    #define BOLDMAGENTA "\033[1m\033[35m"      // Bold Magenta
    #define BOLDCYAN    "\033[1m\033[36m"      // Bold Cyan
    #define BOLDWHITE   "\033[1m\033[37m"      // Bold White
    #define CLEAR "\033[2J"  // clear screen escape code
    #define coutBlack(x) (std::cout << BLACK << (x) << RESET)
    #define coutBoldBlack(x) (std::cout << BOLDBLACK << (x) << RESET)
    #define coutRed(x) (std::cout << RED << (x) << RESET)
    #define coutBoldRed(x) (std::cout << BOLDRED << (x) << RESET)
    #define coutGreen(x) (std::cout << GREEN << (x) << RESET)
    #define coutBoldGreen(x) (std::cout << BOLDGREEN << (x) << RESET)
    #define coutYellow(x) (std::cout << YELLOW << (x) << RESET)
    #define coutBoldYellow(x) (std::cout << BOLDYELLOW << (x) << RESET)
    #define coutBlue(x) (std::cout << BLUE << (x) << RESET)
    #define coutBoldBlue(x) (std::cout << BOLDBLUE << (x) << RESET)
    #define coutMagenta(x) (std::cout << MAGENTA << (x) << RESET)
    #define coutBoldMagenta(x) (std::cout << BOLDMAGENTA << (x) << RESET)
    #define coutCyan(x) (std::cout << CYAN << (x) << RESET)
    #define coutBoldCyan(x) (std::cout << BOLDCYAN << (x) << RESET)
    #define coutWhite(x) (std::cout << WHITE << (x) << RESET)
    #define coutBoldWhite(x) (std::cout << BOLDWHITE << (x) << RESET)   

    #define cerr(x) (std::cerr << (x))
    #define cout(x) (std::cout << (x))
    //... etc
#else 
    #define cerr(x)
    #define cout(x)
    //... etc
#endif

#endif // DEBUGHELPER_HPP

Not only are the cout's easier to type now, they are also easy to disable.
 
UPDATE: As someone commented below, you could use a logger too, but the #defines helped me in the specific way I wanted.

2 comments:

Anonymous said...

pretty dumb, the right thing to do is to use a configuration log level and have your logger check this before printing out your log, so as to prevent recompilations
the only sane reason to use conditional compilation is for multi-platform code and assert statements

Nav said...

Well Anonymous, firstly, it'd help if you mentioned who you are, so that we could have had a productive conversation. Secondly, yes you are right about using a good logger. However, rather than jump to the conclusion that what I've mentioned is dumb, I hope you'd try to see that there was a need for me to see the outputs in specific colours. It was this need that led to the creation of the #defines. Conditional compilation is "sanely" used in more ways than what you've mentioned. Do try to view solutions positively and from the perspective of the person who created it.