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.
相关问题
- Multiple sockets for clients to connect to
- What is the best way to do a search in a large fil
- glDrawElements only draws half a quad
- Index of single bit in long integer (in C) [duplic
- Equivalent of std::pair in C
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.
Another option to consider is zf_log:
For example, that will output INFO log message:
Which will look like:
Exact representation is configurable and depends on build type (debug / release).
There is a log4c C library that mimics the log4j Java library. From the log4c documentation: