Derive-C
Loading...
Searching...
No Matches
file.h
Go to the documentation of this file.
1#pragma once
2
6#include <stdarg.h>
7#include <stdio.h>
8
9typedef struct {
10 FILE* stream;
13
21
22#define DC_LOG_ANSI_RESET "\033[0m"
23#define DC_LOG_ANSI_PURPLE "\033[35m"
24#define DC_LOG_ANSI_BLUE "\033[34m"
25#define DC_LOG_ANSI_GREEN "\033[32m"
26#define DC_LOG_ANSI_ORANGE "\033[33m"
27#define DC_LOG_ANSI_RED "\033[31m"
28
30 switch (level) {
31 case DC_TRACE:
32 return DC_LOG_ANSI_PURPLE;
33 case DC_DEBUG:
34 return DC_LOG_ANSI_BLUE;
35 case DC_INFO:
36 return DC_LOG_ANSI_GREEN;
37 case DC_WARN:
38 return DC_LOG_ANSI_ORANGE;
39 case DC_ERROR:
40 return DC_LOG_ANSI_RED;
41 }
42 return DC_LOG_ANSI_RESET;
43}
44
46 if (level < self->filter) {
47 return false;
48 }
49 if (self->parent != NULL) {
50 return dc_log_file_should_log(self->parent, level);
51 }
52 return true;
53}
54
56 return self->filter;
57}
58
60 if (self->parent != NULL) {
62 fprintf(stream, "/");
63 }
64 fprintf(stream, "%s", self->id.name);
65}
66
67DC_PUBLIC static void dc_log_file_log(dc_log_file* self, dc_log_location location,
68 dc_log_level level, const char* const message, ...)
69#if defined(__clang__) || defined(__GNUC__)
70 __attribute__((format(printf, 4, 5)))
71#endif
72 ;
73
75 dc_log_level level, const char* const message, ...) {
76 if (!dc_log_file_should_log(self, level)) {
77 return;
78 }
79
80 if (self->ansi_colours) {
81 fprintf(self->stream, "%s", dc_log_level_ansi_colour(level));
82 }
83
85 fprintf(self->stream, "[");
86 dc_datetime_format(&dt, self->stream);
87 fprintf(self->stream, "] [%s] [", dc_log_level_to_string(level));
89 fprintf(self->stream, "] ");
90
91 va_list args;
92 va_start(args, message);
93 vfprintf(self->stream, message, args);
94 va_end(args);
95
96 if (level <= DC_DEBUG) {
97 fprintf(self->stream, " [%s:%d]", location.file, location.line);
98 }
99
100 if (self->ansi_colours) {
101 fprintf(self->stream, "%s", DC_LOG_ANSI_RESET);
102 }
103
104 fprintf(self->stream, "\n");
105 fflush(self->stream);
106}
107
109 dc_log_level old_level = self->filter;
110 self->filter = level;
111 dc_log_file_log(self, DC_LOCATION, DC_INFO, "Log level changed from %s to %s",
113}
114
116 dc_log_id id) {
117 dc_log_file self = {
118 .parent = NULL,
119 .stream = config.stream,
120 .id = id,
121 .filter = DC_INFO,
122 .ansi_colours = config.ansi_colours,
123 };
124 dc_log_file_log(&self, DC_LOCATION, DC_INFO, "Logger created");
125 return self;
126}
127
129 dc_log_file self = {
130 .parent = parent,
131 .stream = parent->stream,
132 .id = id,
133 .filter = parent->filter,
134 .ansi_colours = parent->ansi_colours,
135 };
136 dc_log_file_log(&self, DC_LOCATION, DC_INFO, "Logger created");
137 return self;
138}
139
141 dc_log_file_log(self, DC_LOCATION, DC_INFO, "Logger deleted");
142}
143
144DC_PUBLIC static void dc_log_file_debug(dc_log_file const* self, dc_debug_fmt fmt, FILE* stream) {
145 fprintf(stream, "dc_log_file@%p {\n", (void const*)self);
146 fmt = dc_debug_fmt_scope_begin(fmt);
147
148 dc_debug_fmt_print(fmt, stream, "parent: ");
149 if (self->parent != NULL) {
150 dc_log_file_debug(self->parent, fmt, stream);
151 } else {
152 fprintf(stream, "NULL");
153 }
154 fprintf(stream, ",\n");
155
156 dc_debug_fmt_print(fmt, stream, "stream: %p,\n", (void const*)self->stream);
157 dc_debug_fmt_print(fmt, stream, "id: \"%s\",\n", self->id.name);
158 dc_debug_fmt_print(fmt, stream, "filter: %s,\n", dc_log_level_to_string(self->filter));
159 dc_debug_fmt_print(fmt, stream, "ansi_colours: %s,\n", self->ansi_colours ? "true" : "false");
160
161 fmt = dc_debug_fmt_scope_end(fmt);
162 dc_debug_fmt_print(fmt, stream, "}");
163}
164
166
167#if !defined DC_LOGGER
168 #define DC_LOGGER dc_log_file
169#endif
#define DC_DEBUG(DEBUG_FN, DEBUG_PTR)
Definition dump.h:92
static DC_PUBLIC dc_log_file dc_log_file_from_parent(dc_log_file *parent, dc_log_id id)
Definition file.h:128
static DC_PUBLIC dc_log_level dc_log_file_get_filter(dc_log_file const *self)
Definition file.h:55
#define DC_LOG_ANSI_ORANGE
Definition file.h:26
static DC_PUBLIC void dc_log_file_debug(dc_log_file const *self, dc_debug_fmt fmt, FILE *stream)
Definition file.h:144
#define DC_LOG_ANSI_RESET
Definition file.h:22
static DC_PUBLIC void dc_log_file_set_filter(dc_log_file *self, dc_log_level level)
Definition file.h:108
static DC_PUBLIC dc_log_file dc_log_file_new_global(dc_log_file_global_config config, dc_log_id id)
Definition file.h:115
#define DC_LOG_ANSI_BLUE
Definition file.h:24
static DC_INTERNAL char const * dc_log_level_ansi_colour(dc_log_level level)
Definition file.h:29
struct _dc_log_file dc_log_file
static DC_PUBLIC void dc_log_file_delete(dc_log_file *self)
Definition file.h:140
#define DC_LOG_ANSI_PURPLE
Definition file.h:23
static DC_INTERNAL void dc_log_file_print_descriptor(dc_log_file const *self, FILE *stream)
Definition file.h:59
#define DC_LOG_ANSI_RED
Definition file.h:27
static DC_PUBLIC void dc_log_file_log(dc_log_file *self, dc_log_location location, dc_log_level level, const char *const message,...)
Definition file.h:74
#define DC_LOG_ANSI_GREEN
Definition file.h:25
static DC_PUBLIC bool dc_log_file_should_log(dc_log_file const *self, dc_log_level level)
Definition file.h:45
static DC_PUBLIC void dc_debug_fmt_print(dc_debug_fmt fmt, FILE *stream, const char *format,...)
Definition fmt.h:32
static DC_PUBLIC dc_debug_fmt dc_debug_fmt_scope_end(dc_debug_fmt fmt)
Definition fmt.h:57
static DC_PUBLIC dc_debug_fmt dc_debug_fmt_scope_begin(dc_debug_fmt fmt)
Definition fmt.h:50
#define DC_PUBLIC
Definition namespace.h:25
#define DC_INTERNAL
Definition namespace.h:30
FILE * stream
Definition file.h:16
bool ansi_colours
Definition file.h:19
dc_log_id id
Definition file.h:17
struct _dc_log_file * parent
Definition file.h:15
dc_log_level filter
Definition file.h:18
Debug format helpers for debug printin data structures.
Definition fmt.h:11
char name[DC_LOG_ID_LENGTH]
Definition trait.h:40
char const * file
Definition trait.h:30
static DC_PUBLIC dc_datetime dc_datetime_now_utc(void)
Definition timestamp.h:49
static DC_PUBLIC void dc_datetime_format(dc_datetime const *dt, FILE *stream)
Definition timestamp.h:66
#define DC_LOCATION
Definition trait.h:34
dc_log_level
Definition trait.h:5
@ DC_INFO
Definition trait.h:8
@ DC_TRACE
Definition trait.h:6
@ DC_ERROR
Definition trait.h:10
@ DC_WARN
Definition trait.h:9
static DC_PUBLIC char const * dc_log_level_to_string(dc_log_level level)
Definition trait.h:13
#define DC_TRAIT_LOGGER(SELF)
Definition trait.h:43
static DC_PUBLIC FILE * stream(SELF *self)
Definition template.h:108