Boost logo

Boost Users :

Subject: [Boost-users] text_file_backend with append and rotate
From: Victor Yankee (victor.whiskey.yankee_at_[hidden])
Date: 2012-12-06 18:44:07


Hello everyone!

How to create a text_file_backend that appends to existing log file and
also has rotation. The init function below always creates a new file every
time I run my program no matter the rotation settings.

I am using svn version 774 (latest as of Dec 5, 2012. I tried many ways but
no joy. There is not an example like this, and there does not seem to be a
test for appending either, so maybe it is not supported, but that would be
bad. So I must be doing something wrong.

Here is my function to set up the logger (using my custom severity levels):

void init()
   {
   typedef boost::log::sinks::synchronous_sink<
boost::log::sinks::text_file_backend > FileSink_t;

   boost::shared_ptr< FileSink_t> mySink(
            new FileSink_t(
                  boost::log::keywords::file_name = "%Y%m%d_%H%M%S_%5N.log"
                , boost::log::keywords::rotation_size = 16 * 1024 * 1024 /3
                , boost::log::keywords::time_based_rotation =
boost::log::sinks::file::rotation_at_time_point(0,0,0)
                , boost::log::keywords::auto_flush = true
                , boost::log::keywords::open_mode=(std::ios::out |
std::ios::app) // seems to have no effect
                ));

    // this method of setting append does not seem to work either, so I
must be doing something wrong
    mySink->locked_backend()->set_open_mode(std::ios::out|std::ios::app);

mySink->locked_backend()->set_file_collector(boost::log::sinks::file::make_collector(

              boost::log::keywords::target = "logs"
            , boost::log::keywords::max_size = 16 * 1024 * 1024
            , boost::log::keywords::min_free_space = 2 * 16 * 1024 * 1024
));

    mySink->set_filter(boost::log::filters::attr< LOGGING::LOG_LEVEL
>("Severity", std::nothrow) >= LOGGING::warning);

    mySink->locked_backend()->scan_for_files(); mySink->set_formatter(
          boost::log::formatters::format("[%1% | %2%] %3%")
        % boost::log::formatters::date_time< boost::posix_time::ptime
>("TimeStamp")
        % boost::log::formatters::attr< LOGGING::LOG_LEVEL >("Severity",
std::nothrow)
        % boost::log::formatters::message() );

    boost::log::core::get()->add_sink(mySink);
    boost::log::core::get()->add_global_attribute("TimeStamp",
boost::log::attributes::local_clock());
    }



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