https://code.google.com/p/hardysimpson-zlog/downloads/list
照著文件 install ... 然後開始寫測試程式 !!
test_hello.cpp
extern "C" {
#include "zlog.h"
}
int rc;
zlog_category_t * logp ;
rc = zlog_init("/home/informix/log/zlog.conf");
if (rc) {
printf("init failed\n");
return -1;
}
logp = zlog_get_category("my_cat");
if (!logp) {
printf("get cat fail\n");
zlog_fini();
return -2;
}
先看看 zlog.conf 我們該寫什麼內容 :
[global]
buffer min = 15360
buffer max = 2mb
[formats]
simple = "%m%n"
[rules]
my_cat.DEBUG "/home/informix/log/test.log"; simple
這些參數都是設定 log 紀錄格式以及 buffer 大小之類的 , 可參考文件 , 非常有彈性 !!
void * thread1x(void *threadcnt)
{
char s1[1050] ;
int d1 ;
double f1 = 12345.6789 ;
d1 = (int)(long) threadcnt ;
if(d1==101)
memset(s1,'A',1049) ;
else
memset(s1,'a',1049) ;
s1[1049]=0x00 ;
printf("before while loop(%d) \n",d1) ;
pthread_detach(pthread_self());
while(1)
{
zlog_info(logp, "hello, zlog");
zlog_info(logp, "(%s)(%08d)(%08.2f)",s1,d1,f1);
sleep(1) ;
}
}
最後,別忘了 close log file : zlog_fini();
g++ test_hello.cpp -I/usr/local/include -lzlog -lpthread -o test_hellocpp.exe
從 test.log 可見多筆 log data !!
留言列表