有什么C语言的日志库吗?(如log4j for C)[英] Is there any log library for C? (like log4j for C)

本文是小编为大家收集整理的关于有什么C语言的日志库吗?(如log4j for C)的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我已经在 J​​ava 中工作了很长时间,并且习惯于使用 log4j 库来记录日志.太棒了,现在我正在转向 C,我想看看是否有类似的库用于这种语言的日志.

推荐答案

目前我知道的日志记录库如下:log4c、sclog4c、syslog、zlog.

log4c

log4c 被发明为 C 的 Log4J.如果您专门寻找"类似 Log4J 的东西",因为您希望它类似于"Log4J",那么这很可能是您想要的正在寻找.

链接

sclog4c

sclog4c 被发明为与 java.util.logging 最常用的功能一样简单 - 尽可能简单.如果您正在寻找"类似 Log4J 的东西",因为您希望它尽可能小且简单,那么这很可能就是您要寻找的东西.

链接

系统日志

syslog 最初由 Eric Allman 作为 sendmail 的一部分开发,并已成为 POSIX 环境中守护程序/服务器日志记录的事实标准.它是基于客户端-服务器的,通常想要记录某些内容的守护进程会将日志数据发送到侦听 UDP 端口 514 的 syslogd.如果您因为实际上想要记录守护程序或服务器而专门寻找"类似 Log4J 的东西",那么这很可能就是您要寻找的东西.

链接

zlog

这个被发明出来就像 log4c,只是 - 根据它的描述 - 同时更小和更灵活.

链接

杂项

权力与精益

由于 C 链接、思考和工作的方式不同,我不会寻找在一般情况下功能强大的日志框架 - 与 Java 不同.如果您要使用"成熟的桌面应用程序"及其他应用程序,那么使用 Java 等功能强大的框架进行日志记录无疑是一个不错的选择.如果您正在实施命令行工具或类似工具,我敢打赌精益框架更好——为什么你要依赖 lib2xml 只是为了记录......

速度

如果速度响应.出于某种原因,不要浪费周期对您很重要,请寻找一个日志框架,它使用宏在评估其他参数之前评估日志级别.

缺点是您不能使用具有副作用的参数调用日志例程.但这不应该是一个用例.如果日志语句由于包含副作用而不能被忽略,那将是令人惊讶的.

好处是这样一个框架中的日志语句添加的周期太少,以至于它们几乎不存在 - 只是访问全局、​​检查和条件分支,跳过其余的日志代码 - 2 条指令,在当今的许多 CPU 上,最佳情况下为 1 个周期.

免责声明

我是 sclog4c 的作者.

本文地址:https://www.itbaoku.cn/post/1574877.html

问题描述

I've been working in Java for a long time, and I've been accostumed to use the log4j library for logs. It's a wonderful, and now that I'm moving to C I'd like to find if there is a similar library for logs in this language.

推荐答案

So far I know of the following libraries for logging: log4c, sclog4c, syslog, zlog.

log4c

log4c was invented to be a Log4J for C. If you're specifically looking for "something like Log4J" because you want it to be like "Log4J", this is most likely what you're looking for.

Links

sclog4c

sclog4c was invented to be as simple as the most frequently used features of java.util.logging - as simple as possible. If you're looking for "something like Log4J" because you want it to be as small and simple as possible, this is most likely what you're looking for.

Links

syslog

syslog was originally developed by Eric Allman as part of sendmail and has become the defacto standard for daemon / server logging in POSIX environments. It is client-server based, usually the daemon that wants something to be logged will send the log data to a syslogd listening on UDP port 514. If you're specifically looking for "something like Log4J" because you actually want to log a daemon or server, this is most likely what you're looking for.

Links

zlog

This one was invented to be like log4c, just - according to its description - smaller and more flexible at the same time.

Links

Miscellaneous

Power vs. Lean

Because of the different way how C links, thinks and works, I would not look for a logging framework which is powerful in a general case - unlike in Java. If you're going for "full-blown desktop applications" and beyond, logging with powerful frameworks like in Java is certainly a good way to go. If you're implementing command line tools or similar, I bet that a lean framework is better - why would you want to depend on lib2xml just for the sake of logging...

Speed

In case speed resp. not wasting cycles matters to you for some reason, look for a logging framework which uses macros to evaluate the log level before the other arguments are evaluated.

The downside is that you cannot call a log routine with arguments that have side-effects. But this shouldn't be a use case anyway. It would be astonishing if log statements were not ignorable because of containing side-effects.

The upside is that log statements in such a framework add so few cycles that they're almost not there - just an access to a global, a check and a conditional branch, skipping the rest of the log code - 2 instructions, 1 cycle in the best case on many of today's CPUs.

Disclaimer

I am the author of sclog4c.