aboutsummaryrefslogtreecommitdiff
path: root/any_log.h
blob: dcfbfcc4cb784f4c623dcac9d5a7a657e4431bc6 (plain)
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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
// any_log
//
// A single header library that provides a simple and somewhat opinionated
// interface for logging and structured logging.
//
// To use this library you should choose a suitable file to put the
// implementation and define ANY_LOG_IMPLEMENT. For example
//
//    #define ANY_LOG_IMPLEMENT
//    #include "any_log.h"
//
// Additionally, you can customize the library behavior by defining certain
// macros in the file where you put the implementation. You can see which are
// supported by reading the code guarded by ANY_LOG_IMPLEMENT.
//
// This library is licensed under the terms of the MIT license.
// A copy of the license is included at the end of this file.
//

#ifndef ANY_LOG_INCLUDE
#define ANY_LOG_INCLUDE

// These values represent the decreasing urgency of a log invocation.
//
// panic: indicates a fatal error and using it will result in
//        the program termination (see any_log_panic)
//
// error: indicates a (non-fatal) error
//
// warn: indicates a warning
//
// info: indicates an information (potentially useful to the user)
//
// debug: indicates debugging information
//
// trace: indicates verbose debugging information and can be completely
//        disabled by defining ANY_LOG_NO_TRACE before including
//
// NOTE: The value ANY_LOG_ALL is not an actual level and it is used as
//       a sentinel to indicate the last value of any_log_level_t
//
typedef enum {
    ANY_LOG_PANIC,
    ANY_LOG_ERROR,
    ANY_LOG_WARN,
    ANY_LOG_INFO,
    ANY_LOG_DEBUG,
    ANY_LOG_TRACE,
    ANY_LOG_ALL,
} any_log_level_t;

// The value of ANY_LOG_MODULE is used to indicate the current module.
// By default it is defined as __FILE__, so that it will coincide with the
// source file path (relative to the compiler cwd).
//
// You can customize ANY_LOG_MODULE before including the header by simply
// defining it. For example
//
//    #define ANY_LOG_MODULE "my-library"
//    #include "any_log.h"
//
#ifndef ANY_LOG_MODULE
#define ANY_LOG_MODULE __FILE__
#endif

// C99 and later define the __func__ variable
#ifndef ANY_LOG_FUNC
#define ANY_LOG_FUNC __func__
#endif

// log_panic is implemented with the function any_log_panic, which takes
// some extra parameters compared with the other log levels. This way we can
// include as many information as possible for identifying fatal errors.
//
// You can change the format string and exit function in the implementation
// (see ANY_LOG_EXIT, ANY_LOG_PANIC_BEFORE and ANY_LOG_PANIC_AFTER).
//
// NOTE: log_panic will always terminate the program and should be used only
//       for non recoverable situations! For normal errors just use log_error
//
#define log_panic(...) any_log_panic(__FILE__, __LINE__, ANY_LOG_MODULE, ANY_LOG_FUNC, __VA_ARGS__)

// log_[level] provide normal printf style logging.
//
// The logs will be filtered according to the global log level. See any_log_level.
//
// You should invoke log_[level] with a format string and any number of
// matched arguments. For example
//
//    log_error("This is an error");
//    log_debug("The X is %d (padding %d)", X, 10);
//
// log_trace and log_debug can be disabled completely (to avoid their overhead
// in release/optimized builds) by defining ANY_LOG_NO_TRACE and ANY_LOG_NO_DEBUG
// respectively. As this will work only if they are defined before every header
// include, it is recommended to define this from the compiler.
//
#define log_error(...) any_log_format(ANY_LOG_ERROR, ANY_LOG_MODULE, ANY_LOG_FUNC, __VA_ARGS__)
#define log_warn(...)  any_log_format(ANY_LOG_WARN, ANY_LOG_MODULE, ANY_LOG_FUNC, __VA_ARGS__)
#define log_info(...)  any_log_format(ANY_LOG_INFO, ANY_LOG_MODULE, ANY_LOG_FUNC, __VA_ARGS__)

#ifdef ANY_LOG_NO_DEBUG
#define log_debug(...)
#else
#define log_debug(...) any_log_format(ANY_LOG_DEBUG, ANY_LOG_MODULE, ANY_LOG_FUNC, __VA_ARGS__)
#endif

#ifdef ANY_LOG_NO_TRACE
#define log_trace(...)
#else
#define log_trace(...) any_log_format(ANY_LOG_TRACE, ANY_LOG_MODULE, ANY_LOG_FUNC, __VA_ARGS__)
#endif

