如何将 clog to tee 重新定义为原始 clog 和日志文件?
我在这里看到了一个有用的开始:
http://www.cs。 technion.ac.il/~imaman/programs/teestream.html
并且它非常适合创建一个同时进入 clog 和日志文件的新流。
但是,如果我尝试将 clog 重新定义为新流,它将不起作用,因为新流具有与 clog 相同的 rdbuf() ,因此以下内容无效:
clog.rdbuf(myTee.rdbuf());
那么如何修改 tee 类以拥有自己的 rdbuf( ) 那么哪个可以成为 clog 的目标呢?
谢谢。
-威廉
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您确实想继续使用 std::clog 作为 tee,而不是将输出发送到不同的流,则需要降低一级:从 Streambuf 派生,而不是从 ostream 派生。 然后您可以执行以下操作:
有关如何派生您自己的streambuf类的更多信息,请参阅此处。
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:
For more information on how to derive your own streambuf class, see here.
您不想做您想做的事情,因为“tee”在 rdbuf 级别不起作用。 因此,将 rdbuf 设置为其他值将不起作用,输出只会进入一个流。
您需要遵循该示例:
例如,
然后在各处使用木屐而不是原来的木屐。
You don't want to do what your've trying to do because the 'tee' is not working at the rdbuf level. So setting the rdbuf to something else will not work, the output will only go to one stream.
You need to follow there example:
e.g.
then use clog everywhere instead of your original clog.
这是我创建的课程,似乎可以完成这项工作,感谢所有提供帮助的人!
-威廉
Here is the class I created that seems to do the job, thanks to all who helped out!
-William
我只会使用 Boost iostreams 来做到这一点。
<代码>
#include
#include
#include
#include
I would just use the Boost iostreams stuff to do it.
#include <iostream>
#include <fstream>
#include <boost/iostreams/tee.hpp>
#include <boost/iostreams/stream.hpp>