gpt4 book ai didi

c++ - boost 日志记录严重性级别过滤器未兑现

转载 作者:行者123 更新时间:2023-11-30 05:39:28 30 4
gpt4 key购买 nike

这让我抓狂。我正在明确设置严重性级别,但 boost.log 不遵守它。记录具有所有严重级别的所有消息。

这是我使用的代码:

is_verbose 标志显然是从命令行获得的。并通过以下方式记录了一条消息:

BOOST_LOG_TRIVIAL(debug) << "Hello there!" ;

将被记录并显示在终端上。

谢谢。

#include "boost/log/core.hpp"
#include "boost/log/expressions.hpp"
#include "boost/log/utility/setup/file.hpp"
#include "boost/log/trivial.hpp"
#include "boost/log/attributes.hpp"
#include "boost/log/utility/setup/common_attributes.hpp"

#include "Logger.h"

namespace logging = boost::log;

void Logger::init(bool is_verbose) {

if(is_verbose)
logging::core::get()->set_filter(
logging::trivial::severity >= logging::trivial::info);
else
logging::core::get()->set_filter(
logging::trivial::severity >= logging::trivial::error);

namespace keywords = boost::log::keywords;
namespace sinks = boost::log::sinks;

logging::add_file_log(
keywords::auto_flush = true,
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%][%Severity%]: %Message%"
);

logging::add_common_attributes();

}

最佳答案

这行得通

Live On Coliru

#define BOOST_ALL_DYN_LINK

#include <boost/log/core.hpp>
#include <boost/log/sources/logger.hpp>
#include <boost/log/expressions.hpp>
#include <boost/log/utility/setup/file.hpp>
#include <boost/log/trivial.hpp>
#include <boost/log/attributes.hpp>
#include <boost/log/utility/setup/common_attributes.hpp>

namespace logging = boost::log;

struct Logger {
void init(bool is_verbose) {
namespace keywords = boost::log::keywords;
namespace sinks = boost::log::sinks;
namespace trivial = boost::log::trivial;

if(!is_verbose)
logging::core::get()->set_filter(trivial::severity >= trivial::warning);

logging::add_file_log(
keywords::auto_flush = true,
keywords::file_name = is_verbose? "verbose_%N.log":"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%][%Severity%]: %Message%"
);

logging::add_common_attributes();
}
};

int main(int argc, char**) {
Logger logger;
logger.init(argc>1);

{
using sl = boost::log::trivial::severity_level;
logging::sources::severity_logger<sl> lg;

BOOST_LOG_SEV(lg, sl::trace) << "trace";
BOOST_LOG_SEV(lg, sl::fatal) << "fatal";
}

BOOST_LOG_TRIVIAL(trace) << "trivial trace";
BOOST_LOG_TRIVIAL(fatal) << "trivial fatal";
}

测试:

g++ -std=c++11 -Os -Wall -pedantic -pthread main.cpp \
-lboost_thread -lboost_system -lboost_log -lboost_log_setup
./a.out
./a.out -v
tail *.log

打印

==> sample_0.log <==
[2015-Sep-02 09:49:22.172371][]: fatal
[2015-Sep-02 09:49:22.174915][]: trivial fatal

==> verbose_0.log <==
[2015-Sep-02 09:49:22.226536][]: trace
[2015-Sep-02 09:49:22.227292][]: fatal
[2015-Sep-02 09:49:22.227380][]: trivial trace
[2015-Sep-02 09:49:22.227408][]: trivial fatal

关于c++ - boost 日志记录严重性级别过滤器未兑现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32346376/

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