libcwdversion 2.0.0
The C++ Debugging Support Library
Loading...
Searching...
No Matches
debug.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2000-2004, 2017-2020, 2025-2026 Carlo Wood
2// SPDX-License-Identifier: MIT
3
4#pragma once
5
6// Libcwd-2 defines things that were originally in cwds.
7#include "cwds_debug.h"
8
17
18#ifndef CWDEBUG
19
20// If you run into this error then you included <libcwd/debug.h> (or any other libcwd header file)
21// while the macro CWDEBUG was not defined. Doing so would cause the compilation of your
22// application to fail on machines that do not have libcwd installed. Instead you should use:
23// #include "debug.h"
24// and add a file debug.h to your applications source distribution. Please see the the example-project
25// that comes with the source code of libcwd (and is included in the documentation that comes with
26// a libcwd package (ie: /usr/share/doc/libcwd/example-project) for a description of the content of "debug.h".
27// Note: CWDEBUG should be defined on the compiler commandline, for example: g++ -DCWDEBUG ...
28// which normally happens automatically, simply by linking with libcwd (when using cmake).
29#error You are including <libcwd/debug.h> while CWDEBUG is not defined. See the comments in this header file for more information.
30
31#endif // CWDEBUG
32
33#ifndef LIBCWD_DEBUG_H
34#define LIBCWD_DEBUG_H
35
36#ifdef CWDEBUG
37
38// The following header is also needed for end-applications, despite its name.
39#include "libraries_debug.h"
40
41// For use in applications
42// clang-format off
69#define Debug(/*STATEMENTS*/...) LibcwDebug(LIBCWD_DEBUG_CHANNELS, __VA_ARGS__)
70
97#define Dout(cntrl, ...) LibcwDout(LIBCWD_DEBUG_CHANNELS, ::libcwd::libcw_do, cntrl, __VA_ARGS__)
98
105#define DoutEntering(cntrl, ...) \
106 int __cwds_debug_indentation = 2; \
107 LibcwDoutScopeBegin(LIBCWD_DEBUG_CHANNELS, ::libcwd::libcw_do, cntrl) \
108 LibcwDoutStream << "Entering " << __VA_ARGS__; \
109 LibcwDoutScopeEnd; \
110 ::libcwd::Indent __cwds_debug_indent(__cwds_debug_indentation);
111
119#define DoutFatal(cntrl, ...) LibcwDoutFatal(LIBCWD_DEBUG_CHANNELS, ::libcwd::libcw_do, cntrl, __VA_ARGS__)
120
144#define ForAllDebugChannels(/*STATEMENT*/...) LibcwdForAllDebugChannels(LIBCWD_DEBUG_CHANNELS, __VA_ARGS__)
145
162#define ForAllDebugObjects(/*STATEMENT*/...) LibcwdForAllDebugObjects(LIBCWD_DEBUG_CHANNELS, __VA_ARGS__)
163
164namespace libcwd {
165
172struct Indent
173{
176
178 explicit Indent(int indent) : M_indent(indent) { if (M_indent > 0) libcwd::libcw_do.inc_indent(M_indent); }
179
181 ~Indent() { if (M_indent > 0) libcwd::libcw_do.dec_indent(M_indent); }
182};
183
192struct Mark
193{
196
198 explicit Mark(char m = '|') : M_indent(libcwd::libcw_do.get_indent())
199 {
200 libcwd::libcw_do.push_marker();
201 libcwd::libcw_do.marker().append(std::string(M_indent, ' ') + m + ' ');
202 // This is basically a decrement of M_indent.
203 libcwd::libcw_do.set_indent(0);
204 }
205 explicit Mark(char const* utf8_m) : M_indent(libcwd::libcw_do.get_indent())
206 {
207 libcwd::libcw_do.push_marker();
208 libcwd::libcw_do.marker().append(std::string(M_indent, ' ') + utf8_m + ' ');
209 // This is basically a decrement of M_indent.
210 libcwd::libcw_do.set_indent(0);
211 }
212#ifdef __cpp_char8_t
213 explicit Mark(char8_t const* utf8_m) : Mark(reinterpret_cast<char const*>(utf8_m)) { }
214#endif
215
217 ~Mark() { end(); }
218
219 void end()
220 {
221 if (M_indent != -1)
222 {
223 libcwd::libcw_do.pop_marker();
224 // Restore indentation relative to possible other indentation increments that happened in the mean time.
225 libcwd::libcw_do.inc_indent(M_indent);
226 // Mark that end() was already called.
227 M_indent = -1;
228 }
229 }
230};
231
232} // namespace libcwd
233
234#endif // CWDEBUG
235
236// clang-format on
237#include <libcwd/init_functions.h>
238
240#if defined(CWDEBUG) || !defined(NDEBUG)
241#define CW_DEBUG 1
242#else
243#define CW_DEBUG 0
244#endif
245
246#ifdef CWDEBUG
247
248NAMESPACE_DEBUG_START
249using namespace libcwd::init_functions;
250NAMESPACE_DEBUG_END
251
252// Build the LIBCWD_DEBUG_CHANNELS::dc facade.
253// Users should insert their debug channels in here.
254NAMESPACE_DEBUG_CHANNELS_START
255using namespace libcwd::channels::dc;
256using libcwd::Channel;
257NAMESPACE_DEBUG_CHANNELS_END
258
259#endif // CWDEBUG
260#endif // LIBCWD_DEBUG_H
This object represents a debug channel, it has a fixed label. A debug channel can be viewed upon as a...
Definition Channel.h:74
This is the header file that third-party library headers should include.
This namespace contains the standard debug channels of libcwd.
namespace for libcwd.
DebugObject libcw_do
The default debug object.
Definition debug.cxx:222
int M_indent
The extra number of spaces that were added to the indentation.
Definition debug.h:175
~Indent()
Destructor.
Definition debug.h:181
Indent(int indent)
Construct an Indent object.
Definition debug.h:178
Mark(char m='|')
Construct a Mark object.
Definition debug.h:198
int M_indent
The old indentation.
Definition debug.h:195
~Mark()
Destructor.
Definition debug.h:217