我正在寻找一个高效和简单的C日志库,它可以将日志输出到一个文件中。在日志中显示消息,如下所示:
date-time tag message如果能控制消息的详细程度和文件的大小,那就太好了。
我找到了两个适合我的项目。It log4c和nglogc。
log4c似乎太大了。nglogc挺适合的,而且还有多余的功能。也许你可以告诉我更多的变种?
发布于 2011-06-29 00:09:45
你可以使用这个
文件记录器.h
#ifndef LOGGER_H
#define LOGGER_H
void logger(const char* tag, const char* message);
#endif /* LOG_H */文件logger.c
#include "logger.h"
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void logger(const char* tag, const char* message) {
time_t now;
time(&now);
printf("%s [%s]: %s\n", ctime(&now), tag, message);
}它可能并不完美,但它确实满足了您所呈现的需求。
发布于 2012-10-01 21:29:52
我推荐我自己写的日志库- zlog!
在zlog中满足您的需求的方法是:
$ vi /etc/zlog.conf
[formats]
simple = "%D %c %m%n"
# don't know what the tag mean in your question, so put category of zlog instead
# log level is also available here, add %V means level
[rules]
my_cat.* "xxx.log"; simple
$ vi hello.c
#include <stdio.h>
#include "zlog.h"
int main(int argc, char** argv)
{
int rc;
zlog_category_t *c;
rc = dzlog_init("/etc/zlog.conf", "my_cat");
if (rc) {
printf("init failed\n");
return -1;
}
zlog_info(c, "hello, zlog");
zlog_fini();
return 0;
} 它将在当前目录中生成xxx.log,格式为
2012-09-30 07:22:50 my_cat hello, zlog链接:
下载:https://github.com/HardySimpson/zlog/archive/latest-stable.tar.gz
UsersGuide:http://hardysimpson.github.com/zlog/UsersGuide-EN.html
主页:http://hardysimpson.github.com/zlog/
发布于 2014-05-03 22:55:50
Here is mine:
log.h
------
#ifndef LOG_H
#define LOG_H
void log_error(const char* message, ...); void log_info(const char* message, ...); void log_debug(const char* message, ...);
#endif
log.c
------
#include "log.h"
void log_format(const char* tag, const char* message, va_list args) { time_t now; time(&now); char * date =ctime(&now); date[strlen(date) - 1] = '\0'; printf("%s [%s] ", date, tag); vprintf(message, args); printf("\n"); }
void log_error(const char* message, ...) { va_list args; va_start(args, message); log_format("error", message, args); va_end(args); }
void log_info(const char* message, ...) { va_list args; va_start(args, message); log_format("info", message, args); va_end(args); }
void log_debug(const char* message, ...) { va_list args; va_start(args, message); log_format("debug", message, args); va_end(args); }玩得开心!
https://stackoverflow.com/questions/6508461
复制相似问题