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?
这篇关于如何组合输出流,以便输出一次到达多个位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何组合输出流,以便输出一次到达多个位置?
基础教程推荐
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- CString 到 char* 2021-01-01
- 初始化列表*参数*评估顺序 2021-01-01
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
