C语言 C 的日志库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6508461/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 09:01:16  来源:igfitidea点击:

Logging library for C

clogging

提问by trafalgarx

I am looking for a productive and simple logging library for C, which can output the log to a file. Displaying messages in the log I want to make like this:

我正在为 C 寻找一个高效且简单的日志库,它可以将日志输出到文件中。在日志中显示消息我想像这样:

date-time tag message

It would be nice to control the level of detail of messages and control the size of the file.

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

I found two projects that are suitable for me. It log4cand nglogc.

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

log4c seemed too big. nglogc quite fit, but also has a redundant functional. maybe you tell me more variants?

log4c 似乎太大了。nglogc 相当适合,而且还具有冗余功能。也许你告诉我更多的变种?

回答by Edwin Buck

You can use this

你可以用这个

File logger.h

文件记录器.h

#ifndef LOGGER_H
#define LOGGER_H

void logger(const char* tag, const char* message);

#endif /* LOG_H */

File logger.c

文件记录器.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);
}

It's probably not perfect, but it does satisfy the needs as you have presented them.

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

回答by HardySimpson

I suggest the log library which is written by myself --- zlog!

推荐自己写的日志库——zlog!

The way to fit your need in zlog is:

在 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;
} 

It will generate xxx.log in current directory as

它将在当前目录中生成 xxx.log 作为

2012-09-30 07:22:50 my_cat hello, zlog

Links:

链接:

Download: https://github.com/HardySimpson/zlog/archive/latest-stable.tar.gz

下载:https: //github.com/HardySimpson/zlog/archive/latest-stable.tar.gz

UsersGuide: http://hardysimpson.github.com/zlog/UsersGuide-EN.html

用户指南:http: //hardysimpson.github.com/zlog/UsersGuide-EN.html

Hompage: http://hardysimpson.github.com/zlog/

主页:http://hardysimpson.github.com/zlog/

回答by Babacar

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] = '
init_slog("example", 1, 3);
'; 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); }

Have fun!

玩得开心!

回答by Sun Dro

You can use this simple logging library: https://github.com/kala13x/slog

你可以使用这个简单的日志库:https: //github.com/kala13x/slog

Here is an example how to use:

这是一个如何使用的示例:

At first you must init log. with init_log() function. First argument is log filename, second argument is log to file (1 enabled, 0 disabled) and third argument is max log level

首先你必须初始化日志。使用 init_log() 函数。第一个参数是日志文件名,第二个参数是日志到文件(1 启用,0 禁用),第三个参数是最大日志级别

slog(0, "Test message with level 0");
slog(2, "Test message with level 2");
slog(0, "Test message with int argument: %d", int_arg);

print and log something

打印并记录一些东西

// file log.c
void log(const char* tag, const char* message) {
   time_t now;
   struct tm _calendar_time;
   char _buf[MAX_COUNT];

   time(&now);
   localtime_s(&_calendar_time, &now);

   strftime(_buf, MAX_COUNT, "%c", &_calendar_time);
   printf("%s [%s]: %s\n", _buf, tag, message);
}

Outout will be something like that:

Outout 将是这样的:

2015:04:02:56 - Test message with level 0
2015:04:02:56 - Test message with level 2
2015:04:02:56 - Test message with int argument: 69

2015:04:02:56 - 级别为 0 的测试消息
2015:04:02:56 - 级别为 2 的测试消息
2015:04:02:56 - 带有 int 参数的测试消息:69

回答by wonder.mice

Take a look at the zf_loglogging library. It's small, simple and provides essentials only. From README.md:

查看zf_log日志记录库。它小巧、简单,仅提供必需品。来自 README.md:

This is just a thin wrapper around sprintf() function. It provides less than 20% of functionality found in more sophisticated libraries, but covers more than 80% of common use cases. Focus is made on simplicity, ease of use and performance (to be more precise - low overhead).

这只是 sprintf() 函数的一个薄包装。它提供的功能不到更复杂的库中的 20%,但涵盖了 80% 以上的常见用例。重点在于简单性、易用性和性能(更准确地说 - 低开销)。

Features:

特征:

  • Debug logging is reduced to no-op in release builds
  • Arguments are not evaluated when the message is not logged
  • No "unused" warning for variables used in log statements only
  • Log a memory region as HEX and ASCII
  • Custom output functions
  • 调试日志在发布版本中减少为无操作
  • 未记录消息时不评估参数
  • 仅在日志语句中使用的变量没有“未使用”警告
  • 将内存区域记录为 HEX 和 ASCII
  • 自定义输出函数

回答by jtuki

I am also finding solutions on this problem. The answer from @edwin-buck is just simple and OK for my need.

我也在寻找这个问题的解决方案。@edwin-buck 的答案很简单,可以满足我的需要。

I really know nothing on multi-threading and thread-safe, but after compiling under Visual Studio compiler (it can give some warnings and tips), and searching through google, I think a few modification might make the code above thread-safe and better.

我对多线程和线程安全一无所知,但是在Visual Studio编译器下编译后(它可以给出一些警告和提示),并通过google搜索,我认为一些修改可能会使上面的代码线程安全并且更好.

##代码##

Feel free to correct me if wrong.

如果错了,请随时纠正我