zlog日志函数库
在C的世界里面没有特别好的日志函数库(就像JAVA里面的的log4j,或者C++的log4cxx)。C程序员都喜欢用自己的轮子。printf就是个挺好的轮子,但没办法通过配置改变日志的格式或者输出文件。syslog是个系统级别的轮子,不过速度慢,而且功能比较单调
下载:https://github.com/HardySimpson/zlog/releases
安装
# make
# sudo make install
or
# make PREFIX=./install install
使用
# cc test.c -L/usr/local/lib -lzlog -lpthread -I/usr/local/include
配置文件(举例)
[global]
strict init = true
buffer min = 1024
buffer max = 2MB
rotate lock file = /tmp/zlog.lock
default format = "%d.%us %-6V (%c:%F:%L) - %m%n"
file perms = 600
[levels]
TRACE = 10
CRIT = 130, LOG_CRIT
[formats]
simple = "%m%n"
normal = "%d %m%n"
[rules]
default.* >stdout; simple
*.* "%12.2E(HOME)/log/%c.log", 1MB*12; simple
my_.INFO >stderr;
my_cat.!ERROR "/var/log/aa.log"
my_dog.=DEBUG >syslog, LOG_LOCAL0; simple
my_mice.* $user_define;
levels:(level string) = (level int), (syslog level, optional)
level:日志等级(1-253),越大越重要。默认LOG_DEBUG
Rules:(category).(level) (output), (options, optional); (format name, optional)
接口(API)
int zlog_init(const char *confpath);
int zlog_reload(const char *confpath);
void zlog_fini(void);
zlog_init:读取配置文件到内存
zlog_reload:从confpath重载配置
zlog_fini:清理内存
typedef struct zlog_category_s zlog_category_t;
zlog_category_t *zlog_get_category(const char *cname);
zlog_get_category:寻找匹配分类
void zlog(zlog_category_t * category,
const char *file, size_t filelen,
const char *func, size_t funclen,
long line, int level,
const char *format, ...);
void vzlog(zlog_category_t * category,
const char *file, size_t filelen,
const char *func, size_t funclen,
long line, int level,
const char *format, va_list args);
void hzlog(zlog_category_t * category,
const char *file, size_t filelen,
const char *func, size_t funclen,
long line, int level,
const void *buf, size_t buflen);
vzlog:可变参数
hzlog:16进制的形式表示
宏定义
#define zlog_xxx(cat, ...) \
zlog(cat, __FILE__, sizeof(__FILE__)-1, __func__, sizeof(__func__)-1, __LINE__, \
ZLOG_LEVEL_XXX, __VA_ARGS__)
#define dzlog_xxx(...) \
dzlog(__FILE__, sizeof(__FILE__)-1, __func__, sizeof(__func__)-1, __LINE__, \
ZLOG_LEVEL_XXX, __VA_ARGS__)
#define zlog_xxx(cat, format, args...) \
zlog(cat, __FILE__, sizeof(__FILE__)-1, __func__, sizeof(__func__)-1, __LINE__, \
ZLOG_LEVEL_XXX, format, ##args)
#define dzlog_xxx(format, args...) \
dzlog(__FILE__, sizeof(__FILE__)-1, __func__, sizeof(__func__)-1, __LINE__, \
ZLOG_LEVEL_XXX, format, ##args)
#define vzlog_xxx(cat, format, args) \
vzlog(cat, __FILE__, sizeof(__FILE__)-1, __func__, sizeof(__func__)-1, __LINE__, \
ZLOG_LEVEL_XXX, format, args)
#define hzlog_xxx(cat, buf, buf_len) \
hzlog(cat, __FILE__, sizeof(__FILE__)-1, __func__, sizeof(__func__)-1, __LINE__, \
ZLOG_LEVEL_XXX, buf, buf_len)
#define vdzlog_xxx(format, args) \
vdzlog(__FILE__, sizeof(__FILE__)-1, __func__, sizeof(__func__)-1, __LINE__, \
ZLOG_LEVEL_XXX, format, args)
#define hdzlog_xxx(buf, buf_len) \
hdzlog(__FILE__, sizeof(__FILE__)-1, __func__, sizeof(__func__)-1, __LINE__, \
ZLOG_LEVEL_XXX, buf, buf_len)
fatal
error
warn
notice
info
debug
举例
[rules]
test.* >stderr
int rc;
zlog_category_t *zc;
rc = zlog_init("test.conf");
if (rc)
{
printf("init failed\n");
return -1;
}
zc = zlog_get_category("test");
{
printf("get cat fail\n");
zlog_fini();
return -2;
}
zlog_info(zc, "hello furong.");
zlog_fini();
# ./a.out
2017-05-25 13:46:35 INFO [14789:test.c:22] hello furong.
其他
还支持文件转档和用户自定义输出等