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

Annotation of /FigKernelPackages/Tracer.pm

Parent Directory Parent Directory | Revision Log Revision Log


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

1 : olson 1.1 package Tracer;
2 :    
3 :     require Exporter;
4 :     @ISA = ('Exporter');
5 : parrello 1.6 @EXPORT = qw(Trace T TSetup QTrace Confess Cluck Min Max Assert);
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 : parrello 1.7 use FIG_Config;
11 : redwards 1.8
12 :     my $UsePageBuilder=1;
13 :     eval {
14 :     require PageBuilder;
15 :     };
16 :     undef $UsePageBuilder if ($@);
17 : olson 1.1
18 :     =head1 Tracing and Debugging Helpers
19 :    
20 :     =head2 Introduction
21 :    
22 :     This package provides simple tracing for debugging and reporting purposes. To use it simply call the
23 :     L</TSetup> method to set the options and call L</Trace> to write out trace messages. Each trace
24 : parrello 1.2 message has a I<trace level> and I<category> associated with it. In addition, the tracing package itself
25 :     has a list of categories and a single trace level set by the B<TSetup> method. Only messages whose trace
26 : olson 1.1 level is less than or equal to this package's trace level and whose category is activated will
27 : parrello 1.2 be written. Thus, a higher trace level on a message indicates that the message
28 :     is less likely to be seen. A higher trace level passed to B<Setup> means more trace messages will
29 : olson 1.1 appear. To generate a trace message, use the following syntax.
30 :    
31 :     C<< Trace($message) if T(errors => 4); >>
32 :    
33 : parrello 1.2 This statement will produce a trace message if the trace level is 4 or more and the C<errors>
34 : parrello 1.3 category is active. Note that the special category C<main> is always active, so
35 : olson 1.1
36 : parrello 1.3 C<< Trace($message) if T(main => 4); >>
37 : olson 1.1
38 :     will trace if the trace level is 4 or more.
39 :    
40 :     If the category name is the same as the package name, all you need is the number. So, if the
41 :     following call is made in the B<Sprout> package, it will appear if the C<Sprout> category is
42 :     active and the trace level is 2 or more.
43 :    
44 :     C<< Trace($message) if T(2); >>
45 :    
46 : parrello 1.2 To set up tracing, you call the C</Setup> method. The method takes as input a trace level, a list
47 : olson 1.1 of category names, and a set of options. The trace level and list of category names are
48 :     specified as a space-delimited string. Thus
49 :    
50 :     C<< TSetup('3 errors Sprout ERDB', 'HTML'); >>
51 :    
52 : parrello 1.7 sets the trace level to 3, activates the C<errors>, C<Sprout>, and C<ERDB> categories, and
53 :     specifies that messages should be output as HTML paragraphs. The parameters are formatted
54 :     to make it easier to input tracing configuration on a web form.
55 : olson 1.1
56 :     In addition to HTML and file output for trace messages, you can specify that the trace messages
57 :     be queued. The messages can then be retrieved by calling the L</QTrace> method. This approach
58 :     is useful if you are building a web page. Instead of having the trace messages interspersed with
59 :     the page output, they can be gathered together and displayed at the end of the page. This makes
60 :     it easier to debug page formatting problems.
61 :    
62 : parrello 1.4 Finally, you can specify that all trace messages be emitted as warnings.
63 :    
64 : olson 1.1 The flexibility of tracing makes it superior to simple use of directives like C<die> and C<warn>.
65 :     Tracer calls can be left in the code with minimal overhead and then turned on only when needed.
66 :     Thus, debugging information is available and easily retrieved even when the application is
67 :     being used out in the field.
68 :    
69 :     =cut
70 : parrello 1.2
71 : olson 1.1 # Declare the configuration variables.
72 :    
73 :     my $Destination = "NONE"; # Description of where to send the trace output.
74 : parrello 1.3 my %Categories = ( main => 1 );
75 : olson 1.1 # hash of active category names
76 :     my $TraceLevel = 0; # trace level; a higher trace level produces more
77 :     # messages
78 :     my @Queue = (); # queued list of trace messages.
79 : parrello 1.7 my $LastCategory = "main"; # name of the last category interrogated
80 : olson 1.1
81 :     =head2 Public Methods
82 :    
83 :     =head3 TSetup
84 :    
85 :     C<< TSetup($categoryList, $target); >>
86 :    
87 :     This method is used to specify the trace options. The options are stored as package data
88 :     and interrogated by the L</Trace> and L</T> methods.
89 :    
90 :     =over 4
91 :    
92 :     =item categoryList
93 :    
94 :     A string specifying the trace level and the categories to be traced, separated by spaces.
95 :     The trace level must come first.
96 :    
97 :     =item target
98 :    
99 :     The destination for the trace output. To send the trace output to a file, specify the file
100 :     name preceded by a ">" symbol. If a double symbol is used (">>"), then the data is appended
101 :     to the file. Otherwise the file is cleared before tracing begins. In addition to sending
102 : parrello 1.6 the trace messages to a file, you can specify a special destination. C<HTML> will cause
103 :     tracing to the standard output with each line formatted as an HTML paragraph. C<TEXT>
104 : parrello 1.5 will cause tracing to the standard output as ordinary text. C<ERROR> will cause trace
105 :     messages to be sent to the standard error output as ordinary text. C<QUEUE> will cause trace
106 : parrello 1.6 messages to be stored in a queue for later retrieval by the L</QTrace> method. C<WARN> will
107 :     cause trace messages to be emitted as warnings using the B<warn> directive. C<NONE> will
108 :     cause tracing to be suppressed.
109 : olson 1.1
110 :     =back
111 :    
112 :     =cut
113 :    
114 :     sub TSetup {
115 :     # Get the parameters.
116 :     my ($categoryList, $target) = @_;
117 :     # Parse the category list.
118 :     my @categoryData = split /\s+/, $categoryList;
119 :     # Extract the trace level.
120 :     $TraceLevel = shift @categoryData;
121 :     # Build the category hash.
122 :     for my $category (@categoryData) {
123 :     $Categories{$category} = 1;
124 :     }
125 :     # Now we need to process the destination information. The most important special
126 :     # case is the single ">", which requires we clear the file first. After doing
127 :     # so, we tack on another ">" sign so that future trace messages are appended.
128 :     if ($target =~ m/^>[^>]/) {
129 :     open TRACEFILE, $target;
130 :     print TRACEFILE Now() . " Tracing initialized.\n";
131 :     close TRACEFILE;
132 :     $Destination = ">$target";
133 :     } else {
134 :     $Destination = uc($target);
135 :     }
136 :     }
137 :    
138 : parrello 1.6 =head3 SetLevel
139 :    
140 :     C<< Tracer::SetLevel($newLevel); >>
141 :    
142 :     Modify the trace level. A higher trace level will cause more messages to appear.
143 :    
144 :     =over 4
145 :    
146 :     =item newLevel
147 :    
148 :     Proposed new trace level.
149 :    
150 :     =back
151 :    
152 :     =cut
153 :    
154 :     sub SetLevel {
155 :     $TraceLevel = $_[0];
156 :     }
157 :    
158 : olson 1.1 =head3 Now
159 :    
160 :     C<< my $string = Tracer::Now(); >>
161 :    
162 :     Return a displayable time stamp containing the local time.
163 :    
164 :     =cut
165 :    
166 :     sub Now {
167 :     my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
168 :     my $retVal = _p2($mon+1) . "/" . _p2($mday) . "/" . ($year + 1900) . " " .
169 :     _p2($hour) . ":" . _p2($min) . ":" . _p2($sec);
170 : parrello 1.2 return $retVal;
171 : olson 1.1 }
172 :    
173 :     # Pad a number to 2 digits.
174 :     sub _p2 {
175 :     my ($value) = @_;
176 :     $value = "0$value" if ($value < 10);
177 :     return $value;
178 :     }
179 :    
180 :     =head3 LogErrors
181 :    
182 :     C<< Tracer::LogErrors($fileName); >>
183 :    
184 :     Route the standard error output to a log file.
185 :    
186 :     =over 4
187 :    
188 :     =item fileName
189 :    
190 :     Name of the file to receive the error output.
191 :    
192 :     =back
193 :    
194 :     =cut
195 :    
196 :     sub LogErrors {
197 :     # Get the file name.
198 :     my ($fileName) = @_;
199 :     # Open the file as the standard error output.
200 :     open STDERR, '>', $fileName;
201 :     }
202 :    
203 : parrello 1.5 =head3 ReadOptions
204 :    
205 :     C<< my %options = Tracer::ReadOptions($fileName); >>
206 :    
207 :     Read a set of options from a file. Each option is encoded in a line of text that has the
208 :     format
209 :    
210 :     I<optionName>C<=>I<optionValue>C<; >I<comment>
211 :    
212 :     The option name must consist entirely of letters, digits, and the punctuation characters
213 :     C<.> and C<_>, and is case sensitive. Blank lines and lines in which the first nonblank
214 :     character is a semi-colon will be ignored. The return hash will map each option name to
215 :     the corresponding option value.
216 :    
217 :     =over 4
218 :    
219 :     =item fileName
220 :    
221 :     Name of the file containing the option data.
222 :    
223 :     =item RETURN
224 :    
225 :     Returns a hash mapping the option names specified in the file to their corresponding option
226 :     value.
227 :    
228 :     =back
229 :    
230 :     =cut
231 :    
232 :     sub ReadOptions {
233 :     # Get the parameters.
234 :     my ($fileName) = @_;
235 :     # Open the file.
236 :     (open CONFIGFILE, "<$fileName") || Confess("Could not open option file $fileName.");
237 :     # Count the number of records read.
238 :     my ($records, $comments) = 0;
239 :     # Create the return hash.
240 :     my %retVal = ();
241 :     # Loop through the file, accumulating key-value pairs.
242 :     while (my $line = <CONFIGFILE>) {
243 :     # Denote we've read a line.
244 :     $records++;
245 :     # Determine the line type.
246 :     if ($line =~ /^\s*[\n\r]/) {
247 :     # A blank line is a comment.
248 :     $comments++;
249 :     } elsif ($line =~ /^\s*([A-Za-z0-9_\.]+)=([^;]*);/) {
250 :     # Here we have an option assignment.
251 :     retVal{$1} = $2;
252 :     } elsif ($line =~ /^\s*;/) {
253 :     # Here we have a text comment.
254 :     $comments++;
255 :     } else {
256 :     # Here we have an invalid line.
257 :     Trace("Invalid option statement in record $records.") if T(0);
258 :     }
259 :     }
260 :     # Return the hash created.
261 :     return %retVal;
262 :     }
263 :    
264 : olson 1.1 =head3 GetOptions
265 :    
266 :     C<< Tracer::GetOptions(\%defaults, \%options); >>
267 :    
268 :     Merge a specified set of options into a table of defaults. This method takes two hash references
269 :     as input and uses the data from the second to update the first. If the second does not exist,
270 :     there will be no effect. An error will be thrown if one of the entries in the second hash does not
271 :     exist in the first.
272 :    
273 :     Consider the following example.
274 :    
275 :     C<< my $optionTable = GetOptions({ dbType => 'mySQL', trace => 0 }, $options); >>
276 :    
277 :     In this example, the variable B<$options> is expected to contain at most two options-- B<dbType> and
278 :     B<trace>. The default database type is C<mySQL> and the default trace level is C<0>. If the value of
279 :     B<$options> is C<< {dbType => 'Oracle'} >>, then the database type will be changed to C<Oracle> and
280 :     the trace level will remain at 0. If B<$options> is undefined, then the database type and trace level
281 :     will remain C<mySQL> and C<0>. If, on the other hand, B<$options> is defined as
282 :    
283 :     C<< {databaseType => 'Oracle'} >>
284 :    
285 :     an error will occur because the B<databaseType> option does not exist.
286 :    
287 :     =over 4
288 :    
289 :     =item defaults
290 :    
291 :     Table of default option values.
292 :    
293 :     =item options
294 :    
295 :     Table of overrides, if any.
296 :    
297 :     =item RETURN
298 :    
299 :     Returns a reference to the default table passed in as the first parameter.
300 :    
301 :     =back
302 :    
303 :     =cut
304 :    
305 :     sub GetOptions {
306 :     # Get the parameters.
307 :     my ($defaults, $options) = @_;
308 :     # Check for overrides.
309 :     if ($options) {
310 :     # Loop through the overrides.
311 :     while (my ($option, $setting) = each %{$options}) {
312 :     # Insure this override exists.
313 :     if (!exists $defaults->{$option}) {
314 :     croak "Unrecognized option $option encountered.";
315 :     } else {
316 :     # Apply the override.
317 :     $defaults->{$option} = $setting;
318 :     }
319 :     }
320 :     }
321 :     # Return the merged table.
322 :     return $defaults;
323 :     }
324 :    
325 :     =head3 MergeOptions
326 :    
327 :     C<< Tracer::MergeOptions(\%table, \%defaults); >>
328 :    
329 :     Merge default values into a hash table. This method looks at the key-value pairs in the
330 :     second (default) hash, and if a matching key is not found in the first hash, the default
331 :     pair is copied in. The process is similar to L</GetOptions>, but there is no error-
332 :     checking and no return value.
333 :    
334 :     =over 4
335 :    
336 :     =item table
337 :    
338 :     Hash table to be updated with the default values.
339 :    
340 :     =item defaults
341 :    
342 :     Default values to be merged into the first hash table if they are not already present.
343 :    
344 :     =back
345 :    
346 :     =cut
347 :    
348 :     sub MergeOptions {
349 :     # Get the parameters.
350 :     my ($table, $defaults) = @_;
351 :     # Loop through the defaults.
352 :     while (my ($key, $value) = each %{$defaults}) {
353 :     if (!exists $table->{$key}) {
354 :     $table->{$key} = $value;
355 :     }
356 :     }
357 :     }
358 :    
359 :     =head3 Trace
360 :    
361 :     C<< Trace($message); >>
362 :    
363 :     Write a trace message to the target location specified in L</TSetup>. If there has not been
364 :     any prior call to B<TSetup>.
365 :    
366 :     =over 4
367 :    
368 :     =item message
369 :    
370 :     Message to write.
371 :    
372 :     =back
373 :    
374 :     =cut
375 :    
376 :     sub Trace {
377 :     # Get the parameters.
378 :     my ($message) = @_;
379 :     # Get the timestamp.
380 :     my $timeStamp = Now();
381 : parrello 1.5 # Format the message.
382 : parrello 1.7 my $formatted = "$timeStamp <$LastCategory>: $message";
383 : olson 1.1 # Process according to the destination.
384 :     if ($Destination eq "TEXT") {
385 :     # Write the message to the standard output.
386 : parrello 1.5 print "$formatted\n";
387 :     } elsif ($Destination eq "ERROR") {
388 :     # Write the message to the error output.
389 :     print STDERR "$formatted\n";
390 : olson 1.1 } elsif ($Destination eq "QUEUE") {
391 :     # Push the message into the queue.
392 : parrello 1.5 push @Queue, "$formatted";
393 : olson 1.1 } elsif ($Destination eq "HTML") {
394 :     # Convert the message to HTML and write it to the standard output.
395 :     my $escapedMessage = CGI::escapeHTML($message);
396 : parrello 1.5 print "<p>$formatted</p>\n";
397 : parrello 1.4 } elsif ($Destination eq "WARN") {
398 :     # Emit the message as a warning.
399 :     warn $message;
400 : olson 1.1 } elsif ($Destination =~ m/^>>/) {
401 :     # Write the trace message to an output file.
402 :     open TRACING, $Destination;
403 : parrello 1.5 print TRACING "$formatted\n";
404 : olson 1.1 close TRACING;
405 :     }
406 :     }
407 :    
408 :     =head3 T
409 :    
410 : parrello 1.2 C<< my $switch = T($category, $traceLevel); >>
411 : olson 1.1
412 :     or
413 : parrello 1.2
414 : olson 1.1 C<< my $switch = T($traceLevel); >>
415 :    
416 :     Return TRUE if the trace level is at or above a specified value and the specified category
417 :     is active, else FALSE. If no category is specified, the caller's package name is used.
418 :    
419 :     =over 4
420 :    
421 :     =item category
422 :    
423 :     Category to which the message belongs. If not specified, the caller's package name is
424 :     used.
425 :    
426 :     =item traceLevel
427 :    
428 :     Relevant tracing level.
429 :    
430 :     =item RETURN
431 :    
432 :     TRUE if a message at the specified trace level would appear in the trace, else FALSE.
433 :    
434 :     =back
435 :    
436 :     =cut
437 :    
438 :     sub T {
439 :     # Declare the return variable.
440 :     my $retVal = 0;
441 :     # Only proceed if tracing is turned on.
442 :     if ($Destination ne "NONE") {
443 :     # Get the parameters.
444 :     my ($category, $traceLevel) = @_;
445 :     if (!defined $traceLevel) {
446 :     # Here we have no category, so we need to get the calling package.
447 :     $traceLevel = $category;
448 :     my ($package, $fileName, $line) = caller;
449 : parrello 1.3 # If there is no calling package, we default to "main".
450 : olson 1.1 if (!$package) {
451 : parrello 1.3 $category = "main";
452 : olson 1.1 } else {
453 :     $category = $package;
454 :     }
455 :     }
456 : parrello 1.7 # Save the category name.
457 :     $LastCategory = $category;
458 :     # Use the category and tracelevel to compute the result.
459 : olson 1.1 $retVal = ($traceLevel <= $TraceLevel && exists $Categories{$category});
460 : parrello 1.3 }
461 : olson 1.1 # Return the computed result.
462 : parrello 1.3 return $retVal;
463 : olson 1.1 }
464 :    
465 :     =head3 ParseCommand
466 :    
467 :     C<< my ($options, @arguments) = Tracer::ParseCommand(\%optionTable, @inputList); >>
468 :    
469 :     Parse a command line consisting of a list of parameters. The initial parameters may be option
470 : parrello 1.2 specifiers of the form C<->I<option> or C<->I<option>C<=>I<value>. The options are stripped
471 :     off and merged into a table of default options. The remainder of the command line is
472 : olson 1.1 returned as a list of positional arguments. For example, consider the following invocation.
473 :    
474 :     C<< my ($options, @arguments) = ParseCommand({ errors => 0, logFile => 'trace.log'}, @words); >>
475 :    
476 :     In this case, the list @words will be treated as a command line. There are two options available,
477 :     B<errors> and B<logFile>. If @words has the following format
478 :    
479 :     C<< -logFile=error.log apple orange rutabaga >>
480 :    
481 :     then at the end of the invocation, C<$options> will be
482 :    
483 :     C<< { errors => 0, logFile => 'error.log' } >>
484 :    
485 :     and C<@arguments> will contain
486 :    
487 :     C<< apple orange rutabaga >>
488 :    
489 : parrello 1.2 The parser allows for some escape sequences. See L</UnEscape> for a description. There is no
490 : olson 1.1 support for quote characters.
491 :    
492 :     =over 4
493 :    
494 :     =item optionTable
495 :    
496 :     Table of default options.
497 :    
498 :     =item inputList
499 :    
500 :     List of words on the command line.
501 :    
502 :     =item RETURN
503 :    
504 :     Returns a reference to the option table and a list of the positional arguments.
505 :    
506 :     =back
507 :    
508 :     =cut
509 :    
510 :     sub ParseCommand {
511 :     # Get the parameters.
512 :     my ($optionTable, @inputList) = @_;
513 :     # Process any options in the input list.
514 :     my %overrides = ();
515 :     while ((@inputList > 0) && ($inputList[0] =~ /^-/)) {
516 :     # Get the current option.
517 :     my $arg = shift @inputList;
518 :     # Pull out the option name.
519 :     $arg =~ /^-([^=]*)/g;
520 :     my $name = $1;
521 :     # Check for an option value.
522 :     if ($arg =~ /\G=(.*)$/g) {
523 :     # Here we have a value for the option.
524 :     $overrides{$name} = UnEscape($1);
525 :     } else {
526 :     # Here there is no value, so we use 1.
527 :     $overrides{$name} = 1;
528 :     }
529 :     }
530 :     # Merge the options into the defaults.
531 :     GetOptions($optionTable, \%overrides);
532 :     # Translate the remaining parameters.
533 :     my @retVal = ();
534 :     for my $inputParm (@inputList) {
535 :     push @retVal, UnEscape($inputParm);
536 :     }
537 :     # Return the results.
538 :     return ($optionTable, @retVal);
539 :     }
540 :    
541 :     =head3 UnEscape
542 :    
543 :     C<< my $realString = Tracer::UnEscape($codedString); >>
544 :    
545 :     Replace escape sequences with their actual equivalents. C<\b> will be replaced by a space,
546 :     C<\t> by a tab, C<\n> by a new-line character, and C<\\> by a back-slash.
547 :    
548 :     =over 4
549 :    
550 :     =item codedString
551 :    
552 :     String to un-escape.
553 :    
554 :     =item RETURN
555 :    
556 :     Returns a copy of the original string with the escape sequences converted to their actual
557 :     values.
558 :    
559 :     =back
560 :    
561 :     =cut
562 :    
563 :     sub UnEscape {
564 :     # Get the parameter.
565 :     my ($codedString) = @_;
566 :     # Initialize the return variable.
567 :     my $retVal = "";
568 : parrello 1.2 # Loop through the parameter string, looking for escape sequences. We can't do
569 :     # translating because it causes problems with the escaped slash. ("\\b" becomes
570 : olson 1.1 # "\ " no matter what we do.)
571 :     while (length $codedString > 0) {
572 :     # Look for the first escape sequence.
573 :     if ($codedString =~ /^(.*?)\\(\\|b|n|t)/) {
574 :     # Here we found it. The text preceding the sequence is in $1. The sequence
575 :     # itself is in $2. First, move the clear text to the return variable.
576 :     $retVal .= $1;
577 :     $codedString = substr $codedString, (2 + length $1);
578 :     # Decode the escape sequence.
579 :     my $char = $2;
580 :     $char =~ tr/\\btn/\\ \t\n/;
581 :     $retVal .= $char;
582 :     } else {
583 :     # Here there are no more escape sequences. The rest of the string is
584 :     # transferred unmodified.
585 :     $retVal .= $codedString;
586 :     $codedString = "";
587 :     }
588 :     }
589 :     # Return the result.
590 : parrello 1.2 return $retVal;
591 : olson 1.1 }
592 :    
593 :     =head3 ParseRecord
594 :    
595 :     C<< my @fields = Tracer::ParseRecord($line); >>
596 :    
597 :     Parse a tab-delimited data line. The data line is split into field values. Embedded tab
598 :     and new-line characters in the data line must be represented as C<\t> and C<\n>, respectively.
599 :     These will automatically be converted.
600 :    
601 :     =over 4
602 :    
603 :     =item line
604 :    
605 :     Line of data containing the tab-delimited fields.
606 :    
607 :     =item RETURN
608 :    
609 :     Returns a list of the fields found in the data line.
610 :    
611 :     =back
612 :    
613 :     =cut
614 :    
615 :     sub ParseRecord {
616 :     # Get the parameter.
617 :     my ($line) = @_;
618 :     # Remove the trailing new-line, if any.
619 :     chomp $line;
620 :     # Split the line read into pieces using the tab character.
621 :     my @retVal = split /\t/, $line;
622 :     # Trim and fix the escapes in each piece.
623 :     for my $value (@retVal) {
624 :     # Trim leading whitespace.
625 :     $value =~ s/^\s+//;
626 :     # Trim trailing whitespace.
627 :     $value =~ s/\s+$//;
628 :     # Delete the carriage returns.
629 :     $value =~ s/\r//g;
630 :     # Convert the escapes into their real values.
631 :     $value =~ s/\\t/"\t"/ge;
632 :     $value =~ s/\\n/"\n"/ge;
633 :     }
634 :     # Return the result.
635 :     return @retVal;
636 :     }
637 :    
638 :     =head3 Merge
639 :    
640 :     C<< my @mergedList = Tracer::Merge(@inputList); >>
641 :    
642 :     Sort a list of strings and remove duplicates.
643 :    
644 :     =over 4
645 :    
646 :     =item inputList
647 :    
648 :     List of scalars to sort and merge.
649 :    
650 :     =item RETURN
651 :    
652 :     Returns a list containing the same elements sorted in ascending order with duplicates
653 :     removed.
654 :    
655 :     =back
656 :    
657 :     =cut
658 :    
659 :     sub Merge {
660 :     # Get the input list in sort order.
661 :     my @inputList = sort @_;
662 :     # Only proceed if the list has at least two elements.
663 :     if (@inputList > 1) {
664 :     # Now we want to move through the list splicing out duplicates.
665 :     my $i = 0;
666 :     while ($i < @inputList) {
667 :     # Get the current entry.
668 :     my $thisEntry = $inputList[$i];
669 :     # Find out how many elements duplicate the current entry.
670 :     my $j = $i + 1;
671 :     my $dup1 = $i + 1;
672 :     while ($j < @inputList && $inputList[$j] eq $thisEntry) { $j++; };
673 :     # If the number is nonzero, splice out the duplicates found.
674 :     if ($j > $dup1) {
675 :     splice @inputList, $dup1, $j - $dup1;
676 :     }
677 :     # Now the element at position $dup1 is different from the element before it
678 :     # at position $i. We push $i forward one position and start again.
679 :     $i++;
680 :     }
681 :     }
682 :     # Return the merged list.
683 :     return @inputList;
684 :     }
685 :    
686 :     =head3 GetFile
687 :    
688 : parrello 1.6 C<< my @fileContents = Tracer::GetFile($fileName); >>
689 : olson 1.1
690 :     Return the entire contents of a file.
691 :    
692 :     =over 4
693 :    
694 :     =item fileName
695 :    
696 :     Name of the file to read.
697 :    
698 :     =item RETURN
699 :    
700 : parrello 1.6 In a list context, returns the entire file as a list with the line terminators removed.
701 :     In a scalar context, returns the entire file as a string.
702 : olson 1.1
703 :     =back
704 :    
705 :     =cut
706 :    
707 :     sub GetFile {
708 :     # Get the parameters.
709 :     my ($fileName) = @_;
710 :     # Declare the return variable.
711 : parrello 1.6 my @retVal = ();
712 : olson 1.1 # Open the file for input.
713 :     my $ok = open INPUTFILE, "<$fileName";
714 :     if (!$ok) {
715 : parrello 1.6 # If we had an error, trace it. We will automatically return a null value.
716 :     Trace("Could not open \"$fileName\" for input.") if T(0);
717 : olson 1.1 } else {
718 : parrello 1.6 # Read the whole file into the return variable, stripping off an terminator
719 :     # characters.
720 :     my $lineCount = 0;
721 :     while (my $line = <INPUTFILE>) {
722 :     $lineCount++;
723 :     $line =~ s/(\r|\n)+$//g;
724 :     push @retVal, $line;
725 : olson 1.1 }
726 :     # Close it.
727 :     close INPUTFILE;
728 : parrello 1.6 my $actualLines = @retVal;
729 :     Trace("$lineCount lines read from $fileName. $actualLines processed.") if T(0);
730 : olson 1.1 }
731 : parrello 1.6 # Return the file's contents in the desired format.
732 :     if (wantarray) {
733 :     return @retVal;
734 :     } else {
735 :     return join "\n", @retVal;
736 :     }
737 : olson 1.1 }
738 :    
739 :     =head3 QTrace
740 :    
741 :     C<< my $data = QTrace($format); >>
742 :    
743 :     Return the queued trace data in the specified format.
744 :    
745 :     =over 4
746 :    
747 :     =item format
748 :    
749 :     C<html> to format the data as an HTML list, C<text> to format it as straight text.
750 :    
751 :     =back
752 :    
753 :     =cut
754 :    
755 :     sub QTrace {
756 :     # Get the parameter.
757 :     my ($format) = @_;
758 :     # Create the return variable.
759 :     my $retVal = "";
760 :     # Process according to the format.
761 :     if ($format =~ m/^HTML$/i) {
762 :     # Convert the queue into an HTML list.
763 :     $retVal = "<ul>\n";
764 :     for my $line (@Queue) {
765 :     my $escapedLine = CGI::escapeHTML($line);
766 :     $retVal .= "<li>$escapedLine</li>\n";
767 :     }
768 :     $retVal .= "</ul>\n";
769 :     } elsif ($format =~ m/^TEXT$/i) {
770 :     # Convert the queue into a list of text lines.
771 :     $retVal = join("\n", @Queue) . "\n";
772 :     }
773 :     # Clear the queue.
774 :     @Queue = ();
775 :     # Return the formatted list.
776 :     return $retVal;
777 :     }
778 :    
779 :     =head3 Confess
780 :    
781 :     C<< Confess($message); >>
782 :    
783 :     Trace the call stack and abort the program with the specified message. The stack
784 :     trace will only appear if the trace level for this package is 1 or more. When used with
785 : parrello 1.6 the OR operator and the L</Assert> method, B<Confess> can function as a debugging assert.
786 :     So, for example
787 : olson 1.1
788 : parrello 1.6 C<< Assert($recNum >= 0) || Confess("Invalid record number $recNum."); >>
789 : olson 1.1
790 :     Will abort the program with a stack trace if the value of C<$recNum> is negative.
791 :    
792 :     =over 4
793 :    
794 :     =item message
795 :    
796 :     Message to include in the trace.
797 :    
798 :     =back
799 :    
800 :     =cut
801 :    
802 :     sub Confess {
803 :     # Get the parameters.
804 :     my ($message) = @_;
805 :     # Trace the call stack.
806 :     Cluck($message) if T(1);
807 :     # Abort the program.
808 : parrello 1.5 croak(">>> $message");
809 : olson 1.1 }
810 :    
811 : parrello 1.6 =head3 Assert
812 :    
813 :     C<< Assert($condition1, $condition2, ... $conditionN); >>
814 :    
815 :     Return TRUE if all the conditions are true. This method can be used in conjunction with
816 :     the OR operator and the L</Confess> method, B<Assert> can function as a debugging assert.
817 :     So, for example
818 :    
819 :     C<< Assert($recNum >= 0) || Confess("Invalid record number $recNum."); >>
820 :    
821 :     Will abort the program with a stack trace if the value of C<$recNum> is negative.
822 :    
823 :     =cut
824 :     sub Assert {
825 :     my $retVal = 1;
826 :     LOOP: for my $condition (@_) {
827 :     if (! $condition) {
828 :     $retVal = 0;
829 :     last LOOP;
830 :     }
831 :     }
832 :     return $retVal;
833 :     }
834 :    
835 : olson 1.1 =head3 Cluck
836 :    
837 :     C<< Cluck($message); >>
838 :    
839 :     Trace the call stack. Note that for best results, you should qualify the call with a
840 :     trace condition. For example,
841 :    
842 :     C<< Cluck("Starting record parse.") if T(3); >>
843 :    
844 :     will only trace the stack if the trace level for the package is 3 or more.
845 :    
846 :     =over 4
847 :    
848 :     =item message
849 :    
850 :     Message to include in the trace.
851 :    
852 :     =back
853 :    
854 :     =cut
855 :    
856 :     sub Cluck {
857 :     # Get the parameters.
858 :     my ($message) = @_;
859 : parrello 1.5 # Trace what's happening.
860 :     Trace("Stack trace for event: $message");
861 : olson 1.1 my $confession = longmess($message);
862 : parrello 1.5 # Convert the confession to a series of trace messages. Note we skip any
863 :     # messages relating to calls into Tracer.
864 : olson 1.1 for my $line (split /\s*\n/, $confession) {
865 : parrello 1.5 Trace($line) if ($line !~ /Tracer\.pm/);
866 : olson 1.1 }
867 :     }
868 :    
869 : parrello 1.5 =head3 Min
870 :    
871 :     C<< my $min = Min($value1, $value2, ... $valueN); >>
872 :    
873 :     Return the minimum argument. The arguments are treated as numbers.
874 :    
875 :     =over 4
876 :    
877 :     =item $value1, $value2, ... $valueN
878 :    
879 :     List of numbers to compare.
880 :    
881 :     =item RETURN
882 :    
883 :     Returns the lowest number in the list.
884 :    
885 :     =back
886 :    
887 :     =cut
888 :    
889 :     sub Min {
890 :     # Get the parameters. Note that we prime the return value with the first parameter.
891 :     my ($retVal, @values) = @_;
892 :     # Loop through the remaining parameters, looking for the lowest.
893 :     for my $value (@values) {
894 :     if ($value < $retVal) {
895 :     $retVal = $value;
896 :     }
897 :     }
898 :     # Return the minimum found.
899 :     return $retVal;
900 :     }
901 :    
902 :     =head3 Max
903 :    
904 :     C<< my $max = Max($value1, $value2, ... $valueN); >>
905 :    
906 :     Return the maximum argument. The arguments are treated as numbers.
907 :    
908 :     =over 4
909 :    
910 :     =item $value1, $value2, ... $valueN
911 :    
912 :     List of numbers to compare.
913 :    
914 :     =item RETURN
915 :    
916 :     Returns the highest number in the list.
917 :    
918 :     =back
919 :    
920 :     =cut
921 :    
922 :     sub Max {
923 :     # Get the parameters. Note that we prime the return value with the first parameter.
924 :     my ($retVal, @values) = @_;
925 :     # Loop through the remaining parameters, looking for the highest.
926 :     for my $value (@values) {
927 :     if ($value > $retVal) {
928 :     $retVal = $value;
929 :     }
930 :     }
931 :     # Return the maximum found.
932 :     return $retVal;
933 :     }
934 :    
935 :     =head3 AddToListMap
936 :    
937 :     C<< Tracer::AddToListMap(\%hash, $key, $value); >>
938 :    
939 :     Add a key-value pair to a hash of lists. If no value exists for the key, a singleton list
940 :     is created for the key. Otherwise, the new value is pushed onto the list.
941 :    
942 :     =over 4
943 :    
944 :     =item hash
945 :    
946 :     Reference to the target hash.
947 :    
948 :     =item key
949 :    
950 :     Key for which the value is to be added.
951 :    
952 :     =item value
953 :    
954 :     Value to add to the key's value list.
955 :    
956 :     =back
957 :    
958 :     =cut
959 :    
960 :     sub AddToListMap {
961 :     # Get the parameters.
962 :     my ($hash, $key, $value) = @_;
963 :     # Process according to whether or not the key already has a value.
964 :     if (! exists $hash->{$key}) {
965 :     $hash->{$key} = [$value];
966 :     } else {
967 :     push @{$hash->{$key}}, $value;
968 :     }
969 :     }
970 : olson 1.1
971 : parrello 1.7 =head3 DebugMode
972 :    
973 :     C<< if (Tracer::DebugMode) { ...code... } >>
974 :    
975 :     Return TRUE if debug mode has been turned on in FIG_Config, else output
976 :     an error page and return FALSE.
977 :    
978 :     Certain CGI scripts are too dangerous to exist in the production
979 :     environment. This method provides a simple way to prevent them
980 :     from working unless they are explicitly turned on in the configuration
981 :     file by setting C<$FIG_Config::debug_mode> to 1. If debugging mode
982 :     is not turned on, an error web page will be output.
983 :    
984 :     =cut
985 :    
986 :     sub DebugMode {
987 :     # Declare the return variable.
988 :     my $retVal;
989 :     # Check the debug configuration.
990 :     if ($FIG_Config::debug_mode) {
991 :     $retVal = 1;
992 :     } else {
993 :     # Here debug mode is off, so we generate an error page.
994 : redwards 1.8 # RAE: PageBuilder is not part of the standard distro, and broke my machine
995 :     if ($UsePageBuilder)
996 :     {
997 :     my $pageString = PageBuilder::Build(">Html/ErrorPage.html", {}, "Html");
998 :     print $pageString;
999 :     }
1000 :     else
1001 :     {
1002 :     print STDERR "There was an error here in FiGKernelPackages/Tracer.pm\n";
1003 :     }
1004 :    
1005 : parrello 1.7 }
1006 :     # Return the determination indicator.
1007 :     return $retVal;
1008 :     }
1009 :    
1010 : redwards 1.8 1;

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3