tokyo tyrant原始碼分析 日誌系統實現

2021-05-09 12:50:06 字數 1576 閱讀 4244

enum {                                   /* enumeration for logging levels */

ttlogdebug,                            /* debug */

ttloginfo,                             /* information */

ttlogerror,                            /* error */

ttlogsystem                            /* system */

int g_loglevel = ttloginfo; /* global,default */

static void do_log(int level, const char *msg, void *opq){

if(level < g_loglevel) return;

logarg *arg = (logarg *)opq;

char date[48];

tcdatestrwww(int64_max, int_max, date);

char buf[linebufsiz];

int len = snprintf(buf, linebufsiz, "%s/t%s/n", date, msg);

tcwrite(arg ? arg->fd : 1, buf, len);

程式引數中關於log的選項:-ld (g_loglevel = ttlogdebug), -le (g_loglevel = ttlogerror)

tyrant的日誌系統實現的很簡單,4個level,預設為info,可以在通過執行時引數設定為debug或error。低於指定level的msg直接忽略,高的記錄到檔案或輸出到stdout。

程式啟動時,arg->fd = 1,這時開始檢查使用者指定引數的有效性,因無效引數而報出的warning都輸出到stdout(==1)。然後才設定arg->fd為使用者引數"-log"指定的檔案(當然如果不指定該引數就繼續輸出到stdout)。

**很多重要的warning,包括一些引數的錯誤,都是用level info輸出的,所以如果你指定"-le"(error)的話,你將看不到它們!

系統沒提供引數讓使用者指定level system是因為很多錯誤資訊都是用level error輸出的,使用者不應忽略它們。

我推薦使用者就使用預設的level info,很多有用的出錯提示都是都是在這個level輸出的,而且和level error相比基本不會降低程式效能

。如果用於除錯或實驗,可以用level debug,能得到更多資訊。

當然,這只是普遍情況。很少數的情況,比如你用mask引數禁止了tyrant的某個命令的使用,而客戶端又大量傳送這個命令的請求時,就會

有大量level info的資訊輸出。實際中你應該視你的log輸出量決定,如果level info的輸出量並不大,就用它。

有人可能擔心在需要高頻率執行、對效能要求苛刻的**片段中使用log輸出函式的負面影響:在生產環境中雖然調高了log level,但頻繁的函式呼叫開銷不可避免。解決辦法很簡單:使用巨集。比如把level的判定拿出來,放到巨集中。

Cartographer原始碼篇 原始碼分析 1

在安裝編譯cartographer 1.0.0的時候,我們可以看到 主要包括cartorgarpher ros cartographer ceres sover三個部分。其中,ceres solver用於非線性優化,求解最小二乘問題 cartographer ros為ros平台的封裝,獲取感測器資料...

AbstractListView原始碼分析3

normal list that does not indicate choices public static final int choice mode none 0 the list allows up to one choice public static final int choice ...

Android AsyncTask原始碼分析

android中只能在主線程中進行ui操作,如果是其它子執行緒,需要借助非同步訊息處理機制handler。除此之外,還有個非常方便的asynctask類,這個類內部封裝了handler和執行緒池。本文先簡要介紹asynctask的用法,然後分析具體實現。asynctask是乙個抽象類,我們需要建立子...