[Bio] / FigKernelPackages / Tracer.pm Repository:
ViewVC logotype

Annotation of /FigKernelPackages/Tracer.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.4 - (view) (download) (as text)

1 : olson 1.1 package Tracer;
2 :    
3 :     require Exporter;
4 :     @ISA = ('Exporter');
5 : parrello 1.2 @EXPORT = qw(Trace T TSetup QTrace Confess Cluck);
6 : olson 1.1 @EXPORT_OK = qw(GetFile GetOptions Merge MergeOptions ParseCommand ParseRecord UnEscape);
7 :     use strict;
8 :     use Carp qw(longmess croak);
9 :     use CGI;
10 :    
11 :     =head1 Tracing and Debugging Helpers
12 :    
13 :     =head2 Introduction
14 :    
15 :     This package provides simple tracing for debugging and reporting purposes. To use it simply call the
16 :     L</TSetup> method to set the options and call L</Trace> to write out trace messages. Each trace
17 : parrello 1.2 message has a I<trace level> and I<category> associated with it. In addition, the tracing package itself
18 :     has a list of categories and a single trace level set by the B<TSetup> method. Only messages whose trace
19 : olson 1.1 level is less than or equal to this package's trace level and whose category is activated will
20 : parrello 1.2 be written. Thus, a higher trace level on a message indicates that the message
21 :     is less likely to be seen. A higher trace level passed to B<Setup> means more trace messages will
22 : olson 1.1 appear. To generate a trace message, use the following syntax.
23 :    
24 :     C<< Trace($message) if T(errors => 4); >>
25 :    
26 : parrello 1.2 This statement will produce a trace message if the trace level is 4 or more and the C<errors>
27 : parrello 1.3 category is active. Note that the special category C<main> is always active, so
28 : olson 1.1
29 : parrello 1.3 C<< Trace($message) if T(main => 4); >>
30 : olson 1.1
31 :     will trace if the trace level is 4 or more.
32 :    
33 :     If the category name is the same as the package name, all you need is the number. So, if the
34 :     following call is made in the B<Sprout> package, it will appear if the C<Sprout> category is
35 :     active and the trace level is 2 or more.
36 :    
37 :     C<< Trace($message) if T(2); >>
38 :    
39 : parrello 1.2 To set up tracing, you call the C</Setup> method. The method takes as input a trace level, a list
40 : olson 1.1 of category names, and a set of options. The trace level and list of category names are
41 :     specified as a space-delimited string. Thus
42 :    
43 :     C<< TSetup('3 errors Sprout ERDB', 'HTML'); >>
44 :    
45 :     sets the trace level to 3, activated the C<errors>, C<Sprout>, and C<ERDB> categories, and
46 :     specifies that messages should be output as HTML paragraphs. The idea is to make it easier to
47 :     input tracing configuration on a web form.
48 :    
49 :     In addition to HTML and file output for trace messages, you can specify that the trace messages
50 :     be queued. The messages can then be retrieved by calling the L</QTrace> method. This approach
51 :     is useful if you are building a web page. Instead of having the trace messages interspersed with
52 :     the page output, they can be gathered together and displayed at the end of the page. This makes
53 :     it easier to debug page formatting problems.
54 :    
55 : parrello 1.4 Finally, you can specify that all trace messages be emitted as warnings.
56 :    
57 : olson 1.1 The flexibility of tracing makes it superior to simple use of directives like C<die> and C<warn>.
58 :     Tracer calls can be left in the code with minimal overhead and then turned on only when needed.
59 :     Thus, debugging information is available and easily retrieved even when the application is
60 :     being used out in the field.
61 :    
62 :     =cut
63 : parrello 1.2
64 : olson 1.1 # Declare the configuration variables.
65 :    
66 :     my $Destination = "NONE"; # Description of where to send the trace output.
67 : parrello 1.3 my %Categories = ( main => 1 );
68 : olson 1.1 # hash of active category names
69 :     my $TraceLevel = 0; # trace level; a higher trace level produces more
70 :     # messages
71 :     my @Queue = (); # queued list of trace messages.
72 :    
73 :     =head2 Public Methods
74 :    
75 :     =head3 TSetup
76 :    
77 :     C<< TSetup($categoryList, $target); >>
78 :    
79 :     This method is used to specify the trace options. The options are stored as package data
80 :     and interrogated by the L</Trace> and L</T> methods.
81 :    
82 :     =over 4
83 :    
84 :     =item categoryList
85 :    
86 :     A string specifying the trace level and the categories to be traced, separated by spaces.
87 :     The trace level must come first.
88 :    
89 :     =item target
90 :    
91 :     The destination for the trace output. To send the trace output to a file, specify the file
92 :     name preceded by a ">" symbol. If a double symbol is used (">>"), then the data is appended
93 :     to the file. Otherwise the file is cleared before tracing begins. In addition to sending
94 : parrello 1.4 the trace messages to a file, you can specify a special destination. C<HTML> will
95 : olson 1.1 cause tracing to the standard output with each line formatted as an HTML paragraph. C<TEXT>
96 :     will cause tracing to the standard output as ordinary text. C<QUEUE> will cause trace messages
97 : parrello 1.4 to be stored in a queue for later retrieval by the L</QTrace> method. C<WARN> will cause
98 :     trace messages to be emitted as warnings using the B<warn> directive. C<NONE> will cause
99 : olson 1.1 tracing to be suppressed.
100 :    
101 :     =back
102 :    
103 :     =cut
104 :    
105 :     sub TSetup {
106 :     # Get the parameters.
107 :     my ($categoryList, $target) = @_;
108 :     # Parse the category list.
109 :     my @categoryData = split /\s+/, $categoryList;
110 :     # Extract the trace level.
111 :     $TraceLevel = shift @categoryData;
112 :     # Build the category hash.
113 :     for my $category (@categoryData) {
114 :     $Categories{$category} = 1;
115 :     }
116 :     # Now we need to process the destination information. The most important special
117 :     # case is the single ">", which requires we clear the file first. After doing
118 :     # so, we tack on another ">" sign so that future trace messages are appended.
119 :     if ($target =~ m/^>[^>]/) {
120 :     open TRACEFILE, $target;
121 :     print TRACEFILE Now() . " Tracing initialized.\n";
122 :     close TRACEFILE;
123 :     $Destination = ">$target";
124 :     } else {
125 :     $Destination = uc($target);
126 :     }
127 :     }
128 :    
129 :     =head3 Now
130 :    
131 :     C<< my $string = Tracer::Now(); >>
132 :    
133 :     Return a displayable time stamp containing the local time.
134 :    
135 :     =cut
136 :    
137 :     sub Now {
138 :     my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
139 :     my $retVal = _p2($mon+1) . "/" . _p2($mday) . "/" . ($year + 1900) . " " .
140 :     _p2($hour) . ":" . _p2($min) . ":" . _p2($sec);
141 : parrello 1.2 return $retVal;
142 : olson 1.1 }
143 :    
144 :     # Pad a number to 2 digits.
145 :     sub _p2 {
146 :     my ($value) = @_;
147 :     $value = "0$value" if ($value < 10);
148 :     return $value;
149 :     }
150 :    
151 :     =head3 LogErrors
152 :    
153 :     C<< Tracer::LogErrors($fileName); >>
154 :    
155 :     Route the standard error output to a log file.
156 :    
157 :     =over 4
158 :    
159 :     =item fileName
160 :    
161 :     Name of the file to receive the error output.
162 :    
163 :     =back
164 :    
165 :     =cut
166 :    
167 :     sub LogErrors {
168 :     # Get the file name.
169 :     my ($fileName) = @_;
170 :     # Open the file as the standard error output.
171 :     open STDERR, '>', $fileName;
172 :     }
173 :    
174 :     =head3 GetOptions
175 :    
176 :     C<< Tracer::GetOptions(\%defaults, \%options); >>
177 :    
178 :     Merge a specified set of options into a table of defaults. This method takes two hash references
179 :     as input and uses the data from the second to update the first. If the second does not exist,
180 :     there will be no effect. An error will be thrown if one of the entries in the second hash does not
181 :     exist in the first.
182 :    
183 :     Consider the following example.
184 :    
185 :     C<< my $optionTable = GetOptions({ dbType => 'mySQL', trace => 0 }, $options); >>
186 :    
187 :     In this example, the variable B<$options> is expected to contain at most two options-- B<dbType> and
188 :     B<trace>. The default database type is C<mySQL> and the default trace level is C<0>. If the value of
189 :     B<$options> is C<< {dbType => 'Oracle'} >>, then the database type will be changed to C<Oracle> and
190 :     the trace level will remain at 0. If B<$options> is undefined, then the database type and trace level
191 :     will remain C<mySQL> and C<0>. If, on the other hand, B<$options> is defined as
192 :    
193 :     C<< {databaseType => 'Oracle'} >>
194 :    
195 :     an error will occur because the B<databaseType> option does not exist.
196 :    
197 :     =over 4
198 :    
199 :     =item defaults
200 :    
201 :     Table of default option values.
202 :    
203 :     =item options
204 :    
205 :     Table of overrides, if any.
206 :    
207 :     =item RETURN
208 :    
209 :     Returns a reference to the default table passed in as the first parameter.
210 :    
211 :     =back
212 :    
213 :     =cut
214 :    
215 :     sub GetOptions {
216 :     # Get the parameters.
217 :     my ($defaults, $options) = @_;
218 :     # Check for overrides.
219 :     if ($options) {
220 :     # Loop through the overrides.
221 :     while (my ($option, $setting) = each %{$options}) {
222 :     # Insure this override exists.
223 :     if (!exists $defaults->{$option}) {
224 :     croak "Unrecognized option $option encountered.";
225 :     } else {
226 :     # Apply the override.
227 :     $defaults->{$option} = $setting;
228 :     }
229 :     }
230 :     }
231 :     # Return the merged table.
232 :     return $defaults;
233 :     }
234 :    
235 :     =head3 MergeOptions
236 :    
237 :     C<< Tracer::MergeOptions(\%table, \%defaults); >>
238 :    
239 :     Merge default values into a hash table. This method looks at the key-value pairs in the
240 :     second (default) hash, and if a matching key is not found in the first hash, the default
241 :     pair is copied in. The process is similar to L</GetOptions>, but there is no error-
242 :     checking and no return value.
243 :    
244 :     =over 4
245 :    
246 :     =item table
247 :    
248 :     Hash table to be updated with the default values.
249 :    
250 :     =item defaults
251 :    
252 :     Default values to be merged into the first hash table if they are not already present.
253 :    
254 :     =back
255 :    
256 :     =cut
257 :    
258 :     sub MergeOptions {
259 :     # Get the parameters.
260 :     my ($table, $defaults) = @_;
261 :     # Loop through the defaults.
262 :     while (my ($key, $value) = each %{$defaults}) {
263 :     if (!exists $table->{$key}) {
264 :     $table->{$key} = $value;
265 :     }
266 :     }
267 :     }
268 :    
269 :     =head3 Trace
270 :    
271 :     C<< Trace($message); >>
272 :    
273 :     Write a trace message to the target location specified in L</TSetup>. If there has not been
274 :     any prior call to B<TSetup>.
275 :    
276 :     =over 4
277 :    
278 :     =item message
279 :    
280 :     Message to write.
281 :    
282 :     =back
283 :    
284 :     =cut
285 :    
286 :     sub Trace {
287 :     # Get the parameters.
288 :     my ($message) = @_;
289 :     # Get the timestamp.
290 :     my $timeStamp = Now();
291 :     # Process according to the destination.
292 :     if ($Destination eq "TEXT") {
293 :     # Write the message to the standard output.
294 :     print "$timeStamp $message\n";
295 :     } elsif ($Destination eq "QUEUE") {
296 :     # Push the message into the queue.
297 :     push @Queue, "$timeStamp $message";
298 :     } elsif ($Destination eq "HTML") {
299 :     # Convert the message to HTML and write it to the standard output.
300 :     my $escapedMessage = CGI::escapeHTML($message);
301 :     print "<p>$timeStamp $message</p>\n";
302 : parrello 1.4 } elsif ($Destination eq "WARN") {
303 :     # Emit the message as a warning.
304 :     warn $message;
305 : olson 1.1 } elsif ($Destination =~ m/^>>/) {
306 :     # Write the trace message to an output file.
307 :     open TRACING, $Destination;
308 :     print TRACING "$timeStamp $message\n";
309 :     close TRACING;
310 :     }
311 :     }
312 :    
313 :     =head3 T
314 :    
315 : parrello 1.2 C<< my $switch = T($category, $traceLevel); >>
316 : olson 1.1
317 :     or
318 : parrello 1.2
319 : olson 1.1 C<< my $switch = T($traceLevel); >>
320 :    
321 :     Return TRUE if the trace level is at or above a specified value and the specified category
322 :     is active, else FALSE. If no category is specified, the caller's package name is used.
323 :    
324 :     =over 4
325 :    
326 :     =item category
327 :    
328 :     Category to which the message belongs. If not specified, the caller's package name is
329 :     used.
330 :    
331 :     =item traceLevel
332 :    
333 :     Relevant tracing level.
334 :    
335 :     =item RETURN
336 :    
337 :     TRUE if a message at the specified trace level would appear in the trace, else FALSE.
338 :    
339 :     =back
340 :    
341 :     =cut
342 :    
343 :     sub T {
344 :     # Declare the return variable.
345 :     my $retVal = 0;
346 :     # Only proceed if tracing is turned on.
347 :     if ($Destination ne "NONE") {
348 :     # Get the parameters.
349 :     my ($category, $traceLevel) = @_;
350 :     if (!defined $traceLevel) {
351 :     # Here we have no category, so we need to get the calling package.
352 :     $traceLevel = $category;
353 :     my ($package, $fileName, $line) = caller;
354 : parrello 1.3 # If there is no calling package, we default to "main".
355 : olson 1.1 if (!$package) {
356 : parrello 1.3 $category = "main";
357 : olson 1.1 } else {
358 :     $category = $package;
359 :     }
360 :     }
361 :     # Use the package and tracelevel to compute the result.
362 :     $retVal = ($traceLevel <= $TraceLevel && exists $Categories{$category});
363 : parrello 1.3 }
364 : olson 1.1 # Return the computed result.
365 : parrello 1.3 return $retVal;
366 : olson 1.1 }
367 :    
368 :     =head3 ParseCommand
369 :    
370 :     C<< my ($options, @arguments) = Tracer::ParseCommand(\%optionTable, @inputList); >>
371 :    
372 :     Parse a command line consisting of a list of parameters. The initial parameters may be option
373 : parrello 1.2 specifiers of the form C<->I<option> or C<->I<option>C<=>I<value>. The options are stripped
374 :     off and merged into a table of default options. The remainder of the command line is
375 : olson 1.1 returned as a list of positional arguments. For example, consider the following invocation.
376 :    
377 :     C<< my ($options, @arguments) = ParseCommand({ errors => 0, logFile => 'trace.log'}, @words); >>
378 :    
379 :     In this case, the list @words will be treated as a command line. There are two options available,
380 :     B<errors> and B<logFile>. If @words has the following format
381 :    
382 :     C<< -logFile=error.log apple orange rutabaga >>
383 :    
384 :     then at the end of the invocation, C<$options> will be
385 :    
386 :     C<< { errors => 0, logFile => 'error.log' } >>
387 :    
388 :     and C<@arguments> will contain
389 :    
390 :     C<< apple orange rutabaga >>
391 :    
392 : parrello 1.2 The parser allows for some escape sequences. See L</UnEscape> for a description. There is no
393 : olson 1.1 support for quote characters.
394 :    
395 :     =over 4
396 :    
397 :     =item optionTable
398 :    
399 :     Table of default options.
400 :    
401 :     =item inputList
402 :    
403 :     List of words on the command line.
404 :    
405 :     =item RETURN
406 :    
407 :     Returns a reference to the option table and a list of the positional arguments.
408 :    
409 :     =back
410 :    
411 :     =cut
412 :    
413 :     sub ParseCommand {
414 :     # Get the parameters.
415 :     my ($optionTable, @inputList) = @_;
416 :     # Process any options in the input list.
417 :     my %overrides = ();
418 :     while ((@inputList > 0) && ($inputList[0] =~ /^-/)) {
419 :     # Get the current option.
420 :     my $arg = shift @inputList;
421 :     # Pull out the option name.
422 :     $arg =~ /^-([^=]*)/g;
423 :     my $name = $1;
424 :     # Check for an option value.
425 :     if ($arg =~ /\G=(.*)$/g) {
426 :     # Here we have a value for the option.
427 :     $overrides{$name} = UnEscape($1);
428 :     } else {
429 :     # Here there is no value, so we use 1.
430 :     $overrides{$name} = 1;
431 :     }
432 :     }
433 :     # Merge the options into the defaults.
434 :     GetOptions($optionTable, \%overrides);
435 :     # Translate the remaining parameters.
436 :     my @retVal = ();
437 :     for my $inputParm (@inputList) {
438 :     push @retVal, UnEscape($inputParm);
439 :     }
440 :     # Return the results.
441 :     return ($optionTable, @retVal);
442 :     }
443 :    
444 :     =head3 UnEscape
445 :    
446 :     C<< my $realString = Tracer::UnEscape($codedString); >>
447 :    
448 :     Replace escape sequences with their actual equivalents. C<\b> will be replaced by a space,
449 :     C<\t> by a tab, C<\n> by a new-line character, and C<\\> by a back-slash.
450 :    
451 :     =over 4
452 :    
453 :     =item codedString
454 :    
455 :     String to un-escape.
456 :    
457 :     =item RETURN
458 :    
459 :     Returns a copy of the original string with the escape sequences converted to their actual
460 :     values.
461 :    
462 :     =back
463 :    
464 :     =cut
465 :    
466 :     sub UnEscape {
467 :     # Get the parameter.
468 :     my ($codedString) = @_;
469 :     # Initialize the return variable.
470 :     my $retVal = "";
471 : parrello 1.2 # Loop through the parameter string, looking for escape sequences. We can't do
472 :     # translating because it causes problems with the escaped slash. ("\\b" becomes
473 : olson 1.1 # "\ " no matter what we do.)
474 :     while (length $codedString > 0) {
475 :     # Look for the first escape sequence.
476 :     if ($codedString =~ /^(.*?)\\(\\|b|n|t)/) {
477 :     # Here we found it. The text preceding the sequence is in $1. The sequence
478 :     # itself is in $2. First, move the clear text to the return variable.
479 :     $retVal .= $1;
480 :     $codedString = substr $codedString, (2 + length $1);
481 :     # Decode the escape sequence.
482 :     my $char = $2;
483 :     $char =~ tr/\\btn/\\ \t\n/;
484 :     $retVal .= $char;
485 :     } else {
486 :     # Here there are no more escape sequences. The rest of the string is
487 :     # transferred unmodified.
488 :     $retVal .= $codedString;
489 :     $codedString = "";
490 :     }
491 :     }
492 :     # Return the result.
493 : parrello 1.2 return $retVal;
494 : olson 1.1 }
495 :    
496 :     =head3 ParseRecord
497 :    
498 :     C<< my @fields = Tracer::ParseRecord($line); >>
499 :    
500 :     Parse a tab-delimited data line. The data line is split into field values. Embedded tab
501 :     and new-line characters in the data line must be represented as C<\t> and C<\n>, respectively.
502 :     These will automatically be converted.
503 :    
504 :     =over 4
505 :    
506 :     =item line
507 :    
508 :     Line of data containing the tab-delimited fields.
509 :    
510 :     =item RETURN
511 :    
512 :     Returns a list of the fields found in the data line.
513 :    
514 :     =back
515 :    
516 :     =cut
517 :    
518 :     sub ParseRecord {
519 :     # Get the parameter.
520 :     my ($line) = @_;
521 :     # Remove the trailing new-line, if any.
522 :     chomp $line;
523 :     # Split the line read into pieces using the tab character.
524 :     my @retVal = split /\t/, $line;
525 :     # Trim and fix the escapes in each piece.
526 :     for my $value (@retVal) {
527 :     # Trim leading whitespace.
528 :     $value =~ s/^\s+//;
529 :     # Trim trailing whitespace.
530 :     $value =~ s/\s+$//;
531 :     # Delete the carriage returns.
532 :     $value =~ s/\r//g;
533 :     # Convert the escapes into their real values.
534 :     $value =~ s/\\t/"\t"/ge;
535 :     $value =~ s/\\n/"\n"/ge;
536 :     }
537 :     # Return the result.
538 :     return @retVal;
539 :     }
540 :    
541 :     =head3 Merge
542 :    
543 :     C<< my @mergedList = Tracer::Merge(@inputList); >>
544 :    
545 :     Sort a list of strings and remove duplicates.
546 :    
547 :     =over 4
548 :    
549 :     =item inputList
550 :    
551 :     List of scalars to sort and merge.
552 :    
553 :     =item RETURN
554 :    
555 :     Returns a list containing the same elements sorted in ascending order with duplicates
556 :     removed.
557 :    
558 :     =back
559 :    
560 :     =cut
561 :    
562 :     sub Merge {
563 :     # Get the input list in sort order.
564 :     my @inputList = sort @_;
565 :     # Only proceed if the list has at least two elements.
566 :     if (@inputList > 1) {
567 :     # Now we want to move through the list splicing out duplicates.
568 :     my $i = 0;
569 :     while ($i < @inputList) {
570 :     # Get the current entry.
571 :     my $thisEntry = $inputList[$i];
572 :     # Find out how many elements duplicate the current entry.
573 :     my $j = $i + 1;
574 :     my $dup1 = $i + 1;
575 :     while ($j < @inputList && $inputList[$j] eq $thisEntry) { $j++; };
576 :     # If the number is nonzero, splice out the duplicates found.
577 :     if ($j > $dup1) {
578 :     splice @inputList, $dup1, $j - $dup1;
579 :     }
580 :     # Now the element at position $dup1 is different from the element before it
581 :     # at position $i. We push $i forward one position and start again.
582 :     $i++;
583 :     }
584 :     }
585 :     # Return the merged list.
586 :     return @inputList;
587 :     }
588 :    
589 :     =head3 GetFile
590 :    
591 :     C<< my $fileContents = Tracer::GetFile($fileName); >>
592 :    
593 :     Return the entire contents of a file.
594 :    
595 :     =over 4
596 :    
597 :     =item fileName
598 :    
599 :     Name of the file to read.
600 :    
601 :     =item RETURN
602 :    
603 :     Returns the entire file as a single string. If an error occurs, will return
604 :     an empty string.
605 :    
606 :     =back
607 :    
608 :     =cut
609 :    
610 :     sub GetFile {
611 :     # Get the parameters.
612 :     my ($fileName) = @_;
613 :     # Declare the return variable.
614 :     my $retVal = "";
615 :     # Open the file for input.
616 :     my $ok = open INPUTFILE, "<$fileName";
617 :     if (!$ok) {
618 :     # If we had an error, trace it. We will automatically return a null string.
619 :     Trace(0, "Could not open \"$fileName\" for input.");
620 :     } else {
621 :     # Read the whole file into the return variable.
622 :     while (<INPUTFILE>) {
623 :     $retVal .= $_;
624 :     }
625 :     # Close it.
626 :     close INPUTFILE;
627 :     }
628 :     # Return the file's contents.
629 :     return $retVal;
630 :     }
631 :    
632 :     =head3 QTrace
633 :    
634 :     C<< my $data = QTrace($format); >>
635 :    
636 :     Return the queued trace data in the specified format.
637 :    
638 :     =over 4
639 :    
640 :     =item format
641 :    
642 :     C<html> to format the data as an HTML list, C<text> to format it as straight text.
643 :    
644 :     =back
645 :    
646 :     =cut
647 :    
648 :     sub QTrace {
649 :     # Get the parameter.
650 :     my ($format) = @_;
651 :     # Create the return variable.
652 :     my $retVal = "";
653 :     # Process according to the format.
654 :     if ($format =~ m/^HTML$/i) {
655 :     # Convert the queue into an HTML list.
656 :     $retVal = "<ul>\n";
657 :     for my $line (@Queue) {
658 :     my $escapedLine = CGI::escapeHTML($line);
659 :     $retVal .= "<li>$escapedLine</li>\n";
660 :     }
661 :     $retVal .= "</ul>\n";
662 :     } elsif ($format =~ m/^TEXT$/i) {
663 :     # Convert the queue into a list of text lines.
664 :     $retVal = join("\n", @Queue) . "\n";
665 :     }
666 :     # Clear the queue.
667 :     @Queue = ();
668 :     # Return the formatted list.
669 :     return $retVal;
670 :     }
671 :    
672 :     =head3 Confess
673 :    
674 :     C<< Confess($message); >>
675 :    
676 :     Trace the call stack and abort the program with the specified message. The stack
677 :     trace will only appear if the trace level for this package is 1 or more. When used with
678 :     the OR operator, this method can function as a debugging assert. So, for example
679 :    
680 :     C<< ($recNum >= 0) || Confess("Invalid record number $recNum."); >>
681 :    
682 :     Will abort the program with a stack trace if the value of C<$recNum> is negative.
683 :    
684 :     =over 4
685 :    
686 :     =item message
687 :    
688 :     Message to include in the trace.
689 :    
690 :     =back
691 :    
692 :     =cut
693 :    
694 :     sub Confess {
695 :     # Get the parameters.
696 :     my ($message) = @_;
697 :     # Trace the call stack.
698 :     Cluck($message) if T(1);
699 :     # Abort the program.
700 :     die $message;
701 :     }
702 :    
703 :     =head3 Cluck
704 :    
705 :     C<< Cluck($message); >>
706 :    
707 :     Trace the call stack. Note that for best results, you should qualify the call with a
708 :     trace condition. For example,
709 :    
710 :     C<< Cluck("Starting record parse.") if T(3); >>
711 :    
712 :     will only trace the stack if the trace level for the package is 3 or more.
713 :    
714 :     =over 4
715 :    
716 :     =item message
717 :    
718 :     Message to include in the trace.
719 :    
720 :     =back
721 :    
722 :     =cut
723 :    
724 :     sub Cluck {
725 :     # Get the parameters.
726 :     my ($message) = @_;
727 :     my $confession = longmess($message);
728 :     # Convert the confession to a series of trace messages.
729 :     for my $line (split /\s*\n/, $confession) {
730 :     Trace($line);
731 :     }
732 :     }
733 :    
734 :    
735 :     1;

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3