-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfemtolog.h
483 lines (429 loc) · 12.6 KB
/
femtolog.h
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
/*
* femtolog
* Copyright (c) 2021-2022 SanCloud Ltd
* Based on log.c, Copyright (c) 2020 rxi
* SPDX-License-Identifier: MIT
*/
/**
* \file femtolog.h
*/
#pragma once
#include <stdarg.h>
#include <stdbool.h>
#ifdef DOXYGEN
#define FEMTOLOG_MIN_LEVEL 0
#define static
#endif
#if defined(__GNUC__) || defined(__clang__)
#define femtolog_attr_printf(fmt_index, args_index) \
__attribute__((format(printf, fmt_index, args_index)))
#define femtolog_attr_noreturn() __attribute__((noreturn))
#else
#define femtolog_attr_printf(fmt_index, args_index)
#define femtolog_attr_noreturn()
#endif
/**
* \defgroup management Management functions
* @{
*/
/**
* Prototype for an output function to be used by femtolog.
*
* When femtolog is initialised via femtolog_init(), a function of this
* type is given. This function will be called by femtolog to output
* each logging message, provided that the current log level is less
* than or equal to the message log level.
*
* The program using femtolog is responsible for implementing such a
* function and can therefore control where log messages are sent. For
* example, messages could be sent to stderr or to a file in a typical
* userspace program; or to a serial port in a freestanding application.
*
* An example of an appropriate function can be found in
* femtolog-example.c.
*
* \param prefix The message's log level, rendered as an upper-case
* string.
* \param fmt The message's printf-style format string.
* \param args The message's additional arguments as a va_list, to
* be interpreted according to the format string.
*/
typedef void (*femtolog_output_fn)(const char *prefix, const char *fmt, va_list args);
/**
* Initialise the femtolog library.
*
* \note This function must be called at the start of program execution,
* before any other functions from the femtolog library are used.
*
* \param level The initial log level. This will typically be
* \ref FEMTOLOG_INFO when a program is used in
* production or \ref FEMTOLOG_DEBUG when a program is
* used in development.
* \param output_fn The initial output function.
*/
void femtolog_init(int level, femtolog_output_fn output_fn);
/**
* Determine if a given log level is valid.
*
* \param level The log level to test.
*
* \returns true if \a level is a valid log level, otherwise false.
*/
bool femtolog_level_is_valid(int level);
/**
* Convert a log level to an upper-case string.
*
* \param level The log level to convert.
*
* \returns The name of the given log level or "(unknown)" if \a level
* is not a valid log level.
*/
const char *femtolog_level_to_name(int level);
/**
* Convert an upper-case string to a log level.
*
* \param name The string to convert.
*
* \returns The log level, or \ref FEMTOLOG_UNKNOWN if \a name is not a
* valid log level.
*/
int femtolog_name_to_level(const char *name);
/**
* Change the current log level.
*
* \param level The new log level.
*/
void femtolog_set_level(int level);
/**
* Get the current log level.
*
* \returns The current log level.
*/
int femtolog_get_level();
/**
* Set the current output function.
*
* \param output_fn The new output function.
*/
void femtolog_set_output_fn(femtolog_output_fn output_fn);
/**
* Get the current output function.
*
* \returns The current output function.
*/
femtolog_output_fn femtolog_get_output_fn();
/**@}*/
/**
* \defgroup logging Logging types and functions
* @{
*/
/**
* "TRACE": The lowest logging level, for highly detailed debug
* output which will rarely be enabled.
*/
#define FEMTOLOG_TRACE 0
/**
* "DEBUG": The logging level for verbose debug messages which are
* intended for use by developers and when investigating issues.
*/
#define FEMTOLOG_DEBUG 1
/**
* "INFO": The standard logging level for messages intended for
* general users of the program.
*/
#define FEMTOLOG_INFO 2
/**
* "WARN": The logging level for warnings which indicate a potential
* issue.
*/
#define FEMTOLOG_WARN 3
/**
* "ERROR": The logging level for program error messages, used when
* the program can recover or otherwise continue to execute.
*/
#define FEMTOLOG_ERROR 4
/**
* "FATAL": The logging level for program error messages, used when
* the program cannot continue.
*/
#define FEMTOLOG_FATAL 5
/**
* "UNKNOWN": A placeholder logging level used internally by
* femtolog.
*/
#define FEMTOLOG_UNKNOWN -1
/**
* Emit a log message with a dynamic log level and arguments in va_list
* form.
*
* \note Calls to this function are always retained during compilation,
* regardless of the value of \ref FEMTOLOG_MIN_LEVEL.
*
* \param level The message log level.
* \param fmt A printf-style format string.
* \param args Additional arguments as a va_list, interpreted
* according to the format string.
*/
void femtolog_vlog(int level, const char *fmt, va_list args);
/**
* Emit a log message with a dynamic log level.
*
* \note Calls to this function are always retained during compilation,
* regardless of the value of \ref FEMTOLOG_MIN_LEVEL.
*
* \param level The message log level.
* \param fmt A printf-style format string.
* \param ... Additional arguments, interpreted according to the
* format string.
*/
femtolog_attr_printf(2, 3) static inline void femtolog_log(int level, const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
femtolog_vlog(level, fmt, args);
va_end(args);
}
#ifndef FEMTOLOG_NO_MACROS
#ifndef FEMTOLOG_MIN_LEVEL
#define FEMTOLOG_MIN_LEVEL FEMTOLOG_INFO
#endif
#if (FEMTOLOG_MIN_LEVEL < FEMTOLOG_TRACE) || (FEMTOLOG_MIN_LEVEL > FEMTOLOG_FATAL)
#error FEMTOLOG_MIN_LEVEL is invalid
#endif
#if (FEMTOLOG_MIN_LEVEL <= FEMTOLOG_TRACE)
/**
* Emit a log message at the level \ref FEMTOLOG_TRACE.
*
* \note This function is not available if \ref FEMTOLOG_NO_MACROS is
* defined.
*
* \note This function is a no-op if \ref FEMTOLOG_MIN_LEVEL is set to a
* higher level than \ref FEMTOLOG_TRACE.
*
* \param fmt A printf-style format string.
* \param ... Additional arguments, interpreted according to the format
* string.
*/
femtolog_attr_printf(1, 2) static inline void log_trace(const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
femtolog_vlog(FEMTOLOG_TRACE, fmt, args);
va_end(args);
}
#else
#define log_trace(...)
#endif
#if FEMTOLOG_MIN_LEVEL <= FEMTOLOG_DEBUG
/**
* Emit a log message at the level \ref FEMTOLOG_DEBUG.
*
* \note This function is not available if \ref FEMTOLOG_NO_MACROS is
* defined.
*
* \note This function is a no-op if \ref FEMTOLOG_MIN_LEVEL is set to a
* higher level than \ref FEMTOLOG_DEBUG.
*
* \param fmt A printf-style format string.
* \param ... Additional arguments, interpreted according to the format
* string.
*/
femtolog_attr_printf(1, 2) static inline void log_debug(const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
femtolog_vlog(FEMTOLOG_DEBUG, fmt, args);
va_end(args);
}
#else
#define log_debug(...)
#endif
#if FEMTOLOG_MIN_LEVEL <= FEMTOLOG_INFO
/**
* Emit a log message at the level \ref FEMTOLOG_INFO.
*
* \note This function is not available if \ref FEMTOLOG_NO_MACROS is
* defined.
*
* \note This function is a no-op if \ref FEMTOLOG_MIN_LEVEL is set to a
* higher level than \ref FEMTOLOG_INFO.
*
* \param fmt A printf-style format string.
* \param ... Additional arguments, interpreted according to the format
* string.
*/
femtolog_attr_printf(1, 2) static inline void log_info(const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
femtolog_vlog(FEMTOLOG_INFO, fmt, args);
va_end(args);
}
#else
#define log_info(...)
#endif
#if FEMTOLOG_MIN_LEVEL <= FEMTOLOG_WARN
/**
* Emit a log message at the level \ref FEMTOLOG_WARN.
*
* \note This function is not available if \ref FEMTOLOG_NO_MACROS is
* defined.
*
* \note This function is a no-op if \ref FEMTOLOG_MIN_LEVEL is set to a
* higher level than \ref FEMTOLOG_WARN.
*
* \param fmt A printf-style format string.
* \param ... Additional arguments, interpreted according to the format
* string.
*/
femtolog_attr_printf(1, 2) static inline void log_warn(const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
femtolog_vlog(FEMTOLOG_WARN, fmt, args);
va_end(args);
}
#else
#define log_warn(...)
#endif
#if FEMTOLOG_MIN_LEVEL <= FEMTOLOG_ERROR
/**
* Emit a log message at the level \ref FEMTOLOG_ERROR.
*
* \note This function is not available if \ref FEMTOLOG_NO_MACROS is
* defined.
*
* \note This function is a no-op if \ref FEMTOLOG_MIN_LEVEL is set to a
* higher level than \ref FEMTOLOG_ERROR.
*
* \param fmt A printf-style format string.
* \param ... Additional arguments, interpreted according to the format
* string.
*/
femtolog_attr_printf(1, 2) static inline void log_error(const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
femtolog_vlog(FEMTOLOG_ERROR, fmt, args);
va_end(args);
}
#else
#define log_error(...)
#endif
#if FEMTOLOG_MIN_LEVEL <= FEMTOLOG_FATAL
/**
* Emit a log message at the level \ref FEMTOLOG_FATAL.
*
* \note This function is not available if \ref FEMTOLOG_NO_MACROS is
* defined.
*
* \note This function is a no-op if \ref FEMTOLOG_MIN_LEVEL is set to a
* higher level than \ref FEMTOLOG_FATAL.
*
* \param fmt A printf-style format string.
* \param ... Additional arguments, interpreted according to the format
* string.
*/
femtolog_attr_printf(1, 2) static inline void log_fatal(const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
femtolog_vlog(FEMTOLOG_FATAL, fmt, args);
va_end(args);
}
#else
#define log_fatal(...)
#endif
/**
* Emit a log message at a dynamic level.
*
* \note This function is not available if \ref FEMTOLOG_NO_MACROS is
* defined.
*
* \note Calls to this function are always retained during compilation,
* regardless of the value of \ref FEMTOLOG_MIN_LEVEL.
*
* \param level The message log level.
* \param fmt A printf-style format string.
* \param ... Additional arguments, interpreted according to the
* format string.
*/
femtolog_attr_printf(2, 3) static inline void log_dynamic(int level, const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
femtolog_vlog(level, fmt, args);
va_end(args);
}
#endif
#ifndef FEMTOLOG_FREESTANDING
#include <stdlib.h>
#ifndef FEMTOLOG_NO_MACROS
#if FEMTOLOG_MIN_LEVEL <= FEMTOLOG_FATAL
/**
* Emit a log message at the level \ref FEMTOLOG_FATAL and exit the
* program with an error condition of 1.
*
* \note This function is not available if \ref FEMTOLOG_NO_MACROS is
* defined or if \ref FEMTOLOG_FREESTANDING is defined.
*
* \note This function does not emit a log message if \ref
* FEMTOLOG_MIN_LEVEL is set to a higher level than \ref
* FEMTOLOG_FATAL. In this case the program will still exit.
*
* \param fmt A printf-style format string.
* \param ... Additional arguments, interpreted according to the format
* string.
*/
femtolog_attr_printf(1, 2)
femtolog_attr_noreturn() static inline void log_fatal_exit(const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
femtolog_vlog(FEMTOLOG_FATAL, fmt, args);
va_end(args);
exit(1);
}
#else
#define log_fatal_exit(...) exit(1)
#endif
#endif /* FEMTOLOG_NO_MACROS */
#endif /* FEMTOLOG_FREESTANDING */
/**@}*/
/**
* \defgroup config Configuration macros
* @{
*/
#ifdef DOXYGEN
/**
* Disable definition of log_*() functions by femtolog.
*
* This macro may be used if the log_*() function names clash with some
* other library used by a program. If this macro is defined, log
* messages must instead be emitted by calling femtolog_log() or
* femtolog_vlog().
*/
#define FEMTOLOG_NO_MACROS
/**
* Build femtolog for a freestanding environment.
*
* A freestanding environment is one where no runtime library is
* provided. Defining this macro allows femtolog to be used in OS
* development, bootloaders, UEFI applications or similar environments.
*/
#define FEMTOLOG_FREESTANDING
/**
* \def FEMTOLOG_MIN_LEVEL
* Set the minimum level for which log messages will be retained during
* compilation.
*
* All log_*() function calls with a log level lower than the given
* minimum will be discarded during compilation. This can be useful when
* using femtolog in memory constrained environments or time sensitive
* applications.
*
* If this macro is not defined by the program using femtolog, it will
* default to \ref FEMTOLOG_INFO.
*/
#endif
/**@}*/