Logging in C with support for monoid logging
logging jsonl monoid c
0

Configure Feed

Select the types of activity you want to include in your feed.

C 76.4%
Just 18.9%
Nix 4.7%
Shell 0.1%
Other 0.1%
3 1 0

Clone this repository

https://tangled.org/stau.space/clog https://tangled.org/did:plc:uzmfw4yp4oat526jz4i73dw4
git@tangled.org:stau.space/clog git@tangled.org:did:plc:uzmfw4yp4oat526jz4i73dw4

For self-hosted knots, clone URLs may differ based on your setup.



README.md

clog#

A C logging library with monoid logging and three output formats.

Output formats#

Set globally before any logging calls. Defaults to JSONL.

clog_set_fmt(CLOG_FMT_JSONL);      // default
clog_set_fmt(CLOG_FMT_TEXT);
clog_set_fmt(CLOG_FMT_TEXT_COLOR); // ANSI colors
  • JSONL: one JSON object per line, suitable for log aggregators and jq:
{"ts":"Mon May 18 14:23:01 2026","level":"ERR","file":"server.c","line":42,"func":"handle","msg":"connection refused"}
  • TEXT: human-readable, easy to grep:
server.c:42:handle Mon May 18 14:23:01 2026 ERR connection refused
  • TEXT_COLOR: same as TEXT with ANSI level colors (cyan/white/yellow/bold red for INF/DBG/WRN/ERR).

Levels: CLOG_INF, CLOG_DBG, CLOG_WRN, CLOG_ERR.

Imperative mode#

Each call immediately prints to stdout. Source location is captured automatically.

#include "clog.h"

clog(CLOG_INF, "server started");
clog(CLOG_ERR, "connection refused");

Monoid mode#

Log entries accumulate in a CLog (a dynamic array of ClogEntry). Nothing is printed until the caller decides to flush. Safe to pass through a call chain; the caller that holds the CLog controls when output happens.

#include "clog.h"

void work(CLog *log) {
    clogm(CLOG_INF, *log, "doing work");
    clogm(CLOG_WRN, *log, "something looks odd");
}

int main(void) {
    CLog log = clog_log_init();

    work(&log);

    clog_log_print(log);   // flush everything at once
    clog_log_free(log);
    return 0;
}

CLog is a typed dynamic array (ClogEntry *) backed by dynarr. Entries are value types stored contiguously — plain indexing (log[i]) works.

Building#

just          # static (.build/libclog.a) + dynamic (.build/libclog.so)
just test     # compile and run tests with ASan + UBSan
just clean    # remove .build/

Linking#

Copy include/clog.h, include/dynarr.h, include/prelude.h, and either libclog.a or libclog.so into your project, then compile with -I<include_dir> -L<lib_dir> -lclog.