BlackBox is a fast, minimal logging library for C — inspired by aircraft flight recorders: log everything.
Note
Built and tested exclusively on macOS (POSIX).
Uses pthread, unistd, and Mach-O (_NSGetExecutablePath) APIs.
- Zero external dependencies and extremely easy to integrate
- Pure C
- Color-coded terminal output (TTY / ANSI-aware)
- Runtime log-level control via
LOG_LEVELS=... - Optional file logging (
logs/<timestamp>.log+logs/latest.log) - Optional stderr redirection to a separate error log
- Thread-safe via
pthread_mutex - Custom runtime and compile-time assertions:
ASSERT(expr, ...)BUILD_ASSERT(cond, msg)
git clone https://github.com/abnore/BlackBox.git
cd BlackBoxBlackBox ships with a minimal Makefile that builds a shared library and can optionally install it system-wide.
makeThis creates:
libblackbox.dylib
To use the library locally, include the header from the repo:
#include "blackbox.h"Compile your program with:
clang main.c -L. -lblackbox -o mainWhere:
-L.tells the linker to search the current directory-lblackboxlinks againstlibblackbox.dylib
To remove the locally-built shared library:
make cleanThis removes:
libblackbox.dylib
To install into /usr/local:
make installThis installs:
blackbox.h->/usr/local/include/libblackbox.dylib->/usr/local/lib/
After installation, you can use BlackBox in any C project on your system:
#include <blackbox.h>Compile with:
clang main.c -lblackbox -o mainNo -L. needed — /usr/local/lib is in the default macOS search path.
make uninstallThis removes:
/usr/local/include/blackbox.h/usr/local/lib/libblackbox.dylib
#include <blackbox.h>
int main(void) {
// LOG_DEFAULT will expand into: NO_LOG, LOG_COLORS, STDERR_TO_TERMINAL;
init_log(LOG_DEFAULT);
// Examples written out:
// init_log(LOG, NO_COLORS, STDERR_TO_TERMINAL); // Log to file
// init_log(LOG, NO_COLORS, STDERR_TO_LOG); // Redirect stderr
FATAL("Unrecoverable error (this does NOT abort, only logs it)");
ERROR("Recoverable error; operation failed");
WARN("Non-fatal anomaly");
INFO("General status / progress");
DEBUG("Developer debug information (x=%d)", 42);
TRACE("Verbose internal tracing");
// Example assertion (this WILL abort):
// ASSERT(ptr != NULL, "ptr must not be NULL (id=%d)", id);
shutdown_log();
return 0;
}Note
BlackBox uses pthread mutexes internally.
On macOS, pthreads are provided by libSystem, so -pthread is not required.
log_set_color_output(bool enabled);
log_enable_level(log_level lvl);
log_disable_level(log_level lvl);
log_level_is_enabled(log_level lvl);log_set_color_output(true);
log_enable_level(LOG_LEVEL_TRACE);
log_disable_level(LOG_LEVEL_DEBUG);
if (log_level_is_enabled(LOG_LEVEL_TRACE)) {
TRACE("Trace is enabled!");
}Control verbosity at runtime:
LOG_LEVELS=+DEBUG,-TRACE ./your_app| Token | Meaning |
|---|---|
ALL |
Enable all levels |
NONE |
Disable all levels |
+LEVEL |
Enable a level |
-LEVEL |
Disable a level |
| comma-separated | Multiple operations |
| case-insensitive | debug == DEBUG |
LOG_LEVELS=ALL ./app
LOG_LEVELS=-debug,-trace ./app
LOG_LEVELS=+warn,+error ./app
LOG_LEVELS=NONE,+fatal ./appASSERT(ptr != NULL, "ptr must not be NULL (id=%d)", id);
BUILD_ASSERT(sizeof(Header) == 64, "Header must be 64 bytes");# Example Makefile For BlackBox
CC = clang
LDFLAGS = -lblackbox
SRC = example.c
DIR = bin
OUT = test
BIN = $(DIR)/$(OUT)
LOG_LEVELS ?= ALL
all: $(BIN)
$(BIN): $(SRC)
@mkdir -p $(DIR)
@echo "Building example -> $(BIN)"
@$(CC) $^ $(LDFLAGS) -o $@
@echo
@echo "Now run:"
@echo " - make run "
@echo " - make run-info "
@echo " - make run-debug "
@echo " - make run-fatal "
@echo
@echo "And see without re-compiling, levels are run-time controlled"
run: $(BIN)
@echo "Running with LOG_LEVELS='$(LOG_LEVELS)'"
@LOG_LEVELS="$(LOG_LEVELS)" ./$(BIN)
run-info: ; @$(MAKE) run LOG_LEVELS=+info,+warn
run-debug: ; @$(MAKE) run LOG_LEVELS=+debug,-trace
run-fatal: ; @$(MAKE) run LOG_LEVELS=+fatal
clean:
@echo "Cleaning bin/"
@rm -f $(BIN)
@rmdir $(DIR) 2>/dev/null || true
.PHONY: all run run-info run-debug run-fatal cleanLOG_COLORSenables ANSI colors only whenstdoutis a TTY- File logs never contain color escape codes
log_set_color_output()overrides auto-detection
- Target: macOS
- Depends on:
pthreadunistd.hsys/stat.hlibgen.hmach-o/dyld.h
Not cross-platform as-is. A Linux/Windows port would require replacing Mach-O path handling and parts of the filesystem logic.
BlackBox is released under The Unlicense — dedicated to the public domain.
