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

Annotation of /FigKernelPackages/Tracer.pm

Parent Directory Parent Directory | Revision Log Revision Log


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

1 : olson 1.30 #
2 :     # Copyright (c) 2003-2006 University of Chicago and Fellowship
3 :     # for Interpretations of Genomes. All Rights Reserved.
4 :     #
5 :     # This file is part of the SEED Toolkit.
6 :     #
7 :     # The SEED Toolkit is free software. You can redistribute
8 :     # it and/or modify it under the terms of the SEED Toolkit
9 :     # Public License.
10 :     #
11 :     # You should have received a copy of the SEED Toolkit Public License
12 :     # along with this program; if not write to the University of Chicago
13 :     # at info@ci.uchicago.edu or the Fellowship for Interpretation of
14 :     # Genomes at veronika@thefig.info or download a copy from
15 :     # http://www.theseed.org/LICENSE.TXT.
16 :     #
17 :    
18 : olson 1.1 package Tracer;
19 :    
20 : parrello 1.12 require Exporter;
21 :     @ISA = ('Exporter');
22 : parrello 1.35 @EXPORT = qw(Trace T TSetup QTrace Confess Cluck Min Max Assert Open OpenDir TICK StandardSetup ScriptSetup ScriptFinish);
23 : parrello 1.12 @EXPORT_OK = qw(GetFile GetOptions Merge MergeOptions ParseCommand ParseRecord UnEscape Escape);
24 :     use strict;
25 :     use Carp qw(longmess croak);
26 :     use CGI;
27 :     use FIG_Config;
28 : parrello 1.9 use PageBuilder;
29 : parrello 1.21 use Digest::MD5;
30 : parrello 1.36 use File::Basename;
31 : olson 1.1
32 :     =head1 Tracing and Debugging Helpers
33 :    
34 :     =head2 Introduction
35 :    
36 :     This package provides simple tracing for debugging and reporting purposes. To use it simply call the
37 :     L</TSetup> method to set the options and call L</Trace> to write out trace messages. Each trace
38 : parrello 1.2 message has a I<trace level> and I<category> associated with it. In addition, the tracing package itself
39 :     has a list of categories and a single trace level set by the B<TSetup> method. Only messages whose trace
40 : olson 1.1 level is less than or equal to this package's trace level and whose category is activated will
41 : parrello 1.2 be written. Thus, a higher trace level on a message indicates that the message
42 : parrello 1.10 is less likely to be seen. A higher trace level passed to B<TSetup> means more trace messages will
43 : olson 1.1 appear. To generate a trace message, use the following syntax.
44 :    
45 :     C<< Trace($message) if T(errors => 4); >>
46 :    
47 : parrello 1.2 This statement will produce a trace message if the trace level is 4 or more and the C<errors>
48 : parrello 1.3 category is active. Note that the special category C<main> is always active, so
49 : olson 1.1
50 : parrello 1.3 C<< Trace($message) if T(main => 4); >>
51 : olson 1.1
52 :     will trace if the trace level is 4 or more.
53 :    
54 :     If the category name is the same as the package name, all you need is the number. So, if the
55 :     following call is made in the B<Sprout> package, it will appear if the C<Sprout> category is
56 :     active and the trace level is 2 or more.
57 :    
58 :     C<< Trace($message) if T(2); >>
59 :    
60 : parrello 1.10 To set up tracing, you call the L</TSetup> method. The method takes as input a trace level, a list
61 : olson 1.1 of category names, and a set of options. The trace level and list of category names are
62 :     specified as a space-delimited string. Thus
63 :    
64 :     C<< TSetup('3 errors Sprout ERDB', 'HTML'); >>
65 :    
66 : parrello 1.7 sets the trace level to 3, activates the C<errors>, C<Sprout>, and C<ERDB> categories, and
67 : parrello 1.12 specifies that messages should be output as HTML paragraphs.
68 :    
69 :     To turn on tracing for ALL categories, use an asterisk. The call below sets every category to
70 :     level 3 and writes the output to the standard error output. This sort of thing might be
71 :     useful in a CGI environment.
72 :    
73 :     C<< TSetup('3 *', 'WARN'); >>
74 : olson 1.1
75 :     In addition to HTML and file output for trace messages, you can specify that the trace messages
76 :     be queued. The messages can then be retrieved by calling the L</QTrace> method. This approach
77 :     is useful if you are building a web page. Instead of having the trace messages interspersed with
78 :     the page output, they can be gathered together and displayed at the end of the page. This makes
79 :     it easier to debug page formatting problems.
80 :    
81 : parrello 1.4 Finally, you can specify that all trace messages be emitted as warnings.
82 :    
83 : olson 1.1 The flexibility of tracing makes it superior to simple use of directives like C<die> and C<warn>.
84 :     Tracer calls can be left in the code with minimal overhead and then turned on only when needed.
85 :     Thus, debugging information is available and easily retrieved even when the application is
86 :     being used out in the field.
87 :    
88 : parrello 1.10 There is no hard and fast rule on how to use trace levels. The following is therefore only
89 :     a suggestion.
90 :    
91 :     =over 4
92 :    
93 : parrello 1.32 =item Error 0
94 : parrello 1.10
95 :     Message indicates an error that may lead to incorrect results or that has stopped the
96 :     application entirely.
97 :    
98 : parrello 1.32 =item Warning 1
99 : parrello 1.10
100 :     Message indicates something that is unexpected but that probably did not interfere
101 :     with program execution.
102 :    
103 : parrello 1.32 =item Notice 2
104 : parrello 1.10
105 :     Message indicates the beginning or end of a major task.
106 :    
107 : parrello 1.32 =item Information 3
108 : parrello 1.10
109 :     Message indicates a subtask. In the FIG system, a subtask generally relates to a single
110 :     genome. This would be a big loop that is not expected to execute more than 500 times or so.
111 :    
112 : parrello 1.32 =item Detail 4
113 : parrello 1.10
114 :     Message indicates a low-level loop iteration.
115 :    
116 :     =back
117 :    
118 : olson 1.1 =cut
119 : parrello 1.2
120 : olson 1.1 # Declare the configuration variables.
121 :    
122 : parrello 1.12 my $Destination = "NONE"; # Description of where to send the trace output.
123 :     my $TeeFlag = 0; # TRUE if output is going to a file and to the
124 :     # standard output
125 : parrello 1.3 my %Categories = ( main => 1 );
126 : parrello 1.12 # hash of active category names
127 :     my $TraceLevel = 0; # trace level; a higher trace level produces more
128 :     # messages
129 :     my @Queue = (); # queued list of trace messages.
130 : parrello 1.7 my $LastCategory = "main"; # name of the last category interrogated
131 : parrello 1.11 my $SetupCount = 0; # number of times TSetup called
132 : parrello 1.12 my $AllTrace = 0; # TRUE if we are tracing all categories.
133 : olson 1.1
134 :     =head2 Public Methods
135 :    
136 :     =head3 TSetup
137 :    
138 :     C<< TSetup($categoryList, $target); >>
139 :    
140 :     This method is used to specify the trace options. The options are stored as package data
141 :     and interrogated by the L</Trace> and L</T> methods.
142 :    
143 :     =over 4
144 :    
145 :     =item categoryList
146 :    
147 :     A string specifying the trace level and the categories to be traced, separated by spaces.
148 :     The trace level must come first.
149 :    
150 :     =item target
151 :    
152 :     The destination for the trace output. To send the trace output to a file, specify the file
153 :     name preceded by a ">" symbol. If a double symbol is used (">>"), then the data is appended
154 : parrello 1.10 to the file. Otherwise the file is cleared before tracing begins. Precede the first ">"
155 :     symbol with a C<+> to echo output to a file AND to the standard output. In addition to
156 :     sending the trace messages to a file, you can specify a special destination. C<HTML> will
157 :     cause tracing to the standard output with each line formatted as an HTML paragraph. C<TEXT>
158 : parrello 1.5 will cause tracing to the standard output as ordinary text. C<ERROR> will cause trace
159 : parrello 1.9 messages to be sent to the standard error output as ordinary text. C<QUEUE> will cause trace
160 : parrello 1.6 messages to be stored in a queue for later retrieval by the L</QTrace> method. C<WARN> will
161 : parrello 1.9 cause trace messages to be emitted as warnings using the B<warn> directive. C<NONE> will
162 : parrello 1.6 cause tracing to be suppressed.
163 : olson 1.1
164 :     =back
165 :    
166 :     =cut
167 :    
168 :     sub TSetup {
169 : parrello 1.12 # Get the parameters.
170 :     my ($categoryList, $target) = @_;
171 :     # Parse the category list.
172 :     my @categoryData = split /\s+/, $categoryList;
173 :     # Extract the trace level.
174 :     $TraceLevel = shift @categoryData;
175 :     # Presume category-based tracing until we learn otherwise.
176 :     $AllTrace = 0;
177 :     # Build the category hash. Note that if we find a "*", we turn on non-category
178 : parrello 1.33 # tracing. We must also clear away any pre-existing data.
179 : parrello 1.34 %Categories = ( main => 1 );
180 : parrello 1.12 for my $category (@categoryData) {
181 :     if ($category eq '*') {
182 :     $AllTrace = 1;
183 :     } else {
184 : parrello 1.13 $Categories{lc $category} = 1;
185 : parrello 1.12 }
186 :     }
187 :     # Now we need to process the destination information. The most important special
188 :     # cases are the single ">", which requires we clear the file first, and the
189 :     # "+" prefix which indicates a double echo.
190 :     if ($target =~ m/^\+?>>?/) {
191 :     if ($target =~ m/^\+/) {
192 :     $TeeFlag = 1;
193 :     $target = substr($target, 1);
194 :     }
195 :     if ($target =~ m/^>[^>]/) {
196 :     open TRACEFILE, $target;
197 :     print TRACEFILE Now() . " Tracing initialized.\n";
198 :     close TRACEFILE;
199 :     $Destination = ">$target";
200 :     } else {
201 :     $Destination = $target;
202 :     }
203 :     } else {
204 :     $Destination = uc($target);
205 :     }
206 :     # Increment the setup counter.
207 :     $SetupCount++;
208 : parrello 1.11 }
209 :    
210 : parrello 1.31 =head3 StandardSetup
211 :    
212 : parrello 1.36 C<< my ($options, @parameters) = StandardSetup(\@categories, \%options, $parmHelp, @ARGV); >>
213 : parrello 1.31
214 :     This method performs standard command-line parsing and tracing setup. The return
215 :     values are a hash of the command-line options and a list of the positional
216 :     parameters. Tracing is automatically set up and the command-line options are
217 :     validated.
218 :    
219 :     This is a complex method that does a lot of grunt work. The parameters can
220 :     be more easily understood, however, once they are examined individually.
221 :    
222 :     The I<categories> parameter is the most obtuse. It is a reference to a list of
223 :     special-purpose tracing categories. Most tracing categories are PERL package
224 :     names. So, for example, if you wanted to turn on tracing inside the B<Sprout>,
225 :     B<ERDB>, and B<SproutLoad> packages, you would specify the categories
226 :    
227 :     ["Sprout", "SproutLoad", "ERDB"]
228 :    
229 :     This would cause trace messages in the specified three packages to appear in
230 :     the output. There are threer special tracing categories that are automatically
231 :     handled by this method. In other words, if you used L</TSetup> you would need
232 :     to include these categories manually, but if you use this method they are turned
233 :     on automatically.
234 :    
235 :     =over 4
236 :    
237 :     =item FIG
238 :    
239 :     Turns on trace messages inside the B<FIG> package.
240 :    
241 :     =item SQL
242 :    
243 :     Traces SQL commands and activity.
244 :    
245 :     =item Tracer
246 :    
247 :     Traces error messages and call stacks.
248 :    
249 :     =back
250 :    
251 :     C<SQL> is only turned on if the C<-sql> option is specified in the command line.
252 :     The trace level is specified using the C<-trace> command-line option. For example,
253 :     the following command line for C<TransactFeatures> turns on SQL tracing and runs
254 :     all tracing at level 3.
255 :    
256 :     TransactFeatures -trace=3 -sql register ../xacts IDs.tbl
257 :    
258 :     Standard tracing is output to the standard output and echoed to the file
259 :     C<trace.log> in the FIG temporary directory.
260 :    
261 : parrello 1.35 The default trace level is 2. To get all messages, specify a trace level of 4.
262 :     For a genome-by-genome update, use 3.
263 : parrello 1.31
264 :     The I<options> parameter is a reference to a hash containing the command-line
265 : parrello 1.36 options, their default values, and an explanation of what they mean. Command-line
266 :     options may be in the form of switches or keywords. In the case of a switch, the
267 :     option value is 1 if it is specified and 0 if it is not specified. In the case
268 :     of a keyword, the value is separated from the option name by an equal sign. You
269 :     can see this last in the command-line example above.
270 : parrello 1.31
271 :     An example at this point would help. Consider, for example, the command-line utility
272 :     C<TransactFeatures>. It accepts a list of positional parameters plus the options
273 :     C<safe>, C<noAlias>, C<start>, and C<tblFiles>. To start up this command, we execute
274 :     the following code.
275 :    
276 :     my ($options, @parameters) = Tracer::StandardSetup(["DocUtils"],
277 : parrello 1.36 { safe => [0, "use database transactions"],
278 :     noAlias => [0, "do not expect aliases in CHANGE transactions"],
279 :     start => [' ', "start with this genome"],
280 :     tblFiles => [0, "output TBL files containing the corrected IDs"] },
281 :     "command transactionDirectory IDfile",
282 :     @ARGV);
283 : parrello 1.31
284 :    
285 :     The call to C<ParseCommand> specifies the default values for the options and
286 :     stores the actual options in a hash that is returned as C<$options>. The
287 :     positional parameters are returned in C<@parameters>.
288 :    
289 :     The following is a sample command line for C<TransactFeatures>.
290 :    
291 :     TransactFeatures -trace=2 -noAlias register ../xacts IDs.tbl
292 :    
293 :     In this case, C<register>, C<../xacts>, and C<IDs.tbl> are the positional
294 :     parameters, and would find themselves in I<@parameters> after executing the
295 :     above code fragment. The tracing would be set to level 2, and the categories
296 :     would be C<FIG>, C<Tracer>, and <DocUtils>. C<FIG> and C<Tracer> are standard,
297 :     and C<DocUtils> was included because it came in within the first parameter
298 :     to this method. The I<$options> hash would be
299 :    
300 :     { trace => 2, sql => 0, safe => 0,
301 :     noAlias => 1, start => ' ', tblFiles => 0 }
302 :    
303 :     Use of C<StandardSetup> in this way provides a simple way of performing
304 :     standard tracing setup and command-line parsing. Note that the caller is
305 :     not even aware of the command-line switches C<-trace> and C<-sql>, which
306 :     are used by this method to control the tracing. If additional tracing features
307 :     need to be added in the future, they can be processed by this method without
308 :     upsetting the command-line utilities.
309 :    
310 : parrello 1.36 Finally, if the special option C<-h> is specified, the option names will
311 :     be traced at level 0 and the program will exit without processing.
312 :     This provides a limited help capability. For example, if the user enters
313 :    
314 :     TransactFeatures -h
315 :    
316 :     he would see the following output.
317 :    
318 :     TransactFeatures [options] command transactionDirectory IDfile
319 :     -trace tracing level (default 2)
320 :     -sql trace SQL commands
321 :     -safe use database transactions
322 :     -noAlias do not expect aliases in CHANGE transactions
323 :     -start start with this genome
324 :     -tblFiles output TBL files containing the corrected IDs
325 :    
326 : parrello 1.31 The parameters to this method are as follows.
327 :    
328 :     =over 4
329 :    
330 :     =item categories
331 :    
332 :     Reference to a list of tracing category names. These should be names of
333 :     packages whose internal workings will need to be debugged to get the
334 :     command working.
335 :    
336 :     =item options
337 :    
338 :     Reference to a hash containing the legal options for the current command mapped
339 : parrello 1.36 to their default values and descriptions. The user can override the defaults
340 :     by specifying the options as command-line switches prefixed by a hyphen.
341 :     Tracing-related options may be added to this hash. If the C<-h> option is
342 :     specified on the command line, the option descriptions will be used to
343 :     explain the options.
344 :    
345 :     =item parmHelp
346 :    
347 :     A string that vaguely describes the positional parameters. This is used
348 :     if the user specifies the C<-h> option.
349 : parrello 1.31
350 :     =item ARGV
351 :    
352 :     List of command line parameters, including the option switches, which must
353 :     precede the positional parameters and be prefixed by a hyphen.
354 :    
355 :     =item RETURN
356 :    
357 :     Returns a list. The first element of the list is the reference to a hash that
358 :     maps the command-line option switches to their values. These will either be the
359 :     default values or overrides specified on the command line. The remaining
360 :     elements of the list are the position parameters, in order.
361 :    
362 :     =back
363 :    
364 :     =cut
365 :    
366 :     sub StandardSetup {
367 :     # Get the parameters.
368 : parrello 1.36 my ($categories, $options, $parmHelp, @argv) = @_;
369 : parrello 1.31 # Add the tracing options.
370 : parrello 1.36 $options->{trace} = [2, "tracing level"];
371 :     $options->{sql} = [0, "turn on SQL tracing"];
372 :     $options->{h} = [0, "display command-line options"];
373 :     # Create a parsing hash from the options hash. The parsing hash
374 :     # contains the default values rather than the default value
375 :     # and the description. While we're at it, we'll memorize the
376 :     # length of the longest option name.
377 :     my $longestName = 0;
378 :     my %parseOptions = ();
379 :     for my $key (keys %{$options}) {
380 :     if (length $key > $longestName) {
381 :     $longestName = length $key;
382 :     }
383 :     $parseOptions{$key} = $options->{$key}->[0];
384 :     }
385 : parrello 1.31 # Parse the command line.
386 : parrello 1.36 my ($retOptions, @retParameters) = ParseCommand(\%parseOptions, @argv);
387 : parrello 1.31 # Now we want to set up tracing. First, we need to know if SQL is to
388 :     # be traced.
389 :     my @cats = @{$categories};
390 :     if ($retOptions->{sql}) {
391 :     push @cats, "SQL";
392 :     }
393 :     # Add the default categories.
394 :     push @cats, "Tracer", "FIG";
395 :     # Next, we create the category string by prefixing the trace level
396 :     # and joining the categories.
397 : parrello 1.36 my $cats = join(" ", $parseOptions{trace}, @cats);
398 : parrello 1.31 # Now set up the tracing.
399 :     TSetup($cats, "+>$FIG_Config::temp/trace.log");
400 : parrello 1.36 # Check for the "h" option. If it is specified, dump the command-line
401 :     # options and exit the program.
402 :     if ($retOptions->{h}) {
403 :     $0 =~ m#[/\\](\w+)(\.pl)?$#i;
404 :     Trace("$1 [options] $parmHelp") if T(0);
405 :     for my $key (sort keys %{$options}) {
406 :     my $name = Pad($key, $longestName, 0, ' ');
407 :     my $desc = $options->{$key}->[1];
408 :     if ($options->{$key}->[0]) {
409 :     $desc .= " (default " . $options->{$key}->[0] . ")";
410 :     }
411 :     Trace(" $name $desc") if T(0);
412 :     }
413 :     exit(0);
414 :     }
415 : parrello 1.31 # Return the parsed parameters.
416 :     return ($retOptions, @retParameters);
417 :     }
418 :    
419 : parrello 1.11 =head3 Setups
420 :    
421 :     C<< my $count = Tracer::Setups(); >>
422 :    
423 :     Return the number of times L</TSetup> has been called.
424 :    
425 :     This method allows for the creation of conditional tracing setups where, for example, we
426 :     may want to set up tracing if nobody else has done it before us.
427 :    
428 :     =cut
429 :    
430 :     sub Setups {
431 : parrello 1.12 return $SetupCount;
432 : olson 1.1 }
433 :    
434 : parrello 1.10 =head3 Open
435 :    
436 :     C<< my $handle = Open($fileHandle, $fileSpec, $message); >>
437 :    
438 : parrello 1.11 Open a file.
439 : parrello 1.10
440 :     The I<$fileSpec> is essentially the second argument of the PERL C<open>
441 :     function. The mode is specified using Unix-like shell information. So, for
442 :     example,
443 :    
444 : parrello 1.12 Open(\*LOGFILE, '>>/usr/spool/news/twitlog', "Could not open twit log.");
445 : parrello 1.10
446 :     would open for output appended to the specified file, and
447 :    
448 : parrello 1.12 Open(\*DATASTREAM, "| sort -u >$outputFile", "Could not open $outputFile.");
449 : parrello 1.10
450 :     would open a pipe that sorts the records written and removes duplicates. Note
451 : parrello 1.11 the use of file handle syntax in the Open call. To use anonymous file handles,
452 :     code as follows.
453 : parrello 1.10
454 : parrello 1.12 my $logFile = Open(undef, '>>/usr/spool/news/twitlog', "Could not open twit log.");
455 : parrello 1.10
456 : parrello 1.11 The I<$message> parameter is used if the open fails. If it is set to C<0>, then
457 :     the open returns TRUE if successful and FALSE if an error occurred. Otherwise, a
458 :     failed open will throw an exception and the third parameter will be used to construct
459 :     an error message. If the parameter is omitted, a standard message is constructed
460 :     using the file spec.
461 : parrello 1.10
462 : parrello 1.12 Could not open "/usr/spool/news/twitlog"
463 : parrello 1.10
464 :     Note that the mode characters are automatically cleaned from the file name.
465 :     The actual error message from the file system will be captured and appended to the
466 :     message in any case.
467 :    
468 : parrello 1.12 Could not open "/usr/spool/news/twitlog": file not found.
469 : parrello 1.10
470 :     In some versions of PERL the only error message we get is a number, which
471 :     corresponds to the C++ C<errno> value.
472 :    
473 : parrello 1.12 Could not open "/usr/spool/news/twitlog": 6.
474 : parrello 1.10
475 :     =over 4
476 :    
477 :     =item fileHandle
478 :    
479 :     File handle. If this parameter is C<undef>, a file handle will be generated
480 :     and returned as the value of this method.
481 :    
482 :     =item fileSpec
483 :    
484 :     File name and mode, as per the PERL C<open> function.
485 :    
486 :     =item message (optional)
487 :    
488 :     Error message to use if the open fails. If omitted, a standard error message
489 :     will be generated. In either case, the error information from the file system
490 : parrello 1.11 is appended to the message. To specify a conditional open that does not throw
491 :     an error if it fails, use C<0>.
492 : parrello 1.10
493 :     =item RETURN
494 :    
495 : parrello 1.11 Returns the name of the file handle assigned to the file, or C<undef> if the
496 :     open failed.
497 : parrello 1.10
498 :     =back
499 :    
500 :     =cut
501 :    
502 :     sub Open {
503 : parrello 1.12 # Get the parameters.
504 :     my ($fileHandle, $fileSpec, $message) = @_;
505 :     # Attempt to open the file.
506 :     my $rv = open $fileHandle, $fileSpec;
507 :     # If the open failed, generate an error message.
508 :     if (! $rv) {
509 :     # Save the system error message.
510 :     my $sysMessage = $!;
511 :     # See if we need a default message.
512 :     if (!$message) {
513 :     # Clean any obvious mode characters and leading spaces from the
514 :     # filename.
515 :     my ($fileName) = FindNamePart($fileSpec);
516 :     $message = "Could not open \"$fileName\"";
517 :     }
518 :     # Terminate with an error using the supplied message and the
519 :     # error message from the file system.
520 :     Confess("$message: $!");
521 :     }
522 :     # Return the file handle.
523 :     return $fileHandle;
524 : parrello 1.10 }
525 :    
526 : parrello 1.11 =head3 FindNamePart
527 :    
528 :     C<< my ($fileName, $start, $len) = Tracer::FindNamePart($fileSpec); >>
529 :    
530 :     Extract the portion of a file specification that contains the file name.
531 :    
532 :     A file specification is the string passed to an C<open> call. It specifies the file
533 :     mode and name. In a truly complex situation, it can specify a pipe sequence. This
534 :     method assumes that the file name is whatever follows the first angle bracket
535 :     sequence. So, for example, in the following strings the file name is
536 :     C</usr/fig/myfile.txt>.
537 :    
538 :     >>/usr/fig/myfile.txt
539 :     </usr/fig/myfile.txt
540 :     | sort -u > /usr/fig/myfile.txt
541 :    
542 :     If the method cannot find a file name using its normal methods, it will return the
543 :     whole incoming string.
544 :    
545 :     =over 4
546 :    
547 :     =item fileSpec
548 :    
549 :     File specification string from which the file name is to be extracted.
550 :    
551 :     =item RETURN
552 :    
553 :     Returns a three-element list. The first element contains the file name portion of
554 :     the specified string, or the whole string if a file name cannot be found via normal
555 :     methods. The second element contains the start position of the file name portion and
556 :     the third element contains the length.
557 :    
558 :     =back
559 :    
560 :     =cut
561 :     #: Return Type $;
562 :     sub FindNamePart {
563 :     # Get the parameters.
564 :     my ($fileSpec) = @_;
565 : parrello 1.12 # Default to the whole input string.
566 :     my ($retVal, $pos, $len) = ($fileSpec, 0, length $fileSpec);
567 : parrello 1.11 # Parse out the file name if we can.
568 : parrello 1.12 if ($fileSpec =~ m/(<|>>?)(.+?)(\s*)$/) {
569 :     $retVal = $2;
570 :     $len = length $retVal;
571 :     $pos = (length $fileSpec) - (length $3) - $len;
572 :     }
573 : parrello 1.11 # Return the result.
574 :     return ($retVal, $pos, $len);
575 :     }
576 :    
577 :     =head3 OpenDir
578 :    
579 : parrello 1.31 C<< my @files = OpenDir($dirName, $filtered, $flag); >>
580 : parrello 1.11
581 :     Open a directory and return all the file names. This function essentially performs
582 :     the functions of an C<opendir> and C<readdir>. If the I<$filtered> parameter is
583 : parrello 1.31 set to TRUE, all filenames beginning with a period (C<.>), dollar sign (C<$>),
584 : parrello 1.33 or pound sign (C<#>) and all filenames ending with a tilde C<~>) will be
585 :     filtered out of the return list. If the directory does not open and I<$flag> is not
586 :     set, an exception is thrown. So, for example,
587 : parrello 1.11
588 : parrello 1.12 my @files = OpenDir("/Volumes/fig/contigs", 1);
589 : parrello 1.29
590 : parrello 1.11 is effectively the same as
591 :    
592 : parrello 1.12 opendir(TMP, "/Volumes/fig/contigs") || Confess("Could not open /Volumes/fig/contigs.");
593 : parrello 1.33 my @files = grep { $_ !~ /^[\.\$\#]/ && $_ !~ /~$/ } readdir(TMP);
594 : parrello 1.11
595 :     Similarly, the following code
596 :    
597 : parrello 1.31 my @files = grep { $_ =~ /^\d/ } OpenDir("/Volumes/fig/orgs", 0, 1);
598 : parrello 1.29
599 : parrello 1.11 Returns the names of all files in C</Volumes/fig/orgs> that begin with digits and
600 : parrello 1.31 automatically returns an empty list if the directory fails to open.
601 : parrello 1.11
602 :     =over 4
603 :    
604 :     =item dirName
605 :    
606 :     Name of the directory to open.
607 :    
608 :     =item filtered
609 :    
610 :     TRUE if files whose names begin with a period (C<.>) should be automatically removed
611 :     from the list, else FALSE.
612 :    
613 : parrello 1.31 =item flag
614 :    
615 :     TRUE if a failure to open is okay, else FALSE
616 :    
617 : parrello 1.11 =back
618 :    
619 :     =cut
620 :     #: Return Type @;
621 :     sub OpenDir {
622 :     # Get the parameters.
623 : parrello 1.31 my ($dirName, $filtered, $flag) = @_;
624 : parrello 1.11 # Declare the return variable.
625 : parrello 1.31 my @retVal = ();
626 : parrello 1.12 # Open the directory.
627 :     if (opendir(my $dirHandle, $dirName)) {
628 :     # The directory opened successfully. Get the appropriate list according to the
629 :     # strictures of the filter parameter.
630 :     if ($filtered) {
631 : parrello 1.33 @retVal = grep { $_ !~ /^[\.\$\#]/ && $_ !~ /~$/ } readdir $dirHandle;
632 : parrello 1.12 } else {
633 :     @retVal = readdir $dirHandle;
634 :     }
635 : parrello 1.31 } elsif (! $flag) {
636 :     # Here the directory would not open and it's considered an error.
637 : parrello 1.12 Confess("Could not open directory $dirName.");
638 :     }
639 : parrello 1.11 # Return the result.
640 :     return @retVal;
641 :     }
642 :    
643 : parrello 1.6 =head3 SetLevel
644 :    
645 :     C<< Tracer::SetLevel($newLevel); >>
646 :    
647 :     Modify the trace level. A higher trace level will cause more messages to appear.
648 :    
649 :     =over 4
650 :    
651 :     =item newLevel
652 :    
653 :     Proposed new trace level.
654 :    
655 :     =back
656 :    
657 :     =cut
658 :    
659 :     sub SetLevel {
660 :     $TraceLevel = $_[0];
661 :     }
662 :    
663 : olson 1.1 =head3 Now
664 :    
665 :     C<< my $string = Tracer::Now(); >>
666 :    
667 :     Return a displayable time stamp containing the local time.
668 :    
669 :     =cut
670 :    
671 :     sub Now {
672 : parrello 1.12 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
673 :     my $retVal = _p2($mon+1) . "/" . _p2($mday) . "/" . ($year + 1900) . " " .
674 :     _p2($hour) . ":" . _p2($min) . ":" . _p2($sec);
675 :     return $retVal;
676 : olson 1.1 }
677 :    
678 :     # Pad a number to 2 digits.
679 :     sub _p2 {
680 : parrello 1.12 my ($value) = @_;
681 :     $value = "0$value" if ($value < 10);
682 :     return $value;
683 : olson 1.1 }
684 :    
685 :     =head3 LogErrors
686 :    
687 :     C<< Tracer::LogErrors($fileName); >>
688 :    
689 :     Route the standard error output to a log file.
690 :    
691 :     =over 4
692 :    
693 :     =item fileName
694 :    
695 :     Name of the file to receive the error output.
696 :    
697 :     =back
698 :    
699 :     =cut
700 :    
701 :     sub LogErrors {
702 : parrello 1.12 # Get the file name.
703 :     my ($fileName) = @_;
704 :     # Open the file as the standard error output.
705 :     open STDERR, '>', $fileName;
706 : olson 1.1 }
707 :    
708 : parrello 1.5 =head3 ReadOptions
709 :    
710 :     C<< my %options = Tracer::ReadOptions($fileName); >>
711 :    
712 :     Read a set of options from a file. Each option is encoded in a line of text that has the
713 :     format
714 :    
715 :     I<optionName>C<=>I<optionValue>C<; >I<comment>
716 :    
717 :     The option name must consist entirely of letters, digits, and the punctuation characters
718 : parrello 1.9 C<.> and C<_>, and is case sensitive. Blank lines and lines in which the first nonblank
719 :     character is a semi-colon will be ignored. The return hash will map each option name to
720 : parrello 1.5 the corresponding option value.
721 :    
722 :     =over 4
723 :    
724 :     =item fileName
725 :    
726 :     Name of the file containing the option data.
727 :    
728 :     =item RETURN
729 :    
730 :     Returns a hash mapping the option names specified in the file to their corresponding option
731 :     value.
732 :    
733 :     =back
734 :    
735 :     =cut
736 :    
737 :     sub ReadOptions {
738 : parrello 1.12 # Get the parameters.
739 :     my ($fileName) = @_;
740 :     # Open the file.
741 :     (open CONFIGFILE, "<$fileName") || Confess("Could not open option file $fileName.");
742 :     # Count the number of records read.
743 :     my ($records, $comments) = 0;
744 :     # Create the return hash.
745 :     my %retVal = ();
746 :     # Loop through the file, accumulating key-value pairs.
747 :     while (my $line = <CONFIGFILE>) {
748 :     # Denote we've read a line.
749 :     $records++;
750 :     # Determine the line type.
751 :     if ($line =~ /^\s*[\n\r]/) {
752 :     # A blank line is a comment.
753 :     $comments++;
754 :     } elsif ($line =~ /^\s*([A-Za-z0-9_\.]+)=([^;]*);/) {
755 :     # Here we have an option assignment.
756 :     retVal{$1} = $2;
757 :     } elsif ($line =~ /^\s*;/) {
758 :     # Here we have a text comment.
759 :     $comments++;
760 :     } else {
761 :     # Here we have an invalid line.
762 :     Trace("Invalid option statement in record $records.") if T(0);
763 :     }
764 :     }
765 :     # Return the hash created.
766 :     return %retVal;
767 : parrello 1.5 }
768 :    
769 : olson 1.1 =head3 GetOptions
770 :    
771 :     C<< Tracer::GetOptions(\%defaults, \%options); >>
772 :    
773 :     Merge a specified set of options into a table of defaults. This method takes two hash references
774 :     as input and uses the data from the second to update the first. If the second does not exist,
775 :     there will be no effect. An error will be thrown if one of the entries in the second hash does not
776 :     exist in the first.
777 :    
778 :     Consider the following example.
779 :    
780 :     C<< my $optionTable = GetOptions({ dbType => 'mySQL', trace => 0 }, $options); >>
781 :    
782 :     In this example, the variable B<$options> is expected to contain at most two options-- B<dbType> and
783 :     B<trace>. The default database type is C<mySQL> and the default trace level is C<0>. If the value of
784 :     B<$options> is C<< {dbType => 'Oracle'} >>, then the database type will be changed to C<Oracle> and
785 :     the trace level will remain at 0. If B<$options> is undefined, then the database type and trace level
786 :     will remain C<mySQL> and C<0>. If, on the other hand, B<$options> is defined as
787 :    
788 :     C<< {databaseType => 'Oracle'} >>
789 :    
790 :     an error will occur because the B<databaseType> option does not exist.
791 :    
792 :     =over 4
793 :    
794 :     =item defaults
795 :    
796 :     Table of default option values.
797 :    
798 :     =item options
799 :    
800 :     Table of overrides, if any.
801 :    
802 :     =item RETURN
803 :    
804 :     Returns a reference to the default table passed in as the first parameter.
805 :    
806 :     =back
807 :    
808 :     =cut
809 :    
810 :     sub GetOptions {
811 : parrello 1.12 # Get the parameters.
812 :     my ($defaults, $options) = @_;
813 :     # Check for overrides.
814 :     if ($options) {
815 :     # Loop through the overrides.
816 :     while (my ($option, $setting) = each %{$options}) {
817 :     # Insure this override exists.
818 :     if (!exists $defaults->{$option}) {
819 :     croak "Unrecognized option $option encountered.";
820 :     } else {
821 :     # Apply the override.
822 :     $defaults->{$option} = $setting;
823 :     }
824 :     }
825 :     }
826 :     # Return the merged table.
827 :     return $defaults;
828 : olson 1.1 }
829 :    
830 :     =head3 MergeOptions
831 :    
832 :     C<< Tracer::MergeOptions(\%table, \%defaults); >>
833 :    
834 :     Merge default values into a hash table. This method looks at the key-value pairs in the
835 :     second (default) hash, and if a matching key is not found in the first hash, the default
836 :     pair is copied in. The process is similar to L</GetOptions>, but there is no error-
837 :     checking and no return value.
838 :    
839 :     =over 4
840 :    
841 :     =item table
842 :    
843 :     Hash table to be updated with the default values.
844 :    
845 :     =item defaults
846 :    
847 :     Default values to be merged into the first hash table if they are not already present.
848 :    
849 :     =back
850 :    
851 :     =cut
852 :    
853 :     sub MergeOptions {
854 : parrello 1.12 # Get the parameters.
855 :     my ($table, $defaults) = @_;
856 :     # Loop through the defaults.
857 :     while (my ($key, $value) = each %{$defaults}) {
858 :     if (!exists $table->{$key}) {
859 :     $table->{$key} = $value;
860 :     }
861 :     }
862 : olson 1.1 }
863 :    
864 :     =head3 Trace
865 :    
866 :     C<< Trace($message); >>
867 :    
868 :     Write a trace message to the target location specified in L</TSetup>. If there has not been
869 :     any prior call to B<TSetup>.
870 :    
871 :     =over 4
872 :    
873 :     =item message
874 :    
875 :     Message to write.
876 :    
877 :     =back
878 :    
879 :     =cut
880 :    
881 :     sub Trace {
882 : parrello 1.12 # Get the parameters.
883 :     my ($message) = @_;
884 :     # Get the timestamp.
885 :     my $timeStamp = Now();
886 :     # Format the message. Note we strip off any line terminators at the end.
887 :     my $formatted = "$timeStamp <$LastCategory>: " . Strip($message);
888 :     # Process according to the destination.
889 :     if ($Destination eq "TEXT") {
890 :     # Write the message to the standard output.
891 :     print "$formatted\n";
892 :     } elsif ($Destination eq "ERROR") {
893 :     # Write the message to the error output.
894 :     print STDERR "$formatted\n";
895 :     } elsif ($Destination eq "QUEUE") {
896 :     # Push the message into the queue.
897 :     push @Queue, "$formatted";
898 :     } elsif ($Destination eq "HTML") {
899 :     # Convert the message to HTML and write it to the standard output.
900 :     my $escapedMessage = CGI::escapeHTML($message);
901 :     print "<p>$formatted</p>\n";
902 : parrello 1.4 } elsif ($Destination eq "WARN") {
903 :     # Emit the message as a warning.
904 :     warn $message;
905 : parrello 1.12 } elsif ($Destination =~ m/^>>/) {
906 :     # Write the trace message to an output file.
907 : parrello 1.14 (open TRACING, $Destination) || die "Tracing open for \"$Destination\" failed: $!";
908 : parrello 1.12 print TRACING "$formatted\n";
909 :     close TRACING;
910 :     # If the Tee flag is on, echo it to the standard output.
911 :     if ($TeeFlag) {
912 :     print "$formatted\n";
913 :     }
914 :     }
915 : olson 1.1 }
916 :    
917 :     =head3 T
918 :    
919 : parrello 1.2 C<< my $switch = T($category, $traceLevel); >>
920 : olson 1.1
921 : parrello 1.12 or
922 : parrello 1.2
923 : olson 1.1 C<< my $switch = T($traceLevel); >>
924 :    
925 :     Return TRUE if the trace level is at or above a specified value and the specified category
926 :     is active, else FALSE. If no category is specified, the caller's package name is used.
927 :    
928 :     =over 4
929 :    
930 :     =item category
931 :    
932 :     Category to which the message belongs. If not specified, the caller's package name is
933 :     used.
934 :    
935 :     =item traceLevel
936 :    
937 :     Relevant tracing level.
938 :    
939 :     =item RETURN
940 :    
941 :     TRUE if a message at the specified trace level would appear in the trace, else FALSE.
942 :    
943 :     =back
944 :    
945 :     =cut
946 :    
947 :     sub T {
948 : parrello 1.12 # Declare the return variable.
949 :     my $retVal = 0;
950 :     # Only proceed if tracing is turned on.
951 :     if ($Destination ne "NONE") {
952 :     # Get the parameters.
953 :     my ($category, $traceLevel) = @_;
954 :     if (!defined $traceLevel) {
955 :     # Here we have no category, so we need to get the calling package.
956 :     # The calling package is normally the first parameter. If it is
957 :     # omitted, the first parameter will be the tracelevel. So, the
958 :     # first thing we do is shift the so-called category into the
959 :     # $traceLevel variable where it belongs.
960 :     $traceLevel = $category;
961 :     my ($package, $fileName, $line) = caller;
962 : parrello 1.3 # If there is no calling package, we default to "main".
963 : parrello 1.12 if (!$package) {
964 : parrello 1.3 $category = "main";
965 : parrello 1.12 } else {
966 :     $category = $package;
967 :     }
968 :     }
969 : parrello 1.7 # Save the category name.
970 :     $LastCategory = $category;
971 : parrello 1.13 # Convert it to lower case before we hash it.
972 :     $category = lc $category;
973 : parrello 1.12 # Use the category and tracelevel to compute the result.
974 : parrello 1.36 if (ref $traceLevel) {
975 :     Confess("Bad trace level.");
976 :     } elsif (ref $TraceLevel) {
977 :     Confess("Bad trace config.");
978 :     }
979 : parrello 1.12 $retVal = ($traceLevel <= $TraceLevel && ($AllTrace || exists $Categories{$category}));
980 : parrello 1.3 }
981 : parrello 1.12 # Return the computed result.
982 : parrello 1.3 return $retVal;
983 : olson 1.1 }
984 :    
985 :     =head3 ParseCommand
986 :    
987 :     C<< my ($options, @arguments) = Tracer::ParseCommand(\%optionTable, @inputList); >>
988 :    
989 :     Parse a command line consisting of a list of parameters. The initial parameters may be option
990 : parrello 1.2 specifiers of the form C<->I<option> or C<->I<option>C<=>I<value>. The options are stripped
991 :     off and merged into a table of default options. The remainder of the command line is
992 : olson 1.1 returned as a list of positional arguments. For example, consider the following invocation.
993 :    
994 :     C<< my ($options, @arguments) = ParseCommand({ errors => 0, logFile => 'trace.log'}, @words); >>
995 :    
996 :     In this case, the list @words will be treated as a command line. There are two options available,
997 :     B<errors> and B<logFile>. If @words has the following format
998 :    
999 :     C<< -logFile=error.log apple orange rutabaga >>
1000 :    
1001 :     then at the end of the invocation, C<$options> will be
1002 :    
1003 :     C<< { errors => 0, logFile => 'error.log' } >>
1004 :    
1005 :     and C<@arguments> will contain
1006 :    
1007 :     C<< apple orange rutabaga >>
1008 :    
1009 : parrello 1.2 The parser allows for some escape sequences. See L</UnEscape> for a description. There is no
1010 : olson 1.1 support for quote characters.
1011 :    
1012 :     =over 4
1013 :    
1014 :     =item optionTable
1015 :    
1016 :     Table of default options.
1017 :    
1018 :     =item inputList
1019 :    
1020 :     List of words on the command line.
1021 :    
1022 :     =item RETURN
1023 :    
1024 :     Returns a reference to the option table and a list of the positional arguments.
1025 :    
1026 :     =back
1027 :    
1028 :     =cut
1029 :    
1030 :     sub ParseCommand {
1031 : parrello 1.12 # Get the parameters.
1032 :     my ($optionTable, @inputList) = @_;
1033 :     # Process any options in the input list.
1034 :     my %overrides = ();
1035 :     while ((@inputList > 0) && ($inputList[0] =~ /^-/)) {
1036 :     # Get the current option.
1037 :     my $arg = shift @inputList;
1038 :     # Pull out the option name.
1039 :     $arg =~ /^-([^=]*)/g;
1040 :     my $name = $1;
1041 :     # Check for an option value.
1042 :     if ($arg =~ /\G=(.*)$/g) {
1043 :     # Here we have a value for the option.
1044 :     $overrides{$name} = UnEscape($1);
1045 :     } else {
1046 :     # Here there is no value, so we use 1.
1047 :     $overrides{$name} = 1;
1048 :     }
1049 :     }
1050 :     # Merge the options into the defaults.
1051 :     GetOptions($optionTable, \%overrides);
1052 :     # Translate the remaining parameters.
1053 :     my @retVal = ();
1054 :     for my $inputParm (@inputList) {
1055 :     push @retVal, UnEscape($inputParm);
1056 :     }
1057 :     # Return the results.
1058 :     return ($optionTable, @retVal);
1059 : olson 1.1 }
1060 :    
1061 : parrello 1.9 =head3 Escape
1062 :    
1063 :     C<< my $codedString = Tracer::Escape($realString); >>
1064 :    
1065 : parrello 1.25 Escape a string for use in a command length. Tabs will be replaced by C<\t>, new-lines
1066 : parrello 1.28 replaced by C<\n>, carriage returns will be deleted, and backslashes will be doubled. The
1067 :     result is to reverse the effect of L</UnEscape>.
1068 : parrello 1.9
1069 :     =over 4
1070 :    
1071 :     =item realString
1072 :    
1073 :     String to escape.
1074 :    
1075 :     =item RETURN
1076 :    
1077 :     Escaped equivalent of the real string.
1078 :    
1079 :     =back
1080 :    
1081 :     =cut
1082 :    
1083 :     sub Escape {
1084 : parrello 1.12 # Get the parameter.
1085 :     my ($realString) = @_;
1086 :     # Initialize the return variable.
1087 :     my $retVal = "";
1088 :     # Loop through the parameter string, looking for sequences to escape.
1089 :     while (length $realString > 0) {
1090 :     # Look for the first sequence to escape.
1091 : parrello 1.27 if ($realString =~ /^(.*?)([\n\t\r\\])/) {
1092 : parrello 1.12 # Here we found it. The text preceding the sequence is in $1. The sequence
1093 :     # itself is in $2. First, move the clear text to the return variable.
1094 :     $retVal .= $1;
1095 : parrello 1.14 # Strip the processed section off the real string.
1096 :     $realString = substr $realString, (length $2) + (length $1);
1097 : parrello 1.28 # Get the matched character.
1098 : parrello 1.12 my $char = $2;
1099 : parrello 1.28 # If we have a CR, we are done.
1100 :     if ($char ne "\r") {
1101 :     # It's not a CR, so encode the escape sequence.
1102 :     $char =~ tr/\t\n/tn/;
1103 :     $retVal .= "\\" . $char;
1104 :     }
1105 : parrello 1.12 } else {
1106 :     # Here there are no more escape sequences. The rest of the string is
1107 :     # transferred unmodified.
1108 :     $retVal .= $realString;
1109 :     $realString = "";
1110 :     }
1111 :     }
1112 :     # Return the result.
1113 :     return $retVal;
1114 : parrello 1.9 }
1115 :    
1116 : olson 1.1 =head3 UnEscape
1117 :    
1118 :     C<< my $realString = Tracer::UnEscape($codedString); >>
1119 :    
1120 : parrello 1.25 Replace escape sequences with their actual equivalents. C<\t> will be replaced by
1121 : parrello 1.28 a tab, C<\n> by a new-line character, and C<\\> by a backslash. C<\r> codes will
1122 :     be deleted.
1123 : olson 1.1
1124 :     =over 4
1125 :    
1126 :     =item codedString
1127 :    
1128 :     String to un-escape.
1129 :    
1130 :     =item RETURN
1131 :    
1132 :     Returns a copy of the original string with the escape sequences converted to their actual
1133 :     values.
1134 :    
1135 :     =back
1136 :    
1137 :     =cut
1138 :    
1139 :     sub UnEscape {
1140 : parrello 1.12 # Get the parameter.
1141 :     my ($codedString) = @_;
1142 :     # Initialize the return variable.
1143 :     my $retVal = "";
1144 :     # Only proceed if the incoming string is nonempty.
1145 :     if (defined $codedString) {
1146 :     # Loop through the parameter string, looking for escape sequences. We can't do
1147 : parrello 1.25 # translating because it causes problems with the escaped slash. ("\\t" becomes
1148 :     # "\<tab>" no matter what we do.)
1149 : parrello 1.12 while (length $codedString > 0) {
1150 :     # Look for the first escape sequence.
1151 : parrello 1.27 if ($codedString =~ /^(.*?)\\(\\|n|t|r)/) {
1152 : parrello 1.12 # Here we found it. The text preceding the sequence is in $1. The sequence
1153 :     # itself is in $2. First, move the clear text to the return variable.
1154 :     $retVal .= $1;
1155 :     $codedString = substr $codedString, (2 + length $1);
1156 : parrello 1.28 # Get the escape value.
1157 : parrello 1.12 my $char = $2;
1158 : parrello 1.28 # If we have a "\r", we are done.
1159 :     if ($char ne 'r') {
1160 :     # Here it's not an 'r', so we convert it.
1161 :     $char =~ tr/\\tn/\\\t\n/;
1162 :     $retVal .= $char;
1163 :     }
1164 : parrello 1.12 } else {
1165 :     # Here there are no more escape sequences. The rest of the string is
1166 :     # transferred unmodified.
1167 :     $retVal .= $codedString;
1168 :     $codedString = "";
1169 :     }
1170 :     }
1171 :     }
1172 :     # Return the result.
1173 :     return $retVal;
1174 : olson 1.1 }
1175 :    
1176 :     =head3 ParseRecord
1177 :    
1178 :     C<< my @fields = Tracer::ParseRecord($line); >>
1179 :    
1180 :     Parse a tab-delimited data line. The data line is split into field values. Embedded tab
1181 :     and new-line characters in the data line must be represented as C<\t> and C<\n>, respectively.
1182 :     These will automatically be converted.
1183 :    
1184 :     =over 4
1185 :    
1186 :     =item line
1187 :    
1188 :     Line of data containing the tab-delimited fields.
1189 :    
1190 :     =item RETURN
1191 :    
1192 :     Returns a list of the fields found in the data line.
1193 :    
1194 :     =back
1195 :    
1196 :     =cut
1197 :    
1198 :     sub ParseRecord {
1199 : parrello 1.12 # Get the parameter.
1200 :     my ($line) = @_;
1201 :     # Remove the trailing new-line, if any.
1202 :     chomp $line;
1203 :     # Split the line read into pieces using the tab character.
1204 :     my @retVal = split /\t/, $line;
1205 :     # Trim and fix the escapes in each piece.
1206 :     for my $value (@retVal) {
1207 :     # Trim leading whitespace.
1208 :     $value =~ s/^\s+//;
1209 :     # Trim trailing whitespace.
1210 :     $value =~ s/\s+$//;
1211 :     # Delete the carriage returns.
1212 :     $value =~ s/\r//g;
1213 :     # Convert the escapes into their real values.
1214 :     $value =~ s/\\t/"\t"/ge;
1215 :     $value =~ s/\\n/"\n"/ge;
1216 :     }
1217 :     # Return the result.
1218 :     return @retVal;
1219 : olson 1.1 }
1220 :    
1221 :     =head3 Merge
1222 :    
1223 :     C<< my @mergedList = Tracer::Merge(@inputList); >>
1224 :    
1225 :     Sort a list of strings and remove duplicates.
1226 :    
1227 :     =over 4
1228 :    
1229 :     =item inputList
1230 :    
1231 :     List of scalars to sort and merge.
1232 :    
1233 :     =item RETURN
1234 :    
1235 :     Returns a list containing the same elements sorted in ascending order with duplicates
1236 :     removed.
1237 :    
1238 :     =back
1239 :    
1240 :     =cut
1241 :    
1242 :     sub Merge {
1243 : parrello 1.12 # Get the input list in sort order.
1244 :     my @inputList = sort @_;
1245 :     # Only proceed if the list has at least two elements.
1246 :     if (@inputList > 1) {
1247 :     # Now we want to move through the list splicing out duplicates.
1248 :     my $i = 0;
1249 :     while ($i < @inputList) {
1250 :     # Get the current entry.
1251 :     my $thisEntry = $inputList[$i];
1252 :     # Find out how many elements duplicate the current entry.
1253 :     my $j = $i + 1;
1254 :     my $dup1 = $i + 1;
1255 :     while ($j < @inputList && $inputList[$j] eq $thisEntry) { $j++; };
1256 :     # If the number is nonzero, splice out the duplicates found.
1257 :     if ($j > $dup1) {
1258 :     splice @inputList, $dup1, $j - $dup1;
1259 :     }
1260 :     # Now the element at position $dup1 is different from the element before it
1261 :     # at position $i. We push $i forward one position and start again.
1262 :     $i++;
1263 :     }
1264 :     }
1265 :     # Return the merged list.
1266 :     return @inputList;
1267 : olson 1.1 }
1268 :    
1269 :     =head3 GetFile
1270 :    
1271 : parrello 1.6 C<< my @fileContents = Tracer::GetFile($fileName); >>
1272 : olson 1.1
1273 : parrello 1.35 or
1274 :    
1275 :     C<< my $fileContents = Tracer::GetFile($fileName); >>
1276 :    
1277 :     Return the entire contents of a file. In list context, line-ends are removed and
1278 :     each line is a list element. In scalar context, line-ends are replaced by C<\n>.
1279 : olson 1.1
1280 :     =over 4
1281 :    
1282 :     =item fileName
1283 :    
1284 :     Name of the file to read.
1285 :    
1286 :     =item RETURN
1287 :    
1288 : parrello 1.6 In a list context, returns the entire file as a list with the line terminators removed.
1289 :     In a scalar context, returns the entire file as a string.
1290 : olson 1.1
1291 :     =back
1292 :    
1293 :     =cut
1294 :    
1295 :     sub GetFile {
1296 : parrello 1.12 # Get the parameters.
1297 :     my ($fileName) = @_;
1298 :     # Declare the return variable.
1299 :     my @retVal = ();
1300 :     # Open the file for input.
1301 :     my $ok = open INPUTFILE, "<$fileName";
1302 :     if (!$ok) {
1303 :     # If we had an error, trace it. We will automatically return a null value.
1304 : parrello 1.16 Trace("Could not open \"$fileName\" for input: $!") if T(0);
1305 : parrello 1.12 } else {
1306 :     # Read the whole file into the return variable, stripping off any terminator
1307 : parrello 1.6 # characters.
1308 :     my $lineCount = 0;
1309 : parrello 1.12 while (my $line = <INPUTFILE>) {
1310 : parrello 1.6 $lineCount++;
1311 : parrello 1.9 $line = Strip($line);
1312 : parrello 1.12 push @retVal, $line;
1313 :     }
1314 :     # Close it.
1315 :     close INPUTFILE;
1316 : parrello 1.6 my $actualLines = @retVal;
1317 : parrello 1.12 }
1318 :     # Return the file's contents in the desired format.
1319 : parrello 1.9 if (wantarray) {
1320 : parrello 1.12 return @retVal;
1321 : parrello 1.6 } else {
1322 :     return join "\n", @retVal;
1323 :     }
1324 : olson 1.1 }
1325 :    
1326 :     =head3 QTrace
1327 :    
1328 :     C<< my $data = QTrace($format); >>
1329 :    
1330 :     Return the queued trace data in the specified format.
1331 :    
1332 :     =over 4
1333 :    
1334 :     =item format
1335 :    
1336 :     C<html> to format the data as an HTML list, C<text> to format it as straight text.
1337 :    
1338 :     =back
1339 :    
1340 :     =cut
1341 :    
1342 :     sub QTrace {
1343 : parrello 1.12 # Get the parameter.
1344 :     my ($format) = @_;
1345 :     # Create the return variable.
1346 :     my $retVal = "";
1347 : parrello 1.14 # Only proceed if there is an actual queue.
1348 :     if (@Queue) {
1349 :     # Process according to the format.
1350 :     if ($format =~ m/^HTML$/i) {
1351 :     # Convert the queue into an HTML list.
1352 :     $retVal = "<ul>\n";
1353 :     for my $line (@Queue) {
1354 :     my $escapedLine = CGI::escapeHTML($line);
1355 :     $retVal .= "<li>$escapedLine</li>\n";
1356 :     }
1357 :     $retVal .= "</ul>\n";
1358 :     } elsif ($format =~ m/^TEXT$/i) {
1359 :     # Convert the queue into a list of text lines.
1360 :     $retVal = join("\n", @Queue) . "\n";
1361 :     }
1362 :     # Clear the queue.
1363 :     @Queue = ();
1364 : parrello 1.12 }
1365 :     # Return the formatted list.
1366 :     return $retVal;
1367 : olson 1.1 }
1368 :    
1369 :     =head3 Confess
1370 :    
1371 :     C<< Confess($message); >>
1372 :    
1373 : parrello 1.22 Trace the call stack and abort the program with the specified message. When used with
1374 : parrello 1.9 the OR operator and the L</Assert> method, B<Confess> can function as a debugging assert.
1375 : parrello 1.6 So, for example
1376 : olson 1.1
1377 : parrello 1.6 C<< Assert($recNum >= 0) || Confess("Invalid record number $recNum."); >>
1378 : olson 1.1
1379 :     Will abort the program with a stack trace if the value of C<$recNum> is negative.
1380 :    
1381 :     =over 4
1382 :    
1383 :     =item message
1384 :    
1385 :     Message to include in the trace.
1386 :    
1387 :     =back
1388 :    
1389 :     =cut
1390 :    
1391 :     sub Confess {
1392 : parrello 1.12 # Get the parameters.
1393 :     my ($message) = @_;
1394 :     # Trace the call stack.
1395 : parrello 1.22 Cluck($message);
1396 : parrello 1.12 # Abort the program.
1397 :     croak(">>> $message");
1398 : olson 1.1 }
1399 :    
1400 : parrello 1.6 =head3 Assert
1401 :    
1402 :     C<< Assert($condition1, $condition2, ... $conditionN); >>
1403 :    
1404 :     Return TRUE if all the conditions are true. This method can be used in conjunction with
1405 : parrello 1.29 the OR operator and the L</Confess> method as a debugging assert.
1406 : parrello 1.6 So, for example
1407 :    
1408 :     C<< Assert($recNum >= 0) || Confess("Invalid record number $recNum."); >>
1409 :    
1410 :     Will abort the program with a stack trace if the value of C<$recNum> is negative.
1411 :    
1412 :     =cut
1413 :     sub Assert {
1414 :     my $retVal = 1;
1415 :     LOOP: for my $condition (@_) {
1416 :     if (! $condition) {
1417 :     $retVal = 0;
1418 :     last LOOP;
1419 :     }
1420 :     }
1421 :     return $retVal;
1422 :     }
1423 :    
1424 : olson 1.1 =head3 Cluck
1425 :    
1426 :     C<< Cluck($message); >>
1427 :    
1428 :     Trace the call stack. Note that for best results, you should qualify the call with a
1429 :     trace condition. For example,
1430 :    
1431 :     C<< Cluck("Starting record parse.") if T(3); >>
1432 :    
1433 :     will only trace the stack if the trace level for the package is 3 or more.
1434 :    
1435 :     =over 4
1436 :    
1437 :     =item message
1438 :    
1439 :     Message to include in the trace.
1440 :    
1441 :     =back
1442 :    
1443 :     =cut
1444 :    
1445 :     sub Cluck {
1446 : parrello 1.12 # Get the parameters.
1447 :     my ($message) = @_;
1448 : parrello 1.5 # Trace what's happening.
1449 :     Trace("Stack trace for event: $message");
1450 : parrello 1.12 my $confession = longmess($message);
1451 :     # Convert the confession to a series of trace messages. Note we skip any
1452 : parrello 1.5 # messages relating to calls into Tracer.
1453 : parrello 1.12 for my $line (split /\s*\n/, $confession) {
1454 :     Trace($line) if ($line !~ /Tracer\.pm/);
1455 :     }
1456 : olson 1.1 }
1457 :    
1458 : parrello 1.5 =head3 Min
1459 :    
1460 :     C<< my $min = Min($value1, $value2, ... $valueN); >>
1461 :    
1462 :     Return the minimum argument. The arguments are treated as numbers.
1463 :    
1464 :     =over 4
1465 :    
1466 :     =item $value1, $value2, ... $valueN
1467 :    
1468 :     List of numbers to compare.
1469 :    
1470 :     =item RETURN
1471 :    
1472 :     Returns the lowest number in the list.
1473 :    
1474 :     =back
1475 :    
1476 :     =cut
1477 :    
1478 :     sub Min {
1479 : parrello 1.12 # Get the parameters. Note that we prime the return value with the first parameter.
1480 :     my ($retVal, @values) = @_;
1481 :     # Loop through the remaining parameters, looking for the lowest.
1482 :     for my $value (@values) {
1483 :     if ($value < $retVal) {
1484 :     $retVal = $value;
1485 :     }
1486 :     }
1487 :     # Return the minimum found.
1488 :     return $retVal;
1489 : parrello 1.5 }
1490 :    
1491 :     =head3 Max
1492 :    
1493 :     C<< my $max = Max($value1, $value2, ... $valueN); >>
1494 :    
1495 :     Return the maximum argument. The arguments are treated as numbers.
1496 :    
1497 :     =over 4
1498 :    
1499 :     =item $value1, $value2, ... $valueN
1500 :    
1501 :     List of numbers to compare.
1502 :    
1503 :     =item RETURN
1504 :    
1505 :     Returns the highest number in the list.
1506 :    
1507 :     =back
1508 :    
1509 :     =cut
1510 :    
1511 :     sub Max {
1512 : parrello 1.12 # Get the parameters. Note that we prime the return value with the first parameter.
1513 :     my ($retVal, @values) = @_;
1514 :     # Loop through the remaining parameters, looking for the highest.
1515 :     for my $value (@values) {
1516 :     if ($value > $retVal) {
1517 :     $retVal = $value;
1518 :     }
1519 :     }
1520 :     # Return the maximum found.
1521 :     return $retVal;
1522 : parrello 1.5 }
1523 :    
1524 :     =head3 AddToListMap
1525 :    
1526 :     C<< Tracer::AddToListMap(\%hash, $key, $value); >>
1527 :    
1528 :     Add a key-value pair to a hash of lists. If no value exists for the key, a singleton list
1529 :     is created for the key. Otherwise, the new value is pushed onto the list.
1530 :    
1531 :     =over 4
1532 :    
1533 :     =item hash
1534 :    
1535 :     Reference to the target hash.
1536 :    
1537 :     =item key
1538 :    
1539 :     Key for which the value is to be added.
1540 :    
1541 :     =item value
1542 :    
1543 :     Value to add to the key's value list.
1544 :    
1545 :     =back
1546 :    
1547 :     =cut
1548 :    
1549 :     sub AddToListMap {
1550 :     # Get the parameters.
1551 :     my ($hash, $key, $value) = @_;
1552 :     # Process according to whether or not the key already has a value.
1553 :     if (! exists $hash->{$key}) {
1554 :     $hash->{$key} = [$value];
1555 :     } else {
1556 :     push @{$hash->{$key}}, $value;
1557 :     }
1558 :     }
1559 : olson 1.1
1560 : parrello 1.7 =head3 DebugMode
1561 :    
1562 :     C<< if (Tracer::DebugMode) { ...code... } >>
1563 :    
1564 : parrello 1.22 Return TRUE if debug mode has been turned on, else output an error
1565 :     page and return FALSE.
1566 : parrello 1.7
1567 :     Certain CGI scripts are too dangerous to exist in the production
1568 :     environment. This method provides a simple way to prevent them
1569 : parrello 1.21 from working unless they are explicitly turned on by creating a password
1570 :     cookie via the B<SetPassword> script. If debugging mode
1571 : parrello 1.22 is not turned on, an error web page will be output directing the
1572 :     user to enter in the correct password.
1573 : parrello 1.7
1574 :     =cut
1575 :    
1576 :     sub DebugMode {
1577 : parrello 1.12 # Declare the return variable.
1578 : parrello 1.21 my $retVal = 0;
1579 : parrello 1.12 # Check the debug configuration.
1580 : parrello 1.21 my $password = CGI::cookie("DebugMode");
1581 :     my $encrypted = Digest::MD5::md5_hex($password);
1582 :     if ($encrypted eq "252dec43280e0c0d6a75ffcec486e61d") {
1583 : parrello 1.12 $retVal = 1;
1584 :     } else {
1585 :     # Here debug mode is off, so we generate an error page.
1586 : parrello 1.9 my $pageString = PageBuilder::Build("<Html/ErrorPage.html", {}, "Html");
1587 : parrello 1.12 print $pageString;
1588 :     }
1589 :     # Return the determination indicator.
1590 : parrello 1.18 return $retVal;
1591 : parrello 1.9 }
1592 :    
1593 :     =head3 Strip
1594 :    
1595 :     C<< my $string = Tracer::Strip($line); >>
1596 :    
1597 :     Strip all line terminators off a string. This is necessary when dealing with files
1598 :     that may have been transferred back and forth several times among different
1599 :     operating environments.
1600 :    
1601 :     =over 4
1602 :    
1603 :     =item line
1604 :    
1605 :     Line of text to be stripped.
1606 :    
1607 :     =item RETURN
1608 :    
1609 :     The same line of text with all the line-ending characters chopped from the end.
1610 :    
1611 :     =back
1612 :    
1613 :     =cut
1614 :    
1615 :     sub Strip {
1616 : parrello 1.12 # Get a copy of the parameter string.
1617 :     my ($string) = @_;
1618 : parrello 1.29 my $retVal = (defined $string ? $string : "");
1619 : parrello 1.9 # Strip the line terminator characters.
1620 :     $retVal =~ s/(\r|\n)+$//g;
1621 : parrello 1.12 # Return the result.
1622 :     return $retVal;
1623 : parrello 1.9 }
1624 :    
1625 :     =head3 Pad
1626 :    
1627 :     C<< my $paddedString = Tracer::Pad($string, $len, $left, $padChar); >>
1628 :    
1629 :     Pad a string to a specified length. The pad character will be a
1630 :     space, and the padding will be on the right side unless specified
1631 :     in the third parameter.
1632 :    
1633 :     =over 4
1634 :    
1635 :     =item string
1636 :    
1637 :     String to be padded.
1638 :    
1639 :     =item len
1640 :    
1641 :     Desired length of the padded string.
1642 :    
1643 :     =item left (optional)
1644 :    
1645 :     TRUE if the string is to be left-padded; otherwise it will be padded on the right.
1646 :    
1647 :     =item padChar (optional)
1648 :    
1649 : parrello 1.22 Character to use for padding. The default is a space.
1650 :    
1651 : parrello 1.9 =item RETURN
1652 :    
1653 : parrello 1.22 Returns a copy of the original string with the pad character added to the
1654 :     specified end so that it achieves the desired length.
1655 : parrello 1.9
1656 :     =back
1657 :    
1658 :     =cut
1659 :    
1660 :     sub Pad {
1661 : parrello 1.12 # Get the parameters.
1662 :     my ($string, $len, $left, $padChar) = @_;
1663 :     # Compute the padding character.
1664 :     if (! defined $padChar) {
1665 :     $padChar = " ";
1666 :     }
1667 :     # Compute the number of spaces needed.
1668 :     my $needed = $len - length $string;
1669 :     # Copy the string into the return variable.
1670 :     my $retVal = $string;
1671 :     # Only proceed if padding is needed.
1672 :     if ($needed > 0) {
1673 :     # Create the pad string.
1674 :     my $pad = $padChar x $needed;
1675 :     # Affix it to the return value.
1676 :     if ($left) {
1677 :     $retVal = $pad . $retVal;
1678 :     } else {
1679 :     $retVal .= $pad;
1680 :     }
1681 :     }
1682 :     # Return the result.
1683 :     return $retVal;
1684 : parrello 1.7 }
1685 :    
1686 : parrello 1.29 =head3 EOF
1687 :    
1688 :     This is a constant that is lexically greater than any useful string.
1689 :    
1690 :     =cut
1691 :    
1692 :     sub EOF {
1693 :     return "\xFF\xFF\xFF\xFF\xFF";
1694 :     }
1695 :    
1696 : parrello 1.15 =head3 TICK
1697 :    
1698 :     C<< my @results = TICK($commandString); >>
1699 :    
1700 :     Perform a back-tick operation on a command. If this is a Windows environment, any leading
1701 :     dot-slash (C<./> will be removed. So, for example, if you were doing
1702 :    
1703 :     `./protein.cgi`
1704 :    
1705 :     from inside a CGI script, it would work fine in Unix, but would issue an error message
1706 :     in Windows complaining that C<'.'> is not a valid command. If instead you code
1707 :    
1708 :     TICK("./protein.cgi")
1709 :    
1710 :     it will work correctly in both environments.
1711 :    
1712 :     =over 4
1713 :    
1714 :     =item commandString
1715 :    
1716 :     The command string to pass to the system.
1717 :    
1718 :     =item RETURN
1719 :    
1720 :     Returns the standard output from the specified command, as a list.
1721 :    
1722 :     =back
1723 :    
1724 :     =cut
1725 :     #: Return Type @;
1726 :     sub TICK {
1727 :     # Get the parameters.
1728 :     my ($commandString) = @_;
1729 :     # Chop off the dot-slash if this is Windows.
1730 :     if ($FIG_Config::win_mode) {
1731 :     $commandString =~ s!^\./!!;
1732 :     }
1733 :     # Activate the command and return the result.
1734 :     return `$commandString`;
1735 :     }
1736 :    
1737 : parrello 1.35 =head3 ScriptSetup
1738 :    
1739 :     C<< my ($query, $varHash) = ScriptSetup(); >>
1740 :    
1741 :     Perform standard tracing and debugging setup for scripts. The value returned is
1742 :     the CGI object followed by a pre-built variable hash.
1743 :    
1744 :     The C<Trace> query parameter is used to determine whether or not tracing is active and
1745 :     which trace modules (other than C<Tracer> and C<FIG>) should be turned on. Specifying
1746 :     the C<CGI> trace module will trace parameter and environment information. Parameters are
1747 :     traced at level 3 and environment variables at level 4. At the end of the script, the
1748 :     client should call L</ScriptFinish> to output the web page.
1749 :    
1750 :     =cut
1751 :    
1752 :     sub ScriptSetup {
1753 :     # Get the CGI query object.
1754 :     my $query = CGI->new();
1755 :     # Check for tracing. Set it up if the user asked for it.
1756 :     if ($query->param('Trace')) {
1757 :     # Set up tracing to be queued for display at the bottom of the web page.
1758 :     TSetup($query->param('Trace') . " FIG Tracer", "QUEUE");
1759 :     # Trace the parameter and environment data.
1760 :     if (T(CGI => 3)) {
1761 :     # Here we want to trace the parameter data.
1762 :     my @names = $query->param;
1763 :     for my $parmName (sort @names) {
1764 :     # Note we skip "Trace", which is for our use only.
1765 :     if ($parmName ne 'Trace') {
1766 :     my @values = $query->param($parmName);
1767 :     Trace("CGI: $parmName = " . join(", ", @values));
1768 :     }
1769 :     }
1770 :     }
1771 :     if (T(CGI => 4)) {
1772 :     # Here we want the environment data too.
1773 :     for my $envName (sort keys %ENV) {
1774 :     Trace("ENV: $envName = $ENV{$envName}");
1775 :     }
1776 :     }
1777 :     } else {
1778 :     # Here tracing is to be turned off. All we allow is errors traced into the
1779 :     # error log.
1780 :     TSetup("0", "WARN");
1781 :     }
1782 :     # Create the variable hash.
1783 :     my $varHash = { DebugData => '' };
1784 :     # If we're in DEBUG mode, set up the debug mode data for forms.
1785 :     if (Tracer::DebugMode) {
1786 :     $varHash->{DebugData} = GetFile("Html/DebugFragment.html");
1787 :     }
1788 :     # Return the query object and variable hash.
1789 :     return ($query, $varHash);
1790 :     }
1791 :    
1792 :     =head3 ScriptFinish
1793 :    
1794 :     C<< ScriptFinish($webData, $varHash); >>
1795 :    
1796 :     Output a web page at the end of a script. Either the string to be output or the
1797 :     name of a template file can be specified. If the second parameter is omitted,
1798 :     it is assumed we have a string to be output; otherwise, it is assumed we have the
1799 :     name of a template file. The template should have the variable C<DebugData>
1800 :     specified in any form that invokes a standard script. If debugging mode is turned
1801 :     on, a form field will be put in that allows the user to enter tracing data.
1802 :     Trace messages will be placed immediately before the terminal C<BODY> tag in
1803 :     the output, formatted as a list.
1804 :    
1805 :     A typical standard script would loook like the following.
1806 :    
1807 :     BEGIN {
1808 :     # Print the HTML header.
1809 :     print "CONTENT-TYPE: text/html\n\n";
1810 :     }
1811 :     use Tracer;
1812 :     use CGI;
1813 :     use FIG;
1814 :     # ... more uses ...
1815 :    
1816 :     my ($query, $varHash) = ScriptSetup();
1817 :     eval {
1818 :     # ... get data from $query, put it in $varHash ...
1819 :     };
1820 :     if ($@) {
1821 :     Trace("Script Error: $@") if T(0);
1822 :     }
1823 :     ScriptFinish("Html/MyTemplate.html", $varHash);
1824 :    
1825 :     The idea here is that even if the script fails, you'll see trace messages and
1826 :     useful output.
1827 :    
1828 :     =over 4
1829 :    
1830 :     =item webData
1831 :    
1832 :     A string containing either the full web page to be written to the output or the
1833 :     name of a template file from which the page is to be constructed. If the name
1834 :     of a template file is specified, then the second parameter must be present;
1835 :     otherwise, it must be absent.
1836 :    
1837 :     =item varHash (optional)
1838 :    
1839 :     If specified, then a reference to a hash mapping variable names for a template
1840 :     to their values. The template file will be read into memory, and variable markers
1841 :     will be replaced by data in this hash reference.
1842 :    
1843 :     =cut
1844 :    
1845 :     sub ScriptFinish {
1846 :     # Get the parameters.
1847 :     my ($webData, $varHash) = @_;
1848 :     # Check for a template file situation.
1849 :     my $outputString;
1850 :     if (defined $varHash) {
1851 :     # Here we have a template file. We need to apply the variables to the template.
1852 :     $outputString = PageBuilder::Build("<$webData", $varHash, "Html");
1853 :     } else {
1854 :     # Here the user gave us a raw string.
1855 :     $outputString = $webData;
1856 :     }
1857 :     # Check for trace messages.
1858 :     if ($Destination eq "QUEUE") {
1859 :     # We have trace messages, so we want to put them at the end of the body. This
1860 :     # is either at the end of the whole string or at the beginning of the BODY
1861 :     # end-tag.
1862 :     my $pos = length $outputString;
1863 :     if ($outputString =~ m#</body>#gi) {
1864 :     $pos = (pos $outputString) - 7;
1865 :     }
1866 :     substr $outputString, $pos, 0, QTrace('Html');
1867 :     }
1868 :     # Write the output string.
1869 :     print $outputString;
1870 :     }
1871 :    
1872 : redwards 1.8 1;

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3