// log_value_[level] provide structured logging.
//
// The logs will be filtered according to the global log level. See any_log_level.
//
// You should always pass a message string (printf style specifiers are ignored)
// and some key-value pairs.
//
// The key are simply strings. It is advised to pass only literals for security.
//
// The value can be of type int, unsigned int, pointer (void *), double and
// string (char *).
//
// The value type is specified by a type specifier at the start of the key
// string and should be like so
//
//    key = (type_specifier ANY_LOG_VALUE_TYPE_SEP)? ...
//
// By default ANY_LOG_VALUE_TYPE_SEP is the character ':'.
//
// The possible type_specifiers are
// d (or i): int
// x (or u): unsigned int
// p: void *
// f: double
// s: char *
//
// If no type specifier is given the function will assume the type given
// by ANY_LOG_VALUE_DEFAULT_TYPE (by default string).
//
// For example
//
//    log_value_info("Created graphical context",
//                   "d:width", width,
//                   "d:height", height,
//                   "p:window", window_handle,
//                   "f:scale", scale_factor_dpi,
//                   "appname", "nice app");
//
// In the implementation you can customize the format of every key-value pair
// and of the message. This is useful if you want to adhere to a structured
// logging format like JSON. For example
//
//    #define ANY_LOG_IMPLEMENT
//    #define ANY_LOG_VALUE_BEFORE(level, module, func, message)
//    "{\"module\": \"%s\", \"function\": \"%s\", \"level\": \"%s\", \"message\": \"%s\"",
//    module, func, any_log_level_strings[level], message
//
//    #define ANY_LOG_VALUE_INT(key, value) "\"%s\": %d", key, value
//    #define ANY_LOG_VALUE_HEX(key, value) "\"%s\": %u", key, value
//    #define ANY_LOG_VALUE_PTR(key, value) "\"%s\": \"%p\"", key, value
//    #define ANY_LOG_VALUE_DOUBLE(key, value) "\"%s\": %lf", key, value
//    #define ANY_LOG_VALUE_STRING(key, value) "\"%s \": \"%s\"", key, value
//    #define ANY_LOG_VALUE_AFTER(level, module, func, message) "}\n"
//    #include "any_log.h"
//
// As with log_trace and log_debug, log_value_trace and log_value_debug can be
// disabled by defining ANY_LOG_NO_TRACE and ANY_LOG_NO_DEBUG respectively.
//
#define log_value_error(...) any_log_value(ANY_LOG_ERROR, ANY_LOG_MODULE, ANY_LOG_FUNC, __VA_ARGS__, (char *)NULL)
#define log_value_warn(...)  any_log_value(ANY_LOG_WARN, ANY_LOG_MODULE, ANY_LOG_FUNC, __VA_ARGS__, (char *)NULL)
#define log_value_info(...)  any_log_value(ANY_LOG_INFO, ANY_LOG_MODULE, ANY_LOG_FUNC, __VA_ARGS__, (char *)NULL)
#define log_value_debug(...) any_log_value(ANY_LOG_DEBUG, ANY_LOG_MODULE, ANY_LOG_FUNC, __VA_ARGS__, (char *)NULL)

#ifdef ANY_LOG_NO_DEBUG
#define log_value_debug(...)
#else
#define log_value_debug(...) any_log_value(ANY_LOG_DEBUG, ANY_LOG_MODULE, ANY_LOG_FUNC, __VA_ARGS__, (char *)NULL)
#endif

#ifdef ANY_LOG_NO_TRACE
#define log_value_trace(...)
#else
#define log_value_trace(...) any_log_value(ANY_LOG_TRACE, ANY_LOG_MODULE, ANY_LOG_FUNC, __VA_ARGS__, (char *)NULL)
#endif

#ifdef __GNUC__
#define ANY_LOG_ATTRIBUTE(...) __attribute__((__VA_ARGS__))
#else
#define ANY_LOG_ATTRIBUTE(...)
#endif

// All log functions will ignore the message if the level is below the
// threshold specified in any_log_level.
//
// To modify the log level you can assign a any_log_level_t to this global.
//
// By default it has value ANY_LOG_LEVEL_DEFAULT (see implementation).
//
extern any_log_level_t any_log_level;

// An array containing the strings corresponding to the log levels.
//
// Can be modified in the implementation by defining the macros [level]_STRING.
//
// The functions any_log_level_to_string and any_log_level_from_string are
// provided for easy conversion.
//
extern const char *any_log_level_strings[ANY_LOG_ALL];

ANY_LOG_ATTRIBUTE(pure)
const char *any_log_level_to_string(any_log_level_t level);

ANY_LOG_ATTRIBUTE(pure)
any_log_level_t any_log_level_from_string(const char *string);

// NOTE: You should never call the functions below directly!
//       See the above explanations on how to use logging.

