提升日志文件无法创建sample.log文件

时间:2019-11-15 21:09:34

标签: c++ windows visual-studio boost-log

我一直在努力处理boost log-我得到了他们简单的示例,将它们写入日志文件(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(https://www.scalyr.com/blog/getting-started-quickly-c-logging/)安装Boost

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;
}

1 个答案:

答案 0 :(得分:3)

在安装了Boost 1.72.0的系统上,使用tutorial中的代码,在遵循Boost.Log教程的同时,我还偶然发现了名为00000.log的日志的问题,即: / p>

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不太熟悉,但看来documentation for the target_file_name parameter暗示在这种情况下这不是必需的,因此以上内容应被视为一种解决方法。 target_file_name参数在Boost 1.67.0及更低版本中也不存在,因此此解决方法也将导致那里的编译问题。

因此,这看起来像是回归分析或记录不充分的重大更改。如果对图书馆更熟悉的人可以进一步阐明这个问题,那将很有趣。

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

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