title: C++ print colored text in cross-platform
body: <p>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?</p>
  <p>Code:</p>
  <pre><code>#ifndef CLIOUTPUT_H_
  #include &lt;stdio.h&gt;
  #if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__)
      #include &lt;Windows.h&gt;
      #define _RED     0x0C
      #define _GREEN   0x0A
      #define _DEFAULT 0x0F
      HANDLE __c = GetStdHandle(STD_OUTPUT_HANDLE);
      #define _COLOREDTEXT(_msg, _color) do { \
          SetConsoleTextAttribute(__c, _color); \
          printf(&quot;%s&quot;, _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(&quot;\033[0;%dm%s\033[0m&quot;, _color, _msg); \
      } while(false)
  
  #else
  # error &quot;Error&quot;
  #endif
  #define CLIOUTPUT_H_
  #endif // CLIOUTPUT_H_
  
  </code></pre>