Boost logo

Boost Users :

Subject: [Boost-users] [iostreams] why doesn't file_sink flush?
From: Andrew Schweitzer (a.schweitzer.grps_at_[hidden])
Date: 2009-05-04 13:56:55


I'm trying to chain debugging logs together with a filtering_ostream.
I've discovered that I need to call flush() on the filtering_ostream to
get characters to appear in console in a timely fashion. This flush()
call doesn't causes the file to be flushed. After some digging, I've
discovered that the boost::iostreams::file_sink doesn't have the
flushable_tag (or implement flush). std::fstream does have a flush
function, and if I use it directly (not chained into a
filtering_ostream), the flush call works as you might expect.

So what is the appropriate way of implementing this? Chaining multiple
logs together seems like a great way to use iostreams library. And out
of curiosity, why doesn't file_sink implement flushable_tag?

Here is the code I was using:

#include <boost/iostreams/stream.hpp>
#include <boost/iostreams/device/file.hpp>
#include <boost/iostreams/stream.hpp>
#include <boost/iostreams/tee.hpp>
#include <boost/iostreams/filtering_stream.hpp>
#include <boost/iostreams/categories.hpp>
#include <boost/iostreams/concepts.hpp>
#include <boost/thread.hpp>
#include <boost/thread/xtime.hpp>
#include <iostream>

int main(int argc, char** argv)
{
        using namespace std;
        using namespace boost;
        using namespace boost::iostreams;

        filtering_ostream fo;

        file_sink fsink("out.txt");
        tee_filter<file_sink> tee_fsink(fsink);
        fo.push(tee_fsink);
        
        fo.push(cout);

        int i = 0;
        while(true)
        {
                fo << i << " ";
                //without this, I don't see "one print / second"
                //with it, I only see them on cout, but not in out.txt.
                fo.flush();
                
                i++;

                boost::xtime xt;
                boost::xtime_get(&xt, boost::TIME_UTC);
                xt.sec += 1;
                boost::thread::sleep(xt);
        }
        
        return 0;
}

tx

Andy


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net