1// $Header$
2//
3// Copyright (C) 2000 - 2007, by
4//
5// Carlo Wood, Run on IRC <carlo@alinoe.com>
6// RSA-1024 0x624ACAD5 1997-01-26                    Sign & Encrypt
7// Fingerprint16 = 32 EC A7 B6 AC DB 65 A6  F6 F6 55 DD 1C DC FF 61
8//
9// This file may be distributed under the terms of the Q Public License
10// version 1.0 as appearing in the file LICENSE.QPL included in the
11// packaging of this file.
12//
13
14#ifndef LIBCWD_CLASS_DEBUG_INL
15#define LIBCWD_CLASS_DEBUG_INL
16
17#ifndef LIBCWD_CLASS_DEBUG_H
18#include <libcwd/class_debug.h>
19#endif
20#ifndef LIBCWD_CLASS_CHANNEL_H
21#include <libcwd/class_channel.h>
22#endif
23#ifndef LIBCWD_CLASS_FATAL_CHANNEL_H
24#include <libcwd/class_fatal_channel.h>
25#endif
26#ifndef LIBCWD_CLASS_CHANNEL_INL
27#include <libcwd/class_channel.inl>
28#endif
29#ifndef LIBCWD_CLASS_FATAL_CHANNEL_INL
30#include <libcwd/class_fatal_channel.inl>
31#endif
32#ifndef LIBCWD_CLASS_DEBUG_STRING_INL
33#include <libcwd/class_debug_string.inl>
34#endif
35
36namespace libcwd {
37
38/** \addtogroup group_formatting */
39/* \{ */
40
41inline
42debug_string_ct&
43debug_ct::margin(void)
44{
45  LIBCWD_TSD_DECLARATION;
46  return LIBCWD_TSD_MEMBER(margin);
47}
48
49inline
50debug_string_ct const&
51debug_ct::margin(void) const
52{
53  LIBCWD_TSD_DECLARATION;
54  return LIBCWD_TSD_MEMBER(margin);
55}
56
57inline
58debug_string_ct&
59debug_ct::marker(void)
60{
61  LIBCWD_TSD_DECLARATION;
62  return LIBCWD_TSD_MEMBER(marker);
63}
64
65inline
66debug_string_ct const&
67debug_ct::marker(void) const
68{
69  LIBCWD_TSD_DECLARATION;
70  return LIBCWD_TSD_MEMBER(marker);
71}
72
73/**
74 * \brief Set number of spaces to indent.
75 */
76inline
77void
78debug_ct::set_indent(unsigned short i)
79{
80  LIBCWD_TSD_DECLARATION;
81  LIBCWD_TSD_MEMBER(indent) = i;
82}
83
84/**
85 * \brief Increment number of spaces to indent.
86 */
87inline
88void
89debug_ct::inc_indent(unsigned short i)
90{
91  LIBCWD_TSD_DECLARATION;
92  LIBCWD_TSD_MEMBER(indent) += i;
93}
94
95/**
96 * \brief Decrement number of spaces to indent.
97 */
98inline
99void
100debug_ct::dec_indent(unsigned short i)
101{
102  LIBCWD_TSD_DECLARATION;
103  int prev_indent = LIBCWD_TSD_MEMBER(indent);
104  LIBCWD_TSD_MEMBER(indent) = (i > prev_indent) ? 0 : (prev_indent - i);
105}
106
107/**
108 * \brief Get the current indentation.
109 */
110inline
111unsigned short
112debug_ct::get_indent(void) const
113{
114  LIBCWD_TSD_DECLARATION;
115  return LIBCWD_TSD_MEMBER(indent);
116}
117
118/** \} */
119
120/** \addtogroup group_destination */
121/** \{ */
122
123/**
124 * \brief Get the \c ostream device as set with set_ostream().
125 */
126inline
127std::ostream*
128debug_ct::get_ostream(void) const
129{
130  return real_os;
131}
132
133inline
134void
135debug_ct::private_set_ostream(std::ostream* os)
136{
137  real_os = os;
138#if CWDEBUG_DEBUG
139  LIBCWD_TSD_DECLARATION;
140  LIBCWD_ASSERT( LIBCWD_TSD_MEMBER(tsd_initialized) );
141#endif
142}
143
144/** \} */
145
146/**
147 * \brief Constructor
148 *
149 * A %debug object must be global.
150 *
151 * \sa group_debug_object
152 * \sa chapter_custom_do
153 */
154inline
155debug_ct::debug_ct(void)
156{
157  LIBCWD_TSD_DECLARATION;
158#if CWDEBUG_DEBUG
159  if (!NS_init(LIBCWD_TSD))
160    core_dump();
161#else
162  (void)NS_init(LIBCWD_TSD);
163#endif
164}
165
166/**
167 * \brief Turn this %debug object off.
168 */
169inline
170void
171debug_ct::off(void)
172{
173  LIBCWD_TSD_DECLARATION;
174  ++LIBCWD_TSD_MEMBER_OFF;
175}
176
177/**
178 * \brief Cancel last call to off().
179 *
180 * Calls to off() and on() has to be done in pairs (first off() then on()).
181 * These pairs can be nested.
182 *
183 * <b>Example:</b>
184 *
185 * \code
186 * int i = 0;
187 * Debug( libcw_do.off() );
188 * Dout( dc::notice, "Adding one to " << i++ );
189 * Debug( libcw_do.on() );
190 * Dout( dc::notice, "i == " << i );
191 * \endcode
192 *
193 * Outputs:
194 *
195 * <PRE class="example-output">NOTICE : i == 0
196 * </PRE>
197 *
198 * Note that the statement <CODE>i++</CODE> was never executed.
199 */
200inline
201void
202debug_ct::on(void)
203{
204  LIBCWD_TSD_DECLARATION;
205#if CWDEBUG_DEBUGOUTPUT
206  if (LIBCWD_TSD_MEMBER(first_time) && LIBCWD_TSD_MEMBER_OFF == -1)
207    LIBCWD_TSD_MEMBER(first_time) = false;
208  else
209    --LIBCWD_TSD_MEMBER_OFF;
210#else
211  --LIBCWD_TSD_MEMBER_OFF;
212#endif
213}
214
215inline
216channel_set_st&
217channel_set_bootstrap_st::operator|(channel_ct const& dc)
218{
219  mask = 0;
220  label = dc.get_label();
221  on = dc.is_on();
222  return *reinterpret_cast<channel_set_st*>(this);
223}
224
225inline
226channel_set_st&
227channel_set_bootstrap_fatal_st::operator|(fatal_channel_ct const& fdc)
228{
229  mask = fdc.get_maskbit();
230  label = fdc.get_label();
231  on = true;
232  return *reinterpret_cast<channel_set_st*>(this);
233}
234
235} // namespace libcwd
236
237#endif // LIBCWD_CLASS_DEBUG_INL
238