-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmicro-log.h
More file actions
1991 lines (1714 loc) · 51.6 KB
/
micro-log.h
File metadata and controls
1991 lines (1714 loc) · 51.6 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
//////////////////////////////////////////////////////////////////////
// SPDX-License-Identifier: MIT
//
// micro-log.h
// ===========
//
// Header-only, configurable, thread safe logging framework in C99.
//
// ```
// 2025-09-21 22:32:36 INFO | Logger initialized
// 2025-09-21 22:32:36 INFO | I’d just like to interject for a moment...
// 2025-09-21 22:32:36 INFO | Closing logger
// ```
//
// Author: Giovanni Santini
// Mail: giovanni.santini@proton.me
// License: MIT
//
//
// Features
// --------
//
// - Multiple logging levels
// - Log to stdout, file, UNIX sockets, and network sockets
// - Configurable metadata (level, date, time, pid, tid, etc.)
// - JSON serialization support
// - Thread-safe logging
// - read settings from file (see the file `settings`)
// - optional colored output
// - compile time settings
//
// Initial implementation based on Oak: https://github.com/San7o/oak
//
// Example
// -------
//
// #define MICRO_LOG_IMPLEMENTATION
// #include "micro-log.h"
//
// int main(void)
// {
// micro_log_init();
// micro_log_set_flags(MICRO_LOG_FLAG_LEVEL
// | MICRO_LOG_FLAG_DATE
// | MICRO_LOG_FLAG_TIME);
//
// micro_log_info("I’d just like to interject for a moment...");
//
// micro_log_close();
// }
//
//
// Usage
// -----
//
// Do this:
//
// #define MICRO_LOG_IMPLEMENTATION
//
// before you include this file in *one* C or C++ file to create the
// implementation.
//
// i.e. it should look like this:
//
// #include ...
// #include ...
// #include ...
// #define MICRO_LOG_IMPLEMENTATION
// #include "micro-log.h"
//
// You can tune the library by #defining certain values. See the
// "Config" comments under "Configuration" below.
//
// !!IMPORTANT: READ THIS FOR MULTITHREADING !!
// Multithreading support is optional as it adds a small performance
// setback since printing must be regulated via a mutex. If you want
// to support logging from multiple threads, then define
// MICRO_LOG_MULTITHREADED before including this file.
//
// (Almost) All log function have two versions: one that interacts
// with a global logger, and another that uses a logger instance you
// supply. This is wanted because most of the time you just want a
// global logger without the need to pass references to loggers, but
// you may also need different loggers for different parts of your
// application, hence both options are provided.
//
// We will see some examples with the global logger. To use a local
// one, you just need to use the version '2' of the functions and pass
// the logger as the first parameter. For example
// `micro_log_info(...)` becomes `micro_log_info2(MicroLog logger,
// ...)`.
//
// After including the library, you can initialize a logger with
// `micro_log_init`.
//
// ```
// micro_log_init();
// ```
//
// If using `micro_log_init2`, you can specify a pointer to a logger.
//
// Remember to close the logger after you are done with
// `micro_log_close()`.
//
// You can set additional settings like the log level with
// `micro_log_set_level` or a file with `micro_log_set_file`, check
// out the function declarations.
//
// To log something, use the macro `micro_log_` with the level you
// want to use, like:
//
// ```
// micro_log_info("I’d like to interject for a moment. %s", text);
// ```
//
// You can format the logs like printf(3).
//
// Check out more examples at the end of the header.
//
// You can also read some settings from a file. Check out the file
// `settings` for additional information.
//
//
// Code
// ----
//
// The official git repository of micro-log.h is hosted at:
//
// https://github.com/San7o/micro-log.h
//
// This is part of a bigger collection of header-only C99 libraries
// called "micro-headers", contributions are welcome:
//
// https://github.com/San7o/micro-headers
//
//
// TODO
// ----
//
// - windows support
//
#ifndef MICRO_LOG
#define MICRO_LOG
#define MICRO_LOG_MAJOR 0
#define MICRO_LOG_MINOR 1
#ifdef __cplusplus
extern "C" {
#endif
//
// Configuration
//
// Config: Enable thread safety by defining MICRO_LOG_MULTITHREADED
//
// Note: This is optional since it adds a (tiny) performance setback
// since printing must be regulated via a mutex.
//
//#define MICRO_LOG_MULTITHREADED
// Config: Enable logging on operating system sockets
//
// Note: Optional since this may not be needed by most applications
//
//#define MICRO_LOG_SOCKETS
// Config: compiler-time log level, value 0 to 6 (included)
//
// All calls to log functions in the family `micro_log_{level}` and
// `micro_log_{level}2` with less priority than this will not be
// compiled at all. Setting this to an high priority will save
// performance since the log level will not have to be checked during
// runtime, and the log functions will not be called at all.
//
// Set this to one among the MICRO_LOG_LEVEL_* values:
//
// MICRO_LOG_LEVEL_{TRACE|DEBUF|INFO|WARN|ERROR|FATAL|DISABLED}
//
#ifndef MICRO_LOG_LEVEL_DEF
#define MICRO_LOG_LEVEL_DEF MICRO_LOG_LEVEL_TRACE
#endif
// Config: maximum size of a log string
#ifndef MICRO_LOG_MAX_STRING_SIZE
#define MICRO_LOG_MAX_STRING_SIZE 1024
#endif
// Config: Prefix for all functions
// For function inlining, set this to `static inline` and then define
// the implementation in all the files
//
#ifndef MICRO_LOG_DEF
#define MICRO_LOG_DEF extern
#endif
// Config: Support date and time printing (includes time.h)
#if 0
#define MICRO_LOG_DATE_TIME
#endif
// Config: Support PID and TID printing (includes pthread.h)
#if 0
#define MICRO_LOG_PID
#endif
// Config: Memory functions
#ifndef MICRO_LOG_MALLOC
#define MICRO_LOG_MALLOC malloc
#endif
#ifndef MICRO_LOG_FREE
#define MICRO_LOG_FREE free
#endif
#ifndef MICRO_LOG_REALLOC
#define MICRO_LOG_REALLOC realloc
#endif
// I/O operations
#ifndef MICRO_LOG_FOPEN
#include <stdio.h>
#define MICRO_LOG_FOPEN fopen
#define MICRO_FILE_MODE_READ "r"
#define MICRO_FILE_MODE_WRITE "w+"
#endif
#ifndef MICRO_LOG_FCLOSE
#define MICRO_LOG_FCLOSE fclose
#endif
#ifndef MICRO_LOG_FREAD
#define MICRO_LOG_FREAD fread
#endif
#ifndef MICRO_LOG_FWRITE
#define MICRO_LOG_FWRITE fwrite
#endif
#ifndef MICRO_LOG_FLUSH
#define MICRO_LOG_FLUSH(...) true
#endif
#ifndef MICRO_LOG_OUT
#define MICRO_LOG_OUT printf
#endif
//
// Errors
//
// Do not expect the error numbers to be the same in the future, use
// the macro
#define MICRO_LOG_OK 0
#define MICRO_LOG_ERROR_LOGGER_NULL 1
#define MICRO_LOG_ERROR_UNIMPLEMENTED 2
#define MICRO_LOG_ERROR_MUTEX_LOCK 3
#define MICRO_LOG_ERROR_MUTEX_UNLOCK 4
#define MICRO_LOG_ERROR_CLOSE_FILE 5
#define MICRO_LOG_ERROR_CLOSE_INET_SOCK 6
#define MICRO_LOG_ERROR_CLOSE_UNIX_SOCK 7
#define MICRO_LOG_ERROR_OPEN_FILE 8
#define MICRO_LOG_ERROR_INET_ADDR_NULL 9
#define MICRO_LOG_ERROR_OPEN_INET_SOCK 10
#define MICRO_LOG_ERROR_INET_CONNECT 11
#define MICRO_LOG_ERROR_PRINTF_SOCK_INET 12
#define MICRO_LOG_ERROR_VPRINTF_SOCK_INET 13
#define MICRO_LOG_ERROR_PRINTF_SOCK_UNIX 14
#define MICRO_LOG_ERROR_VPRINTF_SOCK_UNIX 15
#define MICRO_LOG_ERROR_PRINTF_STDOUT 16
#define MICRO_LOG_ERROR_PRINTF_FILE 17
#define MICRO_LOG_ERROR_VPRINTF_STDOUT 18
#define MICRO_LOG_ERROR_VPRINTF_FILE 19
#define MICRO_LOG_ERROR_INET_ADDR 21
#define MICRO_LOG_ERROR_SOCK_PATH_NULL 22
#define MICRO_LOG_ERROR_OPEN_UNIX_SOCK 23
#define MICRO_LOG_ERROR_UNIX_CONNECT 24
#define MICRO_LOG_ERROR_FLUSH_STDOUT 25
#define MICRO_LOG_ERROR_FLUSH_FILE 26
#define MICRO_LOG_ERROR_FROM_FILE_NULL 27
#define MICRO_LOG_ERROR_INVALID_FILE_SETTING 28
#define MICRO_LOG_ERROR_UNKNOWN_LEVEL 29
#define MICRO_LOG_ERROR_UNKNOWN_FLAG 30
#define MICRO_LOG_ERROR_INVALID_INET_ADDR 31
#define MICRO_LOG_ERROR_INVALID_PORT 32
#define MICRO_LOG_ERROR_INVALID_PROTOCOL 33
#define _MICRO_LOG_ERROR_MAX 34
//
// Macros
//
#ifdef MICRO_LOG_MULTITHREADED
#ifdef _WIN32
#error "TODO: Multithreading support on windows is not yet supported"
#endif
#include <pthread.h>
#endif
#ifndef _SIZE_T_DEFINED
#define _SIZE_T_DEFINED
typedef __SIZE_TYPE__ size_t;
#endif
#ifndef true
#define true 1
#endif
#ifndef false
#define false 0
#endif
#ifndef bool
#define bool _Bool;
#endif
#ifndef NULL
#define NULL ((void*) 0)
#endif
typedef __builtin_va_list _micro_log_va_list;
#define MICRO_LOG_FLAG_NONE (0)
#define MICRO_LOG_FLAG_LEVEL (1 << 0)
#define MICRO_LOG_FLAG_DATE (1 << 1)
#define MICRO_LOG_FLAG_TIME (1 << 2)
#define MICRO_LOG_FLAG_PID (1 << 3)
#define MICRO_LOG_FLAG_TID (1 << 4)
#define MICRO_LOG_FLAG_JSON (1 << 5)
#define MICRO_LOG_FLAG_COLOR (1 << 6)
#define MICRO_LOG_FLAG_FILE (1 << 7)
#define MICRO_LOG_FLAG_LINE (1 << 8)
#define MICRO_LOG_OUT_STDOUT (1 << 0)
#define MICRO_LOG_OUT_FILE (1 << 1)
#ifdef MICRO_LOG_SOCKETS
#define MICRO_LOG_OUT_SOCK_INET (1 << 2)
#if defined(__unix__) || defined(__unix)
#define MICRO_LOG_OUT_SOCK_UNIX (1 << 3)
#endif // __unix__
#endif // MICRO_LOG_SOCKETS
#define _MICRO_LOG_OUT_MAX (1 << 4)
#define MICRO_LOG_RST "\x1B[0m"
#define MICRO_LOG_RED(x) "\x1B[31m" x MICRO_LOG_RST
#define MICRO_LOG_GRN(x) "\x1B[32m" x MICRO_LOG_RST
#define MICRO_LOG_YEL(x) "\x1B[33m" x MICRO_LOG_RST
#define MICRO_LOG_BLU(x) "\x1B[34m" x MICRO_LOG_RST
#define MICRO_LOG_MAG(x) "\x1B[35m" x MICRO_LOG_RST
#define MICRO_LOG_CYN(x) "\x1B[36m" x MICRO_LOG_RST
#define MICRO_LOG_WHT(x) "\x1B[37m" x MICRO_LOG_RST
#define MICRO_LOG_BOLD(x) "\x1B[1m" x MICRO_LOG_RST
#define MICRO_LOG_UNDL(x) "\x1B[4m" x MICRO_LOG_RST
#define MICRO_LOG_LGRAY(x) "\x1B[90m" x MICRO_LOG_RST
// Global logger
// Functions
// micro_log_{write|trace|debug|info|warn|error|fatal}
#define micro_log_write(log_level, ...) \
micro_log_write2(µ_log_global, log_level, __VA_ARGS__)
#if MICRO_LOG_LEVEL_DEF <= MICRO_LOG_LEVEL_TRACE
#define micro_log_trace(...) \
micro_log_write(MICRO_LOG_LEVEL_TRACE, __VA_ARGS__)
#else
#define micro_log_trace(...) micro_log_disabled()
#endif
#if MICRO_LOG_LEVEL_DEF <= MICRO_LOG_LEVEL_DEBUG
#define micro_log_debug(...) \
micro_log_write(MICRO_LOG_LEVEL_DEBUG, __VA_ARGS__)
#else
#define micro_log_debug(...) micro_log_disabled()
#endif
#if MICRO_LOG_LEVEL_DEF <= MICRO_LOG_LEVEL_INFO
#define micro_log_info(...) \
micro_log_write(MICRO_LOG_LEVEL_INFO, __VA_ARGS__)
#else
#define micro_log_info(...) micro_log_disabled()
#endif
#if MICRO_LOG_LEVEL_DEF <= MICRO_LOG_LEVEL_WARN
#define micro_log_warn(...) \
micro_log_write(MICRO_LOG_LEVEL_WARN, __VA_ARGS__)
#else
#define micro_log_warn(...) micro_log_disabled()
#endif
#if MICRO_LOG_LEVEL_DEF <= MICRO_LOG_LEVEL_ERROR
#define micro_log_error(...) \
micro_log_write(MICRO_LOG_LEVEL_ERROR, __VA_ARGS__)
#else
#define micro_log_error(...) micro_log_disabled()
#endif
#if MICRO_LOG_LEVEL_DEF <= MICRO_LOG_LEVEL_FATAL
#define micro_log_fatal(...) \
micro_log_write(MICRO_LOG_LEVEL_FATAL, __VA_ARGS__)
#else
#define micro_log_fatal(...) micro_log_disabled()
#endif
// Local logger
// Functions
// micro_log_{write2|trace2|debug2|info2|warn2|error2|fatal2}
#define micro_log_write2(micro_log, log_level, ...) \
_micro_log_write_impl(micro_log, log_level, __FILE__, __LINE__, __VA_ARGS__)
#if MICRO_LOG_LEVEL_DEF <= MICRO_LOG_LEVEL_TRACE
#define micro_log_trace2(micro_log, ...) \
micro_log_write2(micro_log, MICRO_LOG_LEVEL_TRACE, __VA_ARGS__)
#else
#define micro_log_trace2(micro_log, ...) micro_log_disabled()
#endif
#if MICRO_LOG_LEVEL_DEF <= MICRO_LOG_LEVEL_DEBUG
#define micro_log_debug2(micro_log, ...) \
micro_log_write2(micro_log, MICRO_LOG_LEVEL_DEBUG, __VA_ARGS__)
#else
#define micro_log_debug2(micro_log, ...) micro_log_disabled()
#endif
#if MICRO_LOG_LEVEL_DEF <= MICRO_LOG_LEVEL_INFO
#define micro_log_info2(micro_log, ...) \
micro_log_write2(micro_log, MICRO_LOG_LEVEL_INFO, __VA_ARGS__)
#else
#define micro_log_info2(micro_log, ...) micro_log_disabled()
#endif
#if MICRO_LOG_LEVEL_DEF <= MICRO_LOG_LEVEL_WARN
#define micro_log_warn2(micro_log, ...) \
micro_log_write2(micro_log, MICRO_LOG_LEVEL_WARN, __VA_ARGS__)
#else
#define micro_log_warn2(micro_log, ...) micro_log_disabled()
#endif
#if MICRO_LOG_LEVEL_DEF <= MICRO_LOG_LEVEL_ERROR
#define micro_log_error2(micro_log, ...) \
micro_log_write2(micro_log, MICRO_LOG_LEVEL_ERROR, __VA_ARGS__)
#else
#define micro_log_error2(micro_log, ...) micro_log_disabled()
#endif
#if MICRO_LOG_LEVEL_DEF <= MICRO_LOG_LEVEL_FATAL
#define micro_log_fatal2(micro_log, ...) \
micro_log_write2(micro_log, MICRO_LOG_LEVEL_FATAL, __VA_ARGS__)
#else
#define micro_log_fatal2(micro_log, ...) micro_log_disabled()
#endif
//
// Types and functions
//
// Use the MICRO_LOG_LEVEL_ macros for this value
typedef unsigned int MicroLogLevel;
#define MICRO_LOG_LEVEL_TRACE 0
#define MICRO_LOG_LEVEL_DEBUG 1
#define MICRO_LOG_LEVEL_INFO 2
#define MICRO_LOG_LEVEL_WARN 3
#define MICRO_LOG_LEVEL_ERROR 4
#define MICRO_LOG_LEVEL_FATAL 5
#define MICRO_LOG_LEVEL_DISABLED 6
#define MICRO_LOG_LEVEL_MAX 7
#ifdef MICRO_LOG_SOCKETS
typedef enum
{
MICRO_LOG_PROTO_TCP = 0,
MICRO_LOG_PROTO_UDP,
_MICRO_LOG_PROTO_MAX
} MicroLogProto;
#endif // MICRO_LOG_SOCKETS
typedef int micro_log_error;
// The MicroLog logger
typedef struct {
// MICRO_LOG_FLAG bitfield
// Adds additionally log information based on the flags
long unsigned int flags_bitfield;
// MICRO_LOG_OUT bitfield
// Default value is MICRO_LOG_OUT_STDOUT
long unsigned int out_bitfield;
// The current log level of the logger
// Only logs that are of an higher or equal priority than this will
// be logged.
// Default value is MICRO_LOG_LEVEL_TRACE
MicroLogLevel log_level;
// (optional) Pointer to output file
void *file;
#ifdef MICRO_LOG_SOCKETS
// (optional) Socket file descriptor
int inet_sock_fd;
#if defined(__unix__) || defined(__unix)
int unix_sock_fd;
#endif // __unix__
#endif // MICRO_LOG_SOCKETS
#ifdef MICRO_LOG_MULTITHREADED
// Mutex to protect all writes
pthread_mutex_t write_mutex;
#endif // MICRO_LOG_MULTITHREADED
} MicroLog;
// Format string to [out] buffer (does not allocate)
MICRO_LOG_DEF void micro_log_format(char* out, const char* fmt, ...);
//
// Global logger functions
//
//
// The Global logger
//
// This is a global logger that can be used with the function below.
// You can also create other loggers, check out the other functions
// in the `Local logger functions` group.
extern MicroLog micro_log_global;
// Initialize the global logger
//
// Notes: remember to close it when you are done
MICRO_LOG_DEF micro_log_error micro_log_init(void);
// Read settings from file
//
// Check out the file named `settings` for additional information
// Notes: this expects the global logger to be already initialzied
MICRO_LOG_DEF micro_log_error micro_log_from_file(char *filename);
// Close the global logger
MICRO_LOG_DEF micro_log_error micro_log_close(void);
// Flush all current output streams in the global logger
MICRO_LOG_DEF micro_log_error micro_log_flush(void);
// Set output flags to global logger
//
// These are additional information to the log output, such as the
// date, line, file position etc. You can also set the output to be
// colored or to be serialized in some format like json.
//
// Check out the MICRO_LOG_FLAG_ values to see all the flags available
MICRO_LOG_DEF micro_log_error
micro_log_set_flags(long unsigned int flags_bitfield);
// Set the log level to global logger
//
// Only the logs with an higher level of the current log level will be
// printed.
//
// Check out the MicroLogLevel for a list of the supported levels.
MICRO_LOG_DEF micro_log_error micro_log_set_level(MicroLogLevel level);
// Set the output streams to the global logger
//
// You can toggle some output streams. Check out the MICRO_LOG_OUT_
// values. Remember that if you manually enable the
// MICRO_LOG_OUT_FILE, the logger will try to write to a file so you
// should have already opened it with `miro_log_set_file`. Likewise
// for the other options. By default, when you add a new output
// stream, this will be automatically registered.
MICRO_LOG_DEF micro_log_error micro_log_set_out(int out_flags);
// Set output file of the global logger
//
// The logger will write logs to [filename]. The logger will create
// the file if it does not exist.
MICRO_LOG_DEF micro_log_error micro_log_set_file(char* filename);
#ifdef MICRO_LOG_SOCKETS
// Set output internet socket of the global logger
//
// Note: You need to have defined MICRO_LOG_SOCKETS before including
// this header in torder to use this function
MICRO_LOG_DEF micro_log_error
micro_log_set_socket_inet(char* addr,
int port,
MicroLogProto protocol);
#if defined(__unix__) || defined(__unix)
// Set output unix socket of the global logger
//
// Note: You need to have defined MICRO_LOG_SOCKETS before including
// this header, and be in an unix system to use this function.
MICRO_LOG_DEF micro_log_error micro_log_set_socket_unix(char* path);
#endif // __unix__
#endif // MICRO_LOG_SOCKETS
//
// Local logger functions
//
// These are equivalent to the global ones but they take an additional
// MicroLog first argument, which is the logger what will be modified.
MICRO_LOG_DEF micro_log_error micro_log_init2(MicroLog *micro_log);
// micro_log_from_file2 expects [micro_log] to be already initialzied
MICRO_LOG_DEF micro_log_error
micro_log_from_file2(MicroLog *micro_log, char *filename);
MICRO_LOG_DEF micro_log_error micro_log_close2(MicroLog *micro_log);
MICRO_LOG_DEF micro_log_error micro_log_flush2(MicroLog *micro_log);
MICRO_LOG_DEF micro_log_error
micro_log_set_flags2(MicroLog *micro_log,
long unsigned int flags_bitfield);
MICRO_LOG_DEF micro_log_error
micro_log_set_level2(MicroLog *micro_log,
MicroLogLevel level);
MICRO_LOG_DEF micro_log_error
micro_log_set_out2(MicroLog *micro_log, int out_flags);
MICRO_LOG_DEF micro_log_error
micro_log_set_file2(MicroLog *micro_log,
char* filename);
#ifdef MICRO_LOG_SOCKETS
MICRO_LOG_DEF micro_log_error
micro_log_set_socket_inet2(MicroLog *micro_log,
char* addr,
int port,
MicroLogProto protocol);
#if defined(__unix__) || defined(__unix)
MICRO_LOG_DEF micro_log_error
micro_log_set_socket_unix2(MicroLog *micro_log,
char* path);
#endif // __unix__
#endif // MICRO_LOG_SOCKETS
// Misc
// Inline function that just returns MICRO_LOG_OK
// This is needed when log level is disabled at compile time, and
// it needs a definition in every translation unit to be inlined
static inline int micro_log_disabled(void)
{
return MICRO_LOG_OK;
}
// Get a string of a certain log level, with an optional color
MICRO_LOG_DEF const char*
micro_log_level_string(MicroLogLevel level, bool color);
// The central log function
//
// All other functions call this one for printing. It handles all the
// flags and calls `_micro_log_print_outputs`.
MICRO_LOG_DEF micro_log_error
_micro_log_write_impl(MicroLog *micro_log,
MicroLogLevel level,
const char* file,
int line,
const char *fmt, ...);
// Wraps `_micro_log_print_outputs_args`
MICRO_LOG_DEF micro_log_error
_micro_log_print_outputs(MicroLog *micro_log,
const char* fmt, ...);
// Print formatted string in multiple outputs.
MICRO_LOG_DEF micro_log_error
_micro_log_print_outputs_args(MicroLog *micro_log,
const char* fmt,
_micro_log_va_list args);
//
// Implementation
//
#ifdef MICRO_LOG_IMPLEMENTATION
#ifdef MICRO_LOG_DATE_TIME
#include <time.h>
#endif
#ifdef MICRO_LOG_PID
#include <pthread.h>
#endif
#ifndef MICRO_STATIC_ASSERT
#define MICRO_STATIC_ASSERT(cond, msg) \
typedef char static_assertion_##msg##__LINE__[(cond) ? 1 : -1]
#endif
#define _micro_log_va_start(v, l) __builtin_va_start(v, l)
#define _micro_log_va_end(v) __builtin_va_end(v)
#define _micro_log_va_arg(v, l) __builtin_va_arg(v, l)
#define _micro_log_va_copy(d, s) __builtin_va_copy(d, s)
#ifdef MICRO_LOG_SOCKETS
#ifdef _WIN32
// Windows
#include <winsock2.h>
#include <ws2tcpip.h>
#define close_socket(s) closesocket(s)
#pragma comment(lib, "ws2_32.lib") // link against Winsock library
#define dprintf(...) ; // TODO
#define vdprintf(...) ; // TODO
#error "TODO Support for windows internet sockets"
#else
// POSIX (Linux, macOS, BSD, etc.)
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <sys/un.h>
#define close_socket(s) close(s)
#endif // _WIN32
#endif // MICRO_LOG_SOCKETS
#ifdef MICRO_LOG_MULTITHREADED
#define __MICRO_LOG_LOCK(__micro_log_ptr) \
do { \
if (pthread_mutex_lock(&__micro_log_ptr->write_mutex) != 0) \
{ \
error = MICRO_LOG_ERROR_MUTEX_LOCK; \
goto done; \
} \
} while (0)
#define __MICRO_LOG_UNLOCK(__micro_log_ptr) \
do { \
if (pthread_mutex_unlock(&__micro_log_ptr->write_mutex) != 0) \
{ \
error = MICRO_LOG_ERROR_MUTEX_UNLOCK; \
goto done; \
} \
} while(0)
#else
#define __MICRO_LOG_LOCK(__micro_log_ptr) ;
#define __MICRO_LOG_UNLOCK(__micro_log_ptr) ;
#endif // MICRO_LOG_MULTITHREADED
MicroLog micro_log_global;
MICRO_LOG_DEF micro_log_error micro_log_init(void)
{
return micro_log_init2(µ_log_global);
}
MICRO_LOG_DEF micro_log_error micro_log_from_file(char *filename)
{
return micro_log_from_file2(µ_log_global, filename);
}
MICRO_LOG_DEF micro_log_error micro_log_close(void)
{
return micro_log_close2(µ_log_global);
}
MICRO_LOG_DEF micro_log_error micro_log_flush(void)
{
return micro_log_flush2(µ_log_global);
}
MICRO_LOG_DEF micro_log_error
micro_log_set_flags(long unsigned int flags_bitfield)
{
return micro_log_set_flags2(µ_log_global, flags_bitfield);
}
MICRO_LOG_DEF micro_log_error micro_log_set_level(MicroLogLevel level)
{
return micro_log_set_level2(µ_log_global, level);
}
MICRO_LOG_DEF micro_log_error micro_log_set_out(int out_flags)
{
return micro_log_set_out2(µ_log_global, out_flags);
}
MICRO_LOG_DEF micro_log_error micro_log_set_file(char* filename)
{
return micro_log_set_file2(µ_log_global, filename);
}
#ifdef MICRO_LOG_SOCKETS
MICRO_LOG_DEF micro_log_error
micro_log_set_socket_inet(char* addr,
int port,
MicroLogProto protocol)
{
return micro_log_set_socket_inet2(µ_log_global, addr, port, protocol);
}
#if defined(__unix__) || defined(__unix)
MICRO_LOG_DEF micro_log_error micro_log_set_socket_unix(char* path)
{
return micro_log_set_socket_unix2(µ_log_global, path);
}
#endif // __unix__
#endif // MICRO_LOG_SOCKETS
MICRO_LOG_DEF micro_log_error micro_log_init2(MicroLog *micro_log)
{
if (micro_log == NULL)
return MICRO_LOG_ERROR_LOGGER_NULL;
*micro_log = (MicroLog){
.log_level = MICRO_LOG_LEVEL_TRACE,
.flags_bitfield = 0,
.out_bitfield = MICRO_LOG_OUT_STDOUT,
.file = NULL,
#ifdef MICRO_LOG_SOCKETS
.inet_sock_fd = 0,
#if defined(__unix__) || defined(__unix)
.unix_sock_fd = 0,
#endif // __unix__
#endif // MICRO_LOG_SOCKETS
};
#ifdef MICRO_LOG_MULTITHREADED
pthread_mutex_init(µ_log->write_mutex, NULL);
#endif
micro_log_info2(micro_log, "Logger initialized");
return MICRO_LOG_OK;
}
MICRO_LOG_DEF int _micro_log_get_spaces(char* str, int max)
{
int spaces = 0;
while((*(str + spaces) == ' ' || *(str + spaces) == '\t') && spaces < max)
spaces++;
return spaces;
}
MICRO_LOG_DEF int _micro_log_get_word_len(char* str, int max)
{
int letters = 0;
while(*(str+letters) != ' '
&& *(str+letters) != '\n'
&& *(str+letters) != '\t'
&& letters < max)
letters++;
return letters;
}
typedef struct {
void* handle; // The file handle from micro_platform.open
char buffer[512]; // Internal buffer to minimize syscalls
int pos; // Current position in buffer
int size; // Actual bytes currently in buffer
bool eof;
} MicroStream;
// A minimal replacement for fgetc/getline logic
static int _micro_log_getc(MicroStream* stream)
{
if (stream->pos >= stream->size)
{
if (stream->eof) return -1;
// Use your platform abstraction
int bytes = MICRO_LOG_FREAD(stream->buffer,
1,
sizeof(stream->buffer),
stream->handle);
if (bytes <= 0)
{
stream->eof = true;
return -1;
}
stream->size = bytes;
stream->pos = 0;
}
return (unsigned char)stream->buffer[stream->pos++];
}
static size_t _micro_log_getline(char** lineptr, size_t* n, MicroStream* stream)
{
size_t capacity = (*n > 0) ? *n : 128;
if (*lineptr == NULL) *lineptr = MICRO_LOG_REALLOC(NULL, capacity);
size_t count = 0;
int c;
while ((c = _micro_log_getc(stream)) != -1)
{
// Ensure space for char + null terminator
if (count + 1 >= capacity)
{
capacity *= 2;
char* new_buf = MICRO_LOG_REALLOC(*lineptr, capacity);
if (!new_buf) return -1;
*lineptr = new_buf;
*n = capacity;
}
(*lineptr)[count++] = (char)c;
if (c == '\n') break;
}
if (count == 0) return -1;
(*lineptr)[count] = '\0';
return count;
}
static inline int _micro_log_strncmp(const char *s1, const char *s2, size_t n)
{
while (*s1 == *s2 && n > 0) {
s1++;
s2++;
n--;
}
if (*s2 == '\0')
return 0;
return 1;
}
MICRO_LOG_DEF micro_log_error
micro_log_from_file2(MicroLog *micro_log, char *filename)
{
if (micro_log == NULL)
return MICRO_LOG_ERROR_LOGGER_NULL;
if (filename == NULL)
return MICRO_LOG_ERROR_FROM_FILE_NULL;
micro_log_error error = MICRO_LOG_OK;
void* file = MICRO_LOG_FOPEN(filename, MICRO_FILE_MODE_READ);
if (file == NULL)
{
MICRO_LOG_OUT("Error opening file");
error = MICRO_LOG_ERROR_OPEN_FILE;
goto done;
}
// Example file:
//
// level: debug
// flags: level date time tid pid
// file: output.txt
// inet: 127.0.0.1 5000 TCP
// unix: /tmp/a-socket
// # A comment
//
char *line = NULL;
size_t len = 0;
int spaces;
while(_micro_log_getline(&line, &len, file) > 0)
{
if (_micro_log_strncmp(line, "#", 1) == 0)
{
goto next;
}
if (_micro_log_strncmp(line, "\n", 1) == 0)
{
goto next;
}
if (_micro_log_strncmp(line, "level:", 6) == 0)
{
spaces = _micro_log_get_spaces(line + 6, len - 6);
if (_micro_log_strncmp(line + 6 + spaces, "trace", 5) == 0)
{
micro_log_set_level2(micro_log, MICRO_LOG_LEVEL_TRACE);
}
else if (_micro_log_strncmp(line + 6 + spaces, "debug", 5) == 0)
{
micro_log_set_level2(micro_log, MICRO_LOG_LEVEL_DEBUG);
}
else if (_micro_log_strncmp(line + 6 + spaces, "info", 4) == 0)
{
micro_log_set_level2(micro_log, MICRO_LOG_LEVEL_INFO);
}
else if (_micro_log_strncmp(line + 6 + spaces, "warn", 4) == 0)
{
micro_log_set_level2(micro_log, MICRO_LOG_LEVEL_WARN);
}
else if (_micro_log_strncmp(line + 6 + spaces, "error", 5) == 0)
{
micro_log_set_level2(micro_log, MICRO_LOG_LEVEL_ERROR);
}
else if (_micro_log_strncmp(line + 6 + spaces, "fatal", 5) == 0)
{
micro_log_set_level2(micro_log, MICRO_LOG_LEVEL_FATAL);
}
else if (_micro_log_strncmp(line + 6 + spaces, "disabled", 8) == 0)
{
micro_log_set_level2(micro_log, MICRO_LOG_LEVEL_DISABLED);
} else {
error = MICRO_LOG_ERROR_UNKNOWN_LEVEL;
MICRO_LOG_FREE(line);