Derive-C
Loading...
Searching...
No Matches
utils/log.c

Examples for using loggers.

Examples for using loggers.

#define ITEM dc_log_file
#define ITEM_DELETE dc_log_file_delete
#define NAME logger_vec
static void example_1(dc_log_file* parent) {
DC_SCOPED(dc_log_file) demo = DC_LOGGER_NEW(parent, "%s", "demo");
DC_SCOPED(logger_vec) loggers = logger_vec_new(stdalloc_get_ref());
for (int i = 1; i <= 5; i++) {
dc_log_file logger = DC_LOGGER_NEW(&demo, "logger_%d", i);
logger_vec_push(&loggers, logger);
}
DC_FOR(logger_vec, &loggers, iter, logger) { DC_LOG(*logger, DC_INFO, "hello world"); }
}
static void example_2(dc_log_file* parent) {
DC_SCOPED(dc_log_file) foo = DC_LOGGER_NEW(parent, "%s", "foo");
DC_LOG(foo, DC_DEBUG, "this debug message is visible");
DC_LOG(foo, DC_DEBUG, "this debug message is filtered out");
DC_LOG(foo, DC_INFO, "this info message is visible");
}
static void example_3(dc_log_file* parent) {
DC_SCOPED(dc_log_file) level1 = DC_LOGGER_NEW(parent, "%s", "level1");
DC_SCOPED(dc_log_file) level2 = DC_LOGGER_NEW(&level1, "%s", "level2");
DC_SCOPED(dc_log_file) level3 = DC_LOGGER_NEW(&level2, "%s", "level3");
DC_SCOPED(dc_log_file) level4 = DC_LOGGER_NEW(&level3, "%s", "level4");
DC_LOG(level4, DC_INFO, "logger debug info:\n%s", DC_DEBUG(dc_log_file_debug, &level4));
}
static void example_4(dc_log_file* parent) {
DC_SCOPED(dc_log_file) levels = DC_LOGGER_NEW(parent, "%s", "levels");
DC_LOG(levels, DC_TRACE, "this is a trace message");
DC_LOG(levels, DC_DEBUG, "this is a debug message");
DC_LOG(levels, DC_INFO, "this is an info message");
DC_LOG(levels, DC_WARN, "this is a warning message");
DC_LOG(levels, DC_ERROR, "this is an error message");
}
int main() {
root = NS(DC_LOGGER,
new_global)((NS(DC_LOGGER, global_config)){.stream = stdout, .ansi_colours = true},
(dc_log_id){"root"});
example_1(&root);
example_2(&root);
example_3(&root);
example_4(&root);
return 0;
}
int main()
Definition alloc.c:120
#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
#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