From 43a56680a6c5bc52165dbd9bb7609cbf02be1ea2 Mon Sep 17 00:00:00 2001 From: Martin Michalec Date: Fri, 6 Nov 2026 05:48:49 +0300 Subject: feat(lgr): add logger --- include/lgr.h | 127 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/lgr.c | 119 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 246 insertions(+) create mode 100644 include/lgr.h create mode 100644 src/lgr.c diff --git a/include/lgr.h b/include/lgr.h new file mode 100644 index 0000000..827387d --- /dev/null +++ b/include/lgr.h @@ -0,0 +1,127 @@ +// lgr.h --- logger -*- C -*- +// +// Copyright (c) 2026 Martin Michalec +// +// SPDX-License-Identifier: MIT +// +// This file is part of COSMOS Codebase. +// +// Commentary: +// +// Code: + +#ifndef INCLUDED_COSMOS_LGR_H +#define INCLUDED_COSMOS_LGR_H + +#include "mv.h" + +#ifdef LGR_NO_UNISTD +#define LGR_NO_FEAT_FD +#endif + +#ifndef LGR_NO_STDIO +#include +#else +#define LGR_FEAT_NO_STRM +#endif // LGR_NO_STDIO + +enum lgr_lvl { + LGR_LVL_FATAL, + LGR_LVL_ERROR, + LGR_LVL_WARNING, + LGR_LVL_NOTICE, + LGR_LVL_INFO, +}; + +struct lgr_hdr { + enum lgr_lvl max_lvl; +}; + +typedef void (*lgr_fn) (struct lgr_hdr *ctx, enum lgr_lvl, struct mv); + +struct lgr { + /* */ lgr_fn _fun; + struct lgr_hdr *_ctx; +}; + +#define LGR(fun, ctx) ((struct lgr) { ._fun = fun, ._ctx = &ctx->hdr }) +#define LGR_NULL ((struct lgr) { ._fun = NULL, ._ctx = NULL }) + +#define LGR_LOG(lgr, lvl, fmt, ...) _lgr_log (lgr, lvl, fmt "\n", ##__VA_ARGS__) + +#define LGR_FATAL( lgr, ...) LGR_LOG (lgr, LGR_LVL_FATAL, __VA_ARGS__) +#define LGR_ERROR( lgr, ...) LGR_LOG (lgr, LGR_LVL_ERROR, __VA_ARGS__) +#define LGR_WARNING(lgr, ...) LGR_LOG (lgr, LGR_LVL_WARNING, __VA_ARGS__) +#define LGR_NOTICE( lgr, ...) LGR_LOG (lgr, LGR_LVL_NOTICE, __VA_ARGS__) +#define LGR_INFO( lgr, ...) LGR_LOG (lgr, LGR_LVL_INFO, __VA_ARGS__) + +void _lgr_log (struct lgr, enum lgr_lvl, const char *fmt, ...); + +#ifndef LGR_NO_FEAT_COMBINATORS + +struct lgr_tee { + struct lgr_hdr hdr; + struct lgr a; + struct lgr b; +}; + +void lgr_tee_fun (struct lgr_hdr *ctx, enum lgr_lvl, struct mv); + +#endif // LGR_NO_FEAT_COMBINATORS + +#ifndef LGR_NO_FEAT_DECORATORS + +// TODO(cmmm): add prefix decorator + +#endif // LGR_NO_FEAT_DECORATORS + +#ifndef LGR_NO_FEAT_FD + +struct lgr_fd { + struct lgr_hdr hdr; + /* */ int fd; +}; + +void lgr_fd_fun (struct lgr_hdr *ctx, enum lgr_lvl, struct mv); + +#endif // LGR_NO_FEAT_FD + +#ifndef LGR_NO_FEAT_STRM + +struct lgr_strm { + struct lgr_hdr hdr; + /* */ FILE *strm; +}; + +void lgr_strm_fun (struct lgr_hdr *ctx, enum lgr_lvl, struct mv); + +#endif // LGR_NO_FEAT_STRM + +#ifndef LGR_NO_FEAT_CTX +#include "macros.h" + +#define CTX_LOG(lvl, ...) \ + LGR_LOG (_lgr_ctx_top ? _lgr_ctx_top->lgr : LGR_NULL, lvl, __VA_ARGS__) + +#define CTX_FATAL( ...) CTX_LOG (LGR_LVL_FATAL, __VA_ARGS__) +#define CTX_ERROR( ...) CTX_LOG (LGR_LVL_ERROR, __VA_ARGS__) +#define CTX_WARNING(...) CTX_LOG (LGR_LVL_WARNING, __VA_ARGS__) +#define CTX_NOTICE( ...) CTX_LOG (LGR_LVL_NOTICE, __VA_ARGS__) +#define CTX_INFO( ...) CTX_LOG (LGR_LVL_INFO, __VA_ARGS__) + +struct lgr_ctx_frame { + struct lgr_ctx_frame *next; + struct lgr lgr; +}; + +extern THREAD_LOCAL struct lgr_ctx_frame *_lgr_ctx_top; + +struct lgr_ctx_frame *lgr_ctx_beg (struct lgr); +struct lgr lgr_ctx_end (void); +void lgr_ctx_rollback (struct lgr_ctx_frame *); + +#endif // LGR_NO_FEAT_CTX + +#endif // INCLUDED_COSMOS_LGR_H + +// lgr.h ends here diff --git a/src/lgr.c b/src/lgr.c new file mode 100644 index 0000000..4fa8721 --- /dev/null +++ b/src/lgr.c @@ -0,0 +1,119 @@ +// lgr.c --- logger -*- C -*- +// +// Copyright (c) 2026 Martin Michalec +// +// SPDX-License-Identifier: MIT +// +// This file is part of COSMOS Codebase. +// +// Commentary: +// +// Code: + +#include "lgr.h" +#include "dbg.h" +#include "arena.h" +#include "sb.h" + +#ifndef LGR_NO_UNISTD +#include +#endif // LGR_NO_UNISTD + +#include + +void +_lgr_log (struct lgr lgr, enum lgr_lvl lvl, const char *fmt, ...) +{ + if (lgr._ctx && lvl > lgr._ctx->max_lvl) return; + + va_list ap; + + if (lgr._fun == NULL) { + #ifndef LGR_NO_FEAT_CTX + if (_lgr_ctx_top) { + lgr = _lgr_ctx_top->lgr; + } else + #endif // LGR_NO_FEAT_CTX + #ifndef LGR_NO_FEAT_STDIO + if (1) { + va_start (ap, fmt); + vfprintf (stderr, fmt, ap); + va_end (ap); + return; + } else + #endif // LGR_NO_FEAT_STDIO + return; + } + + // HOLD(cmmm): redo directly with arena support... + struct sb sb = { .hdr.gpa = GPA_SCR }; + va_start (ap, fmt); + sb_vprintf (&sb, fmt, ap); + va_end (ap); + + lgr._fun (lgr._ctx, lvl, ms_mv (sb_ms (sb))); + DA_DROP (&sb); +} + +#ifndef LGR_NO_FEAT_DECORATORS + +#endif // LGR_NO_FEAT_DECORATORS + +#ifndef LGR_NO_FEAT_FD + +void +lgr_fd_fun (struct lgr_hdr *ctx, enum lgr_lvl lvl, struct mv mv) +{ + UNUSED (ctx); + UNUSED (lvl); + UNUSED (mv); + DBG_TODO ("implement"); +} + +#endif // LGR_NO_FEAT_FD + +#ifndef LGR_NO_FEAT_STRM + +void +lgr_strm_fun (struct lgr_hdr *_ctx, enum lgr_lvl lvl, struct mv mv) +{ + struct lgr_strm *ctx = (void *) _ctx; + fwrite (mv.beg, mv_sz (mv), 1, ctx->strm); +} + +#endif // LGR_NO_FEAT_STRM + +#ifndef LGR_NO_FEAT_CTX + +THREAD_LOCAL struct lgr_ctx_frame *_lgr_ctx_top = NULL; + +struct lgr_ctx_frame * +lgr_ctx_beg (struct lgr lgr) +{ + struct lgr_ctx_frame *new = SCR_GRAB_T (*new); + new->lgr = lgr; + new->next = _lgr_ctx_top; + _lgr_ctx_top = new; + return new; +} + +struct lgr +lgr_ctx_end (void) +{ + struct lgr lgr = _lgr_ctx_top->lgr; + _lgr_ctx_top = _lgr_ctx_top->next; + return lgr; +} + +void +lgr_ctx_rollback (struct lgr_ctx_frame *to) +{ + while (_lgr_ctx_top != to) { + DBG_ASSERT_NN (_lgr_ctx_top); + _lgr_ctx_top = _lgr_ctx_top->next; + } +} + +#endif // LGR_NO_FEAT_CTX + +// lgr.c ends here -- cgit v1.3.1