IoStreams Question: How do I get "tee" functionality

However, I am NOT (yet) an expert in the Iostreams library so please excuse me if I am missing something obvious. I add 2 sinks filtering_ostream out; out.push(file_sink("Report.txt")); out.push(cout); I get an exception thrown on the second one (regardless of which one goes in first) In searching the archives, I noticed this mail: http://tinyurl.com/6soeb where Johnathon Turkis writes: "For this we need to define an OutputFilter which stores a reference to an ostream and has a member function 'write' which forwards all characters to the stored ostream as well as to the downstream sink (you don't need to understand the following code -- the point is that it's just a few lines): struct tee : boost::io::output_filter { tee(std::ostream& dest) : dest(dest) { } template<typename Sink> void write(Sink& snk, char* s, std::streamsize n) { // Write to the downstream Sink boost::io::write(snk, s, n); // Write to the stored ostream: dest.write(s, n); } std::ostream& dest; }; We use the tee as follows. Given two ostreams ostream first; ostream second; we define a filtering_ostream which passes data through the tee to both streams: filtering_ostream out; out.push(tee(first)); out.push(second); out << "this gets written to both ostreams\n"; " Which looks "good" except I suspect the library has changed since the above is not compiling for me under VC71. (it complains about wanting a Put() instead of a write()).. Any help appreciated- and also - a request- if "tee" functionality could be officially implemented in the streaming lib for output sinks. OR if it already is, I request that how do to it be spelled out more clearly in the documentation. Thanks Brian Braatz

Brian Braatz wrote:
However, I am NOT (yet) an expert in the Iostreams library so please excuse me if I am missing something obvious.
I add 2 sinks
filtering_ostream out; out.push(file_sink("Report.txt")); out.push(cout);
I get an exception thrown on the second one (regardless of which one goes in first)
This is true. A chain can only contain one device.
In searching the archives, I noticed this mail:
<snip description of 'tee' filter> I apparently didn't test that code, because it contained two errors (write takes a const char* instead of a char*, and only multi-character filters implement write() instead of put()). At the end of this message is an updated and tested version. I plan to include a tee filter in the first released version of the library. It should be slightly more efficient than the example. Jonathan -------------- #include <boost/iostreams/concepts.hpp> #include <boost/iostreams/filtering_stream.hpp> #include <fstream> #include <iostream> struct tee : boost::iostreams::multichar_output_filter { tee(std::ostream& dest) : dest(dest) { } template<typename Sink> void write(Sink& snk, const char* s, std::streamsize n) { // Write to the downstream Sink boost::iostreams::write(snk, s, n); // Write to the stored ostream: dest.write(s, n); } std::ostream& dest; }; int main() { using namespace boost::iostreams; std::ofstream log("C:/log.txt"); filtering_ostream out; out.push(tee(log)); out.push(std::cout); out << "this gets written to both ostreams\n"; }
participants (2)
-
Brian Braatz
-
Jonathan Turkanis