boost::log隨筆

來源:互聯網
上載者:User

標籤:bsp   rds   red   多個   att   ons   xxx   str   控制   

boost::log包含兩個大部分:logger和sink

 

logging::core是全域的一個單例

 

1,sink

在boost::log中有一個預設的sink與控制台流相關關聯;

如果想把日誌輸出到指定的檔案流中可以使用如下代碼:

auto sink=add_log_file("xxx.log");

上面的一行代碼與下面是等價的:

===========================================================================

void init(){// Construct the sinktypedef sinks::synchronous_sink< sinks::text_ostream_backend > text_sink;boost::shared_ptr< text_sink > sink = boost::make_shared< text_sink >(); // Add a stream to write log tosink->locked_backend()->add_stream(boost::make_shared< std::ofstream >("sample.log")); // Register the sink in the logging corelogging::core::get()->add_sink(sink);}=========================================================================== 從上面的代碼中可能知道add_log_file內部是把相關的sink已經加入到loggint::core了; 如果要使用多個sink,可以通過設定channel屬性,eg
 1 typedef boost::log::sources::channel_logger_mt<>  channel_logger; 2  3 std::shard_ptr<channel_logger>  create_logger(const std::string& channel) 4 { 5     auto sink = add_file_log( 6            keywords::file_name="xx.log", 7            keywords::channel=channel 8             );   9 10   //只將屬於此channel的資訊輸出到xx.log11   sink->set_filter(expr::attr<std::string>("Channel")==channel);12 13   return std::shared_ptr<channel_logger>(new channel_logger(keywords::channel));14 }

 

如此就建立一個指向channel相關的logger,有了logger就可以列印日誌了,eg

std::shared<channel_logger> logger = create_logger("my_channel");

 BOOST_LOG(*logger)<<"Hello World!";

 這樣就可以在xx.log檔案中看到Hello World!資訊了  參考連結:http://www.wanguanglu.com/2016/07/28/boost-log-document/#detailed-logging-source 

boost::log隨筆

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.