clog is a single‑header, thread‑safe logging library for C, offering timestamped, context‑rich output with configurable levels and color modes. It works on POSIX and Windows, and requires no dynamic allocations in the hot path.
- Log Levels: TRACE, DEBUG, INFO, WARN, ERROR, FATAL
- Thread Safety: Uses Windows Critical Sections, C11 atomics, or GCC/Clang spin-locks; fallback to no-op in single-threaded builds
- Signal Safety:
Provides a minimal, async-signal-safe logging function
clog_signal_logfor use in signal handlers - Color Modes:
- Auto-detect (ANSI on POSIX TTYs or modern Windows consoles)
- Force ANSI escapes
- Force Windows Console API
- Always/never color
- Contextual Output:
Outputs timestamp, level tag, message, and optional
(file:line in function)location info - Customizable:
clog_set_level(...)to filter by minimum levelclog_set_color_mode(...)to override color behaviorclog_set_output(...)to redirect logs to anyFILE*
- Performance-oriented:
Pre-allocated buffers, no
mallocin log path - Portable: Works on Linux, macOS, Windows (compatible with MSVC, GCC, Clang)
- C compiler supporting C11 (optional)
- POSIX or Win32 API for threading and console handling
git clone https://github.com/0xA1M/clog.git
cd clogThen copy clog.h into your project:
#include "clog.h"INFO("Server started on port %d", port);
WARN("Low disk space: %d%% remaining", percent);
ERROR("Failed to open file: %s", strerror(errno));
DEBUG("Cache hit for key '%s'", key);
TRACE("Entering function %s", __func__);
FATAL("Unexpected null pointer");Set minimum log level (default is INFO):
clog_set_level(CLOG_DEBUG);Control color output (default is CLOG_COLOR_AUTO):
clog_set_color_mode(CLOG_COLOR_NEVER);
clog_set_color_mode(CLOG_COLOR_ALWAYS);
clog_set_color_mode(CLOG_COLOR_ANSI);
clog_set_color_mode(CLOG_COLOR_WIN32);Redirect log output (default is stdout):
FILE *fp = fopen("app.log", "w");
clog_set_output(fp);
// ... logging calls ...
fclose(fp);
⚠️ Note: If you redirect output to a file, you should disable colors usingclog_set_color_mode(CLOG_COLOR_NEVER)as ANSI escape codes may not render properly in files. ✅ TODO: Improve color handling for file outputs.
Cleanup (optional, automatically called via atexit):
clog_cleanup();void clog_set_level(clog_level_t level);
void clog_set_color_mode(clog_color_mode_t mode);
void clog_set_output(FILE *fp);
void clog_cleanup(void);
void clog_signal_log(const char *message); // Async-signal-safe, minimal logging to stderr
enum clog_level_t {
CLOG_TRACE, CLOG_DEBUG, CLOG_INFO,
CLOG_WARN, CLOG_ERROR, CLOG_FATAL
};
enum clog_color_mode_t {
CLOG_COLOR_AUTO, CLOG_COLOR_NEVER,
CLOG_COLOR_ALWAYS, CLOG_COLOR_ANSI,
CLOG_COLOR_WIN32
};All log macros expand to clog_log(...) and automatically capture file, line, and function.
The test suite lives under tests/ and covers:
- Basic functionality and level filtering
- Memory-safety and buffer limits
- Signal-handler safety
- Thread-safety (requires pthreads)
- Performance benchmarks
- Configuration and output redirection
make test✅ clog is tested on Linux (x86_64) with:
- GCC, Clang compilers
- glibc and musl C libraries
- Both multi-threaded and single-threaded builds
- Modern terminal emulators with ANSI support (GNOME Terminal, Alacritty, etc.)
See tests/runner.c for full test coverage and usage examples.
- Fork the repo
- Create your branch:
git checkout -b feature/foo - Commit your changes
- Push:
git push origin feature/foo - Open a Pull Request
✅ Please include tests and make sure all tests pass before submitting.
MIT License. See LICENSE for details.