C++ print colored text in cross-platform

Viewed 67

I implemented my own way of printing colored texts in cross-platform. However, I used macros. Is this a bad practice or something that should be avoided? If so, what alternative methods would you recommend?

Code:

#ifndef CLIOUTPUT_H_
#include <stdio.h>
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__)
    #include <Windows.h>
    #define _RED     0x0C
    #define _GREEN   0x0A
    #define _DEFAULT 0x0F
    HANDLE __c = GetStdHandle(STD_OUTPUT_HANDLE);
    #define _COLOREDTEXT(_msg, _color) do { \
        SetConsoleTextAttribute(__c, _color); \
        printf("%s", _msg); \
        SetConsoleTextAttribute(__c, _DEFAULT); \
    } while(false)

#elif __APPLE__ || __linux__
    #define _RED     31
    #define _GREEN   32
    #define _DEFAULT \033[0m

    #define _COLOREDTEXT(_msg, _color) do { \
        printf("\033[0;%dm%s\033[0m", _color, _msg); \
    } while(false)

#else
# error "Error"
#endif
#define CLIOUTPUT_H_
#endif // CLIOUTPUT_H_

0 Answers
Related