简单的案例用于封装syslog,便于逻辑处理中直接使用
案例代码:
#include <stdio.h>
#include <syslog.h>
#define LOG(type, fmt, args...) syslog(type,"[%s:%s(%d)]:" fmt "\n", __FILE__,__FUNCTION__, __LINE__, ##args)
int main(int argc,char **argv){
char info[20] = "message need printf";
LOG(LOG_INFO,"info[%s]", info);
/*等价于 syslog(LOG_INFO,"[%s:%s(%d)]:" "info[%s]" "\n", __FILE__, __FUNCTION__, __LINE__, j) */
return 0;
}
编译,测试:
gcc test2.c -o test2
./test2
如果没有自定义syslog输出日志位置,则在/var/log/messages会有如下打印
Jun 8 18:59:45 localhost test2: [test2.c:main(11)]:info[message need printf]
至于日志输出的位置可以参考syslog相关配置文件syslog.conf