我正在研究基本的boost example from here。我正在配置我的應用程序所需的內容,但我被卡住了。這裏是我現在的位置:Boost :: log:基於級別的不同格式(HTML格式)
#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。例如,這些消息我在主那裏應該輸出打印這樣的文件:
<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。
謝謝你的回答。我試圖實現它。那'string_view'不是升壓1.55,對不對?我在Debian Jessie上,在整個'/ usr /'目錄下找不到grep。 –
不,它稍後添加。您可以使用'boost :: string_ref'或'std :: string'來代替。 –
非常感謝。我不知道。實際上今天早上我讀了'string_view'並瞭解它是什麼。另一方面,我找到了解決問題的辦法,並在另一個答案中寫下了它。我從你身上學到了很多東西,所以我對此加以讚賞:)再次感謝! –