-
Notifications
You must be signed in to change notification settings - Fork 104
/
Debug.h
1217 lines (1030 loc) · 42.8 KB
/
Debug.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
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
#ifndef Corrade_Utility_Debug_h
#define Corrade_Utility_Debug_h
/*
This file is part of Corrade.
Copyright © 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016,
2017, 2018, 2019, 2020, 2021, 2022, 2023
Vladimír Vondruš <mosra@centrum.cz>
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
/** @file
* @brief Class @ref Corrade::Utility::Debug, @ref Corrade::Utility::Warning, @ref Corrade::Utility::Error, @ref Corrade::Utility::Fatal
* @see @ref Corrade/Utility/DebugStl.h
*/
#include <iosfwd>
#include <type_traits>
#include <utility> /** @todo consider putting this away as well (900 LOC) */
#include "Corrade/Containers/EnumSet.h"
#include "Corrade/Utility/TypeTraits.h" /* IsIterable, IsStringLike, CORRADE_SOURCE_LOCATION_BUILTINS_SUPPORTED */
#include "Corrade/Utility/Utility.h"
#include "Corrade/Utility/visibility.h"
namespace Corrade { namespace Utility {
#ifdef CORRADE_BUILD_DEPRECATED
/**
@brief Source location support in debug output
@m_deprecated_since_latest Use @ref CORRADE_SOURCE_LOCATION_BUILTINS_SUPPORTED
instead.
*/
#if defined(DOXYGEN_GENERATING_OUTPUT) || defined(CORRADE_SOURCE_LOCATION_BUILTINS_SUPPORTED)
#define CORRADE_UTILITY_DEBUG_HAS_SOURCE_LOCATION
#endif
#endif
#ifdef CORRADE_SOURCE_LOCATION_BUILTINS_SUPPORTED
namespace Implementation { struct DebugSourceLocation; }
#endif
/**
@brief Debug output handler
Provides convenient stream interface for passing data to debug output (standard
output). Data are by default separated with spaces and last value is enclosed
with newline character. Example usage:
@snippet Utility.cpp Debug-usage
Support for printing more types can be added by implementing an overload of
@ref operator<<(Debug&, const T&) for given type.
@section Utility-Debug-stl Printing STL types
To optimize compile times, the @ref Corrade/Utility/Debug.h header provides
only support for printing builtin types, generic iterable containers and
@ref std::pair. Printing of @ref std::string and @ref std::tuple is possible if
you include @ref Corrade/Utility/DebugStl.h. The support is provided in a
separate header to avoid unconditional @cpp #include <string> @ce or
@cpp #include <tuple> @ce, which significantly affect compile times. This
header also provides a fallback to @ref std::ostream @cpp operator<<() @ce
overloads, if there's no @cpp operator<<() @ce implemented for printing given
type using @ref Debug. Note that printing @ref std::vector or @ref std::map
containers is already possible with the generic iterable container support in
@ref Corrade/Utility/Debug.h.
On compilers that support C++17 and @ref std::string_view, support for printing
it is provided @ref Corrade/Utility/DebugStlStringView.h. For
similar reasons, it's a dedicated header to avoid unconditional
@cpp #include <string_view> @ce, but this one is even significantly heavier
than the @ref string "<string>" etc. includes on certain implementations, so
it's separate from the others as well.
@section Utility-Debug-scoped-output Scoped output redirection
Output specified in class constructor is used for all instances created during
that instance lifetime. @ref Debug, @ref Warning and @ref Error classes outputs
can be controlled separately:
@snippet Utility.cpp Debug-scoped-output
@section Utility-Debug-modifiers Output modifiers
It's possible to modify the output behavior by calling @ref setFlags() or
@ref setImmediateFlags(). The latter function applies given flag only to the
immediately following value (and then it gets reset back) instead of all
following values. For convenience, the operation of @ref setFlags() /
@ref setImmediateFlags() can be done or by passing a special function to the
output stream.
@subsection Utility-Debug-modifiers-whitespace Explicit whitespace control
Sometimes you might not want to have everything separated by spaces or having
newline at the end --- use @ref Flag::NoNewlineAtTheEnd or the @ref nospace
modifier:
@snippet Utility.cpp Debug-modifiers-whitespace
@subsection Utility-Debug-modifiers-base Printing numbers in a different base
With @ref Flag::Hex or the @ref hex modifier, integers will be printed as
hexadecimal. Pointer values are printed as hexadecimal always, cast them to an
integer type to print them as decimal.
@snippet Utility.cpp Debug-modifiers-base
@subsection Utility-Debug-modifiers-colors Colored output
It is possible to color the output using @ref color(), @ref boldColor() and
@ref invertedColor(). The color is automatically reset to previous value on
destruction to avoid messing up the terminal, you can also use
@ref resetColor() to reset it explicitly.
@snippet Utility.cpp Debug-modifiers-colors
@include UtilityDebug-color.ansi
On POSIX the coloring is done using ANSI color escape sequences and works both
when outputting to a terminal or any other stream. On Windows, by default due
to a platform limitation, the colored output works only when outputting
directly to a terminal without any intermediate buffer. See
@ref CORRADE_UTILITY_USE_ANSI_COLORS for possible alternative.
Note that colors make sense only when they finally appear in a terminal and not
when redirecting output to file. You can control this by setting
@ref Flag::DisableColors based on value of @ref isTty(), for example:
@snippet Utility.cpp Debug-modifiers-colors-disable
Similarly as with scoped output redirection, colors can be also scoped:
@snippet Utility.cpp Debug-modifiers-colors-scoped
With @ref Flag::Color and/or the @ref color modifier, some types can be also
printed as actual 24bit colors. The @ref operator<<(unsigned char) printer can
interpret 8bit values as grayscale colors and other libraries may provide
support for other types. For example, printing a container of 8bit values
together with the @ref packed modifier:
@snippet Utility.cpp Debug-color
This prints the below output on terminals that support it. See the operator
documentation for more information.
@include UtilityDebug-color-grayscale.ansi
@section Utility-Debug-source-location Source location
Similarly to the [dbg! macro in Rust](https://blog.rust-lang.org/2019/01/17/Rust-1.32.0.html#the-dbg-macro),
on supported compilers the utility is able to print source file location and
line where the debug output was executed, improving the "printf debugging"
experience. By default no source location info is printed, in order to do that
prefix the @ref Debug instantiation with an exclamation mark. Additionally,
an otherwise unused exclamated instantiation prints just the file + line alone
(in contrast to unexclamated instantiaton, which is a no-op):
@snippet Utility.cpp Debug-source-location
The above code then may print something like this:
@code{.shell-session}
main.cpp:10: the result is 42
main.cpp:11: the result is 5.25
main.cpp:13
and finally, 42
@endcode
At the moment, this feature is available on GCC at least since version 4.8,
Clang 9+ and MSVC 2019 16.6 and newer. Elsewhere it behaves like the
unexclamated version. You can check for its availability using the
@ref CORRADE_SOURCE_LOCATION_BUILTINS_SUPPORTED predefined macro.
@section Utility-Debug-windows ANSI color support and UTF-8 output on Windows
See the @ref main "Corrade::Main" library for more information about a
convenient way to support ANSI colors and UTF-8 output encoding on Windows.
@section Utility-Debug-multithreading Thread safety
If Corrade is compiled with @ref CORRADE_BUILD_MULTITHREADED enabled (the
default), scoped output redirection and coloring is done thread-locally. This
might cause some performance penalties --- if you are sure that you will never
need to handle these per-thread (and won't need any other functionality enabled
by this option either), build Corrade with the option disabled.
@see @ref Warning, @ref Error, @ref Fatal, @ref CORRADE_ASSERT(),
@ref CORRADE_INTERNAL_ASSERT(), @ref CORRADE_INTERNAL_ASSERT_OUTPUT(),
@ref AndroidLogStreamBuffer, @ref format(), @relativeref{Utility,print()}
@todo Output to more ostreams at once
*/
class CORRADE_UTILITY_EXPORT Debug {
public:
/**
* @brief Debug output flag
* @m_since{2019,10}
*
* @see @ref Flags, @ref Debug(Flags)
*/
enum class Flag: unsigned short {
/** Don't put newline at the end on destruction */
NoNewlineAtTheEnd = 1 << 0,
/**
* Disable colored output in @ref color(), @ref boldColor() and
* @ref resetColor().
* @see @ref isTty()
* @note Note that on @ref CORRADE_TARGET_WINDOWS "Windows" the
* colored output by default works only if outputting directly
* to the console. See also @ref CORRADE_UTILITY_USE_ANSI_COLORS.
*/
DisableColors = 1 << 1,
/**
* Print without spaces between values.
* @see @ref nospace, @ref space
*/
NoSpace = 1 << 2,
/**
* Print complex values (such as containers) in a packed form.
* @see @ref packed, @ref operator<<(Debug&, const Iterable&)
*/
Packed = 1 << 3,
/**
* Print colored values as colored squares in the terminal.
* @see @ref color, @ref operator<<(unsigned char)
*/
Color = 1 << 4,
/* Bit 5 and 6 reserved for Bin and Oct */
/**
* Print integer values as lowercase hexadecimal prefixed with
* `0x`, e.g. @cb{.shell-session} 0xc0ffee @ce instead of
* @cb{.shell-session} 12648430 @ce.
* @see @ref hex, @ref operator<<(const void*)
* @m_since_latest
*/
Hex = 1 << 7
/* When adding values, don't forget to adapt InternalFlag as well
and update PublicFlagMask in Debug.cpp */
};
/**
* @brief Debug output flags
* @m_since{2019,10}
*
* @see @ref Debug(Flags)
*/
typedef Containers::EnumSet<Flag> Flags;
/** @{ @name Output modifiers
* See @ref Utility-Debug-modifiers for more information.
*/
/**
* @brief Debug output modifier
*
* @see @ref nospace, @ref newline, @ref space,
* @ref operator<<(Modifier)
*/
typedef void(*Modifier)(Debug&);
/**
* @brief Output color
*
* @see @ref color(), @ref boldColor(), @ref invertedColor()
*/
enum class Color: char {
/**
* Black
*
* @attention The non-bold version of this color is often invisible
* on terminals with dark background. You might want to use
* @ref Color::Default instead to ensure visibility on both
* bright and dark backgrounds.
*/
Black = 0,
/** Red */
#if !defined(CORRADE_TARGET_WINDOWS) || defined(CORRADE_UTILITY_USE_ANSI_COLORS)
Red = 1,
#else
Red = 4,
#endif
/** Green */
Green = 2,
/** Yellow */
#if !defined(CORRADE_TARGET_WINDOWS) || defined(CORRADE_UTILITY_USE_ANSI_COLORS)
Yellow = 3,
#else
Yellow = 6,
#endif
/** Blue */
#if !defined(CORRADE_TARGET_WINDOWS) || defined(CORRADE_UTILITY_USE_ANSI_COLORS)
Blue = 4,
#else
Blue = 1,
#endif
/** Magenta */
Magenta = 5,
/** Cyan */
#if !defined(CORRADE_TARGET_WINDOWS) || defined(CORRADE_UTILITY_USE_ANSI_COLORS)
Cyan = 6,
#else
Cyan = 3,
#endif
/**
* White
*
* @attention The bold version of this color is often invisible
* on terminals with bright background. You might want to use
* @ref Color::Default instead to ensure visibility on both
* bright and dark backgrounds.
*/
White = 7,
/** Default (implementation/style-defined) */
#if !defined(CORRADE_TARGET_WINDOWS) || defined(CORRADE_UTILITY_USE_ANSI_COLORS)
Default = 9
#else
Default = 7
#endif
};
/**
* @brief Don't put space before next value
*
* Debug output by default separates values with space, this disables
* it for the immediately following value. The default behavior is
* then restored. The following line outputs
* @cb{.shell-session} Value: 16, 24 @ce:
*
* @snippet Utility.cpp Debug-nospace
*
* @see @ref Flag::NoSpace, @ref space, @ref newline
*/
static void nospace(Debug& debug) {
debug._immediateFlags |= InternalFlag::NoSpace;
}
/**
* @brief Output a newline
*
* Puts a newline (not surrounded by spaces) to the output. The
* following two lines are equivalent:
*
* @snippet Utility.cpp Debug-newline
*
* and their output is
*
* @code{.shell-session}
* Value:
* 16
* @endcode
*
* @see @ref nospace
*/
static void newline(Debug& debug) {
debug << nospace << "\n" << nospace;
}
/**
* @brief Output a space
* @m_since{2020,06}
*
* Puts a space (not surrounded by additional spaces) to the output.
* Useful for adding an explicit leading space or for delimiting values
* with spaces when @ref Flag::NoSpace is set. The last two lines are
* equivalent:
*
* @snippet Utility.cpp Debug-space
*
* and the output is
*
* @code{.shell-session}
* Value:
* 16
* @endcode
*
* @see @ref nospace, @ref newline
*/
static void space(Debug& debug) {
debug << nospace << " " << nospace;
}
/**
* @brief Set output color
*
* Resets previous @ref color(), @ref boldColor() or
* @ref invertedColor() setting. The color is also automatically reset
* on object destruction to a value that was active in outer scope. If
* @ref Flag::DisableColors was set, this function does nothing.
*/
static Modifier color(Color color);
/**
* @brief Set bold output color
*
* Resets previous @ref color(), @ref boldColor() or
* @ref invertedColor() setting. The color is also automatically reset
* on object destruction to a value that was active in outer scope. If
* @ref Flag::DisableColors was set, this function does nothing.
*/
static Modifier boldColor(Color color);
#if !defined(CORRADE_TARGET_WINDOWS) || defined(CORRADE_UTILITY_USE_ANSI_COLORS)
/**
* @brief Set inverted output color
* @m_since_latest
*
* The @p color is used for background while foreground is rendered
* with the terminal background color instead. Resets previous
* @ref color(), @ref boldColor() or @ref invertedColor() setting. The
* color is also automatically reset on object destruction to a value
* that was active in outer scope. If @ref Flag::DisableColors was set,
* this function does nothing.
* @partialsupport Not available on @ref CORRADE_TARGET_WINDOWS "Windows"
* with @ref CORRADE_UTILITY_USE_ANSI_COLORS disabled.
*/
static Modifier invertedColor(Color color);
#endif
/**
* @brief Reset output color
*
* Resets any previous @ref color(), @ref boldColor() or
* @ref invertedColor() setting to a value that was active in outer
* scope. The same is also automatically done on object destruction. If
* the color was not changed by this instance or
* @ref Flag::DisableColors was set, this function does nothing.
*/
static void resetColor(Debug& debug);
/**
* @brief Print the next value in a packed form
* @m_since{2019,10}
*
* Enables a more compact output for types that support it (such as
* iterable containers).
* @see @ref Flag::Packed, @ref operator<<(Debug&, const Iterable&)
*/
static void packed(Debug& debug) {
debug._immediateFlags |= InternalFlag::Packed;
}
/**
* @brief Print the next value as a color
* @m_since{2019,10}
*
* Prints color-like values as actual 24bit ANSI color sequences.
* @see @ref Flag::Color, @ref operator<<(unsigned char)
*/
static void color(Debug& debug) {
debug._immediateFlags |= InternalFlag::Color;
}
/**
* @brief Print the next value as hexadecimal
* @m_since_latest
*
* If the next value is integer, it's printed as lowercase hexadecimal
* prefixed with `0x` e.g. @cb{.shell-session} 0xc0ffee @ce instead of
* @cb{.shell-session} 12648430 @ce.
* @see @ref Flag::Hex, @ref operator<<(const void*)
*/
static void hex(Debug& debug) {
debug._immediateFlags |= InternalFlag::Hex;
}
/**
* @brief Debug output modification
*
* See @ref Utility-Debug-modifiers for more information.
*/
Debug& operator<<(Modifier f) {
f(*this);
return *this;
}
/* Since 1.8.17, the original short-hand group closing doesn't work
anymore. FFS. */
/**
* @}
*/
/**
* @brief Default debug output stream
* @m_since{2019,10}
*
* Debug output when no output redirection happens. A pointer to
* @ref std::cout.
* @see @ref output()
*/
static std::ostream* defaultOutput();
/**
* @brief Current debug output stream
*
* Debug output constructed with the @ref Debug(Flags) constructor will
* be using this output stream.
* @see @ref defaultOutput()
*/
static std::ostream* output();
/**
* @brief Whether given output stream is a TTY
*
* Useful for deciding whether to use ANSI colored output using
* @ref Flag::DisableColors. Returns @cpp true @ce if @p output is a
* pointer to @ref std::cout / @ref std::cerr and the stream is not
* redirected to a file, @cpp false @ce otherwise. Calls @cpp isatty() @ce
* on Unix-like systems and Windows with @ref CORRADE_UTILITY_USE_ANSI_COLORS
* enabled, calls Windows APIs if @ref CORRADE_UTILITY_USE_ANSI_COLORS
* is disabled. On platforms without @cpp isatty() @ce equivalent
* returns always @cpp false @ce.
*
* @note Returns @cpp false @ce when running inside Xcode even though
* @cpp isatty() @ce reports a positive value, because Xcode is
* not able to handle ANSI colors inside the output view.
* @note Uses Node.js @cb{.js} process.stdout.isTTY @ce /
* @cb{.js} process.stderr.isTTY @ce instead of @cpp isatty() @ce
* on @ref CORRADE_TARGET_EMSCRIPTEN "Emscripten" because
* @cpp isatty() @ce is not able to detect file redirection.
*/
static bool isTty(std::ostream* output);
/**
* @brief Whether current debug output is a TTY
*
* Calls @ref isTty(std::ostream*) with output of enclosing @ref Debug
* instance or with @ref std::cerr if there isn't any.
* @see @ref Warning::isTty(), @ref Error::isTty()
*/
static bool isTty();
/**
* @brief Default constructor
* @param flags Output flags
*
* Uses output of enclosing @ref Debug instance or uses @ref std::cout
* if there isn't any.
*/
explicit Debug(Flags flags = {});
/**
* @brief Constructor
* @param output Stream where to put debug output. If set to
* @cpp nullptr @ce, no debug output will be written anywhere.
* @param flags Output flags
*
* All new instances created using the default @ref Debug() constructor
* during lifetime of this instance will inherit the output set in
* @p output.
*/
explicit Debug(std::ostream* output, Flags flags = {});
/** @brief Copying is not allowed */
Debug(const Debug&) = delete;
/** @brief Move constructor */
Debug(Debug&&) = default;
/**
* @brief Destructor
*
* Resets the output redirection back to the output of enclosing scope.
* If there was any output, adds newline at the end. Also resets output
* color modifier, if there was any.
* @see @ref resetColor()
*/
~Debug();
/** @brief Copying is not allowed */
Debug& operator=(const Debug&) = delete;
/** @brief Move assignment is not allowed */
Debug& operator=(Debug&&) = delete;
/**
* @brief Flags applied for all following values
* @m_since{2019,10}
*
* @see @ref Utility-Debug-modifiers, @ref immediateFlags()
*/
Flags flags() const;
/**
* @brief Set flags applied for all following values
* @m_since{2019,10}
*
* @see @ref Utility-Debug-modifiers, @ref setImmediateFlags()
*/
void setFlags(Flags flags);
/**
* @brief Flags applied for the immediately following value
* @m_since{2019,10}
*
* Returned value is a combination of @ref flags() and immediate flags.
* The immediate part gets reset after a value is printed.
* @see @ref Utility-Debug-modifiers
*/
Flags immediateFlags() const;
/**
* @brief Set flags to be applied for the immediately following value
* @m_since{2019,10}
*
* Unlike flags set with @ref setFlags(), these get applied only to the
* immediately following value and reset after.
* @see @ref Utility-Debug-modifiers, @ref nospace
*/
void setImmediateFlags(Flags flags);
/**
* @brief Print string to debug output
*
* If there is already something in the output, puts a space before
* the value, unless @ref nospace was set immediately before.
* @see @ref operator<<(Debug&, const std::string&),
* @ref operator<<(Debug&, const T&)
*/
Debug& operator<<(const char* value);
/**
* @overload
* @m_since_latest
*/
Debug& operator<<(Containers::StringView value);
/* Unfortunately we can't have just a StringView overload because
when StringStl.h is included, printing a String also matches
operator<<(DebugOstreamFallback&&), causing ambiguity. And when we
have a String overload, we need a MutableStringView one as well,
because otherwise there's an ambiguity between StringView and
String. Sigh. */
/**
* @overload
* @m_since_latest
*/
Debug& operator<<(Containers::MutableStringView value);
/**
* @overload
* @m_since_latest
*/
Debug& operator<<(const Containers::String& value);
/**
* @brief Print a pointer value to debug output
*
* The value is printed in lowercase hexadecimal prefixed with `0x`,
* for example @cb{.shell-session} 0xdeadbeef @ce. Equivalent to
* enabling @ref Flag::Hex or using the @ref hex modifier and printing
* @cpp reinterpret_cast<std::uintptr_t>(value) @ce instead of
* @cpp value @ce.
*/
Debug& operator<<(const void* value);
/**
* @brief Print a boolean value to debug output
*
* The value is printed as literal @cb{.shell-session} true @ce or
* @cb{.shell-session} false @ce.
*/
Debug& operator<<(bool value);
/**
* @brief Print char to debug output
*
* Printed as a numeric value.
*/
Debug& operator<<(char value);
/**
* @brief Print unsigned char to debug output
*
* If @ref Flag::Color is enabled or @ref color was set immediately
* before, prints the value as a grayscale ANSI 24bit color escape
* sequence using two successive Unicode block characters (to have it
* roughly square). To preserve at least some information when text is
* copied, the square consists of one of the five
* @cb{.shell-session} ░▒▓█ @ce shades, however the color is set for
* both foreground and background so the actual block character is
* indistinguishable when seen on a terminal. See the
* @ref Utility-Debug-modifiers-colors "class documentation" for more
* information.
*
* If @ref Flag::Color is enabled and @ref Flag::DisableColors is set,
* only the shaded character is used, without any ANSI color escape
* sequence.
*
* If @ref Flag::Color is not enabled, the value is printed as a
* number.
*/
Debug& operator<<(unsigned char value);
Debug& operator<<(int value); /**< @overload */
Debug& operator<<(long value); /**< @overload */
Debug& operator<<(long long value); /**< @overload */
Debug& operator<<(unsigned value); /**< @overload */
Debug& operator<<(unsigned long value); /**< @overload */
Debug& operator<<(unsigned long long value); /**< @overload */
/**
* @brief Print `float` value to debug output
*
* Prints the value with 6 significant digits.
*/
Debug& operator<<(float value);
/**
* @brief Print `double` value to debug output
*
* Prints the value with 15 significant digits.
*/
Debug& operator<<(double value);
/**
* @brief Print `long double` value to debug output
*
* Prints the value with 18 significant digits on platforms with 80-bit
* @cpp long double @ce and 15 digits on platforms
* @ref CORRADE_LONG_DOUBLE_SAME_AS_DOUBLE "where it is 64-bit".
*/
Debug& operator<<(long double value);
/**
* @brief Print UTF-32 character to debug output
*
* Prints the value as Unicode codepoint, i.e. @cb{.shell-session} U+0061 @ce.
*/
Debug& operator<<(char32_t value);
/**
* @brief Print UTF-32 character literal to debug output
*
* Prints the value as list of Unicode codepoints, i.e.
* @cb{.shell-session} {U+0061, U+0062, U+0063} @ce.
*/
Debug& operator<<(const char32_t* value);
/**
* @brief Print a nullptr to debug output
*
* Prints the value as @cb{.shell-session} nullptr @ce.
*/
Debug& operator<<(std::nullptr_t);
#ifdef DOXYGEN_GENERATING_OUTPUT
private:
#endif
/* Used by the out-of-class operator<<(Debug&, const std::string&) and
operator<<(Debug&, Implementation::DebugOstreamFallback&&) which is
defined in DebugStl.h. */
template<class T> CORRADE_UTILITY_LOCAL Debug& print(const T& value);
#ifndef DOXYGEN_GENERATING_OUTPUT
protected:
#else
private:
#endif
std::ostream* _output;
enum class InternalFlag: unsigned short {
/* Values matching the Flag enum */
NoNewlineAtTheEnd = 1 << 0,
DisableColors = 1 << 1,
NoSpace = 1 << 2,
Packed = 1 << 3,
Color = 1 << 4,
/* Bit 5 and 6 reserved for Bin and Oct */
Hex = 1 << 7,
ValueWritten = 1 << 8,
ColorWritten = 1 << 9
};
typedef Containers::EnumSet<InternalFlag> InternalFlags;
CORRADE_ENUMSET_FRIEND_OPERATORS(InternalFlags)
CORRADE_UTILITY_LOCAL void cleanupOnDestruction(); /* Needed for Fatal */
InternalFlags _flags;
InternalFlags _immediateFlags;
/* 0 / 4 bytes free */
private:
#ifdef CORRADE_SOURCE_LOCATION_BUILTINS_SUPPORTED
friend Implementation::DebugSourceLocation;
#endif
template<Color c, bool bold> CORRADE_UTILITY_LOCAL static Modifier colorInternal();
#if !defined(CORRADE_TARGET_WINDOWS) || defined(CORRADE_UTILITY_USE_ANSI_COLORS)
template<Color c> CORRADE_UTILITY_LOCAL static Modifier invertedColorInternal();
#endif
CORRADE_UTILITY_LOCAL void resetColorInternal();
std::ostream* _previousGlobalOutput;
#if defined(CORRADE_TARGET_WINDOWS) && !defined(CORRADE_UTILITY_USE_ANSI_COLORS)
unsigned short _previousColorAttributes = 0xffff;
#else
Color _previousColor;
bool _previousColorBold, _previousColorInverted;
#endif
/* 1 byte free */
#ifdef CORRADE_SOURCE_LOCATION_BUILTINS_SUPPORTED
int _sourceLocationLine{};
const char* _sourceLocationFile{};
#endif
};
/** @debugoperatorclassenum{Debug,Debug::Color} */
CORRADE_UTILITY_EXPORT Debug& operator<<(Debug& debug, Debug::Color value);
/** @debugoperatorclassenum{Debug,Debug::Flag} */
CORRADE_UTILITY_EXPORT Debug& operator<<(Debug& debug, Debug::Flag value);
/** @debugoperatorclassenum{Debug,Debug::Flags} */
CORRADE_UTILITY_EXPORT Debug& operator<<(Debug& debug, Debug::Flags value);
CORRADE_ENUMSET_OPERATORS(Debug::Flags)
#ifdef CORRADE_SOURCE_LOCATION_BUILTINS_SUPPORTED
namespace Implementation {
struct CORRADE_UTILITY_EXPORT DebugSourceLocation {
#if defined(CORRADE_TARGET_GCC) || defined(CORRADE_TARGET_CLANG) || defined(CORRADE_TARGET_MSVC)
/* Not using std::source_location because it's not in libc++ 9 yet and
GCC version has a C++14 usage of constexpr */
/*implicit*/ DebugSourceLocation(Debug&& debug, const char* file = __builtin_FILE(), int line = __builtin_LINE());
#else
#error this needs to be implemented for new compilers
#endif
Debug* debug;
};
}
/** @relatesalso Debug
@brief Prefix the output with source location
@m_since{2020,06}
Only on supported compilers, does nothing otherwise. See
@ref Utility-Debug-source-location for more information.
*/
/* Unfortunately it's not possible to add additional (default) arguments to
operator! so we need to use a implicitly convertible type and capture the
source location in its constructor */
inline Debug& operator!(Implementation::DebugSourceLocation debug) {
return *debug.debug;
}
#else
inline Debug& operator!(Debug&& debug) { return debug; }
#endif
#ifndef DOXYGEN_GENERATING_OUTPUT
/* so Debug() << value works */
template<class T> inline Debug& operator<<(Debug&& debug, const T& value) {
return debug << value;
}
#endif
#ifdef DOXYGEN_GENERATING_OUTPUT
/** @relatesalso Debug
@brief Operator for printing custom types to debug output
@param debug Debug class
@param value Value to be printed
Support for printing custom types (i.e. those not handled by @ref std::iostream)
can be added by implementing this function for given type.
The function should convert the type to one of supported types (such as the
builtin types or @ref std::string) and then call @ref Debug::operator<<() with
it. You can also use @ref Debug::nospace and @ref Debug::newline.
*/
template<class T> Debug& operator<<(Debug& debug, const T& value);
#endif
/** @relatesalso Debug
@brief Operator for printing iterable types to debug output
Prints the value as @cb{.shell-session} {a, b, …} @ce. If the type contains
a nested iterable type, the values are separated by newlines. Specifying
@ref Debug::Flag::Packed or using @ref Debug::packed will print the values
tightly-packed without commas and spaces in between.
*/
#ifdef DOXYGEN_GENERATING_OUTPUT
template<class Iterable> Debug& operator<<(Debug& debug, const Iterable& value)
#else
/* libc++ from Apple's Clang "4.2" (3.2-svn) doesn't have constexpr operator
bool for std::integral_constant, thus we need to use ::value instead */
template<class Iterable> Debug& operator<<(typename std::enable_if<IsIterable<Iterable>::value && !IsStringLike<Iterable>::value, Debug&>::type debug, const Iterable& value)
#endif
{
/* True if the values themselves are also containers. A string is
technically a container too, but printing it as separate chars would be
silly. */
constexpr bool hasNestedContainer = IsIterable<decltype(*value.begin())>::value && !IsStringLike<decltype(*value.begin())>::value;
/* Nested containers should get printed with the same flags, so make all
immediate flags temporarily global -- except NoSpace, unless it's also
set globally */
const Debug::Flags prevFlags = debug.flags();
debug.setFlags(prevFlags | (debug.immediateFlags() & ~Debug::Flag::NoSpace));
const char *beg, *sep, *end;
if(debug.immediateFlags() & Debug::Flag::Packed) {
beg = end = "";
sep = hasNestedContainer ? "\n" : "";
} else {
beg = "{";
end = "}";
sep = hasNestedContainer ? ",\n " : ", ";
}
debug << beg << Debug::nospace;
for(auto it = value.begin(); it != value.end(); ++it) {
if(it != value.begin())
debug << Debug::nospace << sep << Debug::nospace;
debug << *it;
}
debug << Debug::nospace << end;
/* Reset the original flags back */
debug.setFlags(prevFlags);
return debug;
}
/** @relatesalso Debug
@brief Print a @ref std::pair to debug output
Prints the value as @cb{.shell-session} (first, second) @ce. Unlike
@ref operator<<(Debug& debug, const Iterable& value), the output is not
affected by @ref Debug::Flag::Packed / @ref Debug::packed.
*/
template<class T, class U> Debug& operator<<(Debug& debug, const std::pair<T, U>& value) {
/* Nested values should get printed with the same flags, so make all
immediate flags temporarily global -- except NoSpace, unless it's also
set globally */
const Debug::Flags prevFlags = debug.flags();
debug.setFlags(prevFlags | (debug.immediateFlags() & ~Debug::Flag::NoSpace));
debug << "(" << Debug::nospace << value.first << Debug::nospace << "," << value.second << Debug::nospace << ")";
/* Reset the original flags back */
debug.setFlags(prevFlags);
return debug;
}
/**
@brief Warning output handler
Same as @ref Debug, but by default writes output to standard error output.
Thus it is possible to separate / mute @ref Debug, @ref Warning and @ref Error
outputs.
@see @ref printError()
*/
class CORRADE_UTILITY_EXPORT Warning: public Debug {
public:
/**
* @brief Default warning output stream
* @m_since{2019,10}
*
* Warning output when no output redirection happens. A pointer to
* @ref std::cerr.
* @see @ref output()
*/
static std::ostream* defaultOutput();