gpt4 book ai didi

c++ - Boost::log:基于级别的不同格式(HTML 格式)

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

我正在研究基础 boost example from here .我正在配置我的应用程序所需的内容,但我被卡住了。这是我现在的位置:

#include <fstream>
#include <iomanip>
#include <boost/date_time/posix_time/posix_time_types.hpp>
#include <boost/log/support/date_time.hpp>
#include <boost/smart_ptr/shared_ptr.hpp>
#include <boost/smart_ptr/make_shared_object.hpp>
#include <boost/log/core.hpp>
#include <boost/log/trivial.hpp>
#include <boost/log/expressions.hpp>
#include <boost/log/sinks/sync_frontend.hpp>
#include <boost/log/sinks/text_ostream_backend.hpp>
#include <boost/log/sources/severity_logger.hpp>
#include <boost/log/sources/record_ostream.hpp>
#include <boost/log/utility/setup/common_attributes.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/date_time/posix_time/posix_time_io.hpp>

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

void init()
{
typedef sinks::synchronous_sink<sinks::text_ostream_backend> text_sink;
boost::log::core::get()->add_global_attribute("TimeStamp", boost::log::attributes::utc_clock());
boost::shared_ptr<text_sink> sink = boost::make_shared<text_sink>();

sink->locked_backend()->add_stream(
boost::make_shared<std::ofstream>("log.html"));

sink->set_formatter
(
expr::stream
<< expr::format_date_time<boost::posix_time::ptime>("TimeStamp", "%Y.%m.%d-%H:%M:%S-UTC")
<< ": <" << logging::trivial::severity << "> " << expr::smessage
);

logging::core::get()->add_sink(sink);
}


int main(int, char*[])
{
std::cout<<"Start"<<std::endl;
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";

std::cout<<"End"<<std::endl;
return 0;
}

我想让格式取决于严重性级别。这是因为我的日志输出是 HTML。例如,我在 main up 中打印的消息应该像这样输出到文件中:

<font color='black'>A trace severity message</font>
<font color='gray'>A debug severity message</font>
<font color='blue'>An informational severity message</font>
<font color='orange'>A warning severity message</font>
<font color='red'>An error severity message</font>
<strong><font color='red'>A fatal severity message</font></strong>

我上面的简单模型是否可行?它需要什么?

if 条件插入到 sink->set_formatter() 中。我也不能在那里使用 C++11 lambda。

最佳答案

有很多方法可以做你想做的事。最直接的方法是使用 conditional formatter :

sink->set_formatter
(
expr::stream
<< expr::if_(logging::trivial::severity <= logging::trivial::severity_level::trace)
[
expr::stream << "<font color='black'>"
]
.else_
[
expr::stream << expr::if_(logging::trivial::severity <= logging::trivial::severity_level::debug)
[
// ...
]
]
<< expr::format_date_time<boost::posix_time::ptime>("TimeStamp", "%Y.%m.%d-%H:%M:%S-UTC")
<< ": <" << logging::trivial::severity << "> " << expr::smessage
);

但是,这种方法可能相当乏味并且性能不理想。

更好的解决方案是将您的函数注入(inject)格式化程序。这是一个例子:

boost::string_view prefix_formatter(
logging::value_ref< logging::trivial::severity_level, logging::trivial::tag::severity > const& level)
{
if (level)
{
switch (level.get())
{
case logging::trivial::severity_level::trace:
return "<font color='black'>";
case logging::trivial::severity_level::debug:
return "<font color='gray'>";
// ...
}
}

return boost::string_view();
}

boost::string_view suffix_formatter(
logging::value_ref< logging::trivial::severity_level, logging::trivial::tag::severity > const& level)
{
if (level)
{
switch (level.get())
{
case logging::trivial::severity_level::trace:
return "</font>";
// ...
}
}

return boost::string_view();
}

sink->set_formatter
(
expr::stream
<< boost::phoenix::bind(&prefix_formatter, logging::trivial::severity.or_none())
<< expr::format_date_time<boost::posix_time::ptime>("TimeStamp", "%Y.%m.%d-%H:%M:%S-UTC")
<< ": <" << logging::trivial::severity << "> " << expr::smessage
<< boost::phoenix::bind(&suffix_formatter, logging::trivial::severity.or_none())
);

这里,如果属性不在日志记录中,or_none 使 value_ref 为空,因此需要在 prefix_formatter 中检查它> 和 suffix_formatter

最后,由于您正在编写 HTML,您可能希望自动将输出中的保留字符转换为转义序列。 XML character decorator可以帮助您。

关于c++ - Boost::log:基于级别的不同格式(HTML 格式),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43285834/

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