How can I compose output streams, so output goes multiple places at once?(如何组合输出流,以便输出一次到达多个位置?)
问题描述
我想将两个(或更多)流合二为一.我的目标是将指向 cout
、cerr
和 clog
的任何输出与原始流一起输出到文件中.(例如,当事情被记录到控制台时.关闭后,我希望仍然能够返回并查看输出.)
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;
};
这似乎很直接.然后 main 中的调用将类似于:
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::iostreams
,但没有看到任何相关内容.
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?
这篇关于如何组合输出流,以便输出一次到达多个位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何组合输出流,以便输出一次到达多个位置?


基础教程推荐
- 使用从字符串中提取的参数调用函数 2022-01-01
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- 为什么语句不能出现在命名空间范围内? 2021-01-01
- 从 std::cin 读取密码 2021-01-01
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01
- Windows Media Foundation 录制音频 2021-01-01