Boost Log set max_size after initialzation

2019-08-21 07:31发布

I'm trying to set the boost log max_size parameter in post initialization stage. until now I was able to set it in the initialization stage like this:

    logging::add_file_log(      
    keywords::auto_flush = true,
    keywords::target =BOOST_LOG_FOLDER,
    keywords::file_name =BOOST_LOG_FILE,
    keywords::time_based_rotation = sinks::file::rotation_at_time_point(0,0,0),
    keywords::rotation_size = 30 * 1024 * 1024,
    keywords::max_size = 60 * 1024 * 1024,
    );

Now I want to change the max_size after this call (according to value from input).

I don't see how this can be done

1条回答
女痞
2楼-- · 2019-08-21 07:51

The max_size parameter specifies the maximum total size of the rotated files in the target directory. It is a collector parameter, so in order to change it you'll have to create and set a new collector to the file sink returned by add_file_log.

typedef sinks::synchronous_sink< sinks::text_file_backend > sink_t;
boost::shared_ptr< sink_t > sink = logging::add_file_log(      
    keywords::auto_flush = true,
    keywords::target =BOOST_LOG_FOLDER,
    keywords::file_name =BOOST_LOG_FILE,
    keywords::time_based_rotation = sinks::file::rotation_at_time_point(0,0,0),
    keywords::rotation_size = 30 * 1024 * 1024,
    keywords::max_size = 60 * 1024 * 1024,
);

sink->locked_backend()->set_file_collector(
    sinks::file::make_collector(
        keywords::target =BOOST_LOG_FOLDER,
        keywords::max_size = 30 * 1024 * 1024
    )
);

Note, however, that the library can only decrease the limit in this way. This is because there is only one collector instance per each target directory so that the limits for the directory are universally maintained throughout the application, even if multiple sinks rotate files into the same directory. make_collector will verify the current limits set for a given target directory and set the most restrictive ones, which for max_size means picking the least allowed value.

查看更多
登录 后发表回答