aboutsummaryrefslogtreecommitdiff
path: root/include/lgr.h
blob: 827387da34e3cc5217a07e46abb9c0c5c9745cd2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// lgr.h --- logger                                                    -*- C -*-
//
// Copyright (c) 2026 Martin Michalec <martin@michalec.dev>
//
// 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 <stdio.h>
#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