Derive-C
Loading...
Searching...
No Matches
log.c
Go to the documentation of this file.
1
4
6#include <derive-c/prelude.h>
8
9#define ITEM dc_log_file
10#define ITEM_DELETE dc_log_file_delete
11#define NAME logger_vec
13
14static void example_1(dc_log_file* parent) {
15 DC_SCOPED(dc_log_file) demo = DC_LOGGER_NEW(parent, "%s", "demo");
16
17 DC_SCOPED(logger_vec) loggers = logger_vec_new(stdalloc_get_ref());
18
19 for (int i = 1; i <= 5; i++) {
20 dc_log_file logger = DC_LOGGER_NEW(&demo, "logger_%d", i);
21 logger_vec_push(&loggers, logger);
22 }
23
24 DC_FOR(logger_vec, &loggers, iter, logger) { DC_LOG(*logger, DC_INFO, "hello world"); }
25}
26
27static void example_2(dc_log_file* parent) {
28 DC_SCOPED(dc_log_file) foo = DC_LOGGER_NEW(parent, "%s", "foo");
29
31 DC_LOG(foo, DC_DEBUG, "this debug message is visible");
32
34 DC_LOG(foo, DC_DEBUG, "this debug message is filtered out");
35 DC_LOG(foo, DC_INFO, "this info message is visible");
36}
37
38static void example_3(dc_log_file* parent) {
39 DC_SCOPED(dc_log_file) level1 = DC_LOGGER_NEW(parent, "%s", "level1");
40 DC_SCOPED(dc_log_file) level2 = DC_LOGGER_NEW(&level1, "%s", "level2");
41 DC_SCOPED(dc_log_file) level3 = DC_LOGGER_NEW(&level2, "%s", "level3");
42 DC_SCOPED(dc_log_file) level4 = DC_LOGGER_NEW(&level3, "%s", "level4");
43
44 DC_LOG(level4, DC_INFO, "logger debug info:\n%s", DC_DEBUG(dc_log_file_debug, &level4));
45}
46
47static void example_4(dc_log_file* parent) {
48 DC_SCOPED(dc_log_file) levels = DC_LOGGER_NEW(parent, "%s", "levels");
49
51
52 DC_LOG(levels, DC_TRACE, "this is a trace message");
53 DC_LOG(levels, DC_DEBUG, "this is a debug message");
54 DC_LOG(levels, DC_INFO, "this is an info message");
55 DC_LOG(levels, DC_WARN, "this is a warning message");
56 DC_LOG(levels, DC_ERROR, "this is an error message");
57}
58
59int main() {
61 root = NS(DC_LOGGER,
62 new_global)((NS(DC_LOGGER, global_config)){.stream = stdout, .ansi_colours = true},
63 (dc_log_id){"root"});
65
66 example_1(&root);
67 example_2(&root);
68 example_3(&root);
69 example_4(&root);
70 return 0;
71}
#define DC_DEBUG(DEBUG_FN, DEBUG_PTR)
Definition dump.h:92
#define DC_LOGGER
Definition file.h:168
static DC_PUBLIC void dc_log_file_debug(dc_log_file const *self, dc_debug_fmt fmt, FILE *stream)
Definition file.h:144
static DC_PUBLIC void dc_log_file_set_filter(dc_log_file *self, dc_log_level level)
Definition file.h:108
struct _dc_log_file dc_log_file
#define DC_FOR(TYPE, INSTANCE, ITER, ITEM)
Definition for.h:13
static void example_1(dc_log_file *parent)
Definition log.c:14
static void example_4(dc_log_file *parent)
Definition log.c:47
static void example_2(dc_log_file *parent)
Definition log.c:27
static void example_3(dc_log_file *parent)
Definition log.c:38
int main()
Definition log.c:59
#define NS(pre, post)
Definition namespace.h:14
#define DC_SCOPED(type,...)
RAII in C. Call the destructor when the variable goes out of scope.
Definition scope.h:5
#define DC_LOGGER_NEW(...)
Definition prelude.h:21
#define DC_LOG(...)
Definition prelude.h:20
@ DC_INFO
Definition trait.h:8
@ DC_TRACE
Definition trait.h:6
@ DC_ERROR
Definition trait.h:10
@ DC_WARN
Definition trait.h:9