boost日志文件无法创建sample.log文件



我已经在boost日志上挣扎了一段时间了——我得到了他们写日志文件的简单示例(http://boost-log.sourceforge.net/libs/log/example/doc/tutorial_file.cpp)。问题是当我使用这部分代码时…

void init()
{
logging::add_file_log("sample.log");
logging::core::get()->set_filter
(
logging::trivial::severity >= logging::trivial::info
);
}

将创建00000.log文件,而不是sample.log文件。当我使用这部分代码时…

void init()
{
logging::add_file_log
(
keywords::file_name = "sample_%N.log",
keywords::rotation_size = 10 * 1024 * 1024,
keywords::time_based_rotation = sinks::file::rotation_at_time_point(0, 0, 0),
keywords::format = "[%TimeStamp%]: %Message%"
);
logging::core::get()->set_filter
(
logging::trivial::severity >= logging::trivial::info
);
}

我收到以下错误消息:在Boost_log_sample.exe中的0x00007FF787AD0393处引发异常:0xC0000005:读取位置0x00003BBA20597184时发生访问冲突。

我使用的是win10 64位,使用的是VS2015,boost_1_71_0。使用NuGet安装boost(https://www.scalyr.com/blog/getting-started-quickly-c-logging/)

我对boost_1_66_0没有这个问题。

这是我的代码:

#include <boost/log/core.hpp>
#include <boost/log/trivial.hpp>
#include <boost/log/expressions.hpp>
#include <boost/log/sinks/text_file_backend.hpp>
#include <boost/log/utility/setup/file.hpp>
#include <boost/log/utility/setup/common_attributes.hpp>
#include <boost/log/sources/severity_logger.hpp>
#include <boost/log/sources/record_ostream.hpp>
namespace logging = boost::log;
namespace src = boost::log::sources;
namespace sinks = boost::log::sinks;
namespace keywords = boost::log::keywords;
void init()
{
logging::add_file_log
(
keywords::file_name = "sample_%N.log",                                        /*< file name pattern >*/
keywords::rotation_size = 10 * 1024 * 1024,                                   /*< rotate files every 10 MiB... >*/
keywords::time_based_rotation = sinks::file::rotation_at_time_point(0, 0, 0), /*< ...or at midnight >*/
keywords::format = "[%TimeStamp%]: %Message%"                                 /*< log record format >*/
);
logging::core::get()->set_filter
(
logging::trivial::severity >= logging::trivial::info
);
}

int main(int, char*[])
{
init();
logging::add_common_attributes();
using namespace logging::trivial;
src::severity_logger< severity_level > lg;
BOOST_LOG_SEV(lg, trace) << "A trace severity message";
BOOST_LOG_SEV(lg, debug) << "A debug severity message";
BOOST_LOG_SEV(lg, info) << "An informational severity message";
BOOST_LOG_SEV(lg, warning) << "A warning severity message";
BOOST_LOG_SEV(lg, error) << "An error severity message";
BOOST_LOG_SEV(lg, fatal) << "A fatal severity message";
return 0;
}

在安装了Boost 1.72.0的系统上执行Boost.log教程时,我也偶然发现了日志被称为00000.log的问题,使用教程中的代码,即:

logging::add_file_log("sample.log");

此外,正如问题中所解释的,我无法在使用旧版本Boost(准确地说是1.67.0(的系统上重现这个问题。日志在那里被命名为sample.log

经过一些实验,我发现在1.72.0的系统上,通过使用target_file_name参数,日志可以根据需要写入sample.log

// DO NOT USE IN PRODUCTION, SEE BELOW
logging::add_file_log
(
keywords::file_name = "sample.log",
keywords::target_file_name = "sample.log"
);

虽然我对Boost.Log不太熟悉,但target_file_name参数的文档似乎暗示在这种情况下不需要这样做,因此上述内容可能被视为一种变通方法。target_file_name参数在Boost 1.67.0及以下版本中也不存在,因此此解决方法也会导致编译问题。

因此,这看起来要么是一种回归,要么是一个记录不足的突破性变化。如果有更熟悉图书馆的人能进一步了解这个问题,那将是一件有趣的事情。

编辑:我在上游打开了一个问题:https://github.com/boostorg/log/issues/104

(不幸的是,我无法重现问题后半部分的崩溃问题(

相关内容

最新更新