记录C的库

tra*_*arx 13 c logging

我正在为C寻找一个高效且简单的日志记录库,它可以将日志输出到文件中.在我想要的日志中显示消息,如下所示:

date-time tag message
Run Code Online (Sandbox Code Playgroud)

控制消息的详细程度并控制文件的大小会很好.

我找到了两个适合我的项目.它是log4cnglogc.

log4c似乎太大了.nglogc非常合适,但也具有冗余功能.也许你告诉我更多变种?

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)

它可能并不完美,但它确实满足了你提出的需求.

  • 记录到文件怎么样?:) (2认同)
  • 没有刷新通常会发生的事情是您最需要的日志消息没有写入磁盘.如果应用程序停止,您将丢失一些消息,这些消息通常是最重要的消息(崩溃的原因).另一方面,磁盘现在具有小的ram缓存,并且写入性能(虽然不是很好)至少是缓冲的.现在对于读/写锁定,这是一个文件,并且大多数文件系统不会阻止不一致的读取(但是如果记录到数据库,这是一个不同的故事) (2认同)

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)

玩得开心!


Sun*_*Dro 5

您可以使用这个简单的日志库: 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