我怎样才能谱写输出流,所以输出为多个分身术? [英] How can I compose output streams, so output goes multiple places at once?

查看:104
本文介绍了我怎样才能谱写输出流,所以输出为多个分身术?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想组成两个(或多个)流为一体。我的目标是,任何输出定向到 COUT CERR 木屐也被输出到文件中,与原始流一起。 (因为当事情被记录到控制台,例如,闭幕后,我想还是可以回去查看输出。)

I'd like to compose two (or more) streams into one. My goal is that any output directed to cout, cerr, and clog also be outputted into a file, along with the original stream. (For when things are logged to the console, for example. After closing, I'd like to still be able to go back and view the output.)

我在想这样做像这样的:

I was thinking of doing something like this:

class stream_compose : public streambuf, private boost::noncopyable
{
public:
    // take two streams, save them in stream_holder,
    // this set their buffers to `this`.
    stream_compose;

    // implement the streambuf interface, routing to both
    // ...

private:
    // saves the streambuf of an ios class,
    // upon destruction restores it, provides
    // accessor to saved stream
    class stream_holder;

    stream_holder mStreamA;
    stream_holder mStreamB;
};

这似乎直截了当不够。在主则呼叫会是这样的:

Which seems straight-forward enough. The call in main then would be something like:

// anything that goes to cout goes to both cout and the file
stream_compose coutToFile(std::cout, theFile);
// and so on

我也看了的boost :: iostream的,但没有看到任何有关。

I also looked at boost::iostreams, but didn't see anything related.

还有没有其他更好/更简单的方法来做到这一点?

Are there any other better/simpler ways to accomplish this?

推荐答案

您何况有没有发现什么了Boost.Iostreams。你有没有考虑 tee_device

You mention having not found anything in Boost.IOStreams. Did you consider tee_device?

这篇关于我怎样才能谱写输出流,所以输出为多个分身术?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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