How to redefine clog to tee to original clog and a log file?

前端 未结 4 672
鱼传尺愫
鱼传尺愫 2020-12-17 04:17

I saw a useful start here:

http://www.cs.technion.ac.il/~imaman/programs/teestream.html

And it works great to make a new stream which goes to both clog and a

4条回答
  •  醉酒成梦
    2020-12-17 05:17

    If you really want to keep using std::clog for the tee instead of sending output to a different stream, you need to work one level lower: Instead of deriving from ostream, derive from streambuf. Then you can do this:

    fstream logFile(...);
    TeeBuf tbuf(logFile.rdbuf(), clog.rdbuf());
    clog.rdbuf(&tbuf);
    

    For more information on how to derive your own streambuf class, see here.

提交回复
热议问题