aboutsummaryrefslogtreecommitdiff
path: root/include/lgr.h
diff options
context:
space:
mode:
authorLibravatar Martin Michalec <martin@michalec.dev>2026-11-06 02:48:49 +0000
committerLibravatar Martin Michalec <martin@michalec.dev>2026-11-06 02:48:49 +0000
commit43a56680a6c5bc52165dbd9bb7609cbf02be1ea2 (patch)
tree9e0542c3231b305b50b18bc18f4898bc8dd89eef /include/lgr.h
parentfeat(ms): add memory span (diff)
downloadcodebase-43a56680a6c5bc52165dbd9bb7609cbf02be1ea2.tar.gz
feat(lgr): add logger
Diffstat (limited to 'include/lgr.h')
-rw-r--r--include/lgr.h127
1 files changed, 127 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
28enum lgr_lvl {
29 LGR_LVL_FATAL,
30 LGR_LVL_ERROR,
31 LGR_LVL_WARNING,
32 LGR_LVL_NOTICE,
33 LGR_LVL_INFO,
34};
35
36struct lgr_hdr {
37 enum lgr_lvl max_lvl;
38};
39
40typedef void (*lgr_fn) (struct lgr_hdr *ctx, enum lgr_lvl, struct mv);
41
42struct 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
58void _lgr_log (struct lgr, enum lgr_lvl, const char *fmt, ...);
59
60#ifndef LGR_NO_FEAT_COMBINATORS
61
62struct lgr_tee {
63 struct lgr_hdr hdr;
64 struct lgr a;
65 struct lgr b;
66};
67
68void 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
80struct lgr_fd {
81 struct lgr_hdr hdr;
82 /* */ int fd;
83};
84
85void 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
91struct lgr_strm {
92 struct lgr_hdr hdr;
93 /* */ FILE *strm;
94};
95
96void 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
112struct lgr_ctx_frame {
113 struct lgr_ctx_frame *next;
114 struct lgr lgr;
115};
116
117extern THREAD_LOCAL struct lgr_ctx_frame *_lgr_ctx_top;
118
119struct lgr_ctx_frame *lgr_ctx_beg (struct lgr);
120struct lgr lgr_ctx_end (void);
121void 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