-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathulog.c
More file actions
1890 lines (1545 loc) · 55.5 KB
/
ulog.c
File metadata and controls
1890 lines (1545 loc) · 55.5 KB
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
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// *************************************************************************
//
// ulog v@ULOG_VERSION@ - A simple customizable logging library.
// https://github.com/an-dr/microlog
//
// *************************************************************************
//
// Original implementation by rxi: https://github.com/rxi
// Modified by Andrei Gramakov: https://agramakov.me, mail@agramakov.me
// Also modified by many beautiful contributors from GitHub
//
// Copyright (c) 2025 Andrei Gramakov. All rights reserved.
//
// This file is licensed under the terms of the MIT license.
// For a copy, see: https://opensource.org/licenses/MIT
//
// *************************************************************************
#include "ulog.h"
#include <stdlib.h>
#include <string.h>
// clang-format off
/* ====================================================================================================================
Core Feature: Static Configuration
=======================================================================================================================
| Build Option | Default | Dependent Macro(s) | Purpose |
| -------------------------------- | -------------------------- | ------------------------- | ------------------------ |
| ULOG_BUILD_COLOR | 0 | ULOG_HAS_COLOR | Compile color code paths |
| ULOG_BUILD_PREFIX_SIZE | 0 | ULOG_HAS_PREFIX | Prefix buffer logic |
| ULOG_BUILD_EXTRA_OUTPUTS | 0 | ULOG_HAS_EXTRA_OUTPUTS | Extra output backends |
| ULOG_BUILD_SOURCE_LOCATION | 1 | ULOG_HAS_SOURCE_LOCATION | File\:line output |
| ULOG_BUILD_LEVEL_SHORT | 0 | ULOG_LEVEL_HAS_SHORT/_LONG| Short level style |
| ULOG_BUILD_TIME | 0 | ULOG_HAS_TIME | Timestamp support |
| ULOG_BUILD_TOPICS_MODE | ULOG_BUILD_TOPICS_MODE_OFF | ULOG_HAS_TOPICS | Topics mode |
| ULOG_BUILD_TOPICS_STATIC_NUM | 0 | - | Topic number |
| ULOG_BUILD_DYNAMIC_CONFIG | 0 | ULOG_HAS_DYNAMIC_CONFIG | Runtime toggles |
| ULOG_BUILD_WARN_NOT_ENABLED | 1 | ULOG_HAS_WARN_NOT_ENABLED | Warning stubs |
| ULOG_BUILD_CONFIG_HEADER_ENABLED | 0 | - | Configuration header mode|
| ULOG_BUILD_CONFIG_HEADER_NAME | "ulog_config.h" | - | Configuration header name|
| ULOG_BUILD_DISABLED | 0 | - | Disable ulog completely |
| ULOG_BUILD_ARRAY_LOGGING | 1 | - | Array formating |
| ULOG_BUILD_ARRAY_MAX_SIZE | 4096 | ULOG_BUILD_ARRAY_LOGGING | Maximum array size |
===================================================================================================================== */
/* ============================================================================
Optional Feature: Disable
============================================================================ */
#if ULOG_BUILD_DISABLED == 0
/* ============================================================================
Optional Feature: Configuration Header
============================================================================ */
#ifdef ULOG_BUILD_CONFIG_HEADER_ENABLED
// If ULOG_BUILD_CONFIG_HEADER_ENABLED is defined, no other ULOG_BUILD_* macros should be defined to avoid conflicts
#ifdef ULOG_BUILD_COLOR
#error "ULOG_BUILD_CONFIG_HEADER_ENABLED cannot be used with ULOG_BUILD_COLOR"
#endif
#ifdef ULOG_BUILD_PREFIX_SIZE
#error "ULOG_BUILD_CONFIG_HEADER_ENABLED cannot be used with ULOG_BUILD_PREFIX_SIZE"
#endif
#ifdef ULOG_BUILD_EXTRA_OUTPUTS
#error "ULOG_BUILD_CONFIG_HEADER_ENABLED cannot be used with ULOG_BUILD_EXTRA_OUTPUTS"
#endif
#ifdef ULOG_BUILD_SOURCE_LOCATION
#error "ULOG_BUILD_CONFIG_HEADER_ENABLED cannot be used with ULOG_BUILD_SOURCE_LOCATION"
#endif
#ifdef ULOG_BUILD_LEVEL_SHORT
#error "ULOG_BUILD_CONFIG_HEADER_ENABLED cannot be used with ULOG_BUILD_LEVEL_SHORT"
#endif
#ifdef ULOG_BUILD_TIME
#error "ULOG_BUILD_CONFIG_HEADER_ENABLED cannot be used with ULOG_BUILD_TIME"
#endif
#ifdef ULOG_BUILD_TOPICS_MODE
#error "ULOG_BUILD_CONFIG_HEADER_ENABLED cannot be used with ULOG_BUILD_TOPICS_MODE"
#endif
#ifdef ULOG_BUILD_DYNAMIC_CONFIG
#error "ULOG_BUILD_CONFIG_HEADER_ENABLED cannot be used with ULOG_BUILD_DYNAMIC_CONFIG"
#endif
#ifdef ULOG_BUILD_WARN_NOT_ENABLED
#error "ULOG_BUILD_CONFIG_HEADER_ENABLED cannot be used with ULOG_BUILD_WARN_NOT_ENABLED"
#endif
// The user provided configuration header
#ifndef ULOG_BUILD_CONFIG_HEADER_NAME
#define ULOG_BUILD_CONFIG_HEADER_NAME "ulog_config.h"
#endif
#include ULOG_BUILD_CONFIG_HEADER_NAME
#endif // ULOG_BUILD_CONFIG_HEADER_ENABLED
/* ============================================================================
Core Feature: Static Configuration
============================================================================ */
#ifndef ULOG_BUILD_COLOR
#define ULOG_HAS_COLOR 0
#else
#define ULOG_HAS_COLOR (ULOG_BUILD_COLOR==1)
#endif
#ifndef ULOG_BUILD_PREFIX_SIZE
#define ULOG_HAS_PREFIX 0
#else
#define ULOG_HAS_PREFIX (ULOG_BUILD_PREFIX_SIZE > 0)
#endif
#ifndef ULOG_BUILD_TIME
#define ULOG_HAS_TIME 0
#else
#define ULOG_HAS_TIME (ULOG_BUILD_TIME==1)
#endif
#ifndef ULOG_BUILD_LEVEL_SHORT
#define ULOG_HAS_LEVEL_LONG 1
#define ULOG_HAS_LEVEL_SHORT 0
#else
#define ULOG_HAS_LEVEL_LONG !ULOG_BUILD_LEVEL_SHORT
#define ULOG_HAS_LEVEL_SHORT ULOG_BUILD_LEVEL_SHORT
#endif
#ifndef ULOG_BUILD_EXTRA_OUTPUTS
#define ULOG_HAS_EXTRA_OUTPUTS 0
#else
#define ULOG_HAS_EXTRA_OUTPUTS (ULOG_BUILD_EXTRA_OUTPUTS > 0)
#endif
#ifndef ULOG_BUILD_SOURCE_LOCATION
#define ULOG_HAS_SOURCE_LOCATION 1
#else
#define ULOG_HAS_SOURCE_LOCATION (ULOG_BUILD_SOURCE_LOCATION == 1)
#endif
#ifndef ULOG_BUILD_WARN_NOT_ENABLED
#define ULOG_HAS_WARN_NOT_ENABLED 1
#else
#define ULOG_HAS_WARN_NOT_ENABLED (ULOG_BUILD_WARN_NOT_ENABLED==1)
#endif
#ifndef ULOG_BUILD_TOPICS_MODE
#define ULOG_HAS_TOPICS 0
#else
/* Topics enabled when mode is not OFF (STATIC or DYNAMIC) */
#define ULOG_HAS_TOPICS (ULOG_BUILD_TOPICS_MODE != ULOG_BUILD_TOPICS_MODE_OFF)
#endif
/* ============================================================================
Optional Feature: Dynamic Configuration
============================================================================ */
#ifndef ULOG_BUILD_DYNAMIC_CONFIG
#define ULOG_HAS_DYNAMIC_CONFIG 0
#else
#define ULOG_HAS_DYNAMIC_CONFIG 1
// Undef macros to avoid conflicts
#undef ULOG_BUILD_EXTRA_OUTPUTS
#undef ULOG_BUILD_PREFIX_SIZE
#undef ULOG_BUILD_TOPICS_MODE
#undef ULOG_HAS_COLOR
#undef ULOG_HAS_EXTRA_OUTPUTS
#undef ULOG_HAS_LEVEL_LONG
#undef ULOG_HAS_LEVEL_SHORT
#undef ULOG_HAS_PREFIX
#undef ULOG_HAS_SOURCE_LOCATION
#undef ULOG_HAS_TIME
#undef ULOG_HAS_TOPICS
#undef ULOG_HAS_WARN_NOT_ENABLED
// Configure features based on runtime config
#define ULOG_BUILD_EXTRA_OUTPUTS 8
#define ULOG_BUILD_PREFIX_SIZE 64
/* In dynamic configuration mode we enable dynamic topics */
#define ULOG_BUILD_TOPICS_MODE ULOG_BUILD_TOPICS_MODE_DYNAMIC
#define ULOG_HAS_COLOR 1
#define ULOG_HAS_EXTRA_OUTPUTS 1
#define ULOG_HAS_LEVEL_LONG 1
#define ULOG_HAS_LEVEL_SHORT 1
#define ULOG_HAS_PREFIX 1
#define ULOG_HAS_SOURCE_LOCATION 1
#define ULOG_HAS_TIME 1
#define ULOG_HAS_TOPICS 1
#define ULOG_HAS_WARN_NOT_ENABLED 0
#endif
// clang-format on
/* ============================================================================
Tools
============================================================================ */
// If testing is enabled, we define NOT_SO_STATIC as empty to allow
// the functions to be visible outside of this file for testing purposes.
#ifdef ULOG_TESTING
#define NOT_VERY_STATIC
#else
#define NOT_VERY_STATIC static
#endif
// Check if the string is empty or not provided
static inline bool is_str_empty(const char *str) {
return (str == NULL) || (str[0] == '\0');
}
/* ============================================================================
Core Feature: Warn Not Enabled
(`warn_not_enabled`, depends on: - )
============================================================================ */
#if ULOG_HAS_WARN_NOT_ENABLED
// Macro to log a warning when a feature is not enabled
// Usage: warn_not_enabled("ULOG_BUILD_TIME")
// Output:
// WARN src/main.c:42: 'ulog_configure_time' ignored: ULOG_BUILD_TIME disabled
#define warn_not_enabled(feature) \
warn_non_enabled_full(__func__, feature, __FILE__, __LINE__)
#define warn_non_enabled_full(func, feature, file, line) \
ulog_log(ULOG_LEVEL_WARN, file, line, NULL, \
"'%s' called with %s disabled", func, feature)
#endif // ULOG_HAS_WARN_NOT_ENABLED
/* ============================================================================
Core Feature: Print
(`print_*`, depends on: - )
============================================================================ */
// Private
// ================
typedef struct {
char *data;
size_t curr_pos;
size_t size;
} print_buffer;
typedef union {
print_buffer buffer;
FILE *stream;
} print_target_descriptor;
typedef enum { PRINT_TARGET_BUFFER, PRINT_TARGET_STREAM } print_target_type;
typedef struct {
print_target_type type;
print_target_descriptor dsc;
} print_target;
static void print_to_target_valist(print_target *tgt, const char *format,
va_list args) {
if (tgt->type == PRINT_TARGET_BUFFER) {
print_buffer *buf = &tgt->dsc.buffer;
if (buf->curr_pos >= buf->size) {
return; // No space available
}
size_t remaining = buf->size - buf->curr_pos;
char *write_pos = buf->data + buf->curr_pos;
int written = vsnprintf(write_pos, remaining, format, args);
if (written < 0) {
return; // Encoding error
}
// Update position, capping at buffer end
if ((size_t)written >= remaining) {
buf->curr_pos = buf->size;
} else {
buf->curr_pos += written;
}
} else if (tgt->type == PRINT_TARGET_STREAM) {
vfprintf(tgt->dsc.stream, format, args);
}
}
static void print_to_target(print_target *tgt, const char *format, ...) {
va_list args;
va_start(args, format);
print_to_target_valist(tgt, format, args);
va_end(args);
}
/* ============================================================================
Core Feature: Events
(`event_*`, depends on: Print)
============================================================================ */
// Private
// ================
static void log_print_message(print_target *tgt, ulog_event *ev);
/// @brief Event structure
struct ulog_event {
const char *message; // Message format string
va_list message_format_args; // Format arguments
#if ULOG_HAS_TOPICS
ulog_topic_id topic;
#endif
#if ULOG_HAS_TIME
struct tm *time;
#endif
#if ULOG_HAS_SOURCE_LOCATION
const char *file; // Event file name
int line; // Event line number
#endif // ULOG_HAS_SOURCE_LOCATION
ulog_level level; // Event debug level
};
ulog_status ulog_event_get_message(ulog_event *ev, char *buffer,
size_t buffer_size) {
if (ev == NULL || buffer == NULL || buffer_size == 0) {
return ULOG_STATUS_INVALID_ARGUMENT;
}
print_target tgt = {.type = PRINT_TARGET_BUFFER,
.dsc.buffer = {buffer, 0, buffer_size}};
// Create a copy of the event to avoid va_list issues
ulog_event ev_copy = *ev;
va_copy(ev_copy.message_format_args, ev->message_format_args);
log_print_message(&tgt, &ev_copy);
va_end(ev_copy.message_format_args);
return ULOG_STATUS_OK;
}
#if ULOG_HAS_TOPICS
ulog_topic_id ulog_event_get_topic(ulog_event *ev) {
if (ev == NULL) {
return ULOG_TOPIC_ID_INVALID; // Invalid topic
}
return ev->topic;
}
#endif // ULOG_HAS_TOPICS
#if ULOG_HAS_TIME
struct tm *ulog_event_get_time(ulog_event *ev) {
if (ev == NULL) {
return NULL;
}
return ev->time;
}
#endif // ULOG_HAS_TIME
#if ULOG_HAS_SOURCE_LOCATION
const char *ulog_event_get_file(ulog_event *ev) {
if (ev == NULL) {
return NULL;
}
return ev->file;
}
int ulog_event_get_line(ulog_event *ev) {
if (ev == NULL) {
return -1;
}
return ev->line;
}
#endif // ULOG_HAS_SOURCE_LOCATION
ulog_level ulog_event_get_level(ulog_event *ev) {
if (ev == NULL) {
return ULOG_LEVEL_TRACE; // Invalid level
}
return ev->level;
}
/* ============================================================================
Core Functionality: Lock
(`lock_*`, depends on: - )
============================================================================ */
// Private
// ================
typedef struct {
ulog_lock_fn function; // Lock function
void *args; // Argument for the lock function
} lock_data_t;
static lock_data_t lock_data = {
.function = NULL, // No lock function by default
.args = NULL, // No lock argument by default
};
static ulog_status lock_lock(void) {
if (lock_data.function != NULL) {
return lock_data.function(true, lock_data.args);
}
return ULOG_STATUS_OK;
}
static ulog_status lock_unlock(void) {
if (lock_data.function != NULL) {
return lock_data.function(false, lock_data.args);
}
return ULOG_STATUS_OK;
}
// Public
// ================
/// @brief Sets the lock function and user data
ulog_status ulog_lock_set_fn(ulog_lock_fn function, void *lock_arg) {
if (function == NULL) {
return ULOG_STATUS_INVALID_ARGUMENT;
}
lock_data.function = function;
lock_data.args = lock_arg;
return ULOG_STATUS_OK;
}
/* ============================================================================
Optional Feature: Dynamic Configuration - Color
(`color_config_*`, depends on: - )
============================================================================ */
#if ULOG_HAS_DYNAMIC_CONFIG
typedef struct {
bool enabled;
} color_config;
static color_config color_cfg = {
.enabled = (bool)ULOG_HAS_COLOR,
};
// Private
// ================
bool color_config_is_enabled(void) {
return color_cfg.enabled;
}
// Public
// ================
ulog_status ulog_color_config(bool enabled) {
if (lock_lock() != ULOG_STATUS_OK) {
return ULOG_STATUS_BUSY; // Failed to acquire lock
}
color_cfg.enabled = enabled;
return lock_unlock();
}
#else // ULOG_HAS_DYNAMIC_CONFIG
// Disabled Public
// ================
#if ULOG_HAS_WARN_NOT_ENABLED
ulog_status ulog_color_config(bool enabled) {
(void)(enabled);
warn_not_enabled("ULOG_BUILD_COLOR");
return ULOG_STATUS_DISABLED;
}
#endif // ULOG_HAS_WARN_NOT_ENABLED
// Disabled Private
// ================
#define color_config_is_enabled() (ULOG_HAS_COLOR)
#endif // ULOG_HAS_DYNAMIC_CONFIG
/* ============================================================================
Optional Feature: Color
(`color_*`, depends on: Print, Color Config)
============================================================================ */
#if ULOG_HAS_COLOR
// Private
// ================
// From https://en.wikipedia.org/wiki/ANSI_escape_code#8-bit
// ANSI color codes from least to most attention-grabbing
static const char *color_levels[] = {
"\x1b[m", // LEVEL_0: Color reset (default)
"\x1b[36m", // LEVEL_1: Color (Cyan)
"\x1b[32m", // LEVEL_2: Color (Green)
"\x1b[33m", // LEVEL_3: Color (Yellow)
"\x1b[31m", // LEVEL_4: Color (Red)
"\x1b[31m\x1b[47m", // LEVEL_5: Color (Red on White)
"\x1b[43m\x1b[31m", // LEVEL_6: Color (Yellow on Red)
"\x1b[41m\x1b[97m", // LEVEL_7: Color (White on Red)
};
#define COLOR_TERMINATOR "\x1b[0m"
static void color_print_start(print_target *tgt, ulog_event *ev) {
if (!color_config_is_enabled()) {
return; // Color is disabled, do not print color codes
}
print_to_target(tgt, "%s", color_levels[ev->level]); // color start
}
static void color_print_end(print_target *tgt) {
if (!color_config_is_enabled()) {
return; // Color is disabled, do not print color codes
}
print_to_target(tgt, "%s", COLOR_TERMINATOR); // color end
}
#else // ULOG_HAS_COLOR
// Disabled Private
// ================
#define color_print_start(tgt, ev) (void)(tgt), (void)(ev)
#define color_print_end(tgt) (void)(tgt)
#endif // ULOG_HAS_COLOR
/* ============================================================================
Optional Feature: Dynamic Configuration - Prefix
(`prefix_config_*`, depends on: - )
============================================================================ */
#if ULOG_HAS_DYNAMIC_CONFIG
typedef struct {
bool enabled;
} prefix_config;
static prefix_config prefix_cfg = {
.enabled = (bool)ULOG_HAS_PREFIX,
};
// Private
// ================
bool prefix_config_is_enabled(void) {
return prefix_cfg.enabled;
}
// Public
// ================
ulog_status ulog_prefix_config(bool enabled) {
if (lock_lock() != ULOG_STATUS_OK) {
return ULOG_STATUS_BUSY;
}
prefix_cfg.enabled = enabled;
return lock_unlock();
}
#else // ULOG_HAS_DYNAMIC_CONFIG
// Disabled Public
// ================
#if ULOG_HAS_WARN_NOT_ENABLED
ulog_status ulog_prefix_config(bool enabled) {
(void)(enabled);
warn_not_enabled("ULOG_BUILD_DYNAMIC_CONFIG");
return ULOG_STATUS_DISABLED;
}
#endif // ULOG_HAS_WARN_NOT_ENABLED
// Disabled Private
// ================
#define prefix_config_is_enabled() (ULOG_HAS_PREFIX)
#endif // ULOG_HAS_DYNAMIC_CONFIG
/* ============================================================================
Optional Feature: Prefix
(`prefix_*`, depends on: Print, Prefix Config)
============================================================================ */
#if ULOG_HAS_PREFIX
// Private
// ================
typedef struct {
ulog_prefix_fn function;
char prefix[ULOG_BUILD_PREFIX_SIZE];
} prefix_data_t;
static prefix_data_t prefix_data = {
.function = NULL,
.prefix = {0},
};
static void prefix_update(ulog_event *ev) {
if (prefix_data.function == NULL || !prefix_config_is_enabled()) {
return;
}
prefix_data.function(ev, prefix_data.prefix, ULOG_BUILD_PREFIX_SIZE);
}
static void prefix_print(print_target *tgt) {
if (prefix_data.function == NULL || !prefix_config_is_enabled()) {
return;
}
print_to_target(tgt, "%s", prefix_data.prefix);
}
// Public
// ================
ulog_status ulog_prefix_set_fn(ulog_prefix_fn function) {
if (lock_lock() != ULOG_STATUS_OK) {
return ULOG_STATUS_BUSY;
}
if (function == NULL) {
return ULOG_STATUS_INVALID_ARGUMENT; // Ignore NULL function
}
prefix_data.function = function;
return lock_unlock();
}
#else // ULOG_HAS_PREFIX
// Disabled Public
// ================
#if ULOG_HAS_WARN_NOT_ENABLED
ulog_status ulog_prefix_set_fn(ulog_prefix_fn function) {
(void)(function);
warn_not_enabled("ULOG_BUILD_PREFIX_SIZE");
return ULOG_STATUS_DISABLED;
}
#endif // ULOG_HAS_WARN_NOT_ENABLED
// Disabled Private
// ================
#define prefix_print(tgt) (void)(tgt)
#define prefix_update(ev) (void)(ev)
#endif // ULOG_HAS_PREFIX
/* ============================================================================
Optional Feature: Dynamic Configuration - Time
(`time_config_*`, depends on: - )
============================================================================ */
#if ULOG_HAS_DYNAMIC_CONFIG
typedef struct {
bool enabled;
} time_config;
static time_config time_cfg = {
.enabled = ULOG_HAS_TIME,
};
// Private
// ================
bool time_config_is_enabled(void) {
return time_cfg.enabled;
}
// Public
// ================
ulog_status ulog_time_config(bool enabled) {
if (lock_lock() != ULOG_STATUS_OK) {
return ULOG_STATUS_BUSY;
}
time_cfg.enabled = enabled;
return lock_unlock();
}
#else // ULOG_HAS_DYNAMIC_CONFIG
// Disabled Public
// ================
#if ULOG_HAS_WARN_NOT_ENABLED
ulog_status ulog_time_config(bool enabled) {
(void)(enabled);
warn_not_enabled("ULOG_BUILD_TIME");
return ULOG_STATUS_DISABLED;
}
#endif // ULOG_HAS_WARN_NOT_ENABLED
// Disabled Private
// ================
#define time_config_is_enabled() (ULOG_HAS_TIME)
#endif // ULOG_HAS_DYNAMIC_CONFIG
/* ============================================================================
Optional Feature: Time
(`time_*`, depends on: Time, Print)
============================================================================ */
#if ULOG_HAS_TIME
#include <time.h>
#define TIME_SHORT_BUF_SIZE 10 // HH:MM:SS(8) + 1 space + null
#define TIME_FULL_BUF_SIZE 21 // YYYY-MM-DD HH:MM:SS(19) + 1 space + null
// Private
// ================
static bool time_print_if_invalid(print_target *tgt, ulog_event *ev) {
if (ev->time == NULL) {
print_to_target(tgt, "INVALID_TIME");
return true; // Time is invalid, print error message
}
return false; // Time is valid
}
/// @brief Fills the event time with the current local time
/// @param ev - Event to fill. Assumed not NULL
static void time_fill_current_time(ulog_event *ev) {
time_t current_time = time(NULL); // Get current time
// Use localtime because existing tests inspect local clock fields.
ev->time = localtime(¤t_time);
}
static void time_print_short(print_target *tgt, ulog_event *ev,
bool append_space) {
if (!time_config_is_enabled() || time_print_if_invalid(tgt, ev)) {
return; // If time is not valid or disabled, stop printing
}
char buf[TIME_SHORT_BUF_SIZE] = {0};
const char *format = append_space ? "%H:%M:%S " : "%H:%M:%S";
strftime(buf, TIME_SHORT_BUF_SIZE, format, ev->time);
print_to_target(tgt, "%s", buf);
}
#if ULOG_HAS_EXTRA_OUTPUTS
static void time_print_full(print_target *tgt, ulog_event *ev,
bool append_space) {
if (!time_config_is_enabled() || time_print_if_invalid(tgt, ev)) {
return; // If time is not valid or disabled, stop printing
}
char buf[TIME_FULL_BUF_SIZE] = {0};
const char *format =
append_space ? "%Y-%m-%d %H:%M:%S " : "%Y-%m-%d %H:%M:%S";
strftime(buf, TIME_FULL_BUF_SIZE, format, ev->time);
print_to_target(tgt, "%s", buf);
}
#else
#define time_print_full(tgt, ev, append_space) (void)(0)
#endif // ULOG_HAS_EXTRA_OUTPUTS
#else // ULOG_HAS_TIME
// Disabled Private
// ================
#define time_print_short(tgt, ev, append_space) (void)(0)
#define time_print_full(tgt, ev, append_space) (void)(0)
#define time_fill_current_time(ev) (void)(ev)
#endif // ULOG_HAS_TIME
/* ============================================================================
Core Feature: Levels
(`level_*`, depends on: Levels Config, Print)
============================================================================ */
// Private
// ================
// clang-format off
#define LEVEL_MIN_VALUE 0
#define LEVEL_NAMES_SHORT {"T", "D", "I", "W", "E", "F", NULL, NULL}
#define LEVEL_NAMES_LONG {"TRACE", "DEBUG", "INFO ", "WARN ", "ERROR", "FATAL", NULL, NULL}
#if ULOG_HAS_LEVEL_SHORT && !ULOG_HAS_LEVEL_LONG
#define LEVEL_NAMES_DEFAULT LEVEL_NAMES_SHORT
#else // ULOG_HAS_LEVEL_LONG or both
#define LEVEL_NAMES_DEFAULT LEVEL_NAMES_LONG
#endif
// clang-format on
typedef struct {
const ulog_level_descriptor *dsc;
} level_data_t;
const ulog_level_descriptor level_names_default = {
.max_level = ULOG_LEVEL_FATAL,
.names = LEVEL_NAMES_DEFAULT,
};
level_data_t level_data = {
.dsc = &level_names_default,
};
static bool level_is_allowed(ulog_level msg_level, ulog_level log_verbosity) {
if (msg_level < log_verbosity || msg_level < LEVEL_MIN_VALUE) {
return false; // Level is higher than the configured level, not allowed
}
return true; // Level is allowed
}
static bool level_is_valid(ulog_level level) {
return (level >= LEVEL_MIN_VALUE && level <= level_data.dsc->max_level);
}
static void level_print(print_target *tgt, ulog_event *ev) {
print_to_target(tgt, "%s ", level_data.dsc->names[ev->level]);
}
// Public
// ================
/// @brief Returns the string representation of the level
const char *ulog_level_to_string(ulog_level level) {
if (level < LEVEL_MIN_VALUE || level >= level_data.dsc->max_level) {
return "?"; // Return a default string for invalid levels
}
return level_data.dsc->names[level];
}
ulog_status ulog_level_set_new_levels(const ulog_level_descriptor *new_levels) {
if (new_levels == NULL || new_levels->names[0] == NULL ||
new_levels->max_level <= LEVEL_MIN_VALUE) {
return ULOG_STATUS_INVALID_ARGUMENT; // Invalid argument
}
if (lock_lock() != ULOG_STATUS_OK) {
return ULOG_STATUS_BUSY; // Failed to acquire lock
}
level_data.dsc = new_levels;
return lock_unlock();
}
ulog_status ulog_level_reset_levels(void) {
if (lock_lock() != ULOG_STATUS_OK) {
return ULOG_STATUS_BUSY; // Failed to acquire lock
}
level_data.dsc = &level_names_default;
return lock_unlock();
}
/* ============================================================================
Optional Feature: Dynamic Configuration - Level
(`level_config_*`, depends on: - )
============================================================================ */
#if ULOG_HAS_DYNAMIC_CONFIG
// Private
// ================
typedef struct {
bool short_style; // Use short level strings
} level_config;
static level_config level_cfg = {
.short_style = false,
};
const ulog_level_descriptor level_names_default_short = {
.max_level = ULOG_LEVEL_FATAL,
.names = LEVEL_NAMES_SHORT,
};
bool level_config_is_short(void) {
return level_cfg.short_style;
}
// Public
// ================
ulog_status ulog_level_config(ulog_level_config_style style) {
if (lock_lock() != ULOG_STATUS_OK) {
return ULOG_STATUS_BUSY;
}
level_cfg.short_style = (style == ULOG_LEVEL_CONFIG_STYLE_SHORT);
if (level_cfg.short_style) {
level_data.dsc = &level_names_default_short;
} else {
level_data.dsc = &level_names_default;
}
return lock_unlock();
}
#else // ULOG_HAS_DYNAMIC_CONFIG
// Disabled Public
// ================
#if ULOG_HAS_WARN_NOT_ENABLED
ulog_status ulog_level_config(ulog_level_config_style style) {
(void)(style);
warn_not_enabled("ULOG_BUILD_DYNAMIC_CONFIG");
return ULOG_STATUS_DISABLED;
}
#endif // ULOG_HAS_WARN_NOT_ENABLED
// Disabled Private
// ================
#define level_config_is_short(void) (ULOG_HAS_LEVEL_SHORT)
#endif // ULOG_HAS_DYNAMIC_CONFIG
/* ============================================================================
Core Feature: Outputs
(`output_*`, depends on: Print, Log, Level)
============================================================================ */
// Private
// ================
#if ULOG_HAS_EXTRA_OUTPUTS
#define OUTPUT_TOTAL_NUM (1 + ULOG_BUILD_EXTRA_OUTPUTS) // stdout + extra
#else
#define OUTPUT_TOTAL_NUM 1 // Only stdout
#endif // ULOG_HAS_EXTRA_OUTPUTS
#define OUTPUT_STDOUT_DEFAULT_LEVEL ULOG_LEVEL_TRACE
// Prototypes
static void output_stdout_handler(ulog_event *ev, void *arg);
static void log_print_event(print_target *tgt, ulog_event *ev, bool full_time,
bool color, bool new_line);
typedef struct {
ulog_output_handler_fn handler;
void *arg;
ulog_level level;
} output;
typedef struct {
output outputs[OUTPUT_TOTAL_NUM]; // order num = id. 0 is for stdout
} output_data_t;
static output_data_t output_data = {
.outputs = {{output_stdout_handler, NULL, OUTPUT_STDOUT_DEFAULT_LEVEL}}};
static void output_handle_single(ulog_event *ev, output *output) {
if (output->handler == NULL) {
return; // Output has been removed, skip it
}
if (level_is_allowed(ev->level, output->level)) {
// Create event copy to avoid va_list issues
ulog_event ev_copy = {0};
memcpy(&ev_copy, ev, sizeof(ulog_event));
// Initialize the va_list for the copied event
// Note: We use a copy of the va_list to avoid issues with passing it
// directly as on some platforms using the same va_list multiple times
// can lead to undefined behavior.
va_copy(ev_copy.message_format_args, ev->message_format_args);
output->handler(&ev_copy, output->arg);
va_end(ev_copy.message_format_args);
}
}
static void output_handle_by_id(ulog_event *ev, ulog_output_id output_id) {
// Validate output ID bounds
if (output_id < 0 || output_id >= OUTPUT_TOTAL_NUM) {
return; // Invalid output ID
}
output_handle_single(ev, &output_data.outputs[output_id]);
}
static void output_handle_all(ulog_event *ev) {
// Processing the message for outputs
for (int i = 0; (i < OUTPUT_TOTAL_NUM); i++) {
output_handle_single(ev, &output_data.outputs[i]);
}
}
static void output_stdout_handler(ulog_event *ev, void *arg) {
(void)(arg); // Unused
print_target tgt = {.type = PRINT_TARGET_STREAM, .dsc.stream = stdout};
log_print_event(&tgt, ev, false, true, true);
}
// Public
// ================
ulog_status ulog_output_level_set(ulog_output_id output, ulog_level level) {
if (!level_is_valid(level)) {
return ULOG_STATUS_INVALID_ARGUMENT;
}
if (output < ULOG_OUTPUT_STDOUT || output >= OUTPUT_TOTAL_NUM) {
return ULOG_STATUS_INVALID_ARGUMENT;
}
if (output_data.outputs[output].handler == NULL) {
return ULOG_STATUS_NOT_FOUND; // Output exists but no handler assigned
}
output_data.outputs[output].level = level;
return ULOG_STATUS_OK;