ANY_LOG_ATTRIBUTE(format(printf, 4, 5))
ANY_LOG_ATTRIBUTE(nonnull(4))
void any_log_format(any_log_level_t level, const char *module,
                    const char *func, const char *format, ...);

ANY_LOG_ATTRIBUTE(nonnull(4))
void any_log_value(any_log_level_t level, const char *module,
                   const char *func, const char *message, ...);

ANY_LOG_ATTRIBUTE(noreturn)
ANY_LOG_ATTRIBUTE(format(printf, 5, 6))
ANY_LOG_ATTRIBUTE(nonnull(1, 4))
void any_log_panic(const char *file, int line, const char *module,
                   const char *func, const char *format, ...);

#endif

#ifdef ANY_LOG_IMPLEMENT

#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// The default value for any_log_level
#ifndef ANY_LOG_LEVEL_DEFAULT
#define ANY_LOG_LEVEL_DEFAULT ANY_LOG_INFO
#endif

any_log_level_t any_log_level = ANY_LOG_LEVEL_DEFAULT;

// Log level strings
#ifndef ANY_LOG_PANIC_STRING
#define ANY_LOG_PANIC_STRING "panic"
#endif
#ifndef ANY_LOG_ERROR_STRING
#define ANY_LOG_ERROR_STRING "error"
#endif
#ifndef ANY_LOG_WARN_STRING
#define ANY_LOG_WARN_STRING "warn"
#endif
#ifndef ANY_LOG_INFO_STRING
#define ANY_LOG_INFO_STRING "info"
#endif
#ifndef ANY_LOG_DEBUG_STRING
#define ANY_LOG_DEBUG_STRING "debug"
#endif
#ifndef ANY_LOG_TRACE_STRING
#define ANY_LOG_TRACE_STRING "trace"
#endif

const char *any_log_level_strings[ANY_LOG_ALL] = {
    ANY_LOG_PANIC_STRING,
    ANY_LOG_ERROR_STRING,
    ANY_LOG_WARN_STRING,
    ANY_LOG_INFO_STRING ,
    ANY_LOG_DEBUG_STRING,
    ANY_LOG_TRACE_STRING,
};

const char *any_log_level_to_string(any_log_level_t level)
{
    return level >= ANY_LOG_PANIC && level <= ANY_LOG_TRACE
        ? any_log_level_strings[level] : "";
}

any_log_level_t any_log_level_from_string(const char *string)
{
    for (any_log_level_t level = ANY_LOG_PANIC; level < ANY_LOG_ALL; level++) {
        if (strcmp(any_log_level_strings[level], string) == 0)
            return level;
    }

    return ANY_LOG_ALL;
}

// Format for any_log_format (used at the start)
#ifndef ANY_LOG_FORMAT_BEFORE
#define ANY_LOG_FORMAT_BEFORE(level, module, func) "[%s %s] %s: ", module, func, any_log_level_strings[level]
#endif

// Format for any_log_format (used at the end)
#ifndef ANY_LOG_FORMAT_AFTER
#define ANY_LOG_FORMAT_AFTER(level, module, func) "\n"
#endif

void any_log_format(any_log_level_t level, const char *module,
                    const char *func, const char *format, ...)
{
    if (level > any_log_level)
        return;

    fprintf(stdout, ANY_LOG_FORMAT_BEFORE(level, module, func));

    va_list args;
    va_start(args, format);
    vfprintf(stdout, format, args);
    va_end(args);

    fprintf(stdout, ANY_LOG_FORMAT_AFTER(level, module, func));

    // NOTE: Suppress compiler warning if the user customizes the format string
    //       and doesn't use these values in it
    (void)module;
    (void)func;
}

// Format for any_log_value (used at the start)
#ifndef ANY_LOG_VALUE_BEFORE
#define ANY_LOG_VALUE_BEFORE(level, module, func, message) "[%s %s] %s: %s [", module, func, any_log_level_strings[level], message
#endif

// Format for any_log_value (used at the end)
#ifndef ANY_LOG_VALUE_AFTER
#define ANY_LOG_VALUE_AFTER(level, module, func, message) "]\n"
#endif

// This is used in the parsing of the type specifier from the key
//
// NOTE: It must be a character
#ifndef ANY_LOG_VALUE_TYPE_SEP
#define ANY_LOG_VALUE_TYPE_SEP ':'
#endif

// Format for pairs with an int value
#ifndef ANY_LOG_VALUE_INT
#define ANY_LOG_VALUE_INT(key, value) "%s=%d", key, value
#endif

