title: Redirection of cout to a file per thread to manage thread-safe logging
body: <p>I have a multithreaded application that uses standard cout for logging, for example, </p>
  
  <pre><code>cout &lt;&lt; "some text" &lt;&lt; endl;
  </code></pre>
  
  <p>The problem is that the program is facing an occasional crash because of non-thread-safe access to shared cout by multiple threads.</p>
  
  <p>Say, my program is named prg.exe and we run as prog.exe > t.log</p>
  
  <p>All logs generated by individual thread gets mingled up, even that's ok, the bad part is the crash whenever there is contention by the competing threads in accessing cout, one trying to flush, other trying yo put something there, resulting in the crash.</p>
  
  <p>Since there are are lot many uses of cout in existing code, used by different threads, this is difficult to change all cout to something else. So, I am trying to adopt the following:</p>
  
  <pre><code>std::streambuf * redirect_output(char * filenm, std::ofstream&amp; filestr)
  {
    std::streambuf *newsb, *oldsb;
    filestr.open(filenm);
    oldsb = std::cout.rdbuf();     // back up cout's streambuf
    newsb = filestr.rdbuf();       // get file's streambuf
    std::cout.rdbuf(newsb);        // assign streambuf to cout
    return oldsb;
  }
  
  void restore_output(std::streambuf * oldsb, std::ofstream&amp; filestr)
  {
    std::cout.rdbuf(oldsb);        // restore cout's original streambuf
    filestr.close();
  }
  void showFileContent(char *filenm)
  {
    std::ifstream infile;
    infile.open(filenm);
    //read data from file
    cout &lt;&lt; "------------- " &lt;&lt; filenm &lt;&lt; " -------------"  &lt;&lt; endl;
    cout &lt;&lt; infile.rdbuf();
    infile.close();
  }
  </code></pre>
  
  <p>Each thread, on start, tries to call redirect_output to redirect cout to a file, separate file per thread. So, for example, if we have 3 threads, we have t1.log, t2.log, t3.log
  and at the end we call restore_output by each thread and eventually in main, we merge the individual log files through </p>
  
  <pre><code>showFileContent("t1.log"); 
  showFileContent("t2.log"); 
  showFileContent("t3.log"); 
  </code></pre>
  
  <p>My question is, is it wise and safe to redirect to individual log files per thread and merge at the end of the main function? 
  Can we merge the individual log files at logical sync points by individual threads?</p>
  
  <p>Other option could be to have a thread-safe singleton class encapsulating built-in i/o and use that singleton object instead of cout e.g. SPCL_IOSTREAM::getOStream()</p>