如何重新定义clog到三通到原始的clog和日志文件? [英] How to redefine clog to tee to original clog and a log file?

查看:237
本文介绍了如何重新定义clog到三通到原始的clog和日志文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里看到一个有用的开始:



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



并且它工作伟大的做一个新的流去阻塞和日志文件。



但是,如果我试图重新定义clog是新的流,它不工作,因为新的流具有与clog相同的rdbuf(),所以以下没有效果:

  clog.rdbuf(myTee。 rdbuf()); 

那么如何修改tee类以具有自己的rdbuf



-William

解决方案

如果你真的想继续使用std :: clog作为发球区域,而不是将输出发送到不同的流,你需要降低一个级别:而不是派生自ostream,派生从streambuf。那么你可以这样做:

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

有关如何派生自己的streambuf类的更多信息,请参阅此处


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 log file.

However, if I try to redefine clog to be the new stream it does not work because the new stream has the same rdbuf() as clog so the following has no effect:

clog.rdbuf(myTee.rdbuf());

So how can I modify the tee class to have its own rdbuf() which can then be the target of clog?

Thanks.

-William

解决方案

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.

这篇关于如何重新定义clog到三通到原始的clog和日志文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