Boost Log 基本用法

2021-07-24 18:59:42 字數 3100 閱讀 3878

**:

boost log 基本用法

flyfish 2014-11-5

根據boost提供的**示例,學習boost log 的基本用法

前提boost版本boost_1_56_0

示例**資料夾 boost_1_56_0\libs\log\example\basic_usage

使用的單詞很形象,整個過程就像流水一樣

假設要輸出的日誌比作水

水                     (hello, world!)

水槽                 (sink)

流向**        (console,file)

從**取        (source)

水的等級        (severity level)

過濾輸出        (filter)

格式輸出        (format)

各部分連線者(core)

示例[cpp]view plain

copy

#include 

#include 

#include 

#include 

#include 

#include 

#include 

#include 

#include 

#include 

namespace

logging = boost::log;  

namespace

sinks = boost::log::sinks;  

namespace

attrs = boost::log::attributes;  

namespace

src = boost::log::sources;  

namespace

expr = boost::log::expressions;  

namespace

keywords = boost::log::keywords;  

using

boost::shared_ptr;  

enum

severity_level  

;  // the formatting logic for the severity level

template

typename

chart, 

typename

traitst >  

inline

std::basic_ostream& operator<

std::basic_ostream& strm, severity_level lvl)  

;  if

(static_cast

size_t

>(lvl) 

sizeof

(str) / 

sizeof

(*str)))  

strm <

else

strm <

int>(lvl);  

return

strm;  

}  int

_tmain(

intargc, 

char

* argv)    

從上到下依次分析

一  日誌嚴重性等級

enum severity_level

;二  日誌等級輸出

template< typename chart, typename traitst >

inline std::basic_ostream< chart, traitst >& operator<< (

std::basic_ostream< chart, traitst >& strm, severity_level lvl)

輸出已經定義的等級描述,日誌等級的數值與字串一一對應,如果在enum severity_level如果沒有定義則輸出數值。

std::basic_ostream對所有的內建型別,進行了過載,輸入各種內建型別

過載operator <

iostream library的類都帶有兩個引數,其中乙個是字元的型別,乙個是與字元型別相關的資訊

就像std::cout一樣輸出各種型別,編譯器自己會進行正確的推導輸出的什麼型別。

static const char* const 表示陣列裡面的指標不可改變  而且指標所指向的字串也不可改變

三 日誌輸出位置

logging::add_console_log

日誌輸出到控制台

logging::add_file_log

日誌輸出到檔案

四 定義源,像std::cout一樣輸出

src::logger lg;

boost_log(lg) << "hello, world!";

五 結果

檔案的輸出

2014-11-05, 19:46:19.513082 [00:00:00] 

[int __cdecl wmain(int,char *) (檔案路徑:**行)] a warning severity message, will pass to the file

2014-11-05, 19:46:19.518082 [00:00:00] 

[int __cdecl wmain(int,char *) (檔案路徑:**行)] an error severity message, will pass to the file

控制台的輸出

2014-nov-05 19:51:30.261856: hello, world!

2014-nov-05 19:51:30.268856: a normal severity message, will not pass to the file

2014-nov-05 19:51:30.275856: a warning severity message, will pass to the file

2014-nov-05 19:51:30.284857: an error severity message, will pass to the file

Boost log庫的使用

只有大於等於設定的日誌級別,才會輸出。將此設定過濾器函式放在日誌使用之前 void set filter void log add file distributed under the boost software license,version 1.0.see accompanying file ...

boost log庫使用一

分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!我的英文部落格參考 我過去習慣使用cppcms提供的日誌庫,簡單好用。從1.54版本,boost發布了日誌庫。當讀了一些文件後,因為下面的一些原因,我決定轉而使用boost log。1.tcp服務大概連線了2000個裝置,tcp server會儲存...

boost log庫使用一

我的英文部落格參考 我過去習慣使用cppcms提供的日誌庫,簡單好用。從1.54版本,boost發布了日誌庫。當讀了一些文件後,因為下面的一些原因,我決定轉而使用boost log。1.tcp服務大概連線了2000個裝置,tcp server會儲存所有詳細資訊。舉個例子,裝置登入到tcp serve...