Search code examples
c++boostiostreamboost-iostreamsboost-log

How to use a compressor Boost::Iostreams filter as a sink in Boost::Log


I'm trying to compress log files created using the Boost Log library instantaneously by utilizing boost::iostreams::gzip_compressor. So when I call BOOST_LOG(), output gets compressed on-the-fly. Here's what I tried so far:

#include <fstream>
#include <iostream>

#include <boost/iostreams/filtering_stream.hpp>
#include <boost/iostreams/filtering_streambuf.hpp>
#include <boost/iostreams/stream.hpp>
#include <boost/iostreams/filter/gzip.hpp>

#include <boost/smart_ptr/shared_ptr.hpp>
#include <boost/smart_ptr/make_shared_object.hpp>

#include <boost/log/core.hpp>
#include <boost/log/trivial.hpp>
#include <boost/log/sinks/sync_frontend.hpp>
#include <boost/log/sinks/text_ostream_backend.hpp>
#include <boost/log/sources/logger.hpp>

void init()
{
    // Construct the sink
    typedef boost::log::sinks::synchronous_sink< boost::log::sinks::text_ostream_backend > text_sink;
    boost::shared_ptr< text_sink > sink = boost::make_shared< text_sink >();

    boost::shared_ptr< std::ofstream > file = boost::make_shared< std::ofstream >( 
            "sample.gz", std::ios_base::out | std::ios_base::binary );
    boost::iostreams::filtering_ostream out;
    out.push( boost::iostreams::gzip_compressor() );
    out.push( *(file.get()) );

    for( int i = 0; i < 10; i++ ) {
        out << "Hello world! " << i << std::endl; // compresses OK
    }

    sink->locked_backend()->add_stream( file );

    // Register the sink in the logging core
    boost::log::core::get()->add_sink( sink );
}

int main( )
{
    init();

    boost::log::sources::logger lg;
    for( int i = 0; i < 10; i++ ) 
        BOOST_LOG(lg) << "Bye world!" << std::endl; // Does not compress

    return 0;
}

I get a feeling I should somehow either 1) put the whole filtering_ostream as a sink, not just file or

2) somehow push the logger sink instead of file in the filtering_ostream.

Can someone point me to the right direction? Thanks!


Solution

  • I think you want to pass the filtering_ostream as your logger stream. You 'll need to do two things:

    1. Make a shared_ptr containing your filtering_ostream, and
    2. Make sure the lifetime of the output file stream is extended until the filtering stream is closed.

    You can accomplish this by using a custom shared_ptr deleter. The deleter is an optional function object passed to the shared_ptr constructor that will be called to free the pointer. You can store a shared_ptr to file in the deleter to make sure the file exists as long as the stream does. In C++11, you can do it with a lambda like this:

    boost::shared_ptr<boost::iostreams::filtering_ostream> out(
       new boost::iostreams::filtering_ostream,
       [file](std::ostream *os) { delete os; });
    
    out->push(boost::iostreams::gzip_compressor());
    out->push(*file);
    
    sink->locked_backend()->add_stream(out);
    

    The [file] part of the lambda keeps a shared_ptr to the ofstream until the deleter is called.

    If you don't have a C++11 compiler, you can do the same thing with an ordinary functor, something like (uncompiled and untested):

    struct MyDeleter {
       boost::shared_ptr<std::ofstream> file_;
    
       MyDeleter(const boost::shared_ptr<std::ofstream>& file)
          : file_(file) {
       }
    
       void operator()(std::ostream *os) {
          delete os;
       }
    };
    
    ...
    
    boost::shared_ptr<boost::iostreams::filtering_ostream> out(
       new boost::iostreams::filtering_ostream,
       MyDeleter(file));