Colored debug macro
Whenever you start developing on C++, the first thing you think is that it is an ugly, boring and archaic environment. After a while you realize the power and wonders that you can achieve with this programming language, and why it is still so widespread.
Today we are going to see how to generate some simple macros, which we can include in our most typical libraries, to be able to show the colored debug output, and thus differentiate between each output of our program in a more simple way.
With a simple line we can make our colored output:
#define DEBUG(…) do{fprintf(stderr,“33[34m”); fprintf(stderr, __VA_ARGS__); fprintf(stderr,“33[0m”);}while(0)
To change the different colors here I leave you a wide range:
#define RESET “33[0m” #define BLACK “33[30m” /* Black */ #define RED “33[31m” /* Red */ #define GREEN “33[32m” /* Green */ #define YELLOW “33[33m” /* Yellow */ #define BLUE “33[34m” /* Blue */ #define MAGENTA “33[35m” /* Magenta */ #define CYAN “33[36m” /* Cyan */ #define WHITE “33[37m” /* White */ #define BOLDBLACK “33[1m33[30m” /* Bold Black */ #define BOLDRED “33[1m33[31m” /* Bold Red */ #define BOLDGREEN “33[1m33[32m” /* Bold Green */ #define BOLDYELLOW “33[1m33[33m” /* Bold Yellow */ #define BOLDBLUE “33[1m33[34m” /* Bold Blue */ #define BOLDMAGENTA “33[1m33[35m” /* Bold Magenta */ #define BOLDCYAN “33[1m33[36m” /* Bold Cyan */ #define BOLDWHITE “33[1m33[37m” /* Bold White */
All we have to do is call it every time we try to use said macro using:
DEBUG(“Text to display”);