#include <stdio.h> #include "LOG.h" #define LOG_STYLES_HELLO ( LOG_STYLE_DATETIMEMS | LOG_STYLE_LOGLEVEL | LOG_STYLE_PID | LOG_STYLE_TID | LOG_STYLE_SOURCE | LOG_STYLE_FORMAT | LOG_STYLE_NEWLINE ) int test_LOG() { char buffer[ 64 + 1 ] = "" ; long buflen = sizeof(buffer)-1 ; /* 创建日志句柄 */ if(CreateLogHandleG() == NULL ) { printf( "创建日志句柄失败errno[%d]\n" , errno ); return -1; } else { printf( "创建日志句柄成功\n" ); } /* 设置日志输出文件名 */ SetLogOutputG( LOG_OUTPUT_FILE , "test_LOG.log" , LOG_NO_OUTPUTFUNC ); /* 设置当前日志过滤等级 */ SetLogLevelG( LOG_LEVEL_INFO ); /* 设置当前行日志风格方案 */ SetLogStylesG( LOG_STYLES_HELLO , LOG_NO_STYLEFUNC); /* 以不同日志等级写行日志 */ DEBUGLOGG( "hello DEBUG" ); /* 这行日志因等级不够,被华丽的过滤了 */ INFOLOGG( "hello INFO" ); WARNLOGG( "hello WARN" ); ERRORLOGG( "hello ERROR" ); FATALLOGG( "hello FATAL" ); /* 以不同日志等级写十六进制块日志 */ DEBUGHEXLOGG( buffer , buflen , "hello DEBUG buffer[%ld]" , buflen ); /* 又一个被华丽的过滤 */ INFOHEXLOGG( buffer , buflen , "hello INFO buffer[%ld]" , buflen ); WARNHEXLOGG( buffer , buflen , "hello WARN buffer[%ld]" , buflen ); ERRORHEXLOGG( buffer , buflen , "hello ERROR buffer[%ld]" , buflen ); FATALHEXLOGG( buffer , buflen , "hello FATAL buffer[%ld]" , buflen ); /* 销毁日志句柄 */ DestroyLogHandleG(); printf( "释放日志句柄\n" ); return 0; } int main() { return -test_LOG(); }
说明一下,代码先创建了一个日志句柄,设置一些属性,然后刷刷刷的写日志,最后销毁日志句柄。
编译、链接、执行
$ gcc -g -fPIC -Wall -Werror -O2 -I. -std=c99 -I/home/calvin/exinc/iLOG3 -c test_LOG.c $ gcc -g -fPIC -Wall -Werror -O2 -o test_LOG test_LOG.o -L. -std=c99 -L/home/calvin/exlib -liLOG3 $ ./test_LOG 创建日志句柄成功 释放日志句柄 $ cat test_LOG.log 2014-02-10 00:26:07.418678 | INFO | 2045:3086292688:test_LOG.c:32 | hello INFO 2014-02-10 00:26:07.419236 | WARN | 2045:3086292688:test_LOG.c:33 | hello WARN 2014-02-10 00:26:07.419506 | ERROR | 2045:3086292688:test_LOG.c:34 | hello ERROR 2014-02-10 00:26:07.419518 | FATAL | 2045:3086292688:test_LOG.c:35 | hello FATAL 2014-02-10 00:26:07.419529 | INFO | 2045:3086292688:test_LOG.c:39 | hello INFO buffer[64] 0 1 2 3 4 5 6 7 8 9 A B C D E F 0123456789ABCDEF 0x00000000 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-02-10 00:26:07.419586 | WARN | 2045:3086292688:test_LOG.c:40 | hello WARN buffer[64] 0 1 2 3 4 5 6 7 8 9 A B C D E F 0123456789ABCDEF 0x00000000 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-02-10 00:26:07.419627 | ERROR | 2045:3086292688:test_LOG.c:41 | hello ERROR buffer[64] 0 1 2 3 4 5 6 7 8 9 A B C D E F 0123456789ABCDEF 0x00000000 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-02-10 00:26:07.419686 | FATAL | 2045:3086292688:test_LOG.c:42 | hello FATAL buffer[64] 0 1 2 3 4 5 6 7 8 9 A B C D E F 0123456789ABCDEF 0x00000000 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 ................你想一次日志输出到多个对象(文件、标准输出、标准错误输出、syslogd或WINDOWS EVENT、远程日志服务器等),没问题,我们用一个LOGS包两个LOG就可以了,看代码
#include <stdio.h> #include "LOGS.h" #define LOG_STYLES_DOG ( LOG_STYLE_DATETIMEMS | LOG_STYLE_LOGLEVEL | LOG_STYLE_PID | LOG_STYLE_TID | LOG_STYLE_SOURCE | LOG_STYLE_FORMAT | LOG_STYLE_NEWLINE ) #define LOG_STYLES_EVENT ( LOG_STYLE_DATETIMEMS | LOG_STYLE_CUSTLABEL1 | LOG_STYLE_FORMAT | LOG_STYLE_NEWLINE ) int test_LOGS() { LOGS *gs = NULL ; LOG *g = NULL ; char buffer[ 64 + 1 ] = "" ; long buflen = sizeof(buffer)-1 ; /* 创建日志句柄集合 */ gs = CreateLogsHandleG() ; /* 带后缀G的函数创建的日志句柄集合自动设置成线程安全的全局缺省日志句柄集合 */ if( gs == NULL ) { printf( "创建日志句柄集合失败errno[%d]\n" , errno ); return -1; } else { printf( "创建日志句柄集合成功\n" ); } /* 创建日志句柄 */ g = CreateLogHandle() ; if( g == NULL ) { printf( "创建日志句柄失败errno[%d]\n" , errno ); return -1; } else { printf( "创建日志句柄成功\n" ); } /* 设置日志输出文件名 */ SetLogOutput( g , LOG_OUTPUT_FILE , "test_LOGS.log" , LOG_NO_OUTPUTFUNC ); /* 设置当前日志过滤等级 */ SetLogLevel( g , LOG_LEVEL_INFO ); /* 设置当前行日志风格方案 */ SetLogStyles( g , LOG_STYLES_DOG , LOG_NO_STYLEFUNC); /* 刚刚创建的日志句柄加到日志句柄集合中 */ AddLogToLogs( gs , "file" , g ); /* 创建日志句柄 */ g = CreateLogHandle() ; if( g == NULL ) { printf( "创建日志句柄失败errno[%d]\n" , errno ); return -1; } else { printf( "创建日志句柄成功\n" ); } /* 设置日志输出文件名 */ SetLogOutput( g , LOG_OUTPUT_STDOUT , NULL , LOG_NO_OUTPUTFUNC ); /* 设置当前日志过滤等级 */ SetLogLevel( g , LOG_LEVEL_ERROR ); /* 设置线程私有标签 */ SetLogCustLabel( g , 1 , "COREIB1" ); /* 设置当前行日志风格方案 */ SetLogStyles( g , LOG_STYLES_EVENT , LOG_NO_STYLEFUNC); /* 刚刚创建的日志句柄加到日志句柄集合中 */ AddLogToLogs( gs , "stdout" , g ); /* 以不同日志等级写行日志集合 */ DEBUGLOGSG( "hello DEBUG" ); /* 这行日志因等级不够,被华丽的过滤了 */ INFOLOGSG( "hello INFO" ); WARNLOGSG( "hello WARN" ); ERRORLOGSG( "hello ERROR" ); FATALLOGSG( "hello FATAL" ); /* 以不同日志等级写十六进制块日志集合 */ DEBUGHEXLOGSG( buffer , buflen , "hello DEBUG buffer[%ld]" , buflen ); /* 又一个被华丽的过滤 */ INFOHEXLOGSG( buffer , buflen , "hello INFO buffer[%ld]" , buflen ); WARNHEXLOGSG( buffer , buflen , "hello WARN buffer[%ld]" , buflen ); ERRORHEXLOGSG( buffer , buflen , "hello ERROR buffer[%ld]" , buflen ); FATALHEXLOGSG( buffer , buflen , "hello FATAL buffer[%ld]" , buflen ); /* 销毁日志句柄集合 */ DestroyLogsHandleG(); /* 日志句柄集合内包含的两个日志句柄连带销毁掉 */ printf( "释放日志句柄集合\n" ); return 0; } int main() { return -test_LOGS(); }编译、链接、执行
$ gcc -g -fPIC -Wall -Werror -O2 -I. -std=c99 -I/home/calvin/exinc/iLOG3 -c test_LOGS.c $ gcc -g -fPIC -Wall -Werror -O2 -o test_LOGS test_LOGS.o -L. -std=c99 -L/home/calvin/exlib -liLOG3 $ ./test_LOGS 创建日志句柄集合成功 创建日志句柄成功 创建日志句柄成功 2014-02-10 00:35:54.807915 | COREIB1 | hello ERROR 2014-02-10 00:35:54.808017 | COREIB1 | hello FATAL 2014-02-10 00:35:54.808750 | COREIB1 | hello ERROR buffer[64] 0 1 2 3 4 5 6 7 8 9 A B C D E F 0123456789ABCDEF 0x00000000 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-02-10 00:35:54.809138 | COREIB1 | hello FATAL buffer[64] 0 1 2 3 4 5 6 7 8 9 A B C D E F 0123456789ABCDEF 0x00000000 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 ................ 释放日志句柄集合 $ cat test_LOGS.log 2014-02-10 00:36:52.250602 | INFO | 2300:3086878416:test_LOGS.c:71 | hello INFO 2014-02-10 00:36:52.251233 | WARN | 2300:3086878416:test_LOGS.c:72 | hello WARN 2014-02-10 00:36:52.251246 | ERROR | 2300:3086878416:test_LOGS.c:73 | hello ERROR 2014-02-10 00:36:52.251568 | FATAL | 2300:3086878416:test_LOGS.c:74 | hello FATAL 2014-02-10 00:36:52.251646 | INFO | 2300:3086878416:test_LOGS.c:78 | hello INFO buffer[64] 0 1 2 3 4 5 6 7 8 9 A B C D E F 0123456789ABCDEF 0x00000000 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-02-10 00:36:52.252086 | WARN | 2300:3086878416:test_LOGS.c:79 | hello WARN buffer[64] 0 1 2 3 4 5 6 7 8 9 A B C D E F 0123456789ABCDEF 0x00000000 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-02-10 00:36:52.252129 | ERROR | 2300:3086878416:test_LOGS.c:80 | hello ERROR buffer[64] 0 1 2 3 4 5 6 7 8 9 A B C D E F 0123456789ABCDEF 0x00000000 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-02-10 00:36:52.252612 | FATAL | 2300:3086878416:test_LOGS.c:81 | hello FATAL buffer[64] 0 1 2 3 4 5 6 7 8 9 A B C D E F 0123456789ABCDEF 0x00000000 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 ................你觉得调用函数来构建日志句柄太烦了,希望用配置文件,没问题,iLOG3自带了一个简单配置文件格式,看
hello_logsconf.conf
id hello output FILE test_logconf.log level INFO styles DATETIME|LOGLEVEL|PID|TID|SOURCE|FORMAT|NEWLINE options CHANGE_TEST rotate_mode SIZE rotate_size 10MB log_bufsize 1MB 5MB id stdout output STDOUT level INFO custlabel1 COREIB1 styles DATETIME|CUSTLABEL1|FORMAT|NEWLINE下面不提供完整代码了,看
#include "LOGSCONF.h" int test_logsconf() { LOGS *gs = NULL ; gs = CreateLogsHandleFromConfig( "hello_logsconf.conf" , NULL ) ; SetGlobalLOGS( gs ); /* 这里就能刷刷刷的写日志了,其效果和上一个例子相同 */ ... return 0; }什么,你嫌配置文件格式太难看了,我另外写了一个姐妹函数库iLOG3CONF_SML,用SML标记语言来配置LOGS,给个我在其它产品中的示例
hetao { logs { log ( id = access ) { output = FILE ; filename = "$HOME$/log/access.log" ; level = DEBUG ; styles = "DATETIME|LOGLEVEL|PID|TID|FORMAT|NEWLINE" ; rotate_mode = SIZE ; rotate_size = 10MB ; } log ( id = error ) { output = FILE ; filename = "$HOME$/log/error.log" ; level = ERROR ; styles = "DATETIME|LOGLEVEL|SOURCE|FORMAT|NEWLINE" ; rotate_mode = SIZE ; rotate_size = 10MB ; } } module { connection ( filename = "$HOME$/so/hetao_module_connection_epoll.so" ) { } protocol ( filename = "$HOME$/so/hetao_module_protocol_http.so" ) { max_header_size = 10KB ; root = "$HOME$/www" ; index_page = "index.html" ; error_pages { error_page { error = 403 ; page = "$HOME$/www/403.html" } ; error_page { error = 404 ; page = "$HOME$/www/404.html" } ; error_page { error = 500 ; page = "$HOME$/www/500.html" } ; error_page { error = 505 ; page = "$HOME$/www/505.html" } ; } } } server { listen_ip = "127.0.0.1" ; listen_port = 8080 ; max_connections = 1024 ; } }代码中这样写就能从配置中构建LOGS
LOGS *gs = NULL ; gs = CreateLogsHandleFromConfig_SML( "hetao.conf" , "/hetao/logs" , NULL ) ;你问性能咋样啊?我拿目前同类日志函数库做了性能测试,目前iLOG3是最快的。具体可参见源码包中的用户指南文档中的性能比较章节。
是不是越看越心动了?那就赶紧下载来玩玩吧。
首页传送门 : [url]http://git.oschina.net/calvinwilliams/iLOG3[/url]
源代码包doc目录中包含了用户指南和参考手册,里面有更详尽的说明