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
|
include(definitions.m4)dnl
__HTMLHEADER
__PAGEHEADER
__PAGESTART
<H2>Tutorial 5: Advanced examples</H2>
<P>In this tutorial you will learn how to list all debugging channels
and how to write a loop that runs over all existing debug channels.
You will make four debug channels in their own namespace and write debug output to a combination of them.
You will also learn how to add an error message after a debug message, how to format a debug message,
how to supress the label and how to supress the newline at the end.
Finally you will learn how to write interrupted debug output
in an example that prints the call to a system call and the result of that.</P>
<A NAME="Running"></A>
<H3>5.1 Running over all Debug Channels</H3>
<P>In <A HREF="tut2.html">tutorial 2</A> you have learned how to create new Debug Channels.
Each new Debug Channel is stored in an internal list, allowing
you to loop over all debug channels without knowing exactly which ones exist.
For example, the following code will <A HREF="tut1.html#turn_on_channel">turn on</A> all
debug channels. It is not allowed to call <CODE>on()</CODE>
for a Debug Channel that is already <I>on</I>: that will result in a runtime error.</P>
<P>A special shortcut function is provided to list all debug channels,
the following code prints a list of all channels after they have been turned on.</P>
<P>Compile as: <SPAN class="shell-command">g++ -g -DCWDEBUG test5.1.1.cc -lcwd -o turn_on</SPAN></P>
<P class="download">[<A HREF="examples5/test5.1.1.cc">download</A>]</P>
<PRE>
<!-- START CODE examples5/test5.1.1.cc -->
#include "sys.h" // See tutorial 2
#include "debug.h"
int main(void)
{
// Turn on debug object `libcw_do'.
Debug( libcw_do.on() );
// Turn on all debug channels that are off.
<SPAN class="highlight">ForAllDebugChannels(</SPAN>
if (!<SPAN class="highlight">debugChannel</SPAN>.is_on())
<SPAN class="highlight">debugChannel</SPAN>.on();
<SPAN class="highlight">);</SPAN>
// Print a listing of all debug channels to debug object `libcw_do'.
Debug( <SPAN class="highlight">list_channels_on(</SPAN>libcw_do<SPAN class="highlight">)</SPAN> );
return 0;
}
<!-- END CODE examples5/test5.1.1.cc -->
</PRE>
<P>This program outputs:</P>
<PRE class="output">
<!-- START OUTPUT examples5/test5.1.1-bin -->
</PRE>
<A NAME="Debug"></A>
<H3>5.2 Debug Channels and name spaces</H3>
<P>The custom debug channel <CODE>dc::ghost</CODE> that you created
in <A HREF="tut2.html">tutorial 2</A> was put in <CODE>namespace debug_channels</CODE>.
The debug channels of libcwd are put in namespace <CODE>libcwd::channels</CODE>.
Nevertheless, it is not necessary to type</P>
<PRE>Dout(libcwd::channels::dc::notice, "Hello World");</PRE>
<P>By default, the macro <CODE>Dout</CODE> et al. automatically include a<CODE> using namespace libcwd::channels</CODE>.
It is possible to change this default namespace by defining <CODE>DEBUGCHANNELS</CODE>
<EM>before</EM> including the header file <CODE>libcwd/debug.h</CODE>.
This is will be shown below.</P>
<P>Of course, you can put your own debug channels also in namespace<CODE> libcwd::channels</CODE>
but that wouldn't garantee not to collide with a new debug channel added to libcwd in the future, or
with debug channels of other libraries that would do the same.</P>
<A NAME="debug.h"></A>
<P>In the following example we will define four new debug channels in their own
name space<CODE> example::debug::channels </CODE> by creating a project specific header file "debug.h"
which needs to be included instead of the debug.h from libcwd.</P>
<P>The example application below uses the fictitious libraries <SPAN STYLE="font-family: courier">libbooster</SPAN>
and <SPAN STYLE="font-family: courier">libturbo</SPAN>, both of which use libcwd themselfs and declare their own debug
channel namespaces <CODE>booster::debug::channels</CODE> and <CODE>turbo::debug::channels</CODE> according to
the rules as mentioned in the <A HREF="../reference-manual/group__chapter__custom__debug__h.html#libraries">reference manual</A>.</P>
<P>The project header file "debug.h":</P>
<P class="download">[<A HREF="examples5/debug.h">download</A>]</P>
<PRE>
<!-- START CODE examples5/debug.h -->
#ifndef EXAMPLE_DEBUG_H
#define EXAMPLE_DEBUG_H
#ifdef CWDEBUG
#ifndef DEBUGCHANNELS // This test is only necessary in libraries
#define DEBUGCHANNELS ::example::debug::channels
#endif
#include <libbooster/debug.h> // Note that these will include
#include <libturbo/debug.h> // libcwd/debug.h for us.
namespace example {
namespace debug {
namespace channels {
namespace dc {
using namespace libcwd; // For class channel_ct
// The list of debug channel namespaces of the libraries that we use:
// (These two already include libcwd::channels::dc)
using namespace ::booster::debug::channels::dc;
using namespace ::turbo::debug::channels::dc;
// Our own debug channels:
extern channel_ct elephant;
extern channel_ct cat;
extern channel_ct mouse;
extern channel_ct owl;
// When the libraries use the same name for any debug channels,
// then here is the place to `hide' these channels by redefining them.
// For example, if `libbooster' defined `notice' too (as does libcwd)
// then we have to redefine it again:
using libcwd::channels::dc::notice;
} // namespace dc
} // namespace channels
} // namespace debug
} // namespace example
// The following is only necessary for libraries.
// Libraries should not use Dout() et al. in their own header files,
// instead we define our own macros here for use in those header files:
#define MyLibHeaderDout(cntrl, data) \
LibcwDout(::example::debug::channels, ::libcwd::libcw_do, cntrl, data)
#define MyLibHeaderDoutFatal(cntrl, data) \
LibcwDoutFatal(::example::debug::channels, ::libcwd::libcw_do, cntrl, data)
#else // !CWDEBUG
// This is needed so people who don't have libcwd installed can still compile it.
// The file "nodebug.h" is provided in the libcwd and needs to be included in your
// own package.
#include "nodebug.h"
#define MyLibHeaderDout(cntrl, data)
#define MyLibHeaderDoutFatal(cntrl, data) LibcwDoutFatal(::std, /*nothing*/, cntrl, data)
#endif // !CWDEBUG
#endif // EXAMPLE_DEBUG_H
<!-- END CODE examples5/debug.h -->
</PRE>
<P>The source file "debug.cc":</P>
<P class="download">[<A HREF="examples5/debug.cc">download</A>]</P>
<PRE>
<!-- START CODE examples5/debug.cc -->
#include "sys.h" // See tutorial 2
#include "debug.h"
#ifdef CWDEBUG
using namespace libcwd;
namespace example {
namespace debug {
namespace channels {
namespace dc {
channel_ct elephant("DUMBO");
channel_ct cat("FELIX");
channel_ct mouse("HILDAGO");
channel_ct owl("EINSTEIN");
}
}
}
}
#endif
<!-- END CODE examples5/debug.cc -->
</PRE>
<A NAME="Combining"></A>
<H3>5.3 Combining channels</H3>
<P>Debug channels can be on or off and they have a label.
Sometimes you might want to write certain debug output to a
debug object when <EM>any</EM> of a given list of debug channels is turned on.
The syntax to do that is based on the fact that debug channels can be viewed upon as a bit mask:
using <CODE>operator|</CODE>.
In the following example we will write debug output to several different combinations
of the custom debug channels that we defined in the previous paragraph.</P>
<P>The source file "test5.3.1.cc":</P>
<P class="download">[<A HREF="examples5/test5.3.1.cc">download</A>]</P>
<PRE>
<!-- START CODE examples5/test5.3.1.cc -->
#include "sys.h" // See § 2
#include "debug.h" // See § 5.2
int main(void)
{
// Start with everything turned on:
Debug( libcw_do.on() );
ForAllDebugChannels( if (!debugChannel.is_on()) debugChannel.on() );
Dout(dc::elephant|dc::owl, "The elephant is called Dumbo, the owl is called Einstein.");
return 0;
}
<!-- END CODE examples5/test5.3.1.cc -->
</PRE>
<P>This program outputs:</P>
<PRE class="output">
<!-- START OUTPUT examples5/test5.3.1-bin -->
</PRE>
<P>using the label of the left most channel that is turned on.</P>
<P>If we turn off <CODE>dc::elephant</CODE>:</P>
<P class="download">[<A HREF="examples5/test5.3.2.cc">download</A>]</P>
<PRE>
<!-- START CODE examples5/test5.3.2.cc -->
#include "sys.h"
#include "debug.h"
int main(void)
{
// Start with everything turned on:
Debug( libcw_do.on() );
ForAllDebugChannels( if (!debugChannel.is_on()) debugChannel.on() );
<SPAN class="highlight">Debug( dc::elephant.off() );</SPAN>
Dout(dc::elephant|dc::owl, "The elephant is called Dumbo, the owl is called Einstein.");
return 0;
}
<!-- END CODE examples5/test5.3.2.cc -->
</PRE>
<P>Then the program outputs</P>
<PRE class="output">
<!-- START OUTPUT examples5/test5.3.2-bin -->
</PRE>
<A NAME="Formatting"></A>
<H3>5.4 Formatting debug output</H3>
<P>It is possible to control the format of debug output in two different ways:</P>
<OL TYPE=1>
<LI>Per call to Dout, using control flags.
<LI>Persistantly per debug object, using methods of the debug object.
</OL>
<A NAME="Control"></A>
<H4>5.4.1 Control flags</H4>
<P>Control flags are unsigned integer bit-masks and are passed along to
<CODE>Dout</CODE> together with the debug channel(s).
For example, in the following code the newline that is normally printed after
each output is suppressed in the first call:</P>
<P class="download">[<A HREF="examples5/test5.4.1.cc">download</A>]</P>
<PRE>
<!-- START CODE examples5/test5.4.1.cc -->
#include "sys.h"
#include "debug.h"
int main(void)
{
Debug( libcw_do.on() );
Debug( dc::notice.on() );
Dout(dc::notice|nonewline_cf, "Hello, ");
Dout(dc::notice, "World");
return 0;
}
<!-- END CODE examples5/test5.4.1.cc -->
</PRE>
<P>This program outputs:</P>
<PRE class="output">
<!-- START OUTPUT examples5/test5.4.1-bin -->
</PRE>
<P>In order to supress the <EM>prefix</EM> in the second call we use
<CODE>noprefix_cf</CODE>:</P>
<P class="download">[<A HREF="examples5/test5.4.2.cc">download</A>]</P>
<PRE>
<!-- START CODE examples5/test5.4.2.cc -->
#include "sys.h"
#include "debug.h"
int main(void)
{
Debug( libcw_do.on() );
Debug( dc::notice.on() );
Dout(dc::notice|nonewline_cf, "Hello, ");
Dout(dc::notice|<SPAN class="highlight">noprefix_cf</SPAN>, "World");
return 0;
}
<!-- END CODE examples5/test5.4.2.cc -->
</PRE>
<P>Now the output is</P>
<PRE class="output">
<!-- START OUTPUT examples5/test5.4.2-bin -->
</PRE>
<P>There are in total eleven <EM>control flags</EM>,
see the <A HREF="../reference-manual/index.html">Reference Manual</A>
for an <A HREF="../reference-manual/group__group__control__flags.html">overview</A>.</P>
<P>Lets get a little bit more practical now.
In the next example we perform a system call and write this fact to <CODE>dc::notice</CODE>
in the same way as <SPAN class="filename">strace(1)</SPAN> would do; the fact that the call is made
is printed first. After the call returns we print the results.</P>
<P class="download">[<A HREF="examples5/test5.4.3.cc">download</A>]</P>
<PRE>
<!-- START CODE examples5/test5.4.3.cc -->
#include "sys.h"
#include "debug.h"
#include <sys/stat.h>
#include <cstdlib>
std::ostream& operator<<(std::ostream& os, struct stat const buf)
{
os << "inode:" << buf.st_ino << "; " << "size:" << buf.st_size;
return os;
}
std::ostream& operator<<(std::ostream& os, struct stat const* bufp)
{
os << "{ " << *bufp << " }";
return os;
}
int main(int argc, char* argv[])
{
Debug( libcw_do.on() );
Debug( dc::notice.on() );
if (argc != 2)
{
std::cerr << "Usage: " << argv[0] << " <file_name>\n";
exit(-1);
}
char const* file_name = argv[1];
struct stat buf;
// Warning: this is NOT the correct way to do this (see below)
Dout(dc::notice|nonewline_cf,
"stat(\"" << file_name << "\", ");
int ret = stat(file_name, &buf);
Dout(dc::notice|noprefix_cf|cond_error_cf(ret != 0),
&buf << ") = " << ret);
return 0;
}
<!-- END CODE examples5/test5.4.3.cc -->
</PRE>
<P>Note the use of <CODE>cond_error_cf(<EM>condition</EM>)</CODE> which is equal to
<CODE>error_cf</CODE> if the condition passed is true, or zero otherwise.
The result of <CODE>error_cf</CODE> is that an error message is printed
after the debug output according to the current value of <CODE>errno</CODE>.
When we run this program with parameter "/bin/ls" we get something like:</P>
<PRE class="output">
<!-- START OUTPUT examples5/test5.4.3-bin /bin/ls -->
</PRE>
<P>And when we use a file that doesn't exist, we get something like:</P>
<PRE class="output">
NOTICE : stat("foobar", { inode:134572072; size:1 }) = -1: ENOENT (No such file or directory)
</PRE>
<P>As you already might have noticed from the comment in the program, this is not
the correct way to do this. The reason that it is wrong is because the call
to <CODE>stat</CODE> could cause debug output itself. Well, it
couldn't in this case, but in a more general case it could :).</P>
<P>Let us replace the call to <CODE>stat</CODE> by a function of
ourselfs that allocates memory (as certain system calls could do too!):</P>
<P class="download">[<A HREF="examples5/test5.4.4.cc">download</A>]</P>
<PRE>
<!-- START CODE examples5/test5.4.4.cc -->
#include "sys.h"
#include "debug.h"
#include <sys/stat.h>
#include <cstdlib>
std::ostream& operator<<(std::ostream& os, struct stat const buf)
{
os << "inode:" << buf.st_ino << "; " << "size:" << buf.st_size;
return os;
}
std::ostream& operator<<(std::ostream& os, struct stat const* bufp)
{
os << "{ " << *bufp << " }";
return os;
}
// We only use this function to show what happens with the debug output,
// you shouldn't do anything like this in a real program.
int stat_with_buf_alloc(char const* file_name, struct stat*& bufp)
{
bufp = new struct stat;
return stat(file_name, bufp);
}
int main(int argc, char* argv[])
{
Debug( libcw_do.on() );
Debug( dc::notice.on() );
<SPAN class="highlight">Debug( dc::malloc.on() );</SPAN>
if (argc != 2)
{
std::cerr << "Usage: " << argv[0] << " <file_name>\n";
exit(-1);
}
char const* file_name = argv[1];
struct stat<SPAN class="highlight">*</SPAN> buf<SPAN class="highlight">p</SPAN>;
// This is NOT the correct way to do this.
Dout(dc::notice|nonewline_cf,
"stat_with_buf_alloc(\"" << file_name << "\", ");
int ret = stat<SPAN class="highlight">_with_buf_alloc</SPAN>(file_name, buf<SPAN class="highlight">p</SPAN>);
Dout(dc::notice|noprefix_cf|cond_error_cf(ret != 0),
bufp << ") = " << ret);
<SPAN class="highlight">
Debug( dc::malloc.off() );
delete bufp;</SPAN>
return 0;
}
<!-- END CODE examples5/test5.4.4.cc -->
</PRE>
<P>Now the call (to <CODE>stat_with_buf_alloc</CODE>) writes debug output
itself which is completely messing up our beautiful attempt to look like the
output of <SPAN class="filename">strace(1)</SPAN>:</P>
<PRE class="output">
<!-- START OUTPUT examples5/test5.4.4-bin /bin/ls -->
</PRE>
<A NAME="interrupted"></A>
<P>Therefore it isn't a good idea to use <CODE>nonewline_cf</CODE> and <CODE>noprefix_cf</CODE> like this.
Use instead <CODE>continued_cf</CODE>, <CODE>dc::continued</CODE> and <CODE>dc::finish</CODE>
which are designed especially for <EM>interrupted debug output</EM>:</P>
<P class="download">[<A HREF="examples5/test5.4.5.cc">download</A>]</P>
<PRE>
<!-- START CODE examples5/test5.4.5.cc -->
#include "sys.h"
#include "debug.h"
#include <sys/stat.h>
#include <cstdlib>
std::ostream& operator<<(std::ostream& os, struct stat const buf)
{
os << "inode:" << buf.st_ino << "; " << "size:" << buf.st_size;
return os;
}
std::ostream& operator<<(std::ostream& os, struct stat const* bufp)
{
os << "{ " << *bufp << " }";
return os;
}
// We only use this function to show what happens with the debug output.
// You shouldn't do anything like this in a real program.
int stat_with_buf_alloc(char const* file_name, struct stat*& bufp)
{
bufp = new struct stat;
return stat(file_name, bufp);
}
int main(int argc, char* argv[])
{
Debug( libcw_do.on() );
Debug( dc::notice.on() );
Debug( dc::malloc.on() );
if (argc != 2)
{
std::cerr << "Usage: " << argv[0] << " <file_name>\n";
exit(-1);
}
char const* file_name = argv[1];
struct stat* bufp;
Dout(dc::notice|<SPAN class="highlight">continued_cf</SPAN>,
"stat_with_buf_alloc(\"" << file_name << "\", ");
int ret = stat_with_buf_alloc(file_name, bufp);
Dout(<SPAN class="highlight">dc::finish</SPAN>|cond_error_cf(ret != 0),
bufp << ") = " << ret);
Debug( dc::malloc.off() );
delete bufp;
return 0;
}
<!-- END CODE examples5/test5.4.5.cc -->
</PRE>
<P>Now the output looks like</P>
<PRE class="output">
<!-- START OUTPUT examples5/test5.4.5-bin /bin/ls -->
</PRE>
<A NAME="Methods"></A>
<H4>5.4.2 Methods of the debug object</H4>
<P>You can also change the format of debug output by calling methods of the debug object.
Consider the following example:</P>
<P class="download">[<A HREF="examples5/test5.4.6.cc">download</A>]</P>
<PRE>
<!-- START CODE examples5/test5.4.6.cc -->
#include "sys.h"
#include "debug.h"
int main(void)
{
Debug( libcw_do.on() );
ForAllDebugChannels( if (!debugChannel.is_on()) debugChannel.on() );
Debug( libcw_do.<SPAN class="highlight">margin().assign(</SPAN>"<-- margin -->", 14<SPAN class="highlight">)</SPAN> );
Debug( libcw_do.<SPAN class="highlight">marker().assign(</SPAN>"<-- marker -->", 14<SPAN class="highlight">)</SPAN> );
Dout(dc::cat|dc::mouse, "The cat chases the mouse.");
Dout(dc::mouse|dc::elephant, "The mouse chases the elephant.");
Dout(dc::notice|<SPAN class="highlight">nolabel_cf</SPAN>, "Setting indentation to 8 spaces:");
Dout(dc::notice|<SPAN class="highlight">blank_label_cf</SPAN>, "<------>");
Debug( libcw_do.<SPAN class="highlight">set_indent(8)</SPAN> );
Dout(dc::cat, "The cat sleeps.");
Dout(dc::elephant, "The elephant looks around:");
Dout(dc::elephant|<SPAN class="highlight">blank_label_cf|blank_marker_cf</SPAN>, "where did the mouse go?");
return 0;
}
<!-- END CODE examples5/test5.4.6.cc -->
</PRE>
<P>This program outputs:</P>
<PRE class="output">
<!-- START OUTPUT examples5/test5.4.6-bin -->
</PRE>
<P>This concludes the first part of this tutorial about debug output.
The following chapters handle the memory allocation debugging support of libcwd.</P>
__PAGEEND
<P class="line"><IMG width=870 height=25 src="../images/lines/owl.png"></P>
<DIV class="buttons">
<A HREF="tut4.html"><IMG width=64 height=32 src="../images/buttons/lr_prev.png" border="0"></A>
<A HREF="index.html"><IMG width=64 height=32 src="../images/buttons/lr_index.png" border="0"></A>
<A HREF="tut6.html"><IMG width=64 height=32 src="../images/buttons/lr_next.png" border="0"></A>
</DIV>
__PAGEFOOTER
__HTMLFOOTER
|