1include(definitions.m4)dnl
2__HTMLHEADER
3__PAGEHEADER
4__PAGESTART
5
6<H1>FAQ</H1>
7
8<P>Warning: The ammount of information included in this FAQ is exhaustive.&nbsp;
9Do NOT read it except as a replacement for self-torture.&nbsp; Instead read the
10<A HREF="index.html">tutorial</A> and skip all the references to
11this FAQ unless you find yourself banging your head into the wall asking yourself
12the same question as is listed in the tutorial.&nbsp; In that case a link will
13bring you here to read just that one question.</P>
14
15<HR SIZE=1 NOSHADE>
16
17<A name="GNU_SOURCE"></A>
18<H3>1. Won't this define make my code non-portable?</H3>
19
20<P>No, not unless you actually use the GNU extensions in parts of your
21application that need to be portable (like non-debugging code).&nbsp;
22While debugging the application you will only benefit from using as
23much compiler support as you can get, allowing the compiler to tell you
24what could possibly be wrong with your code.&nbsp;
25Once the application works, you don't have to define _GNU_SOURCE
26because you won't be including the debug code anymore, nor link with
27libcwd.&nbsp;
28Note that GNU g++ 3.x already defines this macro currently itself as a hack
29to get the libstdc++ headers work properly, hence the test with <CODE>#ifndef</CODE>
30is always needed (see <A HREF="http://gcc.gnu.org/ml/gcc/2002-02/msg00996.html">http://gcc.gnu.org/ml/gcc/2002-02/msg00996.html</A>).</P>
31
32<A name="sys.h"></A>
33<H3>2. Why do I have to include &quot;libcwd/sys.h&quot; as first header file?</H3>
34
35<P>This header file is used to fix operating systems bugs, including bugs
36in the system header files.&nbsp; The only way it can do this is when it
37is included before <EM>any</EM> other header file, including system header
38files.</P>
39
40<P>Any project should have one header file that is included at the top of every source file.&nbsp;
41If you already have one then you can add <CODE>#include&nbsp;&lt;libcwd/sys.h&gt;</CODE> to that file.&nbsp;
42Otherwise you should add such a header file: its a Good Thing(tm) to have.</P>
43
44<P>Because this must be included in <EM>every</EM> source file as very first
45header file, it would make no sense to include it also in another
46header file; so it isn't.&nbsp; As a result, forgetting this header file
47or including any other libcwd header file before including libcwd/sys.h,
48will definitely lead to compile errors in that header file.&nbsp;</P>
49
50<A name="dir"></A>
51<H3>3. Why do I need to type &quot;<SPAN class="H3code">libcwd/sys.h</SPAN>&quot;
52and not just &quot;<SPAN class="H3code">sys.h</SPAN>&quot;?</H3>
53
54<P>The header file names of libcwd are not unique.&nbsp; In order to uniquely
55identify which header file needs to be included the &quot;libcwd/&quot; part
56is needed.</P>
57
58<P>Never use the compiler option <SPAN class="code"><SPAN class="command-line-parameter">-I</SPAN>
59<SPAN class="command-line-variable">/usr/include/libcwd</SPAN></SPAN> so you can skip
60the &quot;libcwd/&quot; part in your <SPAN class="code">#include</SPAN>
61directives.&nbsp; There is no garantee that there isn't a header file name
62collision in that case.</P>
63
64<A name="debug.h"></A>
65<H3>4. What is defined <EM>exactly</EM> in <SPAN class="H3code">libcwd/debug.h</SPAN>?</H3>
66
67<P>Everything.&nbsp;
68Go and read the <A HREF="../reference-manual/index.html">Reference Manual</A> to get <EM>all</EM> gory details if you dare.</P>
69
70<A name="macros"></A>
71<H3>5. Why are you using macros for <SPAN class="H3code">Debug</SPAN> and <SPAN class="H3code">Dout</SPAN>?</H3>
72
73<P>Because it is the only way to easy remove debugging code from an application as function of a macro
74and because it allows for the fastest possible code even without optimisation, which is often the case
75while debugging.&nbsp; A more detailed explanation is given in the <A HREF="../reference-manual/page_why_macro.html">Reference Manual</A>.</P>
76
77<A name="Debug"></A>
78<H3>6. Why do I need to type the <SPAN class="H3code">Debug(&nbsp;&nbsp;)</SPAN> around it?</H3>
79
80<P>The macro <SPAN class="code">Debug()</SPAN> is used for two things. 1) The code inside it is only included
81when the macro <SPAN class="code">CWDEBUG</SPAN> is defined. 2) It includes the namespace <SPAN class="code">libcwd</SPAN>.</P>
82
83<P>As a result, you don't have to add <SPAN class="code">#ifdef CWDEBUG ... #endif</SPAN> around the code and
84in most cases you don't have to type <SPAN class="code">libcwd</SPAN>.&nbsp;
85The expression <SPAN class="code">Debug( STATEMENT );</SPAN> is equivalent with:</P>
86
87<PRE class="code">
88#ifdef CWDEBUG
89  do {
90    using namespace ::libcwd;
91    using namespace DEBUGCHANNELS;
92    { STATEMENT; }
93  } while(0);
94#endif
95</PRE>
96
97<P>Please note that definitions within a <SPAN class="code">Debug()</SPAN> statement will be
98restricted to their own scope.&nbsp;
99Please read the <A HREF="../reference-manual/group__chapter__custom__debug__h.html">Reference Manual</A> for an
100explanation of <SPAN class="code">DEBUGCHANNELS</SPAN>.</P>
101
102<A name="DebugChannels"></A>
103<H3>7. Which Debug Channels exist?&nbsp; Can I make my own?</H3>
104
105<P>This question is covered in chapter
106<A HREF="../reference-manual/group__group__debug__channels.html">Controlling The Output Level (Debug Channels)</A>
107of the Reference Manual.&nbsp;
108As is described there, creating your own debug channels is best done by writing your own <SPAN class="code">debug.h</SPAN>
109header file.&nbsp; The following template is a good start for such a <SPAN class="code">debug.h</SPAN> for an end application
110(a library needs more work):</P>
111
112<PRE class="code">
113#ifndef MY_DEBUG_H
114#define MY_DEBUG_H
115
116#define DEBUGCHANNELS ::myapplication::debug::channels
117#include &lt;libcwd/debug.h&gt;
118
119namespace myapplication {
120  namespace debug {
121    namespace channels {
122      namespace dc {
123        using namespace ::libcwd::channels::dc;
124        extern ::libcwd::channel_ct mychannel;
125	// ... more channels here
126      }
127    }
128  }
129}
130
131#endif // MY_DEBUG_H
132</PRE>
133
134<P>Replace &laquo;<SPAN class="code">MY_DEBUG_H</SPAN>&raquo;,
135&laquo;<SPAN class="code">myapplication::debug</SPAN>&raquo; and &laquo;<SPAN class="code">mychannel</SPAN>&raquo; with your own names.
136
137<P>See the <SPAN class="filename">example-project</SPAN> that comes
138with the source distribution of libcwd for a Real Life example.</P>
139
140<A name="recursive"></A>
141<H3>8. Can I turn Debug Channels off again?&nbsp; Can I do that recursively?</H3>
142
143<P>Debug channels can be switched on and off at any time.&nbsp; At the start of your program you should
144turn on the channels of your choice by calling <SPAN class="code">Debug(dc::<EM>channel</EM>.on())</SPAN>
145<EM>once</EM>.&nbsp; Sometimes you want to temporally turn off certain channels: you want to make
146sure that no debug output is written to that particular debug channel, at that moment.&nbsp; This can be
147achieved by calling the methods <SPAN class="code">off()</SPAN> and <SPAN class="code">on()</SPAN> in
148<EM>pairs</EM> and in that order.&nbsp; For example:</P>
149
150<PRE class="code">
151  // Make sure no allocation debug output is generated:
152  Debug( dc::malloc.off() );
153  char* temporal_buffer = new char [1024];
154  // ... do stuff ...
155  delete [] temporal_buffer;
156  Debug( dc::malloc.on() );
157</PRE>
158
159<P>This will work even when `do stuff' calls a function that also turns <SPAN class="code">dc::malloc</SPAN> off and on:
160after the call to <SPAN class="code">on()</SPAN> the debug channel can still be off: it is restored to the on/off state
161that it was in before the corresponding call to <SPAN class="code">off()</SPAN>.&nbsp; In fact, the calls to
162<SPAN class="code">off()</SPAN> and <SPAN class="code">on()</SPAN> only respectively increment and decrement a counter.</P>
163
164<A name="Channel"></A>
165<H3>9. Why do you call it a Debug <EM>Channel</EM>?&nbsp; What <EM>is</EM> a Debug Channel?</H3>
166
167<P>A Debug Channel is a fictious &quot;news channel&quot;.&nbsp; It should contain information of a certain kind that is
168interesting or not interesting as a whole.&nbsp; A Debug Channel is not a device or stream, a single debug channel is best
169viewed upon as a single bit in a bitmask.&nbsp; Every time you write debug output you have to specify a &quot;bitmask&quot;
170which specifies when that message is written; like when you are cross posting to usenet news groups, specifying multiple
171news groups for a single message.&nbsp; When any of the specified Debug Channels is turned on, then the message is written
172to the output stream of the underlaying debug object.</P>
173
174<A name="OwnDebugObject"></A>
175<H3>10. Can I make my own Debug Object?</H3>
176
177<P><A HREF="../reference-manual/group__chapter__custom__do.html">Yes</A>, you can make as many debug objects as you like.&nbsp;
178Each debug object is associated with one ostream.&nbsp; However, the default debug output macros <CODE>Dout</CODE> and
179<CODE>DoutFatal</CODE> use the default debug object <CODE>libcw_do</CODE>.&nbsp;
180It isn't hard at all to define your own macros though; for example add something like the following to
181<A HREF="../reference-manual/group__chapter__custom__debug__h.html">your own &quot;debug.h&quot;</A> file:</P>
182
183<PRE class="code">
184#ifdef CWDEBUG
185extern libcwd::debug_ct <SPAN class="highlight">my_debug_object</SPAN>;
186#define <SPAN class="highlight">MyDout</SPAN>(cntrl, data) LibcwDout(DEBUGCHANNELS, <SPAN class="highlight">my_debug_object</SPAN>, cntrl, data)
187#define <SPAN class="highlight">MyDoutFatal</SPAN>(cntrl, data) LibcwDoutFatal(DEBUGCHANNELS, <SPAN class="highlight">my_debug_object</SPAN>, cntrl, data)
188#else // !CWDEBUG
189#define <SPAN class="highlight">MyDout</SPAN>(a, b)
190#define <SPAN class="highlight">MyDoutFatal</SPAN>(a, b) LibcwDoutFatal(::std, /*nothing*/, a, b)
191#endif // !CWDEBUG
192</PRE>
193
194<A name="recursive2"></A>
195<H3>11. Can I turn Debug Objects off again? Can I do that recursively?</H3>
196
197<P>Debug objects can be switched on and off at any time.&nbsp; At the start of your program you should
198turn on the debug object(s) by calling <SPAN class="code">Debug(<EM>debugobject</EM>.on())</SPAN>
199<EM>once</EM>.&nbsp; Sometimes you want to temporally turn off all debug output.&nbsp;
200This can be achieved by calling the methods <SPAN class="code">off()</SPAN> and <SPAN class="code">on()</SPAN> in
201<EM>pairs</EM> and in that order.&nbsp; For example:</P>
202
203<PRE class="code">
204  // Disable all debug output to `libcw_do':
205  Debug( libcw_do.off() );
206  // ... do stuff ...
207  Debug( libcw_do.on() );
208</PRE>
209
210<P>This will work even when `do stuff' calls a function that also turns <SPAN class="code">libcw_do</SPAN> off and on:
211after the call to <SPAN class="code">on()</SPAN> the debug object can still be off: it is restored to the on/off state
212that it was in before the corresponding call to <SPAN class="code">off()</SPAN>.&nbsp; In fact, the calls to
213<SPAN class="code">off()</SPAN> and <SPAN class="code">on()</SPAN> only respectively increment and decrement a counter.</P>
214
215<A name="SetOstream"></A>
216<H3>12. How do I set a new <SPAN class="H3code">ostream</SPAN> for a given Debug Object?</H3>
217
218<P>You can change the <SPAN class="code">ostream</SPAN> that is associated with a Debug Object at any time.&nbsp;
219For example, changing the <SPAN class="code">ostream</SPAN> of <SPAN class="code">libcw_do</SPAN> from the
220default <SPAN class="code">cerr</SPAN> to <SPAN class="code">cout</SPAN>:</P>
221
222<PRE class="code">
223  Debug( libcw_do.set_ostream(&cout) );
224</PRE>
225
226<P>See also <A HREF="tut3.html">tutorial 3</A>.</P>
227
228<A name="WhyOff"></A>
229<H3>13. Why are Debug Objects turned off at creation?</H3>
230
231<P>The Debug Objects and Debug Channels are global objects.&nbsp; Because libcwd could not be
232dependant of libcw, they do not use libcw's <CODE>Global&lt;&gt;</CODE> template.&nbsp;
233As a result, the order in which the debug channels and objects are initialized is
234unknown; moreover, other global objects whose constructors might try to write debug output could
235be constructed before the debug objects are initialized!&nbsp; The debug objects are therefore
236designed in a way that independent of there internal state of initialisation they function without
237crashing.&nbsp; It should be obvious that the only way this could be achieved was by creating them
238in the state <EM>off</EM>.</P>
239
240<A name="Order"></A>
241<H3>14. Why do you turn on the debug object after you enable a debug channel, why not the other way around?</H3>
242
243<P>The order in which Debug Channels and Debug Objects are turned on does not matter at all.&nbsp;
244At most, when you think about the Debug Object as the &laquo;main switch&raquo; then it seems to make
245sense to first play with the little channel switches before finally activating the complete Debug
246machinery.&nbsp; Others might think more in the lines of: lets start with setting the debug object
247<EM>on</EM> before I forget it.&nbsp; That is a bit <EM>too</EM> fuzzy (logic) for me though ;)</P>
248
249<A name="Object"></A>
250<H3>15. Why do you call it a Debug <EM>Object</EM>?&nbsp; What <EM>is</EM> a Debug Object?</H3>
251
252<P>Good question.&nbsp; It can't be because I wasn't creative, I am very creative.&nbsp;
253Note that I didn't think of <EM>Object</EM> as in OOP (<EM>that</EM> would be uncreative)
254but more along the lines of an <EM>object</EM>, like in science fiction stories -- objects
255you can't get around.&nbsp; The monolith of <A HREF="http://uk.imdb.com/Title?0062622" target="_blank">2001: A Space Odyssey</A>
256is a good example I guess.</P>
257
258<P>Unlike the monolith however, a Debug Object is not mysterious at all.&nbsp; Basically it
259is a pointer to an <SPAN class="code">ostream</SPAN> with a few extra attributes added to
260give it an internal state for 'on' (pass output on) and 'off' (don't pass output on) as well
261as some formatting information of how to write the data that is passed on to its
262<SPAN class="code">ostream</SPAN>.</P>
263
264<A name="semicolon"></A>
265<H3>16. Do I need to type that semi-colon after the macro?&nbsp; Why isn't it part of the macro?</H3>
266
267<P>Yes, that colon needs to be there.&nbsp;
268It was chosen not to include the semi-colon in the macro because this way it looks
269a bit like a function call which feels more natural.</P>
270
271<P>The code <SPAN class="code">Dout(dc::notice,&nbsp;"Hello World");</SPAN> is definitely
272a <EM>statement</EM> and therefore needs to end on a semi-colon (after expansion).&nbsp; When the
273macro <SPAN class="code">CWDEBUG</SPAN> is not defined, the macro is replaced with
274whitespace but still has to be a statement: it must be a single semi-colon then.</P>
275
276<P>For example,</P>
277
278<PRE class="code">
279  if (error)
280    Dout(dc::notice, "An error occured");
281
282  exit(0);
283  cerr &lt;&lt; "We should never reach this\n";
284</PRE>
285
286<P>If the complete line <SPAN class="code">Dout(dc::notice, "An error occured");</SPAN>,
287including semi-colon is removed (replaced with whitespace), then the line
288<SPAN class="code">exit(0);</SPAN> would be executed only when <SPAN class="code">error</SPAN>
289is <SPAN class="code">true</SPAN>!&nbsp; And when the semi-colon would be included in
290the macro then people could easily be tempted to add a semi-colon anyway (because it
291looks so much better), which would break code like:</P>
292
293<PRE class="code">
294  if (error)
295    Dout(dc::notice, "An error occured");
296  else
297    cout &lt;&lt; "Everything is ok\n";
298</PRE>
299
300<P>because after macro expansion that would become:</P>
301
302<PRE class="code">
303  if (error)
304    ;
305    ;
306  else		// &lt;-- syntax error
307    cout &lt;&lt; "Everything is ok\n";
308</PRE>
309
310<A name="LibcwDout"></A>
311<H3>17. I made my own Debug Object, can I still use <SPAN class="H3code">Dout</SPAN>?</H3>
312
313<P>No, macro <SPAN class="code">Dout</SPAN> et al. use exclusively the debug object that
314comes with libcwd.&nbsp; It is easy to define your own macros however (see <A HREF="#OwnDebugObject">above</A>).&nbsp;
315You are free to <EM>redefine</EM> the <SPAN class="code">Dout</SPAN> macros however, just realize that libcwd
316will continue to use its own debug object (<SPAN class="code">libcw_do</SPAN>), debug output written by libcwd
317in its header files do not use the <SPAN class="code">Dout</SPAN> macro (especially in order to allow you
318to redefine it).</P>
319
320<A name="evaluation"></A>
321<H3>18. Is the second field of the macro still evaluated when the Debug Channel and/or Debug Object are turned off?</H3>
322
323<P>No!&nbsp; And that is a direct result of the fact that <SPAN class="code">Dout</SPAN> et al. are <EM>macros</EM>.&nbsp;
324Indeed this fact could therefore be a little confusing.&nbsp;
325In pseudo-code the macro expansion looks something like</P>
326
327<PRE class="code">
328  if (debug object and any of the debug channels are turned on)
329    the_ostream &lt;&lt; your message;
330</PRE>
331
332<P>and so, &quot;your message&quot; is <EM>not</EM> evaluated when it isn't also
333actually written.&nbsp; This fact is also covered in the
334<A HREF="../reference-manual/classlibcwd_1_1debug__ct.html#eval_example">Reference Manual</A>.</P>
335
336<P>Note that debug code should never have an effect on any of your variables (and thus on the application) anyway.&nbsp;
337In the production version of your application all debug code will be removed and you don't want it to behave differently then!</P>
338
339<A name="suppress"></A>
340<H3>19. Can I suppress that new-line character?</H3>
341
342<P>Yes, and a lot more.&nbsp; See <A HREF="tut5.html#Formatting">tutorial 5.4</A>.</P>
343
344<A name="label"></A>
345<H3>20. What is the maximum length of a label?</H3>
346
347<P>The maximum length of the label of a new Debug Channel is given
348by the constant<SPAN class="code"> libcwd::max_label_len_c</SPAN>.&nbsp;
349At this moment that is 16.</P>
350
351<A name="prefix"></A>
352<H3>21. Why do I have to use the <SPAN class="H3code">dc::</SPAN> prefix?</H3>
353
354<P>This is a complex reason.&nbsp; Basically because of a flaw in the design of namespaces in C++.&nbsp;
355Namespaces have been introduced in order to avoid name collisions, which was a good thing.&nbsp;
356It doesn't make much sense if you constantly have to type <SPAN class="code">::somelibrary::debug::channel::notice</SPAN>
357of course, then you could as well have avoided the name space problem by using
358<SPAN class="code">somelibrary_debug_channel_notice</SPAN> right?&nbsp;
359Therefore you don't have to type the name of the namespace that is &quot;current&quot;.&nbsp;
360There can be only <EM>one</EM> namespace current at a time however.&nbsp; The result is that
361this cannot be used to solve our problem: We want to avoid both, name collisions between debug channels
362and any other variable or function name, but <EM>also</EM> between debug channels defined in
363different libraries.&nbsp; That means we need more than one namespace: A namespace for each of
364the libraries.&nbsp; We cannot make all of them current however.&nbsp; Worse, we cannot
365make any namespace current because it must be possible to add code that writes debug output
366<EM>everywhere</EM>.&nbsp; We can only use the <SPAN class="code">using namespace</SPAN>
367directive.&nbsp; Now here is the real flaw: A <SPAN class="code">using namespace</SPAN> directive
368gives no priority whatsoever to names when resolving them, for example, you can't do this:</P>
369
370<PRE class="code">
371namespace base {
372  int base1;
373  int base2;
374}
375
376namespace derived {
377  using namespace base;
378  int derived1;
379  char base1;
380}
381
382  // ...
383  using namespace derived;
384  base1 = 'a';
385</PRE>
386
387<P>because C++ will make absolutely no difference between variables defined in
388<SPAN class="code">derived</SPAN> and variables defined in <SPAN class="code">base</SPAN>
389but will complain that <SPAN class="code">base1</SPAN> is ambigious.</P>
390
391<P>The only opening that the ANSI/ISO C++ Standard allows us here is in the
392following phrase:</P>
393
394<QUOTE>
395Given <SPAN class="code">X::m</SPAN> (where <SPAN class="code">X</SPAN> is a user-declared namespace),
396or given <SPAN class="code">::m</SPAN> (where <SPAN class="code">X</SPAN> is the global namespace),
397let <SPAN class="code">S</SPAN> be the set of all declarations of <SPAN class="code">m</SPAN> in <SPAN class="code">X</SPAN>
398and in the transitive closure of all namespaces nominated by <I>using-directive</I>s in X and its used namespaces,
399except that <I>using-directive</I>s are ignored in any namespace, including <SPAN class="code">X</SPAN>,
400directly containing one or more declarations of <SPAN class="code">m</SPAN>.&nbsp;
401No namespace is searched  more than once in the lookup of a name.&nbsp; If <SPAN class="code">S</SPAN> is the empty set,
402the program is ill-formed.&nbsp; Otherwise, if <SPAN class="code">S</SPAN> has exactly one member,
403or if the context of the reference is a using-declaration, <SPAN class="code">S</SPAN> is
404the required set of declarations of <SPAN class="code">m</SPAN>.&nbsp;
405Otherwise if the use of <SPAN class="code">m</SPAN> is not one that allows a unique
406declaration to be chosen from <SPAN class="code">S</SPAN>, the program is ill-formed.
407</QUOTE>
408
409<P>Replace <SPAN class="code">X</SPAN> with <SPAN class="code">dc::</SPAN>
410(obviously we don't want to put the debug channels in global namespace)
411and we can use this rule to at least select a specific channel by using
412the trick that the used <SPAN class="code">dc</SPAN> namespace is <EM>not the same</EM>
413namespace for the different libraries.&nbsp; Then we can use debug channels with
414the same name in <SPAN class="code">dc</SPAN> namespaces in <EM>different</EM>
415namespaces in different libraries and use the namespaces of <EM>one</EM> library
416at a time to select the current <SPAN class="code">dc</SPAN> namespace.</P>
417
418<P>If this is over your head then that is probably because I can't explain&nbsp;:).&nbsp;
419Don't worry however, you only need to know <EM>how</EM> to introduce new debug
420channels and not understand how it works.&nbsp; The correct procedure is described
421in the <A HREF="../reference-manual/group__group__debug__channels.html">Reference Manual</A>.</P>
422
423<A name="ownnamespace"></A>
424<H3>22. Can I put my debug channels in my own name space?</H3>
425
426<P>Yes.&nbsp; How, is described in the <A HREF="../reference-manual/group__group__debug__channels.html">Reference Manual</A>.&nbsp;
427For some background information on why this has to be so complex, please read the <A HREF="#prefix">previous question</A>.</P>
428
429<A name="labelwidth"></A>
430<H3>23. Why does it print spaces between the label and the colon?&nbsp; How is the field width of the label determined?</H3>
431
432<P>The colon is indented so it ends up in the same column for all existing debug channels.&nbsp;
433Hence, the longest label of all existing/created debug channels determines the number of spaces.&nbsp;
434This value can be less than the <A HREF="#label">maximum allowed label size</A> of course.</P>
435
436__PAGEEND
437__PAGEFOOTER
438__HTMLFOOTER
439
440