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 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 127 insertions(+) create mode 100644 include/lgr.h (limited to 'include') 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 -- cgit v1.3.1