// Format for pairs with an unsinged int value (hex by default)
#ifndef ANY_LOG_VALUE_HEX
#define ANY_LOG_VALUE_HEX(key, value) "%s=%#x", key, value
#endif

// Format for pairs with a pointer value
#ifndef ANY_LOG_VALUE_PTR
#define ANY_LOG_VALUE_PTR(key, value) "%s=%p", key, value
#endif

// Format for pairs with a double value
#ifndef ANY_LOG_VALUE_DOUBLE
#define ANY_LOG_VALUE_DOUBLE(key, value) "%s=%lf", key, value
#endif

// Format for pairs with a string value
#ifndef ANY_LOG_VALUE_STRING
#define ANY_LOG_VALUE_STRING(key, value) "%s=\"%s\"", key, value
#endif

// The default is to use string
#ifndef ANY_LOG_VALUE_DEFAULT
#define ANY_LOG_VALUE_DEFAULT(key, value) ANY_LOG_VALUE_STRING(key, value)
#define ANY_LOG_VALUE_DEFAULT_TYPE char *
#endif

// This is used as a separator between different pairs
#ifndef ANY_LOG_VALUE_PAIR_SEP
#define ANY_LOG_VALUE_PAIR_SEP ", "
#endif

void any_log_value(any_log_level_t level, const char *module,
                   const char *func, const char *message, ...)
{
    if (level > any_log_level)
        return;

    fprintf(stdout, ANY_LOG_VALUE_BEFORE(level, module, func, message));

    va_list args;
    va_start(args, message);

    // NOTE: This function should be called with at least one pair
    char *key = va_arg(args, char *);

    while (key != NULL) {
        if (key[0] != '\0' && key[1] == ANY_LOG_VALUE_TYPE_SEP) {
            key += 2;
            switch (key[-2]) {
                case 'd':
                case 'i':
                    fprintf(stdout, ANY_LOG_VALUE_INT(key, va_arg(args, int)));
                    break;

                case 'x':
                case 'u':
                    fprintf(stdout, ANY_LOG_VALUE_HEX(key, va_arg(args, unsigned int)));
                    break;

                case 'p':
                    fprintf(stdout, ANY_LOG_VALUE_PTR(key, va_arg(args, void *)));
                    break;

                case 'f':
                    fprintf(stdout, ANY_LOG_VALUE_DOUBLE(key, va_arg(args, double)));
                    break;

                case 's':
                    fprintf(stdout, ANY_LOG_VALUE_STRING(key, va_arg(args, char *)));
                    break;

                default:
                    goto tdefault;
            }
        } else {
tdefault:
            fprintf(stdout, ANY_LOG_VALUE_DEFAULT(key, va_arg(args, ANY_LOG_VALUE_DEFAULT_TYPE)));
        }

        key = va_arg(args, char *);
        if (key == NULL)
            break;

        fprintf(stdout, ANY_LOG_VALUE_PAIR_SEP);
    }

    va_end(args);
    fprintf(stdout, ANY_LOG_VALUE_AFTER(level, module, func, message));

    (void)module;
    (void)func;
    (void)message;
}

// Using log_panic results in a call to any_log_panic, which should terminate
// the program. The value of ANY_LOG_EXIT is used to specify an action to
// take at the end of the aforementioned function.
// By default it is abort
//
// NOTE: This function should never return!
//
#ifndef ANY_LOG_EXIT
#define ANY_LOG_EXIT(file, line, module, func) abort()
#endif

// Format for any_log_panic (used at the start)
#ifndef ANY_LOG_PANIC_BEFORE
#define ANY_LOG_PANIC_BEFORE(file, line, module, func) "[%s %s] %s: ", module, func, any_log_level_strings[ANY_LOG_PANIC]
#endif

// Format for any_log_panic (used at the start)
#ifndef ANY_LOG_PANIC_AFTER
#define ANY_LOG_PANIC_AFTER(file, line, module, func) "\npanic was invoked from %s:%d (%s)\n", file, line, module
#endif

// NOTE: This function *exceptionally* gets more location information
//       because we want to be specific at least for fatal errors
//
void any_log_panic(const char *file, int line, const char *module,
                   const char *func, const char *format, ...)
{
    fprintf(stdout, ANY_LOG_PANIC_BEFORE(file, line, module, func));

    va_list args;
    va_start(args, format);
    vfprintf(stdout, format, args);
    va_end(args);

    fprintf(stdout, ANY_LOG_PANIC_AFTER(file, line, module, func));

    (void)module;
    (void)func;
    (void)file;
    (void)line;

    ANY_LOG_EXIT(file, line, module, func);

    // In a way or another, this function shall not return
    abort();
}

#endif

// MIT License
//
// Copyright (c) 2024 Federico Angelilli
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//