【发布时间】:2018-06-14 16:08:39
【问题描述】:
我正在使用 Boost 1.61,我最终想要的是:
- 如果当前日志文件的大小达到
FILE_ROTATION_SIZE,则旋转当前日志文件 - 使用自定义收集器处理旋转文件以进一步压缩(这是压缩旋转文件的正确方法吗?)
- 如果文件的总大小达到
FILES_MAX_SIZE,则删除一些最旧的(希望压缩的)文件
现在,无需任何收集器,我只需使用以下 sn-p 即可达到第 1 点和第 3 点
sink = logging::add_file_log(
keywords::file_name = fileName,
keywords::rotation_size = FILE_ROTATION_SIZE,
keywords::scan_method = sinks::file::scan_method::scan_matching,
keywords::target = logDir.native(),
keywords::max_size = FILES_MAX_SIZE,
keywords::format =
(
expr::stream
<< expr::format_date_time< boost::posix_time::ptime >("TimeStamp", "%Y-%m-%d %H:%M:%S.%f")
<< " " << expr::attr< boost::log::attributes::current_thread_id::value_type >("ThreadID") << " "
<< "<" << expr::attr< Logger::SeverityLevel >("Severity") << "> "
<< expr::message
<< expr::attr< std::wstring >("Suffix")
),
keywords::auto_flush = true
);
但是,当设置一个带有sink->locked_backend()->set_file_collector(_fileCollector); 继承自的收集器时
boost::log::sinks::file::collector 现在基本上什么都不做文件仍然继续旋转,但旧文件不会被删除。
收集器的外观如下:
class FileCollector : public boost::log::sinks::file::collector
{
virtual void store_file(boost::filesystem::path const& src_path) override;
virtual uintmax_t scan_for_files(boost::log::sinks::file::scan_method method,
boost::filesystem::path const& pattern = boost::filesystem::path(),
unsigned int* counter = 0) override;
};
void FileCollector::store_file(boost::filesystem::path const& src_path)
{
LOG << "src_path: " << src_path;
}
uintmax_t FileCollector::scan_for_files(boost::log::sinks::file::scan_method method,
boost::filesystem::path const& pattern,
unsigned int* counter)
{
return 1;
}
scan_for_files() 根本没有被调用。
从the answer to this question,我可以看到作者说max_size 是一个收集器参数,所以我认为应该有某种方式在我的FileCollector 类中设置它。调用sinks::file::make_collector() 而不是继承自定义收集器似乎不是一种选择,因为它无法提供所需的store_file() 回调,我打算在其中放置压缩逻辑。
这是否意味着我应该继续跟踪总大小并在需要时自行处理删除?
谢谢!
【问题讨论】:
标签: c++ logging boost boost-log