diff options
| author | 2026-11-06 02:48:49 +0000 | |
|---|---|---|
| committer | 2026-11-06 02:48:49 +0000 | |
| commit | 43a56680a6c5bc52165dbd9bb7609cbf02be1ea2 (patch) | |
| tree | 9e0542c3231b305b50b18bc18f4898bc8dd89eef | |
| parent | feat(ms): add memory span (diff) | |
| download | codebase-43a56680a6c5bc52165dbd9bb7609cbf02be1ea2.tar.gz | |
feat(lgr): add logger
| -rw-r--r-- | include/lgr.h | 127 | ||||
| -rw-r--r-- | src/lgr.c | 119 |
2 files changed, 246 insertions, 0 deletions
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 @@ | |||
| 1 | // lgr.h --- logger -*- C -*- | ||
| 2 | // | ||
| 3 | // Copyright (c) 2026 Martin Michalec <martin@michalec.dev> | ||
| 4 | // | ||
| 5 | // SPDX-License-Identifier: MIT | ||
| 6 | // | ||
| 7 | // This file is part of COSMOS Codebase. | ||
| 8 | // | ||
| 9 | // Commentary: | ||
| 10 | // | ||
| 11 | // Code: | ||
| 12 | |||
| 13 | #ifndef INCLUDED_COSMOS_LGR_H | ||
| 14 | #define INCLUDED_COSMOS_LGR_H | ||
| 15 | |||
| 16 | #include "mv.h" | ||
| 17 | |||
| 18 | #ifdef LGR_NO_UNISTD | ||
| 19 | #define LGR_NO_FEAT_FD | ||
| 20 | #endif | ||
| 21 | |||
| 22 | #ifndef LGR_NO_STDIO | ||
| 23 | #include <stdio.h> | ||
| 24 | #else | ||
| 25 | #define LGR_FEAT_NO_STRM | ||
| 26 | #endif // LGR_NO_STDIO | ||
| 27 | |||
| 28 | enum lgr_lvl { | ||
| 29 | LGR_LVL_FATAL, | ||
| 30 | LGR_LVL_ERROR, | ||
| 31 | LGR_LVL_WARNING, | ||
| 32 | LGR_LVL_NOTICE, | ||
| 33 | LGR_LVL_INFO, | ||
| 34 | }; | ||
| 35 | |||
| 36 | struct lgr_hdr { | ||
| 37 | enum lgr_lvl max_lvl; | ||
| 38 | }; | ||
| 39 | |||
| 40 | typedef void (*lgr_fn) (struct lgr_hdr *ctx, enum lgr_lvl, struct mv); | ||
| 41 | |||
| 42 | struct lgr { | ||
| 43 | /* */ lgr_fn _fun; | ||
| 44 | struct lgr_hdr *_ctx; | ||
| 45 | }; | ||
| 46 | |||
| 47 | #define LGR(fun, ctx) ((struct lgr) { ._fun = fun, ._ctx = &ctx->hdr }) | ||
| 48 | #define LGR_NULL ((struct lgr) { ._fun = NULL, ._ctx = NULL }) | ||
| 49 | |||
| 50 | #define LGR_LOG(lgr, lvl, fmt, ...) _lgr_log (lgr, lvl, fmt "\n", ##__VA_ARGS__) | ||
| 51 | |||
| 52 | #define LGR_FATAL( lgr, ...) LGR_LOG (lgr, LGR_LVL_FATAL, __VA_ARGS__) | ||
| 53 | #define LGR_ERROR( lgr, ...) LGR_LOG (lgr, LGR_LVL_ERROR, __VA_ARGS__) | ||
| 54 | #define LGR_WARNING(lgr, ...) LGR_LOG (lgr, LGR_LVL_WARNING, __VA_ARGS__) | ||
| 55 | #define LGR_NOTICE( lgr, ...) LGR_LOG (lgr, LGR_LVL_NOTICE, __VA_ARGS__) | ||
| 56 | #define LGR_INFO( lgr, ...) LGR_LOG (lgr, LGR_LVL_INFO, __VA_ARGS__) | ||
| 57 | |||
| 58 | void _lgr_log (struct lgr, enum lgr_lvl, const char *fmt, ...); | ||
| 59 | |||
| 60 | #ifndef LGR_NO_FEAT_COMBINATORS | ||
| 61 | |||
| 62 | struct lgr_tee { | ||
| 63 | struct lgr_hdr hdr; | ||
| 64 | struct lgr a; | ||
| 65 | struct lgr b; | ||
| 66 | }; | ||
| 67 | |||
| 68 | void lgr_tee_fun (struct lgr_hdr *ctx, enum lgr_lvl, struct mv); | ||
| 69 | |||
| 70 | #endif // LGR_NO_FEAT_COMBINATORS | ||
| 71 | |||
| 72 | #ifndef LGR_NO_FEAT_DECORATORS | ||
| 73 | |||
| 74 | // TODO(cmmm): add prefix decorator | ||
| 75 | |||
| 76 | #endif // LGR_NO_FEAT_DECORATORS | ||
| 77 | |||
| 78 | #ifndef LGR_NO_FEAT_FD | ||
| 79 | |||
| 80 | struct lgr_fd { | ||
| 81 | struct lgr_hdr hdr; | ||
| 82 | /* */ int fd; | ||
| 83 | }; | ||
| 84 | |||
| 85 | void lgr_fd_fun (struct lgr_hdr *ctx, enum lgr_lvl, struct mv); | ||
| 86 | |||
| 87 | #endif // LGR_NO_FEAT_FD | ||
| 88 | |||
| 89 | #ifndef LGR_NO_FEAT_STRM | ||
| 90 | |||
| 91 | struct lgr_strm { | ||
| 92 | struct lgr_hdr hdr; | ||
| 93 | /* */ FILE *strm; | ||
| 94 | }; | ||
| 95 | |||
| 96 | void lgr_strm_fun (struct lgr_hdr *ctx, enum lgr_lvl, struct mv); | ||
| 97 | |||
| 98 | #endif // LGR_NO_FEAT_STRM | ||
| 99 | |||
| 100 | #ifndef LGR_NO_FEAT_CTX | ||
| 101 | #include "macros.h" | ||
| 102 | |||
| 103 | #define CTX_LOG(lvl, ...) \ | ||
| 104 | LGR_LOG (_lgr_ctx_top ? _lgr_ctx_top->lgr : LGR_NULL, lvl, __VA_ARGS__) | ||
| 105 | |||
| 106 | #define CTX_FATAL( ...) CTX_LOG (LGR_LVL_FATAL, __VA_ARGS__) | ||
| 107 | #define CTX_ERROR( ...) CTX_LOG (LGR_LVL_ERROR, __VA_ARGS__) | ||
| 108 | #define CTX_WARNING(...) CTX_LOG (LGR_LVL_WARNING, __VA_ARGS__) | ||
| 109 | #define CTX_NOTICE( ...) CTX_LOG (LGR_LVL_NOTICE, __VA_ARGS__) | ||
| 110 | #define CTX_INFO( ...) CTX_LOG (LGR_LVL_INFO, __VA_ARGS__) | ||
| 111 | |||
| 112 | struct lgr_ctx_frame { | ||
| 113 | struct lgr_ctx_frame *next; | ||
| 114 | struct lgr lgr; | ||
| 115 | }; | ||
| 116 | |||
| 117 | extern THREAD_LOCAL struct lgr_ctx_frame *_lgr_ctx_top; | ||
| 118 | |||
| 119 | struct lgr_ctx_frame *lgr_ctx_beg (struct lgr); | ||
| 120 | struct lgr lgr_ctx_end (void); | ||
| 121 | void lgr_ctx_rollback (struct lgr_ctx_frame *); | ||
| 122 | |||
| 123 | #endif // LGR_NO_FEAT_CTX | ||
| 124 | |||
| 125 | #endif // INCLUDED_COSMOS_LGR_H | ||
| 126 | |||
| 127 | // 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 @@ | |||
| 1 | // lgr.c --- logger -*- C -*- | ||
| 2 | // | ||
| 3 | // Copyright (c) 2026 Martin Michalec <martin@michalec.dev> | ||
| 4 | // | ||
| 5 | // SPDX-License-Identifier: MIT | ||
| 6 | // | ||
| 7 | // This file is part of COSMOS Codebase. | ||
| 8 | // | ||
| 9 | // Commentary: | ||
| 10 | // | ||
| 11 | // Code: | ||
| 12 | |||
| 13 | #include "lgr.h" | ||
| 14 | #include "dbg.h" | ||
| 15 | #include "arena.h" | ||
| 16 | #include "sb.h" | ||
| 17 | |||
| 18 | #ifndef LGR_NO_UNISTD | ||
| 19 | #include <unistd.h> | ||
| 20 | #endif // LGR_NO_UNISTD | ||
| 21 | |||
| 22 | #include <stdarg.h> | ||
| 23 | |||
| 24 | void | ||
| 25 | _lgr_log (struct lgr lgr, enum lgr_lvl lvl, const char *fmt, ...) | ||
| 26 | { | ||
| 27 | if (lgr._ctx && lvl > lgr._ctx->max_lvl) return; | ||
| 28 | |||
| 29 | va_list ap; | ||
| 30 | |||
| 31 | if (lgr._fun == NULL) { | ||
| 32 | #ifndef LGR_NO_FEAT_CTX | ||
| 33 | if (_lgr_ctx_top) { | ||
| 34 | lgr = _lgr_ctx_top->lgr; | ||
| 35 | } else | ||
| 36 | #endif // LGR_NO_FEAT_CTX | ||
| 37 | #ifndef LGR_NO_FEAT_STDIO | ||
| 38 | if (1) { | ||
| 39 | va_start (ap, fmt); | ||
| 40 | vfprintf (stderr, fmt, ap); | ||
| 41 | va_end (ap); | ||
| 42 | return; | ||
| 43 | } else | ||
| 44 | #endif // LGR_NO_FEAT_STDIO | ||
| 45 | return; | ||
| 46 | } | ||
| 47 | |||
| 48 | // HOLD(cmmm): redo directly with arena support... | ||
| 49 | struct sb sb = { .hdr.gpa = GPA_SCR }; | ||
| 50 | va_start (ap, fmt); | ||
| 51 | sb_vprintf (&sb, fmt, ap); | ||
| 52 | va_end (ap); | ||
| 53 | |||
| 54 | lgr._fun (lgr._ctx, lvl, ms_mv (sb_ms (sb))); | ||
| 55 | DA_DROP (&sb); | ||
| 56 | } | ||
| 57 | |||
| 58 | #ifndef LGR_NO_FEAT_DECORATORS | ||
| 59 | |||
| 60 | #endif // LGR_NO_FEAT_DECORATORS | ||
| 61 | |||
| 62 | #ifndef LGR_NO_FEAT_FD | ||
| 63 | |||
| 64 | void | ||
| 65 | lgr_fd_fun (struct lgr_hdr *ctx, enum lgr_lvl lvl, struct mv mv) | ||
| 66 | { | ||
| 67 | UNUSED (ctx); | ||
| 68 | UNUSED (lvl); | ||
| 69 | UNUSED (mv); | ||
| 70 | DBG_TODO ("implement"); | ||
| 71 | } | ||
| 72 | |||
| 73 | #endif // LGR_NO_FEAT_FD | ||
| 74 | |||
| 75 | #ifndef LGR_NO_FEAT_STRM | ||
| 76 | |||
| 77 | void | ||
| 78 | lgr_strm_fun (struct lgr_hdr *_ctx, enum lgr_lvl lvl, struct mv mv) | ||
| 79 | { | ||
| 80 | struct lgr_strm *ctx = (void *) _ctx; | ||
| 81 | fwrite (mv.beg, mv_sz (mv), 1, ctx->strm); | ||
| 82 | } | ||
| 83 | |||
| 84 | #endif // LGR_NO_FEAT_STRM | ||
| 85 | |||
| 86 | #ifndef LGR_NO_FEAT_CTX | ||
| 87 | |||
| 88 | THREAD_LOCAL struct lgr_ctx_frame *_lgr_ctx_top = NULL; | ||
| 89 | |||
| 90 | struct lgr_ctx_frame * | ||
| 91 | lgr_ctx_beg (struct lgr lgr) | ||
| 92 | { | ||
| 93 | struct lgr_ctx_frame *new = SCR_GRAB_T (*new); | ||
| 94 | new->lgr = lgr; | ||
| 95 | new->next = _lgr_ctx_top; | ||
| 96 | _lgr_ctx_top = new; | ||
| 97 | return new; | ||
| 98 | } | ||
| 99 | |||
| 100 | struct lgr | ||
| 101 | lgr_ctx_end (void) | ||
| 102 | { | ||
| 103 | struct lgr lgr = _lgr_ctx_top->lgr; | ||
| 104 | _lgr_ctx_top = _lgr_ctx_top->next; | ||
| 105 | return lgr; | ||
| 106 | } | ||
| 107 | |||
| 108 | void | ||
| 109 | lgr_ctx_rollback (struct lgr_ctx_frame *to) | ||
| 110 | { | ||
| 111 | while (_lgr_ctx_top != to) { | ||
| 112 | DBG_ASSERT_NN (_lgr_ctx_top); | ||
| 113 | _lgr_ctx_top = _lgr_ctx_top->next; | ||
| 114 | } | ||
| 115 | } | ||
| 116 | |||
| 117 | #endif // LGR_NO_FEAT_CTX | ||
| 118 | |||
| 119 | // lgr.c ends here | ||
