diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/lgr.c | 119 |
1 files changed, 119 insertions, 0 deletions
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 | ||
