Derive-C
Loading...
Searching...
No Matches
file.h File Reference
#include <derive-c/core/prelude.h>
#include <derive-c/utils/log/trait.h>
#include <derive-c/utils/timestamp.h>
#include <stdarg.h>
#include <stdio.h>

Go to the source code of this file.

Data Structures

struct  dc_log_file_global_config
struct  _dc_log_file

Macros

#define DC_LOG_ANSI_RESET   "\033[0m"
#define DC_LOG_ANSI_PURPLE   "\033[35m"
#define DC_LOG_ANSI_BLUE   "\033[34m"
#define DC_LOG_ANSI_GREEN   "\033[32m"
#define DC_LOG_ANSI_ORANGE   "\033[33m"
#define DC_LOG_ANSI_RED   "\033[31m"
#define DC_LOGGER   dc_log_file

Typedefs

typedef struct _dc_log_file dc_log_file

Functions

static DC_INTERNAL char const * dc_log_level_ansi_colour (dc_log_level level)
static DC_PUBLIC bool dc_log_file_should_log (dc_log_file const *self, dc_log_level level)
static DC_PUBLIC dc_log_level dc_log_file_get_filter (dc_log_file const *self)
static DC_INTERNAL void dc_log_file_print_descriptor (dc_log_file const *self, FILE *stream)
static DC_PUBLIC void dc_log_file_log (dc_log_file *self, dc_log_location location, dc_log_level level, const char *const message,...)
static DC_PUBLIC void dc_log_file_set_filter (dc_log_file *self, dc_log_level level)
static DC_PUBLIC dc_log_file dc_log_file_new_global (dc_log_file_global_config config, dc_log_id id)
static DC_PUBLIC dc_log_file dc_log_file_from_parent (dc_log_file *parent, dc_log_id id)
static DC_PUBLIC void dc_log_file_delete (dc_log_file *self)
static DC_PUBLIC void dc_log_file_debug (dc_log_file const *self, dc_debug_fmt fmt, FILE *stream)
 DC_TRAIT_LOGGER (dc_log_file)

Macro Definition Documentation

◆ DC_LOG_ANSI_BLUE

#define DC_LOG_ANSI_BLUE   "\033[34m"

Definition at line 24 of file file.h.

◆ DC_LOG_ANSI_GREEN

#define DC_LOG_ANSI_GREEN   "\033[32m"

Definition at line 25 of file file.h.

◆ DC_LOG_ANSI_ORANGE

#define DC_LOG_ANSI_ORANGE   "\033[33m"

Definition at line 26 of file file.h.

◆ DC_LOG_ANSI_PURPLE

#define DC_LOG_ANSI_PURPLE   "\033[35m"

Definition at line 23 of file file.h.

◆ DC_LOG_ANSI_RED

#define DC_LOG_ANSI_RED   "\033[31m"

Definition at line 27 of file file.h.

◆ DC_LOG_ANSI_RESET

#define DC_LOG_ANSI_RESET   "\033[0m"

Definition at line 22 of file file.h.

◆ DC_LOGGER

Typedef Documentation

◆ dc_log_file

typedef struct _dc_log_file dc_log_file
Examples
utils/log.c.

Function Documentation

◆ dc_log_file_debug()

DC_PUBLIC void dc_log_file_debug ( dc_log_file const * self,
dc_debug_fmt fmt,
FILE * stream )
static
Examples
utils/log.c.

Definition at line 144 of file file.h.

144 {
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}
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_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
static DC_PUBLIC char const * dc_log_level_to_string(dc_log_level level)
Definition trait.h:13
static DC_PUBLIC FILE * stream(SELF *self)
Definition template.h:108

◆ dc_log_file_delete()

DC_PUBLIC void dc_log_file_delete ( dc_log_file * self)
static

Definition at line 140 of file file.h.

140 {
141 dc_log_file_log(self, DC_LOCATION, DC_INFO, "Logger deleted");
142}
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_LOCATION
Definition trait.h:34
@ DC_INFO
Definition trait.h:8

◆ dc_log_file_from_parent()

DC_PUBLIC dc_log_file dc_log_file_from_parent ( dc_log_file * parent,
dc_log_id id )
static

Definition at line 128 of file file.h.

128 {
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}
struct _dc_log_file dc_log_file
FILE * stream
Definition file.h:16
bool ansi_colours
Definition file.h:19
dc_log_level filter
Definition file.h:18

◆ dc_log_file_get_filter()

DC_PUBLIC dc_log_level dc_log_file_get_filter ( dc_log_file const * self)
static

Definition at line 55 of file file.h.

55 {
56 return self->filter;
57}

◆ dc_log_file_log()

DC_PUBLIC void dc_log_file_log ( dc_log_file * self,
dc_log_location location,
dc_log_level level,
const char *const message,
... )
static

Definition at line 74 of file file.h.

75 {
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}
#define DC_DEBUG(DEBUG_FN, DEBUG_PTR)
Definition dump.h:92
#define DC_LOG_ANSI_RESET
Definition file.h:22
static DC_INTERNAL char const * dc_log_level_ansi_colour(dc_log_level level)
Definition file.h:29
static DC_INTERNAL void dc_log_file_print_descriptor(dc_log_file const *self, FILE *stream)
Definition file.h:59
static DC_PUBLIC bool dc_log_file_should_log(dc_log_file const *self, dc_log_level level)
Definition file.h:45
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

◆ dc_log_file_new_global()

DC_PUBLIC dc_log_file dc_log_file_new_global ( dc_log_file_global_config config,
dc_log_id id )
static

Definition at line 115 of file file.h.

116 {
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}

◆ dc_log_file_print_descriptor()

DC_INTERNAL void dc_log_file_print_descriptor ( dc_log_file const * self,
FILE * stream )
static

Definition at line 59 of file file.h.

59 {
60 if (self->parent != NULL) {
62 fprintf(stream, "/");
63 }
64 fprintf(stream, "%s", self->id.name);
65}

◆ dc_log_file_set_filter()

DC_PUBLIC void dc_log_file_set_filter ( dc_log_file * self,
dc_log_level level )
static
Examples
utils/log.c.

Definition at line 108 of file file.h.

108 {
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}
dc_log_level
Definition trait.h:5

◆ dc_log_file_should_log()

DC_PUBLIC bool dc_log_file_should_log ( dc_log_file const * self,
dc_log_level level )
static

Definition at line 45 of file file.h.

45 {
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}

◆ dc_log_level_ansi_colour()

DC_INTERNAL char const * dc_log_level_ansi_colour ( dc_log_level level)
static

Definition at line 29 of file file.h.

29 {
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}
#define DC_LOG_ANSI_ORANGE
Definition file.h:26
#define DC_LOG_ANSI_BLUE
Definition file.h:24
#define DC_LOG_ANSI_PURPLE
Definition file.h:23
#define DC_LOG_ANSI_RED
Definition file.h:27
#define DC_LOG_ANSI_GREEN
Definition file.h:25
@ DC_TRACE
Definition trait.h:6
@ DC_ERROR
Definition trait.h:10
@ DC_WARN
Definition trait.h:9

◆ DC_TRAIT_LOGGER()

DC_TRAIT_LOGGER ( dc_log_file )