Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is the class I created that seems to do the job, thanks to all who helped out!</p> <p>-William</p> <pre><code>class TeeStream : public std::basic_filebuf&lt;char, std::char_traits&lt;char&gt; &gt; { private: class FileStream : public std::ofstream { public: FileStream() : logFileName("/my/log/file/location.log") { open(logFileName.c_str(), ios::out | ios::trunc); if (fail()) { cerr &lt;&lt; "Error: failed to open log file: " &lt;&lt; logFileName &lt;&lt; endl; exit(1); } } ~FileStream() { close(); } const char *getLogFileName() const { return logFileName.c_str(); } private: const string logFileName; }; public: typedef std::char_traits&lt;char&gt; traits; typedef std::basic_filebuf&lt;char, traits&gt; baseClass; TeeStream() : baseClass(), _logOutputStream(), _clogBuf(clog.rdbuf()), _fileBuf(_logOutputStream.rdbuf()) { clog.rdbuf(this); _logOutputStream &lt;&lt; "Log file starts here:" &lt;&lt; endl; } ~TeeStream() { clog.rdbuf(_clogBuf); } int_type overflow(char_type additionalChar =traits::eof()) { const int_type eof = traits::eof(); const char_type additionalCharacter = traits::to_char_type(additionalChar); const int_type result1 = _clogBuf-&gt;sputc(additionalCharacter); const int_type result2 = _fileBuf-&gt;sputc(additionalCharacter); if (traits::eq_int_type(eof, result1)) { return eof; } else { return result2; } } int sync() { const int result1 = _clogBuf-&gt;pubsync(); const int result2 = _fileBuf-&gt;pubsync(); if (result1 == -1) { return -1; } else { return result2; } } private: FileStream _logOutputStream; streambuf * const _clogBuf; streambuf * const _fileBuf; }; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload