簡體   English   中英

如何在Boost :: Log中使用壓縮器Boost :: Iostreams過濾器作為接收器

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

我正在嘗試使用boost::iostreams::gzip_compressor來即時壓縮使用Boost Log庫創建的日志文件。 因此,當我調用BOOST_LOG() ,輸出會即時壓縮。 這是我到目前為止所嘗試的:

#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;
}

我覺得我應該以某種方式1)將整個filtering_ostream作為接收器,而不僅僅是file

2)以某種方式推記錄器宿而不是filefiltering_ostream

有人能指出我正確的方向嗎? 謝謝!

我想你想要將filtering_ostream作為記錄器流傳遞。 你需要做兩件事:

  1. 創建一個包含您的filtering_ostreamshared_ptr ,和
  2. 確保延長輸出文件流的生命周期,直到關閉過濾流。

您可以使用自定義shared_ptr刪除器來完成此操作。 deleter是一個傳遞給shared_ptr構造函數的可選函數對象,將被調用以釋放指針。 您可以將shared_ptr存儲到刪除file中的file ,以確保文件存在,只要該流存在。 在C ++ 11中,你可以使用這樣的lambda:

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);

lambda的[file]部分將一個shared_ptr保存到ofstream直到調用了deleter。

如果你沒有C ++ 11編譯器,你可以用普通的仿函數做同樣的事情,比如(未編譯和未經測試):

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));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM