title: Changing color of log output in Windows console using Boost Logs
body: <p>I am using Boost 1.54 Logging APIs and trying to change the color of the console log output.  Below is the link to the minimum test code I am using (felt the code was a bit long to post directly):</p>
  
  <p><a href="http://ideone.com/o2KGw9" rel="nofollow noreferrer">http://ideone.com/o2KGw9</a></p>
  
  <p>So, line 96 works, which is a std::cout to the console in color:</p>
  
  <p><code>std::cout &lt;&lt; colorSet( h, DARKTEAL )&lt;&lt;" I am coloured!!" &lt;&lt; colorSet( h, GRAY ) &lt;&lt; std::endl;</code></p>
  
  <p>But when I try to do the same in boost::log::expressions::stream, in line 77, there is no color in the printed text.</p>
  
  <p><code>logging::formatter fmtconsole = expr::stream &lt;&lt; colorSet( h, BLUE )&lt;&lt; "DEBUG:" &lt;&lt; expr::smessage &lt;&lt; colorSet( h, GRAY ) &lt;&lt; "\n";</code></p>
  
  <p>Below is a screen capture of the output I get:</p>
  
  <p><img src="https://i.stack.imgur.com/MhwwO.png" alt="Console output"></p>
  
  <p>I remember reading a post by in the Boost sourceforge forums by Andrey Semashev that it is possible to write a custom formatter that adds color control codes to the output, but AFIK that works only with Linux terminals, which is why I am trying this approach.</p>
  
  <p>Is there a way to achieve this?</p>
  
  <p>Thanks.</p>
  
  <p>UPDATE:</p>
  
  <p>So the way I achieved it is by giving a custom formatter function as Semasheve suggested:</p>
  
  <pre><code>void my_formatter(logging::record_view const&amp; rec, logging::formatting_ostream&amp; strm)
  {
      HANDLE h = GetStdHandle( STD_OUTPUT_HANDLE );
      // Finally, put the record message to the stream
      boost::locale::generator gen;
      std::locale::global(gen(""));
  
      boost::locale::date_time now;
      std::cout.imbue(std::locale());
  
      std::cout &lt;&lt; colorSet( h, BLUE ) &lt;&lt; boost::locale::as::ftime( "%H:%M:%S" ) &lt;&lt; "[" &lt;&lt; now &lt;&lt; "] ";
      std::cout &lt;&lt; colorSet( h, RED ) &lt;&lt; rec[expr::smessage] &lt;&lt; colorSet( h, GRAY );
  
  }
  </code></pre>
  
  <p>Refer to the main code link to see colorSet definition.</p>
  
  <p>And then set the formatter for the backend like:</p>
  
  <p><code>consolebackend-&gt;set_formatter( &amp;my_formatter );</code></p>
  
  <p>Unfortunately I am not using the formatting_ostream object, which makes this like a not a great solution.  And the reason I couldn't use it was because of the way Boost flushes the stream.  So I would get only the last color in the stream.  Thus I ended up using std::cout directly.</p>
  
  <p>But I am sure it is possible, somehow.</p>
  
  <p>Though this does the job, I still need a mechanism by which I can get a stream as a temporary object, which could be called from a macro.</p>
  
  <p>Maybe something like:</p>
  
  <pre><code>class TMP_LOG
  {
      public:
      TMP_LOG(const Color&amp; c) : col(c), m_str() { }
  
      ~TMP_LOG()
      {
          HANDLE h = GetStdHandle( STD_OUTPUT_HANDLE );
          SetConsoleTextAttribute(h, col);
  
          BOOST_LOG_SEV( slg, normal ) &lt;&lt; m_str.str();
  
          SetConsoleTextAttribute(h, GRAY);
      }
  
      std::ostringstream&amp; stream() { return m_str; } 
  
  private:
      std::ostringstream m_str;
  
      Color col;
  };
  
  #define LOG_DEBUG   TMP_LOG(RED).stream()
  #define LOG_WARN    TMP_LOG(YELLOW).stream()
  </code></pre>