forked from emilk/loguru
-
Notifications
You must be signed in to change notification settings - Fork 0
/
loguru.hpp
2871 lines (2439 loc) · 94 KB
/
loguru.hpp
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
/*
Loguru logging library for C++, by Emil Ernerfeldt.
www.github.com/emilk/loguru
If you find Loguru useful, please let me know on twitter or in a mail!
Twitter: @ernerfeldt
Mail: [email protected]
Website: www.ilikebigbits.com
# License
This software is in the public domain. Where that dedication is not
recognized, you are granted a perpetual, irrevocable license to copy
and modify this file as you see fit.
# Inspiration
Much of Loguru was inspired by GLOG, https://code.google.com/p/google-glog/.
The whole "single header" and public domain is fully due Sean T. Barrett
and his wonderful stb libraries at https://github.com/nothings/stb.
# Version history
* Version 0.1.0 - 2015-03-22 - Works great on Mac.
* Version 0.2.0 - 2015-09-17 - Removed the only dependency.
* Version 0.3.0 - 2015-10-02 - Drop-in replacement for most of GLOG
* Version 0.4.0 - 2015-10-07 - Single-file!
* Version 0.5.0 - 2015-10-17 - Improved file logging
* Version 0.6.0 - 2015-10-24 - Add stack traces
* Version 0.7.0 - 2015-10-27 - Signals
* Version 0.8.0 - 2015-10-30 - Color logging.
* Version 0.9.0 - 2015-11-26 - ABORT_S and proper handling of FATAL
* Version 1.0.0 - 2016-02-14 - ERROR_CONTEXT
* Version 1.1.0 - 2016-02-19 - -v OFF, -v INFO etc
* Version 1.1.1 - 2016-02-20 - textprintf vs strprintf
* Version 1.1.2 - 2016-02-22 - Remove g_alsologtostderr
* Version 1.1.3 - 2016-02-29 - ERROR_CONTEXT as linked list
* Version 1.2.0 - 2016-03-19 - Add get_thread_name()
* Version 1.2.1 - 2016-03-20 - Minor fixes
* Version 1.2.2 - 2016-03-29 - Fix issues with set_fatal_handler throwing an exception
* Version 1.2.3 - 2016-05-16 - Log current working directory in loguru::init().
* Version 1.2.4 - 2016-05-18 - Custom replacement for -v in loguru::init() by bjoernpollex
* Version 1.2.5 - 2016-05-18 - Add ability to print ERROR_CONTEXT of parent thread.
* Version 1.2.6 - 2016-05-19 - Bug fix regarding VLOG verbosity argument lacking ().
* Version 1.2.7 - 2016-05-23 - Fix PATH_MAX problem.
* Version 1.2.8 - 2016-05-26 - Add shutdown() and remove_all_callbacks()
* Version 1.2.9 - 2016-06-09 - Use a monotonic clock for uptime.
* Version 1.3.0 - 2016-07-20 - Fix issues with callback flush/close not being called.
* Version 1.3.1 - 2016-07-20 - Add LOGURU_UNSAFE_SIGNAL_HANDLER to toggle stacktrace on signals.
* Version 1.3.2 - 2016-07-20 - Add loguru::arguments()
* Version 1.4.0 - 2016-09-15 - Semantic versioning + add loguru::create_directories
* Version 1.4.1 - 2016-09-29 - Customize formating with LOGURU_FILENAME_WIDTH
* Version 1.5.0 - 2016-12-22 - LOGURU_USE_FMTLIB by kolis and LOGURU_WITH_FILEABS by scinart
* Version 1.5.1 - 2017-08-08 - Terminal colors on Windows 10 thanks to looki
# Compiling
Just include <loguru.hpp> where you want to use Loguru.
Then, in one .cpp file:
#define LOGURU_IMPLEMENTATION 1
#include <loguru.hpp>
Make sure you compile with -std=c++11 -lstdc++ -lpthread -ldl
# Usage
#include <loguru.hpp>
// Optional, but useful to time-stamp the start of the log.
// Will also detect verbosity level on command line as -v.
loguru::init(argc, argv);
// Put every log message in "everything.log":
loguru::add_file("everything.log", loguru::Append, loguru::Verbosity_MAX);
// Only log INFO, WARNING, ERROR and FATAL to "latest_readable.log":
loguru::add_file("latest_readable.log", loguru::Truncate, loguru::Verbosity_INFO);
// Only show most relevant things on stderr:
loguru::g_stderr_verbosity = 1;
// Or just go with what Loguru suggests:
char log_path[PATH_MAX];
loguru::suggest_log_path("~/loguru/", log_path, sizeof(log_path));
loguru::add_file(log_path, loguru::FileMode::Truncate, loguru::Verbosity_MAX);
LOG_SCOPE_F(INFO, "Will indent all log messages within this scope.");
LOG_F(INFO, "I'm hungry for some %.3f!", 3.14159);
LOG_F(2, "Will only show if verbosity is 2 or higher");
VLOG_F(get_log_level(), "Use vlog for dynamic log level (integer in the range 0-9, inclusive)");
LOG_IF_F(ERROR, badness, "Will only show if badness happens");
auto fp = fopen(filename, "r");
CHECK_F(fp != nullptr, "Failed to open file '%s'", filename);
CHECK_GT_F(length, 0); // Will print the value of `length` on failure.
CHECK_EQ_F(a, b, "You can also supply a custom message, like to print something: %d", a + b);
// Each function also comes with a version prefixed with D for Debug:
DCHECK_F(expensive_check(x)); // Only checked #if LOGURU_DEBUG_CHECKS
DLOG_F("Only written in debug-builds");
// Turn off writing to stderr:
loguru::g_stderr_verbosity = loguru::Verbosity_OFF;
// Turn off writing err/warn in red:
loguru::g_colorlogtostderr = false;
// Throw exceptions instead of aborting on CHECK fails:
loguru::set_fatal_handler([](const loguru::Message& message){
throw std::runtime_error(std::string(message.prefix) + message.message);
});
If you prefer logging with streams:
#define LOGURU_WITH_STREAMS 1
#include <loguru.hpp>
...
LOG_S(INFO) << "Look at my custom object: " << a.cross(b);
CHECK_EQ_S(pi, 3.14) << "Maybe it is closer to " << M_PI;
Before including <loguru.hpp> you may optionally want to define the following to 1:
LOGURU_DEBUG_LOGGING (default 1 #if !NDEBUG, else 0):
Enables debug versions of logging statements.
LOGURU_DEBUG_CHECKS (default 1 #if !NDEBUG, else 0):
Enables debug versions of checks.
LOGURU_REDEFINE_ASSERT (default 0):
Redefine "assert" call Loguru version (!NDEBUG only).
LOGURU_WITH_STREAMS (default 0):
Add support for _S versions for all LOG and CHECK functions:
LOG_S(INFO) << "My vec3: " << x.cross(y);
CHECK_EQ_S(a, b) << "I expected a and b to be the same!";
This is off by default to keep down compilation times.
LOGURU_REPLACE_GLOG (default 0):
Make Loguru mimic GLOG as close as possible,
including #defining LOG, CHECK, VLOG_IS_ON etc.
LOGURU_REPLACE_GLOG implies LOGURU_WITH_STREAMS.
LOGURU_UNSAFE_SIGNAL_HANDLER (default 1):
Make Loguru try to do unsafe but useful things,
like printing a stack trace, when catching signals.
This may lead to bad things like deadlocks in certain situations.
LOGURU_USE_FMTLIB (default 0):
Use fmtlib formatting. See https://github.com/fmtlib/fmt
This will make loguru.hpp depend on <fmt/format.h>
You will need to link against `fmtlib` or use the `FMT_HEADER_ONLY` preprocessor definition.
Feature by kolis (https://github.com/emilk/loguru/pull/22)
LOGURU_WITH_FILEABS (default 0):
When LOGURU_WITH_FILEABS is defined, a check of file change will be performed on every call to file_log.
If the file is moved, or inode changes, file is reopened using the same FileMode as is done by add_file.
Such a scheme is useful if you have a daemon program that moves the log file every 24 hours and expects new file to be created.
Feature by scinart (https://github.com/emilk/loguru/pull/23).
You can also configure:
loguru::g_flush_interval_ms:
If set to zero Loguru will flush on every line (unbuffered mode).
Else Loguru will flush outputs every g_flush_interval_ms milliseconds (buffered mode).
The default is g_flush_interval_ms=0, i.e. unbuffered mode.
# Notes:
* Any arguments to CHECK:s are only evaluated once.
* Any arguments to LOG functions or LOG_SCOPE are only evaluated iff the verbosity test passes.
* Any arguments to LOG_IF functions are only evaluated if the test passes.
*/
// Disable all warnings from gcc/clang:
#if defined(__clang__)
#pragma clang system_header
#elif defined(__GNUC__)
#pragma GCC system_header
#endif
#ifndef LOGURU_HAS_DECLARED_FORMAT_HEADER
#define LOGURU_HAS_DECLARED_FORMAT_HEADER
#if defined(_MSC_VER)
#include <sal.h> // Needed for _In_z_ etc annotations
#endif
// ----------------------------------------------------------------------------
#ifndef LOGURU_SCOPE_TEXT_SIZE
// Maximum length of text that can be printed by a LOG_SCOPE.
// This should be long enough to get most things, but short enough not to clutter the stack.
#define LOGURU_SCOPE_TEXT_SIZE 196
#endif
#ifndef LOGURU_FILENAME_WIDTH
// Width of the column containing the file name
#define LOGURU_FILENAME_WIDTH 23
#endif
#ifndef LOGURU_THREADNAME_WIDTH
// Width of the column containing the thread name
#define LOGURU_THREADNAME_WIDTH 16
#endif
#ifndef LOGURU_CATCH_SIGABRT
// Should Loguru catch SIGABRT to print stack trace etc?
#define LOGURU_CATCH_SIGABRT 1
#endif
#ifndef LOGURU_REDEFINE_ASSERT
#define LOGURU_REDEFINE_ASSERT 0
#endif
#ifndef LOGURU_WITH_STREAMS
#define LOGURU_WITH_STREAMS 0
#endif
#ifndef LOGURU_REPLACE_GLOG
#define LOGURU_REPLACE_GLOG 0
#endif
#if LOGURU_REPLACE_GLOG
#undef LOGURU_WITH_STREAMS
#define LOGURU_WITH_STREAMS 1
#endif
#ifndef LOGURU_UNSAFE_SIGNAL_HANDLER
#define LOGURU_UNSAFE_SIGNAL_HANDLER 1
#endif
#if LOGURU_IMPLEMENTATION
#undef LOGURU_WITH_STREAMS
#define LOGURU_WITH_STREAMS 1
#endif
#ifndef LOGURU_USE_FMTLIB
#define LOGURU_USE_FMTLIB 0
#endif
#ifndef LOGURU_WITH_FILEABS
#define LOGURU_WITH_FILEABS 0
#endif
// --------------------------------------------------------------------
// Utility macros
#define LOGURU_CONCATENATE_IMPL(s1, s2) s1 ## s2
#define LOGURU_CONCATENATE(s1, s2) LOGURU_CONCATENATE_IMPL(s1, s2)
#ifdef __COUNTER__
# define LOGURU_ANONYMOUS_VARIABLE(str) LOGURU_CONCATENATE(str, __COUNTER__)
#else
# define LOGURU_ANONYMOUS_VARIABLE(str) LOGURU_CONCATENATE(str, __LINE__)
#endif
#if defined(__clang__) || defined(__GNUC__)
// Helper macro for declaring functions as having similar signature to printf.
// This allows the compiler to catch format errors at compile-time.
#define LOGURU_PRINTF_LIKE(fmtarg, firstvararg) __attribute__((__format__ (__printf__, fmtarg, firstvararg)))
#define LOGURU_FORMAT_STRING_TYPE const char*
#elif defined(_MSC_VER)
#define LOGURU_PRINTF_LIKE(fmtarg, firstvararg)
#define LOGURU_FORMAT_STRING_TYPE _In_z_ _Printf_format_string_ const char*
#else
#define LOGURU_PRINTF_LIKE(fmtarg, firstvararg)
#define LOGURU_FORMAT_STRING_TYPE const char*
#endif
// Used to mark log_and_abort for the benefit of the static analyzer and optimizer.
#if defined(_MSC_VER)
#define LOGURU_NORETURN __declspec(noreturn)
#else
#define LOGURU_NORETURN __attribute__((noreturn))
#endif
#if defined(_MSC_VER)
#define LOGURU_PREDICT_FALSE(x) (x)
#define LOGURU_PREDICT_TRUE(x) (x)
#else
#define LOGURU_PREDICT_FALSE(x) (__builtin_expect(x, 0))
#define LOGURU_PREDICT_TRUE(x) (__builtin_expect(!!(x), 1))
#endif
#if defined(_MSC_VER) && !defined(__PRETTY_FUNCTION__)
#define __PRETTY_FUNCTION__ __FUNCTION__
#endif
#if LOGURU_USE_FMTLIB
#include <fmt/format.h>
#endif
// --------------------------------------------------------------------
namespace loguru
{
// Simple RAII ownership of a char*.
class Text
{
public:
explicit Text(char* owned_str) : _str(owned_str) {}
~Text();
Text(Text&& t)
{
_str = t._str;
t._str = nullptr;
}
Text(Text& t) = delete;
Text& operator=(Text& t) = delete;
void operator=(Text&& t) = delete;
const char* c_str() const { return _str; }
bool empty() const { return _str == nullptr || *_str == '\0'; }
char* release()
{
auto result = _str;
_str = nullptr;
return result;
}
private:
char* _str;
};
// Like printf, but returns the formated text.
Text textprintf(LOGURU_FORMAT_STRING_TYPE format, ...) LOGURU_PRINTF_LIKE(1, 2);
// Overloaded for variadic template matching.
Text textprintf();
using Verbosity = int;
#undef FATAL
#undef ERROR
#undef WARNING
#undef INFO
#undef MAX
enum NamedVerbosity : Verbosity
{
// You may use Verbosity_OFF on g_stderr_verbosity, but for nothing else!
Verbosity_OFF = -9, // Never do LOG_F(OFF)
// Prefer to use ABORT_F or ABORT_S over LOG_F(FATAL) or LOG_S(FATAL).
Verbosity_FATAL = -3,
Verbosity_ERROR = -2,
Verbosity_WARNING = -1,
// Normal messages. By default written to stderr.
Verbosity_INFO = 0,
// Same as Verbosity_INFO in every way.
Verbosity_0 = 0,
// Verbosity levels 1-9 are generally not written to stderr, but are written to file.
Verbosity_1 = +1,
Verbosity_2 = +2,
Verbosity_3 = +3,
Verbosity_4 = +4,
Verbosity_5 = +5,
Verbosity_6 = +6,
Verbosity_7 = +7,
Verbosity_8 = +8,
Verbosity_9 = +9,
// Don not use higher verbosity levels, as that will make grepping log files harder.
Verbosity_MAX = +9,
};
struct Message
{
// You would generally print a Message by just concating the buffers without spacing.
// Optionally, ignore preamble and indentation.
Verbosity verbosity; // Already part of preamble
const char* filename; // Already part of preamble
unsigned line; // Already part of preamble
const char* preamble; // Date, time, uptime, thread, file:line, verbosity.
const char* indentation; // Just a bunch of spacing.
const char* prefix; // Assertion failure info goes here (or "").
const char* message; // User message goes here.
};
/* Everything with a verbosity equal or greater than g_stderr_verbosity will be
written to stderr. You can set this in code or via the -v argument.
Set to logurur::Verbosity_OFF to write nothing to stderr.
Default is 0, i.e. only log ERROR, WARNING and INFO are written to stderr.
*/
extern Verbosity g_stderr_verbosity;
extern bool g_colorlogtostderr; // True by default.
extern unsigned g_flush_interval_ms; // 0 (unbuffered) by default.
// May not throw!
typedef void (*log_handler_t)(void* user_data, const Message& message);
typedef void (*close_handler_t)(void* user_data);
typedef void (*flush_handler_t)(void* user_data);
// May throw if that's how you'd like to handle your errors.
typedef void (*fatal_handler_t)(const Message& message);
/* Should be called from the main thread.
You don't *need* to call this, but if you do you get:
* Signal handlers installed
* Program arguments logged
* Working dir logged
* Optional -v verbosity flag parsed
* Main thread name set to "main thread"
* Explanation of the preamble (date, threanmae etc) logged
loguru::init() will look for arguments meant for loguru and remove them.
Arguments meant for loguru are:
-v n Set loguru::g_stderr_verbosity level. Examples:
-v 3 Show verbosity level 3 and lower.
-v 0 Only show INFO, WARNING, ERROR, FATAL (default).
-v INFO Only show INFO, WARNING, ERROR, FATAL (default).
-v WARNING Only show WARNING, ERROR, FATAL.
-v ERROR Only show ERROR, FATAL.
-v FATAL Only show FATAL.
-v OFF Turn off logging to stderr.
Tip: You can set g_stderr_verbosity before calling loguru::init.
That way you can set the default but have the user override it with the -v flag.
Note that -v does not affect file logging (see loguru::add_file).
You can use something else instead of "-v" via verbosity_flag.
You can also set verbosity_flag to nullptr.
*/
void init(int& argc, char* argv[], const char* verbosity_flag = "-v");
// Will call remove_all_callbacks(). After calling this, logging will still go to stderr.
void shutdown();
// What ~ will be replaced with, e.g. "/home/your_user_name/"
const char* home_dir();
/* Returns the name of the app as given in argv[0] but without leading path.
That is, if argv[0] is "../foo/app" this will return "app".
*/
const char* argv0_filename();
// Returns all arguments given to loguru::init(), but escaped with a single space as separator.
const char* arguments();
// Returns the path to the current working dir when loguru::init() was called.
const char* current_dir();
// Returns the part of the path after the last / or \ (if any).
const char* filename(const char* path);
// e.g. "foo/bar/baz.ext" will create the directories "foo/" and "foo/bar/"
bool create_directories(const char* file_path_const);
// Writes date and time with millisecond precision, e.g. "20151017_161503.123"
void write_date_time(char* buff, unsigned buff_size);
// Helper: thread-safe version strerror
Text errno_as_text();
/* Given a prefix of e.g. "~/loguru/" this might return
"/home/your_username/loguru/app_name/20151017_161503.123.log"
where "app_name" is a sanitized version of argv[0].
*/
void suggest_log_path(const char* prefix, char* buff, unsigned buff_size);
enum FileMode { Truncate, Append };
/* Will log to a file at the given path.
Any logging message with a verbosity lower or equal to
the given verbosity will be included.
The function will create all directories in 'path' if needed.
If path starts with a ~, it will be replaced with loguru::home_dir()
To stop the file logging, just call loguru::remove_callback(path) with the same path.
*/
bool add_file(const char* path, FileMode mode, Verbosity verbosity);
/* Will be called right before abort().
You can for instance use this to print custom error messages, or throw an exception.
Feel free to call LOG:ing function from this, but not FATAL ones! */
void set_fatal_handler(fatal_handler_t handler);
/* Will be called on each log messages with a verbosity less or equal to the given one.
Useful for displaying messages on-screen in a game, for example.
The given on_close is also expected to flush (if desired).
*/
void add_callback(const char* id, log_handler_t callback, void* user_data,
Verbosity verbosity,
close_handler_t on_close = nullptr,
flush_handler_t on_flush = nullptr);
// Returns true iff the callback was found (and removed).
bool remove_callback(const char* id);
// Shut down all file logging and any other callback hooks installed.
void remove_all_callbacks();
// Returns the maximum of g_stderr_verbosity and all file/custom outputs.
Verbosity current_verbosity_cutoff();
#if LOGURU_USE_FMTLIB
// Actual logging function. Use the LOG macro instead of calling this directly.
void log(Verbosity verbosity, const char* file, unsigned line, LOGURU_FORMAT_STRING_TYPE format, fmt::ArgList args);
FMT_VARIADIC(void, log, Verbosity, const char*, unsigned, LOGURU_FORMAT_STRING_TYPE)
// Log without any preamble or indentation.
void raw_log(Verbosity verbosity, const char* file, unsigned line, LOGURU_FORMAT_STRING_TYPE format, fmt::ArgList args);
FMT_VARIADIC(void, raw_log, Verbosity, const char*, unsigned, LOGURU_FORMAT_STRING_TYPE)
#else // LOGURU_USE_FMTLIB?
// Actual logging function. Use the LOG macro instead of calling this directly.
void log(Verbosity verbosity, const char* file, unsigned line, LOGURU_FORMAT_STRING_TYPE format, ...) LOGURU_PRINTF_LIKE(4, 5);
// Log without any preamble or indentation.
void raw_log(Verbosity verbosity, const char* file, unsigned line, LOGURU_FORMAT_STRING_TYPE format, ...) LOGURU_PRINTF_LIKE(4, 5);
#endif // !LOGURU_USE_FMTLIB
// Helper class for LOG_SCOPE_F
class LogScopeRAII
{
public:
LogScopeRAII() : _file(nullptr) {} // No logging
LogScopeRAII(Verbosity verbosity, const char* file, unsigned line, LOGURU_FORMAT_STRING_TYPE format, ...) LOGURU_PRINTF_LIKE(5, 6);
~LogScopeRAII();
LogScopeRAII(LogScopeRAII&& other) = default;
private:
LogScopeRAII(const LogScopeRAII&) = delete;
LogScopeRAII& operator=(const LogScopeRAII&) = delete;
void operator=(LogScopeRAII&&) = delete;
Verbosity _verbosity;
const char* _file; // Set to null if we are disabled due to verbosity
unsigned _line;
bool _indent_stderr; // Did we?
long long _start_time_ns;
char _name[LOGURU_SCOPE_TEXT_SIZE];
};
// Marked as 'noreturn' for the benefit of the static analyzer and optimizer.
// stack_trace_skip is the number of extrace stack frames to skip above log_and_abort.
LOGURU_NORETURN void log_and_abort(int stack_trace_skip, const char* expr, const char* file, unsigned line, LOGURU_FORMAT_STRING_TYPE format, ...) LOGURU_PRINTF_LIKE(5, 6);
LOGURU_NORETURN void log_and_abort(int stack_trace_skip, const char* expr, const char* file, unsigned line);
// Flush output to stderr and files.
// If g_flush_interval_ms is set to non-zero, this will be called automatically this often.
// If not set, you do not need to call this at al.
void flush();
template<class T> inline Text format_value(const T&) { return textprintf("N/A"); }
template<> inline Text format_value(const char& v) { return textprintf("%c", v); }
template<> inline Text format_value(const int& v) { return textprintf("%d", v); }
template<> inline Text format_value(const unsigned int& v) { return textprintf("%u", v); }
template<> inline Text format_value(const long& v) { return textprintf("%lu", v); }
template<> inline Text format_value(const unsigned long& v) { return textprintf("%ld", v); }
template<> inline Text format_value(const long long& v) { return textprintf("%llu", v); }
template<> inline Text format_value(const unsigned long long& v) { return textprintf("%lld", v); }
template<> inline Text format_value(const float& v) { return textprintf("%f", v); }
template<> inline Text format_value(const double& v) { return textprintf("%f", v); }
/* Thread names can be set for the benefit of readable logs.
If you do not set the thread name, a hex id will be shown instead.
These thread names may or may not be the same as the system thread names,
depending on the system.
Try to limit the thread name to 15 characters or less. */
void set_thread_name(const char* name);
/* Returns the thread name for this thread.
On OSX this will return the system thread name (setable from both within and without Loguru).
On other systems it will return whatever you set in set_thread_name();
If no thread name is set, this will return a hexadecimal thread id.
length should be the number of bytes available in the buffer.
17 is a good number for length.
right_align_hext_id means any hexadecimal thread id will be written to the end of buffer.
*/
void get_thread_name(char* buffer, unsigned long long length, bool right_align_hext_id);
/* Generates a readable stacktrace as a string.
'skip' specifies how many stack frames to skip.
For instance, the default skip (1) means:
don't include the call to loguru::stacktrace in the stack trace. */
Text stacktrace(int skip = 1);
/* Add a string to be replaced with something else in the stack output.
For instance, instead of having a stack trace look like this:
0x41f541 some_function(std::basic_ofstream<char, std::char_traits<char> >&)
You can clean it up with:
auto verbose_type_name = loguru::demangle(typeid(std::ofstream).name());
loguru::add_stack_cleanup(verbose_type_name.c_str(); "std::ofstream");
So the next time you will instead see:
0x41f541 some_function(std::ofstream&)
`replace_with_this` must be shorter than `find_this`.
*/
void add_stack_cleanup(const char* find_this, const char* replace_with_this);
// Example: demangle(typeid(std::ofstream).name()) -> "std::basic_ofstream<char, std::char_traits<char> >"
Text demangle(const char* name);
// ------------------------------------------------------------------------
/*
Not all terminals support colors, but if they do, and g_colorlogtostderr
is set, Loguru will write them to stderr to make errors in red, etc.
You also have the option to manually use them, via the function below.
Note, however, that if you do, the color codes could end up in your logfile!
This means if you intend to use them functions you should either:
* Use them on the stderr/stdout directly (bypass Loguru).
* Don't add file outputs to Loguru.
* Expect some \e[1m things in your logfile.
Usage:
printf("%sRed%sGreen%sBold green%sClear again\n",
loguru::terminal_red(), loguru::terminal_green(),
loguru::terminal_bold(), loguru::terminal_reset());
If the terminal at hand does not support colors the above output
will just not have funky \e[1m things showing.
*/
// Do the output terminal support colors?
bool terminal_has_color();
// Colors
const char* terminal_black();
const char* terminal_red();
const char* terminal_green();
const char* terminal_yellow();
const char* terminal_blue();
const char* terminal_purple();
const char* terminal_cyan();
const char* terminal_light_gray();
const char* terminal_light_red();
const char* terminal_white();
// Formating
const char* terminal_bold();
const char* terminal_underline();
// You should end each line with this!
const char* terminal_reset();
// --------------------------------------------------------------------
// Error context related:
struct StringStream;
// Use this in your EcEntryBase::print_value overload.
void stream_print(StringStream& out_string_stream, const char* text);
class EcEntryBase
{
public:
EcEntryBase(const char* file, unsigned line, const char* descr);
~EcEntryBase();
EcEntryBase(const EcEntryBase&) = delete;
EcEntryBase(EcEntryBase&&) = delete;
EcEntryBase& operator=(const EcEntryBase&) = delete;
EcEntryBase& operator=(EcEntryBase&&) = delete;
virtual void print_value(StringStream& out_string_stream) const = 0;
EcEntryBase* previous() const { return _previous; }
// private:
const char* _file;
unsigned _line;
const char* _descr;
EcEntryBase* _previous;
};
template<typename T>
class EcEntryData : public EcEntryBase
{
public:
using Printer = Text(*)(T data);
EcEntryData(const char* file, unsigned line, const char* descr, T data, Printer&& printer)
: EcEntryBase(file, line, descr), _data(data), _printer(printer) {}
virtual void print_value(StringStream& out_string_stream) const override
{
const auto str = _printer(_data);
stream_print(out_string_stream, str.c_str());
}
private:
T _data;
Printer _printer;
};
// template<typename Printer>
// class EcEntryLambda : public EcEntryBase
// {
// public:
// EcEntryLambda(const char* file, unsigned line, const char* descr, Printer&& printer)
// : EcEntryBase(file, line, descr), _printer(std::move(printer)) {}
// virtual void print_value(StringStream& out_string_stream) const override
// {
// const auto str = _printer();
// stream_print(out_string_stream, str.c_str());
// }
// private:
// Printer _printer;
// };
// template<typename Printer>
// EcEntryLambda<Printer> make_ec_entry_lambda(const char* file, unsigned line, const char* descr, Printer&& printer)
// {
// return {file, line, descr, std::move(printer)};
// }
template <class T>
struct decay_char_array { using type = T; };
template <unsigned long long N>
struct decay_char_array<const char(&)[N]> { using type = const char*; };
template <class T>
struct make_const_ptr { using type = T; };
template <class T>
struct make_const_ptr<T*> { using type = const T*; };
template <class T>
struct make_ec_type { using type = typename make_const_ptr<typename decay_char_array<T>::type>::type; };
/* A stack trace gives you the names of the function at the point of a crash.
With ERROR_CONTEXT, you can also get the values of select local variables.
Usage:
void process_customers(const std::string& filename)
{
ERROR_CONTEXT("Processing file", filename.c_str());
for (int customer_index : ...)
{
ERROR_CONTEXT("Customer index", customer_index);
...
}
}
The context is in effect during the scope of the ERROR_CONTEXT.
Use loguru::get_error_context() to get the contents of the active error contexts.
Example result:
------------------------------------------------
[ErrorContext] main.cpp:416 Processing file: "customers.json"
[ErrorContext] main.cpp:417 Customer index: 42
------------------------------------------------
Error contexts are printed automatically on crashes, and only on crashes.
This makes them much faster than logging the value of a variable.
*/
#define ERROR_CONTEXT(descr, data) \
const loguru::EcEntryData<loguru::make_ec_type<decltype(data)>::type> \
LOGURU_ANONYMOUS_VARIABLE(error_context_scope_)( \
__FILE__, __LINE__, descr, data, \
static_cast<loguru::EcEntryData<loguru::make_ec_type<decltype(data)>::type>::Printer>(loguru::ec_to_text) ) // For better error messages
/*
#define ERROR_CONTEXT(descr, data) \
const auto LOGURU_ANONYMOUS_VARIABLE(error_context_scope_)( \
loguru::make_ec_entry_lambda(__FILE__, __LINE__, descr, \
[=](){ return loguru::ec_to_text(data); }))
*/
using EcHandle = const EcEntryBase*;
/*
Get a light-weight handle to the error context stack on this thread.
The handle is valid as long as the current thread has no changes to its error context stack.
You can pass the handle to loguru::get_error_context on another thread.
This can be very useful for when you have a parent thread spawning several working thread,
and you want the error context of the parent thread to get printed (too) when there is an
error on the child thread. You can accomplish this thusly:
void foo(const char* parameter)
{
ERROR_CONTEXT("parameter", parameter)
const auto parent_ec_handle = loguru::get_thread_ec_handle();
std::thread([=]{
loguru::set_thread_name("child thread");
ERROR_CONTEXT("parent context", parent_ec_handle);
dangerous_code();
}.join();
}
*/
EcHandle get_thread_ec_handle();
// Get a string describing the current stack of error context. Empty string if there is none.
Text get_error_context();
// Get a string describing the error context of the given thread handle.
Text get_error_context_for(EcHandle ec_handle);
// ------------------------------------------------------------------------
Text ec_to_text(const char* data);
Text ec_to_text(char data);
Text ec_to_text(int data);
Text ec_to_text(unsigned int data);
Text ec_to_text(long data);
Text ec_to_text(unsigned long data);
Text ec_to_text(long long data);
Text ec_to_text(unsigned long long data);
Text ec_to_text(float data);
Text ec_to_text(double data);
Text ec_to_text(long double data);
Text ec_to_text(EcHandle);
/*
You can add ERROR_CONTEXT support for your own types by overloading ec_to_text. Here's how:
some.hpp:
namespace loguru {
Text ec_to_text(MySmallType data)
Text ec_to_text(const MyBigType* data)
} // namespace loguru
some.cpp:
namespace loguru {
Text ec_to_text(MySmallType small_value)
{
// Called only when needed, i.e. on a crash.
std::string str = small_value.as_string(); // Format 'small_value' here somehow.
return Text{strdup(str.c_str())};
}
Text ec_to_text(const MyBigType* big_value)
{
// Called only when needed, i.e. on a crash.
std::string str = big_value->as_string(); // Format 'big_value' here somehow.
return Text{strdup(str.c_str())};
}
} // namespace loguru
Any file that include some.hpp:
void foo(MySmallType small, const MyBigType& big)
{
ERROR_CONTEXT("Small", small); // Copy ´small` by value.
ERROR_CONTEXT("Big", &big); // `big` should not change during this scope!
....
}
*/
} // namespace loguru
// --------------------------------------------------------------------
// Logging macros
// LOG_F(2, "Only logged if verbosity is 2 or higher: %d", some_number);
#define VLOG_F(verbosity, ...) \
((verbosity) > loguru::current_verbosity_cutoff()) ? (void)0 \
: loguru::log(verbosity, __FILE__, __LINE__, __VA_ARGS__)
// LOG_F(INFO, "Foo: %d", some_number);
#define LOG_F(verbosity_name, ...) VLOG_F(loguru::Verbosity_ ## verbosity_name, __VA_ARGS__)
#define VLOG_IF_F(verbosity, cond, ...) \
((verbosity) > loguru::current_verbosity_cutoff() || (cond) == false) \
? (void)0 \
: loguru::log(verbosity, __FILE__, __LINE__, __VA_ARGS__)
#define LOG_IF_F(verbosity_name, cond, ...) \
VLOG_IF_F(loguru::Verbosity_ ## verbosity_name, cond, __VA_ARGS__)
#define VLOG_SCOPE_F(verbosity, ...) \
loguru::LogScopeRAII LOGURU_ANONYMOUS_VARIABLE(error_context_RAII_) = \
((verbosity) > loguru::current_verbosity_cutoff()) ? loguru::LogScopeRAII() : \
loguru::LogScopeRAII(verbosity, __FILE__, __LINE__, __VA_ARGS__)
// Raw logging - no preamble, no indentation. Slightly faster than full logging.
#define RAW_VLOG_F(verbosity, ...) \
((verbosity) > loguru::current_verbosity_cutoff()) ? (void)0 \
: loguru::raw_log(verbosity, __FILE__, __LINE__, __VA_ARGS__)
#define RAW_LOG_F(verbosity_name, ...) RAW_VLOG_F(loguru::Verbosity_ ## verbosity_name, __VA_ARGS__)
// Use to book-end a scope. Affects logging on all threads.
#define LOG_SCOPE_F(verbosity_name, ...) \
VLOG_SCOPE_F(loguru::Verbosity_ ## verbosity_name, __VA_ARGS__)
#define LOG_SCOPE_FUNCTION(verbosity_name) LOG_SCOPE_F(verbosity_name, __PRETTY_FUNCTION__)
// -----------------------------------------------
// ABORT_F macro. Usage: ABORT_F("Cause of error: %s", error_str);
// Message is optional
#define ABORT_F(...) loguru::log_and_abort(0, "ABORT: ", __FILE__, __LINE__, __VA_ARGS__)
// --------------------------------------------------------------------
// CHECK_F macros:
#define CHECK_WITH_INFO_F(test, info, ...) \
LOGURU_PREDICT_TRUE((test) == true) ? (void)0 : loguru::log_and_abort(0, "CHECK FAILED: " info " ", __FILE__, \
__LINE__, ##__VA_ARGS__)
/* Checked at runtime too. Will print error, then call fatal_handler (if any), then 'abort'.
Note that the test must be boolean.
CHECK_F(ptr); will not compile, but CHECK_F(ptr != nullptr); will. */
#define CHECK_F(test, ...) CHECK_WITH_INFO_F(test, #test, ##__VA_ARGS__)
#define CHECK_NOTNULL_F(x, ...) CHECK_WITH_INFO_F((x) != nullptr, #x " != nullptr", ##__VA_ARGS__)
#define CHECK_OP_F(expr_left, expr_right, op, ...) \
do \
{ \
auto val_left = expr_left; \
auto val_right = expr_right; \
if (! LOGURU_PREDICT_TRUE(val_left op val_right)) \
{ \
auto str_left = loguru::format_value(val_left); \
auto str_right = loguru::format_value(val_right); \
auto fail_info = loguru::textprintf("CHECK FAILED: %s %s %s (%s %s %s) ", \
#expr_left, #op, #expr_right, str_left.c_str(), #op, str_right.c_str()); \
auto user_msg = loguru::textprintf(__VA_ARGS__); \
loguru::log_and_abort(0, fail_info.c_str(), __FILE__, __LINE__, \
"%s", user_msg.c_str()); \
} \
} while (false)
#ifndef LOGURU_DEBUG_LOGGING
#ifndef NDEBUG
#define LOGURU_DEBUG_LOGGING 1
#else
#define LOGURU_DEBUG_LOGGING 0
#endif
#endif
#if LOGURU_DEBUG_LOGGING
// Debug logging enabled:
#define DLOG_F(verbosity_name, ...) LOG_F(verbosity_name, __VA_ARGS__)
#define DVLOG_F(verbosity, ...) VLOG_F(verbosity, __VA_ARGS__)
#define DLOG_IF_F(verbosity_name, ...) LOG_IF_F(verbosity_name, __VA_ARGS__)
#define DVLOG_IF_F(verbosity, ...) VLOG_IF_F(verbosity, __VA_ARGS__)
#define DRAW_LOG_F(verbosity_name, ...) RAW_LOG_F(verbosity_name, __VA_ARGS__)
#define DRAW_VLOG_F(verbosity, ...) RAW_VLOG_F(verbosity, __VA_ARGS__)
#else
// Debug logging disabled:
#define DLOG_F(verbosity_name, ...)
#define DVLOG_F(verbosity, ...)
#define DLOG_IF_F(verbosity_name, ...)
#define DVLOG_IF_F(verbosity, ...)
#define DRAW_LOG_F(verbosity_name, ...)
#define DRAW_VLOG_F(verbosity, ...)
#endif
#define CHECK_EQ_F(a, b, ...) CHECK_OP_F(a, b, ==, ##__VA_ARGS__)
#define CHECK_NE_F(a, b, ...) CHECK_OP_F(a, b, !=, ##__VA_ARGS__)
#define CHECK_LT_F(a, b, ...) CHECK_OP_F(a, b, < , ##__VA_ARGS__)
#define CHECK_GT_F(a, b, ...) CHECK_OP_F(a, b, > , ##__VA_ARGS__)
#define CHECK_LE_F(a, b, ...) CHECK_OP_F(a, b, <=, ##__VA_ARGS__)
#define CHECK_GE_F(a, b, ...) CHECK_OP_F(a, b, >=, ##__VA_ARGS__)
#ifndef LOGURU_DEBUG_CHECKS
#ifndef NDEBUG
#define LOGURU_DEBUG_CHECKS 1
#else
#define LOGURU_DEBUG_CHECKS 0
#endif
#endif
#if LOGURU_DEBUG_CHECKS
// Debug checks enabled:
#define DCHECK_F(test, ...) CHECK_F(test, ##__VA_ARGS__)
#define DCHECK_NOTNULL_F(x, ...) CHECK_NOTNULL_F(x, ##__VA_ARGS__)
#define DCHECK_EQ_F(a, b, ...) CHECK_EQ_F(a, b, ##__VA_ARGS__)
#define DCHECK_NE_F(a, b, ...) CHECK_NE_F(a, b, ##__VA_ARGS__)
#define DCHECK_LT_F(a, b, ...) CHECK_LT_F(a, b, ##__VA_ARGS__)
#define DCHECK_LE_F(a, b, ...) CHECK_LE_F(a, b, ##__VA_ARGS__)
#define DCHECK_GT_F(a, b, ...) CHECK_GT_F(a, b, ##__VA_ARGS__)
#define DCHECK_GE_F(a, b, ...) CHECK_GE_F(a, b, ##__VA_ARGS__)
#else
// Debug checks disabled:
#define DCHECK_F(test, ...)
#define DCHECK_NOTNULL_F(x, ...)
#define DCHECK_EQ_F(a, b, ...)
#define DCHECK_NE_F(a, b, ...)
#define DCHECK_LT_F(a, b, ...)
#define DCHECK_LE_F(a, b, ...)
#define DCHECK_GT_F(a, b, ...)
#define DCHECK_GE_F(a, b, ...)
#endif // NDEBUG
#ifdef LOGURU_REDEFINE_ASSERT
#undef assert
#ifndef NDEBUG
// Debug:
#define assert(test) CHECK_WITH_INFO_F(!!(test), #test) // HACK
#else
#define assert(test)
#endif
#endif // LOGURU_REDEFINE_ASSERT
#endif // LOGURU_HAS_DECLARED_FORMAT_HEADER
// ----------------------------------------------------------------------------
// .dP"Y8 888888 88""Yb 888888 db 8b d8 .dP"Y8
// `Ybo." 88 88__dP 88__ dPYb 88b d88 `Ybo."
// o.`Y8b 88 88"Yb 88"" dP__Yb 88YbdP88 o.`Y8b
// 8bodP' 88 88 Yb 888888 dP""""Yb 88 YY 88 8bodP'
#if LOGURU_WITH_STREAMS