gpt4 book ai didi

C++ 如何在 Boost Global Logger 上设置严重性过滤器

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:29:04 31 4
gpt4 key购买 nike

几天来我一直在尝试创建一个 Boost Global Logger 以在整个应用程序中使用但我似乎无法在 Global Logger 中设置严重性级别

重要提示:

在下面查看 Andrey 的回答...它被标记为步骤 (a) 和 (b),但我仍然没有做对!


直接来自 Boost 文档 here

...it would be more convenient to have one or several global loggers in order to easily access them in every place when needed. In this regard std::cout is a good example of such a logger.

The library provides a way to declare global loggers that can be accessed pretty much like std::cout. In fact, this feature can be used with any logger, including user-defined ones. Having declared a global logger, one can be sure to have a thread-safe access to this logger instance from any place of the application code. The library also guarantees that a global logger instance will be unique even across module boundaries. This allows employing logging even in header-only components that may get compiled into different modules.


Regardless of the macro you used to declare the logger, you can acquire the logger instance with the static get function of the logger tag:

src::severity_logger_mt< >& lg = my_logger::get();

我从 Boost Logger 大师 Andrey 那里了解到,我的问题是严重性类型不匹配

You have instantiated severity_logger_mt with default template parameters, so the severity level attribute has type int. Your enum values are converted to int and sent to the logging core. You have not set up any sinks, so by default the default sink is used. The sink attempts to extract severity level attribute value from log records, but fails to do that because it expects the severity level to be of type boost::log::trivial::severity_level. After that failure the sink falls back to boost::log::trivial::severity_level::info severity.

If you want to use your enum for severity levels you have to:

------------------------------------(现在,答案在这里! !!!) ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------->

(a) specify it in the logger template parameters and

(b) set up a sink with a formatter that is aware of your enum.

但我不知道该怎么做,因为即使在尝试按照他的指示进行操作之后,严重性级别看起来像接收器仍在回落到 boost::log::trivial::severity_level::info 严重性.谁能帮我弄清楚如何在我的全局记录器中正确设置严重性?这是代码:

标题

#include <boost/log/trivial.hpp>
#include <boost/log/sources/global_logger_storage.hpp>

enum severity_level
{
normal,
warning,
error,
critical
};

BOOST_LOG_GLOBAL_LOGGER(logger, boost::log::sources::severity_logger_mt< severity_level >)

CPP

#include "GlobalLogger.h"

#include <boost/log/expressions/formatters/date_time.hpp>
#include <boost/log/expressions.hpp>
#include <boost/log/sinks/sync_frontend.hpp>
#include <boost/log/sinks/text_ostream_backend.hpp>
#include <boost/log/support/date_time.hpp>
#include <boost/core/null_deleter.hpp>
#include <boost/log/utility/setup/common_attributes.hpp>
#include <boost/make_shared.hpp>
#include <boost/log/utility/setup/file.hpp>
#include <boost/log/utility/setup/console.hpp>
#include <boost/log/sinks.hpp>
#include <fstream>

namespace logging = boost::log;
namespace src = boost::log::sources;
namespace expr = boost::log::expressions;
namespace sinks = boost::log::sinks;
namespace attrs = boost::log::attributes;

bool onlyWarnings(const boost::log::attribute_value_set& set)
{
return set["Severity"].extract<severity_level>() > 0;
}

void severity_and_message(const boost::log::record_view &view, boost::log::formatting_ostream &os)
{
os << view.attribute_values()["Severity"].extract<severity_level>() << ": " <<
view.attribute_values()["Message"].extract<std::string>();
}

BOOST_LOG_GLOBAL_LOGGER_INIT(logger, boost::log::sources::severity_logger_mt< severity_level >)
{
boost::log::sources::severity_logger_mt< severity_level > logger;

// add a text sink
typedef sinks::asynchronous_sink<sinks::text_ostream_backend> text_sink;
boost::shared_ptr<text_sink> sink = boost::make_shared<text_sink>();

// add "console" output stream to our sink
boost::shared_ptr<std::ostream> stream{&std::clog, boost::null_deleter{}};
sink->locked_backend()->add_stream(stream);

// specify the format of the log message
sink->set_formatter(&severity_and_message);

// just log messages with severity >= SEVERITY_THRESHOLD are written
sink->set_filter(&onlyWarnings);

// "register" our sink
logging::core::get()->add_sink(sink);

logging::add_common_attributes();

return logger;
}

main.cpp

#include <iostream>

#include "GlobalLogger.h"

using namespace std;

int main() {
boost::log::sources::severity_logger_mt< severity_level >& lg = logger::get();
BOOST_LOG_SEV(lg, severity_level::normal) << "note";
BOOST_LOG_SEV(lg, severity_level::warning) << "warning";
BOOST_LOG_SEV(lg, severity_level::critical) << "critical";

return 0;
}

我找到了一个更好的例子62.10. A macro to define a global loggerthis SO question 中的工作版本.但是工作示例不使用 get() 方法。因此,在声明 BOOST_LOG_GLOBAL_LOGGER 之后,我能够访问 log::get() 但我仍然无法让它识别严重性

最佳答案

我找到了 Boost Log 专家 Andrey。为了将来帮助其他人,我发布了一个链接到我们的 Sourceforge discussion .在我的显示器上撞了很久之后,他解释并重新访问了 this SO question 中的工作版本.但我终于让它工作了!耶!!!

关于C++ 如何在 Boost Global Logger 上设置严重性过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29785243/

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