How hardysimpson/zlog works
Project Type and Purpose
• zlog is an open‑source, pure C logging library providing high‑performance, thread‑safe, flexible logging for C applications. It offers a log4j‑like model (categories, formats, rules) without external dependencies beyond POSIX (and optional Win32 support). The project includes core library code (src/), configuration parsing, platform abstraction, utility data structures, build scripts, packaging (cpack/), documentation (doc/), tests (test/), and helper tools (tools/).
File Structure Analysis
• Top‐level: BUILD.bazel, CMakeLists.txt, Makefile, README.md, LICENSE, INSTALL, TODO, Changelog
• Configuration/build: cmake/, cpack/, zlog.spec
• Core library: src/ (modules for configuration, categories, rules, formatting, buffering, rotation, threading, platform)
• Utilities: src/zc_*.c, zc_arraylist, zc_hashtable, zc_util, zc_profile, zc_xplatform
• Tests: test/ (unit tests, fuzzers, config scenarios)
• Tools: tools/mk_targz.sh
• Documentation: doc/ (User’s guide, GettingStarted, examples)
Core Components and Their Interactions
a. zlog API Layer
• zlog_init(cfg_path)
• zlog_get_category(name) → zlog_category_t*
• zlog_<level>(category, fmt, …)
• zlog_fini()
b. Configuration Module
• conf.c / conf.h: parse zlog.conf into in‑memory structures: formats, rules, categories
c. Category & Rule Management
• category.c / category_table.c: manage zlog_category_t instances
• rule.c / record_table.c: match category+level to output destinations and formats
d. Formatting Engine
• format.c: parse format strings (e.g., “%m%n”) and apply to log events
e. Event / Record Pipeline
• event.c / record.c: represent a log invocation as an event record
• zlog_info/debug → event → format → record
f. Output Modules (Appenders)
• stdout/stderr, file (with rotater.c for size/time rotation), syslog, user-defined callback
g. Concurrency & Safety
• thread.c: thread‑local data (MDC support)
• lockfile.c: file locking for process‑safe rotation
• zc_xplatform.c / zlog_win.c: OS abstraction (POSIX vs Windows)
h. Utilities
• zc_arraylist, zc_hashtable: in‑memory data structures
• zc_profile: performance counters
• zc_util: miscellaneous helpers
i. Build & Packaging
• CMake/Bazel/Make: build configurations, toolchain support
• cpack/: packaging scripts for RPM/DEB
• zlog.spec: spec file for RPM build
j. Testing & Validation
• test/: unit tests covering each module, fuzzers for init, test harness scripts
Architectural Patterns & Design Principles
• Modular pipeline architecture: API → Config → Event → Format → Output
• Separation of concerns: parsing vs storage vs formatting vs output vs rotation
• Plugin‑like output destinations: each appender implements a common interface
• Data structures abstracted into zc_* utilities for reuse
• Thread‑safe and process‑safe design (locks, thread‑local contexts)
• Platform abstraction layer for cross‑OS support