rxi/log.c
FreeA simple logging library implemented in C99
FreeFree tier
About rxi/log.c
log.c is a minimal logging library implemented in C99, designed to be easily dropped into any C project. It provides six macro-based log levels (TRACE, DEBUG, INFO, WARN, ERROR, FATAL) that output formatted messages to stderr, with optional quiet mode, file output, callback hooks, and thread locking support. Compiled with -DLOG_USE_COLOR, it adds ANSI color escape codes. Released under the MIT license, it is ideal for small to medium C projects needing simple, lightweight logging without external dependencies.
Key Features
Six function-like macros for logging: log_trace, log_debug, log_info, log_warn, log_error, log_fatal
Uses printf-style format strings for log messages
Quiet mode via log_set_quiet() — suppresses stderr output while still writing to files and callbacks
Configurable logging level with log_set_level() — ignores messages below the set level
File output via log_add_fp() — writes logs to one or more file pointers with level filtering
Callback support with log_add_callback() — provides a log_Event structure to custom handler functions
Thread safety via log_set_lock() — set a lock function to synchronize access from multiple threads
ANSI color output when compiled with -DLOG_USE_COLOR
Pros & Cons
Pros
- Extremely lightweight and simple to integrate — just drop in two files
- Clean API with sensible defaults (all levels enabled, output to stderr)
- Supports multiple output targets: stderr, files, and custom callbacks
- Thread safety through optional lock function
- MIT license allows unrestricted use in commercial and open-source projects
Cons
- No built-in support for asynchronous logging or log rotation
- Limited to C projects; no C++ or Python wrappers included
- Does not provide advanced features like JSON formatting, remote logging, or filtering by module
Best For
Adding logging to small to medium C projectsEmbedded systems and resource-constrained environmentsLearning or prototyping logging patterns in CReplacing printf debugging with structured log levels
FAQ
How do I use log.c in my project?
Simply drop `log.c` and `log.h` into your project, compile them alongside your other source files, and use the provided macros (e.g., `log_info("Hello %s", "world")`).
What log levels are available?
Six levels in order of severity: TRACE, DEBUG, INFO, WARN, ERROR, FATAL.
Can I write logs to a file instead of stderr?
Yes, use `log_add_fp(FILE *fp, int level)` to add one or more file pointers. Any messages below the given level are ignored.
Is log.c thread-safe?
It provides `log_set_lock(log_LockFn fn, void *udata)` to set a lock function for multi-threaded use. Without a lock, concurrent calls may interleave output.
What license is log.c under?
MIT license — free to use, modify, and distribute, with no warranty.