標籤:http 使用 os 檔案 2014 re
開源純C日誌函數庫iLOG3快速入門(八、如果你喜歡簡單日誌函數甚於日誌函數庫)
很多網友來信堅持表達了在項目中應使用簡單日誌函數,而不喜歡日誌函數庫,我與之反覆爭論無果,不過話說回來,我也喜歡短小輕快的日誌函數庫,而不是融入太多功能的臃腫庫,比如日誌轉檔我還是堅持用外部shell來實現會更好,這樣的日誌函數庫既可以小很多,也可以更輕快穩定,我只是迫於另外一批功能控網友的“威脅”而加入了太多的功能,他們喜歡極端的一攬子解決方案,並且展示其它諸如log4c也實現了這些功能為由證明他們是正確的,如果我不實現就免談。
最近在研發一個小型的應用伺服器平台tcpdaemon,裡面需要日誌功能,但整個tcpdaemon代碼很少,比iLOG3小的多,於是讓我想到最上面的那些網友的話,經過反覆考量,我決定給我的iLOG3補充一點小鬼靈精怪——附帶一個單獨的迷你型的簡單日誌函數,這樣大家終雩都滿足了吧。
src/新增LOGC.h,LOGC.c,這兩個源檔案不編譯進libiLOG3.so,而是獨立出來提供喜歡簡單日誌函數而反感日誌函數庫的朋友單獨複製到他的項目中去直接源碼包含使用。比如tcpdaemon項目
tcpdaemon-1.1.0/ src/ tcpdaemon.h main.c tcpdaemon.c worker.c LOGC.h LOGC.c ...
在iLOG3的test目錄中有測試案例test_logc.c
#include "../src/LOGC.h"int test_logc(){ char buf[ 64 + 1 ] ; long buflen ; SetLogFile( "%s/log/test_logc.log" , getenv("HOME") ); SetLogLevel( LOGLEVEL_INFO ); DebugLog( __FILE__ , __LINE__ , "call DebugLog" ); InfoLog( __FILE__ , __LINE__ , "call InfoLog" ); WarnLog( __FILE__ , __LINE__ , "call WarnLog" ); ErrorLog( __FILE__ , __LINE__ , "call ErrorLog" ); FatalLog( __FILE__ , __LINE__ , "call FatalLog" ); memset( buf , 0x00 , sizeof(buf) ); buflen = sizeof(buf) - 1 ; DebugHexLog( __FILE__ , __LINE__ , buf , buflen , "call DebugHexLog" ); InfoHexLog( __FILE__ , __LINE__ , buf , buflen , "call InfoHexLog" ); WarnHexLog( __FILE__ , __LINE__ , buf , buflen , "call WarnHexLog" ); ErrorHexLog( __FILE__ , __LINE__ , buf , buflen , "call ErrorHexLog" ); FatalHexLog( __FILE__ , __LINE__ , buf , buflen , "call FatalHexLog" ); return 0;}int main(){ return -test_logc();}
編譯時間只需把LOGC.c一起編譯連結即可
$ gcc -g -fPIC -O2 -Wall -Werror -fno-strict-aliasing -I. -c test_logc.c$ gcc -g -fPIC -O2 -Wall -Werror -fno-strict-aliasing -I. -c ../src/LOGC.c$ gcc -g -fPIC -O2 -Wall -Werror -fno-strict-aliasing -o test_logc test_logc.o LOGC.o -lpthread$ ./test_logc$ cat $HOME/log/test_logc.log2014-07-20 22:35:12.890000 | INFO | 1556:3284:test_logc.c:12 | call InfoLog2014-07-20 22:35:12.906000 | WARN | 1556:3284:test_logc.c:13 | call WarnLog2014-07-20 22:35:12.906000 | ERROR | 1556:3284:test_logc.c:14 | call ErrorLog2014-07-20 22:35:12.906000 | FATAL | 1556:3284:test_logc.c:15 | call FatalLog2014-07-20 22:35:12.906000 | INFO | 1556:3284:test_logc.c:20 | call InfoHexLog 0 1 2 3 4 5 6 7 8 9 A B C D E F 0123456789ABCDEF0x00000000 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................0x00000010 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................0x00000020 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................0x00000030 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................2014-07-20 22:35:12.906000 | WARN | 1556:3284:test_logc.c:21 | call WarnHexLog 0 1 2 3 4 5 6 7 8 9 A B C D E F 0123456789ABCDEF0x00000000 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................0x00000010 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................0x00000020 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................0x00000030 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................2014-07-20 22:35:12.906000 | ERROR | 1556:3284:test_logc.c:22 | call ErrorHexLog 0 1 2 3 4 5 6 7 8 9 A B C D E F 0123456789ABCDEF0x00000000 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................0x00000010 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................0x00000020 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................0x00000030 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................2014-07-20 22:35:12.906000 | FATAL | 1556:3284:test_logc.c:23 | call FatalHexLog 0 1 2 3 4 5 6 7 8 9 A B C D E F 0123456789ABCDEF0x00000000 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................0x00000010 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................0x00000020 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................0x00000030 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
很簡單吧?那就趕緊下載來玩玩吧
首頁傳送門 : [url]http://git.oschina.net/calvinwilliams/iLOG3[/url]
原始碼包doc目錄中包含了使用者指南和參考手冊,裡面有更詳盡的說明