Edw*_*uck 14
你可以用它
文件logger.h
#ifndef LOGGER_H
#define LOGGER_H
void logger(const char* tag, const char* message);
#endif /* LOG_H */
Run Code Online (Sandbox Code Playgroud)
文件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);
}
Run Code Online (Sandbox Code Playgroud)
它可能并不完美,但它确实满足了你提出的需求.
Har*_*son 10
我建议自己写的日志库--- 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;
}
Run Code Online (Sandbox Code Playgroud)
它将在当前目录中生成xxx.log
2012-09-30 07:22:50 my_cat hello, zlog
Run Code Online (Sandbox Code Playgroud)
链接:
下载:https://github.com/HardySimpson/zlog/archive/latest-stable.tar.gz
用户指南:http://hardysimpson.github.com/zlog/UsersGuide-EN.html
Hompage:http://hardysimpson.github.com/zlog/
小智 8
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); }
Run Code Online (Sandbox Code Playgroud)
玩得开心!
您可以使用这个简单的日志库: https ://github.com/kala13x/slog
以下是如何使用的示例:
首先您必须初始化日志。使用 init_log() 函数。第一个参数是日志文件名,第二个参数是日志到文件(1 启用,0 禁用),第三个参数是最大日志级别
init_slog("example", 1, 3);
Run Code Online (Sandbox Code Playgroud)
打印并记录一些东西
slog(0, "Test message with level 0");
slog(2, "Test message with level 2");
slog(0, "Test message with int argument: %d", int_arg);
Run Code Online (Sandbox Code Playgroud)
输出将是这样的:
2015:04:02:56 - 级别为 0 的测试消息
2015:04:02:56 - 级别为 2 的测试消息
2015:04:02:56 - 参数为 int 的测试消息:69