- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在研究基础 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/
不同的 LogCat 方法是: Log.v(); // Verbose Log.d(); // Debug Log.i(); // Info Log.w(); // Warning Log.e();
在android群里,经常会有人问我,android log是怎么用的,今天我就把从网上以及sdk里东拼西凑过来,让大家先一睹为快,希望对大家入门android log有一定的帮助. android
关闭。这个问题是opinion-based 。目前不接受答案。 想要改进这个问题吗?更新问题,以便 editing this post 可以用事实和引文来回答它。 . 已关闭 4 年前。 社区 12
我正在尝试使用 sonarlint 检查代码质量.上面的问题概要,我不明白为什么它要说要大写。但是 this discussion与上述建议相反。哪一个应该被认为是正确的? 最佳答案 这没有正确答案,
随着 n 变大,log*(log n) 和 log(log* n) 这两个函数会更快吗? 这里,log* 函数是迭代对数,定义如下: 我怀疑它们是相同的,只是写法不同,但它们之间有什么区别吗? 最佳答
作为家庭作业,我被要求在 O(log(n)) 中编写一个算法,我可以计算出我编写的算法的复杂度为 O(log(n) + log(n/2) + log(n/4) + log(n/8) + ... + l
我正在使用 Tomee。日志文件夹包含这样的文件 localhost_access_log.2016-12-02.txt localhost.2016-12-02.log catalina.2016-
Android Log.v、Log.d、Log.i、Log.e 等的 ios 等效项是什么?同样在 android 上,我使用 Android 设备监视器和 logcat 来访问我的手机日志,我需要在
我认为下面的代码是 O(log log n) 因为它里面有 i*i 但我对 log n 感到困惑> 和 log (log n)。 for (i=2; i*i<=number; i++) { if
我正在修改 kvm 模块,并在内核代码中添加了 printk 语句。运行虚拟机后,printk 为我提供了错误地址和有关 guest 操作系统的其他信息。 我需要从这个信息中生成统计信息。当我使用 d
我有一个部署为 Windows Azure Web 角色的 WCF 服务。 我正在使用 Enterprise Library 进行异常处理,并且在我的本地 Development Fabric 中,似
关闭。这个问题是opinion-based 。目前不接受答案。 想要改进这个问题吗?更新问题,以便 editing this post 可以用事实和引文来回答它。 . 已关闭 9 年前。 Improv
在 Go 的生产中使用 log.SetFlags(log.LstdFlags | log.Lshortfile) 是好的做法(至少是一般做法)吗?我想知道在生产中这样做是否存在性能或安全问题。因为它不
我想知道什么更快: double value = Math.log(a) - Math.log(b); 或 double value = Math.log(a/b); 我计算值的方式是否会对性能产生影
我有数百个子例程使用 log.Println() 写入日志文件 我正在使用 log.Println 写入 error.log 文件。 func main() { e, err := os.Open
我将 Nuxt 与 SSR 一起使用,并希望有类似于 apaches 的 access.log 和 error.log 的东西 我特别感兴趣的是每次调用的响应时间。 我在 nuxt 文档中找不到任何内
我知道以前有人问过这个问题,但我相信这是一个不同的问题。 Nginx 在 www-data 下运行: $ ps -eo "%U %G %a" | grep nginx root root
我在我的日志文件中发现了一个非常奇怪的条目 Jan 29 01:35:30 vs-proj-handy sshd[5316]: Received disconnect from 130.207.203
对于我正在开发的应用程序,我希望在开发过程中和发布时简化故障排除。我希望能够检索到对 Log 的调用,以了解在 USB 调试中没有连接手机的情况下运行应用程序时的调用,以便可以检索并发送给我。例如,当
我试图捕获 panic 并记录错误: func (s *server) SayHello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloRep
我是一名优秀的程序员,十分优秀!