C++“hello world”Boost tee示例程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/999120/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++ "hello world" Boost tee example program
提问by CW Holeman II
The Boost C++ library has Function Template tee
Boost C++ 库具有函数模板 tee
The class templates tee_filter and tee_device provide two ways to split an output sequence so that all data is directed simultaneously to two different locations.
类模板 tee_filter 和 tee_device 提供了两种分割输出序列的方法,以便将所有数据同时定向到两个不同的位置。
I am looking for a complete C++ example using Boost tee to output to standard out and to a file like "sample.txt".
我正在寻找一个完整的 C++ 示例,使用 Boost tee 输出到标准输出和像“sample.txt”这样的文件。
回答by Matthew Flaschen
Based on help from the question John linked:
根据约翰链接的问题的帮助:
#include <boost/iostreams/tee.hpp>
#include <boost/iostreams/stream.hpp>
#include <fstream>
#include <iostream>
using std::ostream;
using std::ofstream;
using std::cout;
namespace bio = boost::iostreams;
using bio::tee_device;
using bio::stream;
int main()
{
typedef tee_device<ostream, ofstream> TeeDevice;
typedef stream<TeeDevice> TeeStream;
ofstream ofs("sample.txt");
TeeDevice my_tee(cout, ofs);
TeeStream my_split(my_tee);
my_split << "Hello, World!\n";
my_split.flush();
my_split.close();
}
回答by Martin Ba
Here's an example using tee_filter
I'm currently using to tee my Boost.Test output:
这是一个使用tee_filter
我目前正在使用的示例来测试我的 Boost.Test 输出:
{ // init code, use static streams to keep them alive until test run process end
using namespace boost::iostreams;
static ofstream ofs("boost_test_output.log.xml"); // log file
static tee_filter<ostream> fileFilt(ofs); // tee all passed data to logfile
// note derives from `boost::iostreams::output_filter`
static text_xml_readability_filter xmlFilt; // filter all passed data, making the XML output readable
static filtering_ostream filter; // master filter
filter.push(fileFilt); // 1st, tee off any data to the file (raw boost XML)
filter.push(xmlFilt); // 2nd make the xml data stream readable (linebreaks, etc.)
filter.push(cout); // 3rd output the readable XML to cout
boost::unit_test::unit_test_log.set_stream( filter );
}