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

标签 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 安装 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 的系统上使用 tutorial 中的代码遵循 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 不太熟悉,但似乎 documentation for the target_file_name parameter意味着在这种情况下这不是必需的,因此上述内容可能被视为一种解决方法。 target_file_name 参数在 Boost 1.67.0 及更低版本中也不存在,因此此解决方法也会导致编译问题。

因此,这看起来要么是回归,要么是记录不详的重大变更。如果更熟悉该库的人可以进一步阐明这个问题,那就会很有趣。

编辑:我已经向上游提出了一个问题:https://github.com/boostorg/log/issues/104

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

关于c++ - boost日志文件无法创建sample.log文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58884712/

相关文章:

c++ - new int[] 返回的是数组第一个元素的地址还是整个数组的地址?

c++ - 枚举类的关系运算符重载

c++ - LNK2019错误未解析的外部符号

Windows脚本FOR命令重命名目录中的所有文件

python - 在 Windows 上运行带有子进程的 R 脚本,子进程似乎丢失

php - 对于在 APACHE 上运行的 PHP,如何保护 LAN 中的 Windows 机器?

c# - 如何调试 MSBuild 依赖问题?

c++ - 如何将对象从c导出到node addon api

c# - 从 VS2008 测试项目创建独立的可执行文件

visual-studio - Windows Azure Visual Studio 解决方案