gpt4 book ai didi

c++ - 没有创建日志文件

转载 作者:太空宇宙 更新时间:2023-11-04 15:31:10 24 4
gpt4 key购买 nike

我正在使用以下链接中的示例。

https://www.boost.org/doc/libs/1_57_0/libs/log/doc/html/log/detailed/utilities.html#log.detailed.utilities.setup.settings_file

我的代码如下。

#include "stdafx.h"
#include <iostream>
#include <fstream>

#include <boost/log/core.hpp>
#include <boost/log/trivial.hpp>
#include <boost/log/expressions.hpp>
#include <boost/log/utility/setup/file.hpp>
#include <boost/log/utility/setup/common_attributes.hpp>
#include <boost/log/utility/setup/from_stream.hpp>
#include <boost/log/utility/setup/settings.hpp>
#include <boost/log/utility/setup/from_settings.hpp>
#include <boost/regex.hpp>

namespace logging = boost::log;
namespace keywords = boost::log::keywords;
#define BOOST_LOG_DYN_LINK 1


int main(int, char*[])
{
//init_logging();
std::ifstream file("settings.ini");
logging::init_from_stream(file);

BOOST_LOG_TRIVIAL(trace) << "This is a trace severity message";
BOOST_LOG_TRIVIAL(debug) << "This is a debug severity message";
BOOST_LOG_TRIVIAL(info) << "This is an informational severity message";
BOOST_LOG_TRIVIAL(warning) << "This is a warning severity message";
BOOST_LOG_TRIVIAL(error) << "This is an error severity message";
BOOST_LOG_TRIVIAL(fatal) << "and this is a fatal severity message";

return 0;
}

我的 settings.ini 文件如下所示。

# Logging core settings section. May be omitted if no parameters specified within it.
[Core]
DisableLogging=false
Filter="%Severity% > 3"

# Sink settings sections
[Sinks.MySink1]

# Sink destination type
Destination=TextFile
FileName="MyApp.log"
RotationSize=1024

# Formatter string. Optional, by default only log record message text is written.
Format="<%TimeStamp%> - %Message%"

# The flag shows whether the sink should be asynchronous
Asynchronous=false

但是没有创建文件。我做错了什么?

最佳答案

在添加过滤器之前一切正常:

 Filter="%Severity% > 3"

谷歌搜索了一下(通过 Boost.Log Configuration Files 例如)我发现:

How to configure severity_logger using init_from_settings() to get the custom severity levels in the log?

您必须将任何用户定义的属性值类型注册到 库,以便能够将其与过滤器和格式解析器一起使用。 这包括您的严重级别枚举。

像这样添加:

logging::register_simple_filter_factory<logging::trivial::severity_level>("Severity");

现在,修复过滤器表达式以使用文本表示:

Filter="%Severity% > debug"

同样,您 need to add the TimeStamp attribute to core :

logging::core::get()->add_global_attribute("TimeStamp", logging::attributes::local_clock());

现在一切正常:

#include <fstream>
#include <iostream>

#include <boost/log/attributes.hpp>
#include <boost/log/core.hpp>
#include <boost/log/expressions.hpp>
#include <boost/log/trivial.hpp>
#include <boost/log/utility/setup/common_attributes.hpp>
#include <boost/log/utility/setup/file.hpp>
#include <boost/log/utility/setup/from_settings.hpp>
#include <boost/log/utility/setup/from_stream.hpp>
#include <boost/log/utility/setup/settings.hpp>
#include <boost/regex.hpp>

namespace logging = boost::log;
namespace keywords = boost::log::keywords;
#define BOOST_LOG_DYN_LINK 1

int main(int, char *[]) {
std::ifstream file("settings.ini");
logging::core::get()->add_global_attribute("TimeStamp", logging::attributes::local_clock());
logging::register_simple_filter_factory<logging::trivial::severity_level>("Severity");

logging::init_from_stream(file);

BOOST_LOG_TRIVIAL(trace) << "This is a trace severity message";
BOOST_LOG_TRIVIAL(debug) << "This is a debug severity message";
BOOST_LOG_TRIVIAL(info) << "This is an informational severity message";
BOOST_LOG_TRIVIAL(warning) << "This is a warning severity message";
BOOST_LOG_TRIVIAL(error) << "This is an error severity message";
BOOST_LOG_TRIVIAL(fatal) << "and this is a fatal severity message";
}

写入 MyApp.log:

<2018-09-01 00:36:11.972436> - This is an informational severity message
<2018-09-01 00:36:11.972712> - This is a warning severity message
<2018-09-01 00:36:11.972735> - This is an error severity message
<2018-09-01 00:36:11.972747> - and this is a fatal severity message

添加了 Demo Live On Coliru

关于c++ - 没有创建日志文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52122996/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com