c++ - boost 日志旋转文件 - 超出大小

标签 c++ logging visual-c++ boost

我尝试使用 Boost 库 1.61 进行记录,一旦大小超过 5Ko 就旋转文件。但是,每次程序运行时,它都会从第一个日志文件开始,超过最大大小!

也就是说,它将文本附加到 flog000.txt,然后附加到 flog001.txt。

但所需的行为是:追加到最后一个文件 (flog002.txt),然后继续 flog003.txt...

这是我的代码:

#include <boost/log/common.hpp>
#include <boost/log/sinks/text_ostream_backend.hpp>
#include <boost/log/core.hpp>
#include <boost/log/utility/setup/file.hpp>
#include <boost/log/sources/logger.hpp>
#include <boost/log/utility/setup/common_attributes.hpp>

void init()
{
   boost::log::add_file_log
      (
      boost::log::keywords::file_name = "flog%3N.txt",
      boost::log::keywords::open_mode = std::ios_base::app,
      boost::log::keywords::rotation_size = 5 * 1024,
      boost::log::keywords::auto_flush = true
      );
}

int main()
{
   init();
   boost::log::sources::logger lg;
   boost::log::add_common_attributes();

   for (int i(0); i < 1000; ++i)
   {
      BOOST_LOG(lg) << "Hello : " << i << std::endl;
   }
    return 0;
}

最佳答案

“没有文件收集”- 这至少是问题的一部分。

似乎不可能将扫描附加到现有的。

但是,如果您进行扫描,您至少可以防止额外的日志消息被错误地附加到旧日志文件:

Live On Coliru

#include <boost/log/common.hpp>
#include <boost/log/core.hpp>
#include <boost/log/sinks/text_ostream_backend.hpp>
#include <boost/log/sources/logger.hpp>
#include <boost/log/utility/setup/common_attributes.hpp>
#include <boost/log/utility/setup/file.hpp>

void init() {
    auto s = boost::log::add_file_log(
            boost::log::keywords::file_name = "flog%3N.txt",
            boost::log::keywords::open_mode = std::ios_base::app,
            boost::log::keywords::rotation_size = 5 * 1024,
            boost::log::keywords::auto_flush = true
        );

    auto be = s->locked_backend();
    be->set_file_collector(boost::log::sinks::file::make_collector(
                boost::log::keywords::target = "./", // log file & target have same dir
                boost::log::keywords::max_size = 50 * 1024,
                boost::log::keywords::min_free_space = 100000
                ));

    be->scan_for_files(boost::log::sinks::file::scan_method::scan_matching, true);
}

int main() {
    init();
    boost::log::sources::logger lg;
    boost::log::add_common_attributes();

    for (int i(0); i < 1000; ++i) {
        BOOST_LOG(lg) << "Hello : " << i << std::endl;
    }
}

一次运行后:

-rw-rw-r-- 1 sehe sehe 5116 jun  6 00:27 flog000.txt
-rw-rw-r-- 1 sehe sehe 5109 jun  6 00:27 flog001.txt
-rw-rw-r-- 1 sehe sehe 2665 jun  6 00:27 flog002.txt

两次运行后:

-rw-rw-r-- 1 sehe sehe 5116 jun  6 00:27 flog003.txt
-rw-rw-r-- 1 sehe sehe 5109 jun  6 00:27 flog004.txt
-rw-rw-r-- 1 sehe sehe 2665 jun  6 00:27 flog005.txt

三轮后:

-rw-rw-r-- 1 sehe sehe 5116 jun  6 00:28 flog006.txt
-rw-rw-r-- 1 sehe sehe 5109 jun  6 00:28 flog007.txt
-rw-rw-r-- 1 sehe sehe 2665 jun  6 00:28 flog008.txt

关于c++ - boost 日志旋转文件 - 超出大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50700741/

相关文章:

c++ - 如何检测按键是否由 keybd_event 或 SendInput 模拟?

C++:带大括号初始化列表的函数调用表达式 - 标准是否规定在单个元素列表的微不足道的情况下忽略大括号?

logging - 如何记录 cron 作业?

java - log4j 与 System.out.println - 记录器的优势?

c++ - 为什么更喜欢基于模板的静态断言而不是基于 typedef 的静态断言?

c++ - 视觉 2015 C++ 编译器 : Call of static member with object does not lead to compiler error

c++ - CSV数据导入Form控件

python - 将打印重定向到日志文件

c++ - Vc++(Wince平台)如何打开读取16位.raw文件

c++ - float 异常、段错误