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

Annotation of /FigKernelPackages/Tracer.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.63 - (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 : parrello 1.61 #
7 : olson 1.30 # The SEED Toolkit is free software. You can redistribute
8 :     # it and/or modify it under the terms of the SEED Toolkit
9 : parrello 1.61 # Public License.
10 : olson 1.30 #
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.45 @EXPORT = qw(Trace T TSetup QTrace Confess Cluck Min Max Assert Open OpenDir TICK StandardSetup ScriptSetup ScriptFinish Insure ChDir);
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 : parrello 1.47 use Cwd;
28 : parrello 1.12 use FIG_Config;
29 : parrello 1.9 use PageBuilder;
30 : parrello 1.21 use Digest::MD5;
31 : parrello 1.36 use File::Basename;
32 : parrello 1.37 use File::Path;
33 : parrello 1.48 use File::stat;
34 : parrello 1.59 use LWP::UserAgent;
35 : olson 1.1
36 :     =head1 Tracing and Debugging Helpers
37 :    
38 :     =head2 Introduction
39 :    
40 :     This package provides simple tracing for debugging and reporting purposes. To use it simply call the
41 :     L</TSetup> method to set the options and call L</Trace> to write out trace messages. Each trace
42 : parrello 1.2 message has a I<trace level> and I<category> associated with it. In addition, the tracing package itself
43 :     has a list of categories and a single trace level set by the B<TSetup> method. Only messages whose trace
44 : olson 1.1 level is less than or equal to this package's trace level and whose category is activated will
45 : parrello 1.2 be written. Thus, a higher trace level on a message indicates that the message
46 : parrello 1.10 is less likely to be seen. A higher trace level passed to B<TSetup> means more trace messages will
47 : olson 1.1 appear. To generate a trace message, use the following syntax.
48 :    
49 :     C<< Trace($message) if T(errors => 4); >>
50 :    
51 : parrello 1.2 This statement will produce a trace message if the trace level is 4 or more and the C<errors>
52 : parrello 1.3 category is active. Note that the special category C<main> is always active, so
53 : olson 1.1
54 : parrello 1.3 C<< Trace($message) if T(main => 4); >>
55 : olson 1.1
56 :     will trace if the trace level is 4 or more.
57 :    
58 :     If the category name is the same as the package name, all you need is the number. So, if the
59 :     following call is made in the B<Sprout> package, it will appear if the C<Sprout> category is
60 :     active and the trace level is 2 or more.
61 :    
62 :     C<< Trace($message) if T(2); >>
63 :    
64 : parrello 1.10 To set up tracing, you call the L</TSetup> method. The method takes as input a trace level, a list
65 : olson 1.1 of category names, and a set of options. The trace level and list of category names are
66 :     specified as a space-delimited string. Thus
67 :    
68 :     C<< TSetup('3 errors Sprout ERDB', 'HTML'); >>
69 :    
70 : parrello 1.7 sets the trace level to 3, activates the C<errors>, C<Sprout>, and C<ERDB> categories, and
71 : parrello 1.12 specifies that messages should be output as HTML paragraphs.
72 :    
73 :     To turn on tracing for ALL categories, use an asterisk. The call below sets every category to
74 :     level 3 and writes the output to the standard error output. This sort of thing might be
75 :     useful in a CGI environment.
76 :    
77 :     C<< TSetup('3 *', 'WARN'); >>
78 : olson 1.1
79 :     In addition to HTML and file output for trace messages, you can specify that the trace messages
80 :     be queued. The messages can then be retrieved by calling the L</QTrace> method. This approach
81 :     is useful if you are building a web page. Instead of having the trace messages interspersed with
82 :     the page output, they can be gathered together and displayed at the end of the page. This makes
83 :     it easier to debug page formatting problems.
84 :    
85 : parrello 1.4 Finally, you can specify that all trace messages be emitted as warnings.
86 :    
87 : olson 1.1 The flexibility of tracing makes it superior to simple use of directives like C<die> and C<warn>.
88 :     Tracer calls can be left in the code with minimal overhead and then turned on only when needed.
89 :     Thus, debugging information is available and easily retrieved even when the application is
90 :     being used out in the field.
91 :    
92 : parrello 1.10 There is no hard and fast rule on how to use trace levels. The following is therefore only
93 :     a suggestion.
94 :    
95 :     =over 4
96 :    
97 : parrello 1.32 =item Error 0
98 : parrello 1.10
99 :     Message indicates an error that may lead to incorrect results or that has stopped the
100 :     application entirely.
101 :    
102 : parrello 1.32 =item Warning 1
103 : parrello 1.10
104 :     Message indicates something that is unexpected but that probably did not interfere
105 :     with program execution.
106 :    
107 : parrello 1.32 =item Notice 2
108 : parrello 1.10
109 :     Message indicates the beginning or end of a major task.
110 :    
111 : parrello 1.32 =item Information 3
112 : parrello 1.10
113 :     Message indicates a subtask. In the FIG system, a subtask generally relates to a single
114 :     genome. This would be a big loop that is not expected to execute more than 500 times or so.
115 :    
116 : parrello 1.32 =item Detail 4
117 : parrello 1.10
118 :     Message indicates a low-level loop iteration.
119 :    
120 :     =back
121 :    
122 : olson 1.1 =cut
123 : parrello 1.2
124 : olson 1.1 # Declare the configuration variables.
125 :    
126 : parrello 1.12 my $Destination = "NONE"; # Description of where to send the trace output.
127 :     my $TeeFlag = 0; # TRUE if output is going to a file and to the
128 :     # standard output
129 : parrello 1.3 my %Categories = ( main => 1 );
130 : parrello 1.12 # hash of active category names
131 :     my $TraceLevel = 0; # trace level; a higher trace level produces more
132 :     # messages
133 :     my @Queue = (); # queued list of trace messages.
134 : parrello 1.7 my $LastCategory = "main"; # name of the last category interrogated
135 : parrello 1.11 my $SetupCount = 0; # number of times TSetup called
136 : parrello 1.12 my $AllTrace = 0; # TRUE if we are tracing all categories.
137 : olson 1.1
138 :     =head2 Public Methods
139 :    
140 :     =head3 TSetup
141 :    
142 :     C<< TSetup($categoryList, $target); >>
143 :    
144 :     This method is used to specify the trace options. The options are stored as package data
145 :     and interrogated by the L</Trace> and L</T> methods.
146 :    
147 :     =over 4
148 :    
149 :     =item categoryList
150 :    
151 :     A string specifying the trace level and the categories to be traced, separated by spaces.
152 :     The trace level must come first.
153 :    
154 :     =item target
155 :    
156 :     The destination for the trace output. To send the trace output to a file, specify the file
157 :     name preceded by a ">" symbol. If a double symbol is used (">>"), then the data is appended
158 : parrello 1.10 to the file. Otherwise the file is cleared before tracing begins. Precede the first ">"
159 :     symbol with a C<+> to echo output to a file AND to the standard output. In addition to
160 :     sending the trace messages to a file, you can specify a special destination. C<HTML> will
161 :     cause tracing to the standard output with each line formatted as an HTML paragraph. C<TEXT>
162 : parrello 1.5 will cause tracing to the standard output as ordinary text. C<ERROR> will cause trace
163 : parrello 1.9 messages to be sent to the standard error output as ordinary text. C<QUEUE> will cause trace
164 : parrello 1.6 messages to be stored in a queue for later retrieval by the L</QTrace> method. C<WARN> will
165 : parrello 1.9 cause trace messages to be emitted as warnings using the B<warn> directive. C<NONE> will
166 : parrello 1.6 cause tracing to be suppressed.
167 : olson 1.1
168 :     =back
169 :    
170 :     =cut
171 :    
172 :     sub TSetup {
173 : parrello 1.12 # Get the parameters.
174 :     my ($categoryList, $target) = @_;
175 :     # Parse the category list.
176 :     my @categoryData = split /\s+/, $categoryList;
177 :     # Extract the trace level.
178 :     $TraceLevel = shift @categoryData;
179 :     # Presume category-based tracing until we learn otherwise.
180 :     $AllTrace = 0;
181 :     # Build the category hash. Note that if we find a "*", we turn on non-category
182 : parrello 1.33 # tracing. We must also clear away any pre-existing data.
183 : parrello 1.34 %Categories = ( main => 1 );
184 : parrello 1.12 for my $category (@categoryData) {
185 :     if ($category eq '*') {
186 :     $AllTrace = 1;
187 :     } else {
188 : parrello 1.13 $Categories{lc $category} = 1;
189 : parrello 1.12 }
190 :     }
191 :     # Now we need to process the destination information. The most important special
192 :     # cases are the single ">", which requires we clear the file first, and the
193 :     # "+" prefix which indicates a double echo.
194 :     if ($target =~ m/^\+?>>?/) {
195 :     if ($target =~ m/^\+/) {
196 :     $TeeFlag = 1;
197 :     $target = substr($target, 1);
198 :     }
199 :     if ($target =~ m/^>[^>]/) {
200 :     open TRACEFILE, $target;
201 :     print TRACEFILE Now() . " Tracing initialized.\n";
202 :     close TRACEFILE;
203 :     $Destination = ">$target";
204 :     } else {
205 :     $Destination = $target;
206 :     }
207 :     } else {
208 :     $Destination = uc($target);
209 :     }
210 :     # Increment the setup counter.
211 :     $SetupCount++;
212 : parrello 1.11 }
213 :    
214 : parrello 1.31 =head3 StandardSetup
215 :    
216 : parrello 1.36 C<< my ($options, @parameters) = StandardSetup(\@categories, \%options, $parmHelp, @ARGV); >>
217 : parrello 1.31
218 :     This method performs standard command-line parsing and tracing setup. The return
219 :     values are a hash of the command-line options and a list of the positional
220 :     parameters. Tracing is automatically set up and the command-line options are
221 :     validated.
222 :    
223 :     This is a complex method that does a lot of grunt work. The parameters can
224 :     be more easily understood, however, once they are examined individually.
225 :    
226 :     The I<categories> parameter is the most obtuse. It is a reference to a list of
227 :     special-purpose tracing categories. Most tracing categories are PERL package
228 :     names. So, for example, if you wanted to turn on tracing inside the B<Sprout>,
229 :     B<ERDB>, and B<SproutLoad> packages, you would specify the categories
230 :    
231 :     ["Sprout", "SproutLoad", "ERDB"]
232 :    
233 :     This would cause trace messages in the specified three packages to appear in
234 :     the output. There are threer special tracing categories that are automatically
235 :     handled by this method. In other words, if you used L</TSetup> you would need
236 :     to include these categories manually, but if you use this method they are turned
237 :     on automatically.
238 :    
239 :     =over 4
240 :    
241 :     =item FIG
242 :    
243 :     Turns on trace messages inside the B<FIG> package.
244 :    
245 :     =item SQL
246 :    
247 :     Traces SQL commands and activity.
248 :    
249 :     =item Tracer
250 :    
251 :     Traces error messages and call stacks.
252 :    
253 :     =back
254 :    
255 :     C<SQL> is only turned on if the C<-sql> option is specified in the command line.
256 :     The trace level is specified using the C<-trace> command-line option. For example,
257 :     the following command line for C<TransactFeatures> turns on SQL tracing and runs
258 :     all tracing at level 3.
259 :    
260 :     TransactFeatures -trace=3 -sql register ../xacts IDs.tbl
261 :    
262 :     Standard tracing is output to the standard output and echoed to the file
263 : parrello 1.38 C<trace>I<$$>C<.log> in the FIG temporary directory, where I<$$> is the
264 :     process ID. You can also specify the C<user> parameter to put a user ID
265 :     instead of a process ID in the trace file name. So, for example
266 : parrello 1.31
267 : parrello 1.35 The default trace level is 2. To get all messages, specify a trace level of 4.
268 :     For a genome-by-genome update, use 3.
269 : parrello 1.31
270 : parrello 1.38 TransactFeatures -trace=3 -sql -user=Bruce register ../xacts IDs.tbl
271 :    
272 :     would send the trace output to C<traceBruce.log> in the temporary directory.
273 :    
274 : parrello 1.31 The I<options> parameter is a reference to a hash containing the command-line
275 : parrello 1.36 options, their default values, and an explanation of what they mean. Command-line
276 :     options may be in the form of switches or keywords. In the case of a switch, the
277 :     option value is 1 if it is specified and 0 if it is not specified. In the case
278 :     of a keyword, the value is separated from the option name by an equal sign. You
279 :     can see this last in the command-line example above.
280 : parrello 1.31
281 : parrello 1.42 You can specify a different default trace level by setting C<$options->{trace}>
282 :     prior to calling this method.
283 :    
284 : parrello 1.31 An example at this point would help. Consider, for example, the command-line utility
285 :     C<TransactFeatures>. It accepts a list of positional parameters plus the options
286 :     C<safe>, C<noAlias>, C<start>, and C<tblFiles>. To start up this command, we execute
287 :     the following code.
288 :    
289 :     my ($options, @parameters) = Tracer::StandardSetup(["DocUtils"],
290 : parrello 1.36 { safe => [0, "use database transactions"],
291 :     noAlias => [0, "do not expect aliases in CHANGE transactions"],
292 :     start => [' ', "start with this genome"],
293 :     tblFiles => [0, "output TBL files containing the corrected IDs"] },
294 :     "command transactionDirectory IDfile",
295 :     @ARGV);
296 : parrello 1.31
297 :    
298 :     The call to C<ParseCommand> specifies the default values for the options and
299 :     stores the actual options in a hash that is returned as C<$options>. The
300 : parrello 1.61 positional parameters are returned in C<@parameters>.
301 : parrello 1.31
302 :     The following is a sample command line for C<TransactFeatures>.
303 :    
304 :     TransactFeatures -trace=2 -noAlias register ../xacts IDs.tbl
305 :    
306 :     In this case, C<register>, C<../xacts>, and C<IDs.tbl> are the positional
307 :     parameters, and would find themselves in I<@parameters> after executing the
308 :     above code fragment. The tracing would be set to level 2, and the categories
309 :     would be C<FIG>, C<Tracer>, and <DocUtils>. C<FIG> and C<Tracer> are standard,
310 :     and C<DocUtils> was included because it came in within the first parameter
311 :     to this method. The I<$options> hash would be
312 :    
313 :     { trace => 2, sql => 0, safe => 0,
314 :     noAlias => 1, start => ' ', tblFiles => 0 }
315 :    
316 :     Use of C<StandardSetup> in this way provides a simple way of performing
317 :     standard tracing setup and command-line parsing. Note that the caller is
318 :     not even aware of the command-line switches C<-trace> and C<-sql>, which
319 :     are used by this method to control the tracing. If additional tracing features
320 :     need to be added in the future, they can be processed by this method without
321 :     upsetting the command-line utilities.
322 :    
323 : parrello 1.42 If the C<background> option is specified on the command line, then the
324 :     standard and error outputs will be directed to files in the temporary
325 :     directory, using the same suffix as the trace file. So, if the command
326 :     line specified
327 :    
328 :     -user=Bruce -background
329 :    
330 :     then the trace output would go to C<traceBruce.log>, the standard output to
331 :     C<outBruce.log>, and the error output to C<errBruce.log>. This is designed to
332 :     simplify starting a command in the background.
333 :    
334 : parrello 1.36 Finally, if the special option C<-h> is specified, the option names will
335 :     be traced at level 0 and the program will exit without processing.
336 :     This provides a limited help capability. For example, if the user enters
337 :    
338 :     TransactFeatures -h
339 :    
340 :     he would see the following output.
341 :    
342 :     TransactFeatures [options] command transactionDirectory IDfile
343 :     -trace tracing level (default 2)
344 :     -sql trace SQL commands
345 :     -safe use database transactions
346 :     -noAlias do not expect aliases in CHANGE transactions
347 :     -start start with this genome
348 :     -tblFiles output TBL files containing the corrected IDs
349 :    
350 : parrello 1.44 The caller has the option of modifying the tracing scheme by placing a value
351 :     for C<trace> in the incoming options hash. The default value can be overridden,
352 :     or the tracing to the standard output can be turned off by suffixing a minus
353 : parrello 1.61 sign to the trace level. So, for example,
354 : parrello 1.44
355 :     { trace => [0, "tracing level (default 0)"],
356 :     ...
357 :    
358 :     would set the default trace level to 0 instead of 2, while
359 :    
360 :     { trace => ["2-", "tracing level (default 2)"],
361 :     ...
362 :    
363 :     would leave the default at 2, but trace only to the log file, not to the
364 :     standard output.
365 :    
366 : parrello 1.31 The parameters to this method are as follows.
367 :    
368 :     =over 4
369 :    
370 :     =item categories
371 :    
372 :     Reference to a list of tracing category names. These should be names of
373 :     packages whose internal workings will need to be debugged to get the
374 :     command working.
375 :    
376 :     =item options
377 :    
378 :     Reference to a hash containing the legal options for the current command mapped
379 : parrello 1.36 to their default values and descriptions. The user can override the defaults
380 :     by specifying the options as command-line switches prefixed by a hyphen.
381 :     Tracing-related options may be added to this hash. If the C<-h> option is
382 :     specified on the command line, the option descriptions will be used to
383 : parrello 1.44 explain the options. To turn off tracing to the standard output, add a
384 :     minus sign to the value for C<trace> (see above).
385 : parrello 1.36
386 :     =item parmHelp
387 :    
388 :     A string that vaguely describes the positional parameters. This is used
389 :     if the user specifies the C<-h> option.
390 : parrello 1.31
391 : parrello 1.44 =item argv
392 : parrello 1.31
393 :     List of command line parameters, including the option switches, which must
394 :     precede the positional parameters and be prefixed by a hyphen.
395 :    
396 :     =item RETURN
397 :    
398 :     Returns a list. The first element of the list is the reference to a hash that
399 :     maps the command-line option switches to their values. These will either be the
400 :     default values or overrides specified on the command line. The remaining
401 :     elements of the list are the position parameters, in order.
402 :    
403 :     =back
404 :    
405 :     =cut
406 :    
407 :     sub StandardSetup {
408 :     # Get the parameters.
409 : parrello 1.36 my ($categories, $options, $parmHelp, @argv) = @_;
410 : parrello 1.31 # Add the tracing options.
411 : parrello 1.41 if (! exists $options->{trace}) {
412 :     $options->{trace} = [2, "tracing level"];
413 :     }
414 : parrello 1.36 $options->{sql} = [0, "turn on SQL tracing"];
415 :     $options->{h} = [0, "display command-line options"];
416 : parrello 1.38 $options->{user} = [$$, "trace log file name suffix"];
417 : parrello 1.42 $options->{background} = [0, "spool standard and error output"];
418 : parrello 1.36 # Create a parsing hash from the options hash. The parsing hash
419 :     # contains the default values rather than the default value
420 :     # and the description. While we're at it, we'll memorize the
421 :     # length of the longest option name.
422 :     my $longestName = 0;
423 :     my %parseOptions = ();
424 :     for my $key (keys %{$options}) {
425 :     if (length $key > $longestName) {
426 :     $longestName = length $key;
427 :     }
428 :     $parseOptions{$key} = $options->{$key}->[0];
429 :     }
430 : parrello 1.31 # Parse the command line.
431 : parrello 1.36 my ($retOptions, @retParameters) = ParseCommand(\%parseOptions, @argv);
432 : parrello 1.42 # Get the logfile suffix.
433 :     my $suffix = $retOptions->{user};
434 :     # Check for background mode.
435 :     if ($retOptions->{background}) {
436 :     my $outFileName = "$FIG_Config::temp/out$suffix.log";
437 :     my $errFileName = "$FIG_Config::temp/err$suffix.log";
438 :     open STDOUT, ">$outFileName";
439 :     open STDERR, ">$errFileName";
440 :     }
441 : parrello 1.31 # Now we want to set up tracing. First, we need to know if SQL is to
442 :     # be traced.
443 :     my @cats = @{$categories};
444 :     if ($retOptions->{sql}) {
445 :     push @cats, "SQL";
446 :     }
447 :     # Add the default categories.
448 :     push @cats, "Tracer", "FIG";
449 : parrello 1.44 # Next, we create the category string by joining the categories.
450 :     my $cats = join(" ", @cats);
451 :     # Check to determine whether or not the caller wants to turn off tracing
452 :     # to the standard output.
453 :     my $traceLevel = $retOptions->{trace};
454 :     my $textOKFlag = 1;
455 :     if ($traceLevel =~ /^(.)-/) {
456 :     $traceLevel = $1;
457 :     $textOKFlag = 0;
458 :     }
459 :     # Now we set up the trace mode.
460 :     my $traceMode;
461 :     # Verify that we can open a file in the FIG temporary directory.
462 : parrello 1.40 my $traceFileName = "$FIG_Config::temp/trace$suffix.log";
463 :     if (open TESTTRACE, ">$traceFileName") {
464 : parrello 1.44 # Here we can trace to a file.
465 :     $traceMode = ">$traceFileName";
466 :     if ($textOKFlag) {
467 :     # Echo to standard output if the text-OK flag is set.
468 :     $traceMode = "+$traceMode";
469 :     }
470 :     # Close the test file.
471 : parrello 1.40 close TESTTRACE;
472 : parrello 1.44 } else {
473 :     # Here we can't trace to a file. We trace to the standard output if it's
474 :     # okay, and the error log otherwise.
475 :     if ($textOKFlag) {
476 :     $traceMode = "TEXT";
477 :     } else {
478 :     $traceMode = "WARN";
479 :     }
480 : parrello 1.40 }
481 : parrello 1.31 # Now set up the tracing.
482 : parrello 1.44 TSetup("$traceLevel $cats", $traceMode);
483 : parrello 1.36 # Check for the "h" option. If it is specified, dump the command-line
484 :     # options and exit the program.
485 :     if ($retOptions->{h}) {
486 :     $0 =~ m#[/\\](\w+)(\.pl)?$#i;
487 :     Trace("$1 [options] $parmHelp") if T(0);
488 :     for my $key (sort keys %{$options}) {
489 :     my $name = Pad($key, $longestName, 0, ' ');
490 :     my $desc = $options->{$key}->[1];
491 :     if ($options->{$key}->[0]) {
492 :     $desc .= " (default " . $options->{$key}->[0] . ")";
493 :     }
494 :     Trace(" $name $desc") if T(0);
495 :     }
496 :     exit(0);
497 :     }
498 : parrello 1.31 # Return the parsed parameters.
499 :     return ($retOptions, @retParameters);
500 :     }
501 :    
502 : parrello 1.11 =head3 Setups
503 :    
504 :     C<< my $count = Tracer::Setups(); >>
505 :    
506 :     Return the number of times L</TSetup> has been called.
507 :    
508 :     This method allows for the creation of conditional tracing setups where, for example, we
509 :     may want to set up tracing if nobody else has done it before us.
510 :    
511 :     =cut
512 :    
513 :     sub Setups {
514 : parrello 1.12 return $SetupCount;
515 : olson 1.1 }
516 :    
517 : parrello 1.10 =head3 Open
518 :    
519 :     C<< my $handle = Open($fileHandle, $fileSpec, $message); >>
520 :    
521 : parrello 1.11 Open a file.
522 : parrello 1.10
523 :     The I<$fileSpec> is essentially the second argument of the PERL C<open>
524 :     function. The mode is specified using Unix-like shell information. So, for
525 :     example,
526 :    
527 : parrello 1.12 Open(\*LOGFILE, '>>/usr/spool/news/twitlog', "Could not open twit log.");
528 : parrello 1.10
529 :     would open for output appended to the specified file, and
530 :    
531 : parrello 1.12 Open(\*DATASTREAM, "| sort -u >$outputFile", "Could not open $outputFile.");
532 : parrello 1.10
533 :     would open a pipe that sorts the records written and removes duplicates. Note
534 : parrello 1.11 the use of file handle syntax in the Open call. To use anonymous file handles,
535 :     code as follows.
536 : parrello 1.10
537 : parrello 1.12 my $logFile = Open(undef, '>>/usr/spool/news/twitlog', "Could not open twit log.");
538 : parrello 1.10
539 : parrello 1.11 The I<$message> parameter is used if the open fails. If it is set to C<0>, then
540 :     the open returns TRUE if successful and FALSE if an error occurred. Otherwise, a
541 :     failed open will throw an exception and the third parameter will be used to construct
542 :     an error message. If the parameter is omitted, a standard message is constructed
543 :     using the file spec.
544 : parrello 1.10
545 : parrello 1.12 Could not open "/usr/spool/news/twitlog"
546 : parrello 1.10
547 :     Note that the mode characters are automatically cleaned from the file name.
548 :     The actual error message from the file system will be captured and appended to the
549 :     message in any case.
550 :    
551 : parrello 1.12 Could not open "/usr/spool/news/twitlog": file not found.
552 : parrello 1.10
553 :     In some versions of PERL the only error message we get is a number, which
554 :     corresponds to the C++ C<errno> value.
555 :    
556 : parrello 1.12 Could not open "/usr/spool/news/twitlog": 6.
557 : parrello 1.10
558 :     =over 4
559 :    
560 :     =item fileHandle
561 :    
562 :     File handle. If this parameter is C<undef>, a file handle will be generated
563 :     and returned as the value of this method.
564 :    
565 :     =item fileSpec
566 :    
567 :     File name and mode, as per the PERL C<open> function.
568 :    
569 :     =item message (optional)
570 :    
571 :     Error message to use if the open fails. If omitted, a standard error message
572 :     will be generated. In either case, the error information from the file system
573 : parrello 1.11 is appended to the message. To specify a conditional open that does not throw
574 :     an error if it fails, use C<0>.
575 : parrello 1.10
576 :     =item RETURN
577 :    
578 : parrello 1.11 Returns the name of the file handle assigned to the file, or C<undef> if the
579 :     open failed.
580 : parrello 1.10
581 :     =back
582 :    
583 :     =cut
584 :    
585 :     sub Open {
586 : parrello 1.12 # Get the parameters.
587 :     my ($fileHandle, $fileSpec, $message) = @_;
588 :     # Attempt to open the file.
589 :     my $rv = open $fileHandle, $fileSpec;
590 :     # If the open failed, generate an error message.
591 :     if (! $rv) {
592 :     # Save the system error message.
593 :     my $sysMessage = $!;
594 :     # See if we need a default message.
595 :     if (!$message) {
596 :     # Clean any obvious mode characters and leading spaces from the
597 :     # filename.
598 :     my ($fileName) = FindNamePart($fileSpec);
599 :     $message = "Could not open \"$fileName\"";
600 :     }
601 :     # Terminate with an error using the supplied message and the
602 :     # error message from the file system.
603 :     Confess("$message: $!");
604 :     }
605 :     # Return the file handle.
606 :     return $fileHandle;
607 : parrello 1.10 }
608 :    
609 : parrello 1.11 =head3 FindNamePart
610 :    
611 :     C<< my ($fileName, $start, $len) = Tracer::FindNamePart($fileSpec); >>
612 :    
613 :     Extract the portion of a file specification that contains the file name.
614 :    
615 :     A file specification is the string passed to an C<open> call. It specifies the file
616 :     mode and name. In a truly complex situation, it can specify a pipe sequence. This
617 :     method assumes that the file name is whatever follows the first angle bracket
618 :     sequence. So, for example, in the following strings the file name is
619 :     C</usr/fig/myfile.txt>.
620 :    
621 :     >>/usr/fig/myfile.txt
622 :     </usr/fig/myfile.txt
623 :     | sort -u > /usr/fig/myfile.txt
624 :    
625 :     If the method cannot find a file name using its normal methods, it will return the
626 :     whole incoming string.
627 :    
628 :     =over 4
629 :    
630 :     =item fileSpec
631 :    
632 :     File specification string from which the file name is to be extracted.
633 :    
634 :     =item RETURN
635 :    
636 :     Returns a three-element list. The first element contains the file name portion of
637 :     the specified string, or the whole string if a file name cannot be found via normal
638 :     methods. The second element contains the start position of the file name portion and
639 :     the third element contains the length.
640 :    
641 :     =back
642 :    
643 :     =cut
644 :     #: Return Type $;
645 :     sub FindNamePart {
646 :     # Get the parameters.
647 :     my ($fileSpec) = @_;
648 : parrello 1.12 # Default to the whole input string.
649 :     my ($retVal, $pos, $len) = ($fileSpec, 0, length $fileSpec);
650 : parrello 1.11 # Parse out the file name if we can.
651 : parrello 1.12 if ($fileSpec =~ m/(<|>>?)(.+?)(\s*)$/) {
652 :     $retVal = $2;
653 :     $len = length $retVal;
654 :     $pos = (length $fileSpec) - (length $3) - $len;
655 :     }
656 : parrello 1.11 # Return the result.
657 :     return ($retVal, $pos, $len);
658 :     }
659 :    
660 :     =head3 OpenDir
661 :    
662 : parrello 1.31 C<< my @files = OpenDir($dirName, $filtered, $flag); >>
663 : parrello 1.11
664 :     Open a directory and return all the file names. This function essentially performs
665 :     the functions of an C<opendir> and C<readdir>. If the I<$filtered> parameter is
666 : parrello 1.31 set to TRUE, all filenames beginning with a period (C<.>), dollar sign (C<$>),
667 : parrello 1.33 or pound sign (C<#>) and all filenames ending with a tilde C<~>) will be
668 :     filtered out of the return list. If the directory does not open and I<$flag> is not
669 :     set, an exception is thrown. So, for example,
670 : parrello 1.11
671 : parrello 1.12 my @files = OpenDir("/Volumes/fig/contigs", 1);
672 : parrello 1.29
673 : parrello 1.11 is effectively the same as
674 :    
675 : parrello 1.12 opendir(TMP, "/Volumes/fig/contigs") || Confess("Could not open /Volumes/fig/contigs.");
676 : parrello 1.33 my @files = grep { $_ !~ /^[\.\$\#]/ && $_ !~ /~$/ } readdir(TMP);
677 : parrello 1.11
678 :     Similarly, the following code
679 :    
680 : parrello 1.31 my @files = grep { $_ =~ /^\d/ } OpenDir("/Volumes/fig/orgs", 0, 1);
681 : parrello 1.29
682 : parrello 1.11 Returns the names of all files in C</Volumes/fig/orgs> that begin with digits and
683 : parrello 1.31 automatically returns an empty list if the directory fails to open.
684 : parrello 1.11
685 :     =over 4
686 :    
687 :     =item dirName
688 :    
689 :     Name of the directory to open.
690 :    
691 :     =item filtered
692 :    
693 :     TRUE if files whose names begin with a period (C<.>) should be automatically removed
694 :     from the list, else FALSE.
695 :    
696 : parrello 1.31 =item flag
697 :    
698 :     TRUE if a failure to open is okay, else FALSE
699 :    
700 : parrello 1.11 =back
701 :    
702 :     =cut
703 :     #: Return Type @;
704 :     sub OpenDir {
705 :     # Get the parameters.
706 : parrello 1.31 my ($dirName, $filtered, $flag) = @_;
707 : parrello 1.11 # Declare the return variable.
708 : parrello 1.31 my @retVal = ();
709 : parrello 1.12 # Open the directory.
710 :     if (opendir(my $dirHandle, $dirName)) {
711 :     # The directory opened successfully. Get the appropriate list according to the
712 :     # strictures of the filter parameter.
713 :     if ($filtered) {
714 : parrello 1.33 @retVal = grep { $_ !~ /^[\.\$\#]/ && $_ !~ /~$/ } readdir $dirHandle;
715 : parrello 1.12 } else {
716 :     @retVal = readdir $dirHandle;
717 :     }
718 : parrello 1.31 } elsif (! $flag) {
719 :     # Here the directory would not open and it's considered an error.
720 : parrello 1.12 Confess("Could not open directory $dirName.");
721 :     }
722 : parrello 1.11 # Return the result.
723 :     return @retVal;
724 :     }
725 :    
726 : parrello 1.6 =head3 SetLevel
727 :    
728 :     C<< Tracer::SetLevel($newLevel); >>
729 :    
730 :     Modify the trace level. A higher trace level will cause more messages to appear.
731 :    
732 :     =over 4
733 :    
734 :     =item newLevel
735 :    
736 :     Proposed new trace level.
737 :    
738 :     =back
739 :    
740 :     =cut
741 :    
742 :     sub SetLevel {
743 :     $TraceLevel = $_[0];
744 :     }
745 :    
746 : olson 1.1 =head3 Now
747 :    
748 :     C<< my $string = Tracer::Now(); >>
749 :    
750 :     Return a displayable time stamp containing the local time.
751 :    
752 :     =cut
753 :    
754 :     sub Now {
755 : parrello 1.12 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
756 :     my $retVal = _p2($mon+1) . "/" . _p2($mday) . "/" . ($year + 1900) . " " .
757 :     _p2($hour) . ":" . _p2($min) . ":" . _p2($sec);
758 :     return $retVal;
759 : olson 1.1 }
760 :    
761 :     # Pad a number to 2 digits.
762 :     sub _p2 {
763 : parrello 1.12 my ($value) = @_;
764 :     $value = "0$value" if ($value < 10);
765 :     return $value;
766 : olson 1.1 }
767 :    
768 :     =head3 LogErrors
769 :    
770 :     C<< Tracer::LogErrors($fileName); >>
771 :    
772 :     Route the standard error output to a log file.
773 :    
774 :     =over 4
775 :    
776 :     =item fileName
777 :    
778 :     Name of the file to receive the error output.
779 :    
780 :     =back
781 :    
782 :     =cut
783 :    
784 :     sub LogErrors {
785 : parrello 1.12 # Get the file name.
786 :     my ($fileName) = @_;
787 :     # Open the file as the standard error output.
788 :     open STDERR, '>', $fileName;
789 : olson 1.1 }
790 :    
791 : parrello 1.5 =head3 ReadOptions
792 :    
793 :     C<< my %options = Tracer::ReadOptions($fileName); >>
794 :    
795 :     Read a set of options from a file. Each option is encoded in a line of text that has the
796 :     format
797 :    
798 :     I<optionName>C<=>I<optionValue>C<; >I<comment>
799 :    
800 :     The option name must consist entirely of letters, digits, and the punctuation characters
801 : parrello 1.9 C<.> and C<_>, and is case sensitive. Blank lines and lines in which the first nonblank
802 :     character is a semi-colon will be ignored. The return hash will map each option name to
803 : parrello 1.5 the corresponding option value.
804 :    
805 :     =over 4
806 :    
807 :     =item fileName
808 :    
809 :     Name of the file containing the option data.
810 :    
811 :     =item RETURN
812 :    
813 :     Returns a hash mapping the option names specified in the file to their corresponding option
814 :     value.
815 :    
816 :     =back
817 :    
818 :     =cut
819 :    
820 :     sub ReadOptions {
821 : parrello 1.12 # Get the parameters.
822 :     my ($fileName) = @_;
823 :     # Open the file.
824 :     (open CONFIGFILE, "<$fileName") || Confess("Could not open option file $fileName.");
825 :     # Count the number of records read.
826 :     my ($records, $comments) = 0;
827 :     # Create the return hash.
828 :     my %retVal = ();
829 :     # Loop through the file, accumulating key-value pairs.
830 :     while (my $line = <CONFIGFILE>) {
831 :     # Denote we've read a line.
832 :     $records++;
833 :     # Determine the line type.
834 :     if ($line =~ /^\s*[\n\r]/) {
835 :     # A blank line is a comment.
836 :     $comments++;
837 :     } elsif ($line =~ /^\s*([A-Za-z0-9_\.]+)=([^;]*);/) {
838 :     # Here we have an option assignment.
839 :     retVal{$1} = $2;
840 :     } elsif ($line =~ /^\s*;/) {
841 :     # Here we have a text comment.
842 :     $comments++;
843 :     } else {
844 :     # Here we have an invalid line.
845 :     Trace("Invalid option statement in record $records.") if T(0);
846 :     }
847 :     }
848 :     # Return the hash created.
849 :     return %retVal;
850 : parrello 1.5 }
851 :    
852 : olson 1.1 =head3 GetOptions
853 :    
854 :     C<< Tracer::GetOptions(\%defaults, \%options); >>
855 :    
856 :     Merge a specified set of options into a table of defaults. This method takes two hash references
857 :     as input and uses the data from the second to update the first. If the second does not exist,
858 :     there will be no effect. An error will be thrown if one of the entries in the second hash does not
859 :     exist in the first.
860 :    
861 :     Consider the following example.
862 :    
863 :     C<< my $optionTable = GetOptions({ dbType => 'mySQL', trace => 0 }, $options); >>
864 :    
865 :     In this example, the variable B<$options> is expected to contain at most two options-- B<dbType> and
866 :     B<trace>. The default database type is C<mySQL> and the default trace level is C<0>. If the value of
867 :     B<$options> is C<< {dbType => 'Oracle'} >>, then the database type will be changed to C<Oracle> and
868 :     the trace level will remain at 0. If B<$options> is undefined, then the database type and trace level
869 :     will remain C<mySQL> and C<0>. If, on the other hand, B<$options> is defined as
870 :    
871 :     C<< {databaseType => 'Oracle'} >>
872 :    
873 :     an error will occur because the B<databaseType> option does not exist.
874 :    
875 :     =over 4
876 :    
877 :     =item defaults
878 :    
879 :     Table of default option values.
880 :    
881 :     =item options
882 :    
883 :     Table of overrides, if any.
884 :    
885 :     =item RETURN
886 :    
887 :     Returns a reference to the default table passed in as the first parameter.
888 :    
889 :     =back
890 :    
891 :     =cut
892 :    
893 :     sub GetOptions {
894 : parrello 1.12 # Get the parameters.
895 :     my ($defaults, $options) = @_;
896 :     # Check for overrides.
897 :     if ($options) {
898 :     # Loop through the overrides.
899 :     while (my ($option, $setting) = each %{$options}) {
900 :     # Insure this override exists.
901 :     if (!exists $defaults->{$option}) {
902 :     croak "Unrecognized option $option encountered.";
903 :     } else {
904 :     # Apply the override.
905 :     $defaults->{$option} = $setting;
906 :     }
907 :     }
908 :     }
909 :     # Return the merged table.
910 :     return $defaults;
911 : olson 1.1 }
912 :    
913 :     =head3 MergeOptions
914 :    
915 :     C<< Tracer::MergeOptions(\%table, \%defaults); >>
916 :    
917 :     Merge default values into a hash table. This method looks at the key-value pairs in the
918 :     second (default) hash, and if a matching key is not found in the first hash, the default
919 :     pair is copied in. The process is similar to L</GetOptions>, but there is no error-
920 :     checking and no return value.
921 :    
922 :     =over 4
923 :    
924 :     =item table
925 :    
926 :     Hash table to be updated with the default values.
927 :    
928 :     =item defaults
929 :    
930 :     Default values to be merged into the first hash table if they are not already present.
931 :    
932 :     =back
933 :    
934 :     =cut
935 :    
936 :     sub MergeOptions {
937 : parrello 1.12 # Get the parameters.
938 :     my ($table, $defaults) = @_;
939 :     # Loop through the defaults.
940 :     while (my ($key, $value) = each %{$defaults}) {
941 :     if (!exists $table->{$key}) {
942 :     $table->{$key} = $value;
943 :     }
944 :     }
945 : olson 1.1 }
946 :    
947 :     =head3 Trace
948 :    
949 :     C<< Trace($message); >>
950 :    
951 :     Write a trace message to the target location specified in L</TSetup>. If there has not been
952 :     any prior call to B<TSetup>.
953 :    
954 :     =over 4
955 :    
956 :     =item message
957 :    
958 :     Message to write.
959 :    
960 :     =back
961 :    
962 :     =cut
963 :    
964 :     sub Trace {
965 : parrello 1.12 # Get the parameters.
966 :     my ($message) = @_;
967 :     # Get the timestamp.
968 :     my $timeStamp = Now();
969 :     # Format the message. Note we strip off any line terminators at the end.
970 :     my $formatted = "$timeStamp <$LastCategory>: " . Strip($message);
971 :     # Process according to the destination.
972 :     if ($Destination eq "TEXT") {
973 :     # Write the message to the standard output.
974 :     print "$formatted\n";
975 :     } elsif ($Destination eq "ERROR") {
976 :     # Write the message to the error output.
977 :     print STDERR "$formatted\n";
978 :     } elsif ($Destination eq "QUEUE") {
979 :     # Push the message into the queue.
980 :     push @Queue, "$formatted";
981 :     } elsif ($Destination eq "HTML") {
982 :     # Convert the message to HTML and write it to the standard output.
983 :     my $escapedMessage = CGI::escapeHTML($message);
984 :     print "<p>$formatted</p>\n";
985 : parrello 1.4 } elsif ($Destination eq "WARN") {
986 :     # Emit the message as a warning.
987 :     warn $message;
988 : parrello 1.12 } elsif ($Destination =~ m/^>>/) {
989 :     # Write the trace message to an output file.
990 : parrello 1.14 (open TRACING, $Destination) || die "Tracing open for \"$Destination\" failed: $!";
991 : parrello 1.12 print TRACING "$formatted\n";
992 :     close TRACING;
993 :     # If the Tee flag is on, echo it to the standard output.
994 :     if ($TeeFlag) {
995 :     print "$formatted\n";
996 :     }
997 :     }
998 : olson 1.1 }
999 :    
1000 :     =head3 T
1001 :    
1002 : parrello 1.2 C<< my $switch = T($category, $traceLevel); >>
1003 : olson 1.1
1004 : parrello 1.12 or
1005 : parrello 1.2
1006 : olson 1.1 C<< my $switch = T($traceLevel); >>
1007 :    
1008 :     Return TRUE if the trace level is at or above a specified value and the specified category
1009 :     is active, else FALSE. If no category is specified, the caller's package name is used.
1010 :    
1011 :     =over 4
1012 :    
1013 :     =item category
1014 :    
1015 :     Category to which the message belongs. If not specified, the caller's package name is
1016 :     used.
1017 :    
1018 :     =item traceLevel
1019 :    
1020 :     Relevant tracing level.
1021 :    
1022 :     =item RETURN
1023 :    
1024 :     TRUE if a message at the specified trace level would appear in the trace, else FALSE.
1025 :    
1026 :     =back
1027 :    
1028 :     =cut
1029 :    
1030 :     sub T {
1031 : parrello 1.12 # Declare the return variable.
1032 :     my $retVal = 0;
1033 :     # Only proceed if tracing is turned on.
1034 :     if ($Destination ne "NONE") {
1035 :     # Get the parameters.
1036 :     my ($category, $traceLevel) = @_;
1037 :     if (!defined $traceLevel) {
1038 :     # Here we have no category, so we need to get the calling package.
1039 :     # The calling package is normally the first parameter. If it is
1040 :     # omitted, the first parameter will be the tracelevel. So, the
1041 :     # first thing we do is shift the so-called category into the
1042 :     # $traceLevel variable where it belongs.
1043 :     $traceLevel = $category;
1044 :     my ($package, $fileName, $line) = caller;
1045 : parrello 1.3 # If there is no calling package, we default to "main".
1046 : parrello 1.12 if (!$package) {
1047 : parrello 1.3 $category = "main";
1048 : parrello 1.12 } else {
1049 :     $category = $package;
1050 :     }
1051 :     }
1052 : parrello 1.7 # Save the category name.
1053 :     $LastCategory = $category;
1054 : parrello 1.13 # Convert it to lower case before we hash it.
1055 :     $category = lc $category;
1056 : parrello 1.12 # Use the category and tracelevel to compute the result.
1057 : parrello 1.36 if (ref $traceLevel) {
1058 :     Confess("Bad trace level.");
1059 :     } elsif (ref $TraceLevel) {
1060 :     Confess("Bad trace config.");
1061 :     }
1062 : parrello 1.12 $retVal = ($traceLevel <= $TraceLevel && ($AllTrace || exists $Categories{$category}));
1063 : parrello 1.3 }
1064 : parrello 1.12 # Return the computed result.
1065 : parrello 1.3 return $retVal;
1066 : olson 1.1 }
1067 :    
1068 :     =head3 ParseCommand
1069 :    
1070 :     C<< my ($options, @arguments) = Tracer::ParseCommand(\%optionTable, @inputList); >>
1071 :    
1072 :     Parse a command line consisting of a list of parameters. The initial parameters may be option
1073 : parrello 1.2 specifiers of the form C<->I<option> or C<->I<option>C<=>I<value>. The options are stripped
1074 :     off and merged into a table of default options. The remainder of the command line is
1075 : olson 1.1 returned as a list of positional arguments. For example, consider the following invocation.
1076 :    
1077 :     C<< my ($options, @arguments) = ParseCommand({ errors => 0, logFile => 'trace.log'}, @words); >>
1078 :    
1079 :     In this case, the list @words will be treated as a command line. There are two options available,
1080 :     B<errors> and B<logFile>. If @words has the following format
1081 :    
1082 :     C<< -logFile=error.log apple orange rutabaga >>
1083 :    
1084 :     then at the end of the invocation, C<$options> will be
1085 :    
1086 :     C<< { errors => 0, logFile => 'error.log' } >>
1087 :    
1088 :     and C<@arguments> will contain
1089 :    
1090 :     C<< apple orange rutabaga >>
1091 :    
1092 : parrello 1.2 The parser allows for some escape sequences. See L</UnEscape> for a description. There is no
1093 : olson 1.1 support for quote characters.
1094 :    
1095 :     =over 4
1096 :    
1097 :     =item optionTable
1098 :    
1099 :     Table of default options.
1100 :    
1101 :     =item inputList
1102 :    
1103 :     List of words on the command line.
1104 :    
1105 :     =item RETURN
1106 :    
1107 :     Returns a reference to the option table and a list of the positional arguments.
1108 :    
1109 :     =back
1110 :    
1111 :     =cut
1112 :    
1113 :     sub ParseCommand {
1114 : parrello 1.12 # Get the parameters.
1115 :     my ($optionTable, @inputList) = @_;
1116 :     # Process any options in the input list.
1117 :     my %overrides = ();
1118 :     while ((@inputList > 0) && ($inputList[0] =~ /^-/)) {
1119 :     # Get the current option.
1120 :     my $arg = shift @inputList;
1121 :     # Pull out the option name.
1122 :     $arg =~ /^-([^=]*)/g;
1123 :     my $name = $1;
1124 :     # Check for an option value.
1125 :     if ($arg =~ /\G=(.*)$/g) {
1126 :     # Here we have a value for the option.
1127 :     $overrides{$name} = UnEscape($1);
1128 :     } else {
1129 :     # Here there is no value, so we use 1.
1130 :     $overrides{$name} = 1;
1131 :     }
1132 :     }
1133 :     # Merge the options into the defaults.
1134 :     GetOptions($optionTable, \%overrides);
1135 :     # Translate the remaining parameters.
1136 :     my @retVal = ();
1137 :     for my $inputParm (@inputList) {
1138 :     push @retVal, UnEscape($inputParm);
1139 :     }
1140 :     # Return the results.
1141 :     return ($optionTable, @retVal);
1142 : olson 1.1 }
1143 :    
1144 : parrello 1.9 =head3 Escape
1145 :    
1146 :     C<< my $codedString = Tracer::Escape($realString); >>
1147 :    
1148 : parrello 1.25 Escape a string for use in a command length. Tabs will be replaced by C<\t>, new-lines
1149 : parrello 1.28 replaced by C<\n>, carriage returns will be deleted, and backslashes will be doubled. The
1150 :     result is to reverse the effect of L</UnEscape>.
1151 : parrello 1.9
1152 :     =over 4
1153 :    
1154 :     =item realString
1155 :    
1156 :     String to escape.
1157 :    
1158 :     =item RETURN
1159 :    
1160 :     Escaped equivalent of the real string.
1161 :    
1162 :     =back
1163 :    
1164 :     =cut
1165 :    
1166 :     sub Escape {
1167 : parrello 1.12 # Get the parameter.
1168 :     my ($realString) = @_;
1169 :     # Initialize the return variable.
1170 :     my $retVal = "";
1171 :     # Loop through the parameter string, looking for sequences to escape.
1172 :     while (length $realString > 0) {
1173 :     # Look for the first sequence to escape.
1174 : parrello 1.27 if ($realString =~ /^(.*?)([\n\t\r\\])/) {
1175 : parrello 1.12 # Here we found it. The text preceding the sequence is in $1. The sequence
1176 :     # itself is in $2. First, move the clear text to the return variable.
1177 :     $retVal .= $1;
1178 : parrello 1.14 # Strip the processed section off the real string.
1179 :     $realString = substr $realString, (length $2) + (length $1);
1180 : parrello 1.28 # Get the matched character.
1181 : parrello 1.12 my $char = $2;
1182 : parrello 1.28 # If we have a CR, we are done.
1183 :     if ($char ne "\r") {
1184 :     # It's not a CR, so encode the escape sequence.
1185 :     $char =~ tr/\t\n/tn/;
1186 :     $retVal .= "\\" . $char;
1187 :     }
1188 : parrello 1.12 } else {
1189 :     # Here there are no more escape sequences. The rest of the string is
1190 :     # transferred unmodified.
1191 :     $retVal .= $realString;
1192 :     $realString = "";
1193 :     }
1194 :     }
1195 :     # Return the result.
1196 :     return $retVal;
1197 : parrello 1.9 }
1198 :    
1199 : olson 1.1 =head3 UnEscape
1200 :    
1201 :     C<< my $realString = Tracer::UnEscape($codedString); >>
1202 :    
1203 : parrello 1.25 Replace escape sequences with their actual equivalents. C<\t> will be replaced by
1204 : parrello 1.28 a tab, C<\n> by a new-line character, and C<\\> by a backslash. C<\r> codes will
1205 :     be deleted.
1206 : olson 1.1
1207 :     =over 4
1208 :    
1209 :     =item codedString
1210 :    
1211 :     String to un-escape.
1212 :    
1213 :     =item RETURN
1214 :    
1215 :     Returns a copy of the original string with the escape sequences converted to their actual
1216 :     values.
1217 :    
1218 :     =back
1219 :    
1220 :     =cut
1221 :    
1222 :     sub UnEscape {
1223 : parrello 1.12 # Get the parameter.
1224 :     my ($codedString) = @_;
1225 :     # Initialize the return variable.
1226 :     my $retVal = "";
1227 :     # Only proceed if the incoming string is nonempty.
1228 :     if (defined $codedString) {
1229 :     # Loop through the parameter string, looking for escape sequences. We can't do
1230 : parrello 1.25 # translating because it causes problems with the escaped slash. ("\\t" becomes
1231 :     # "\<tab>" no matter what we do.)
1232 : parrello 1.12 while (length $codedString > 0) {
1233 :     # Look for the first escape sequence.
1234 : parrello 1.27 if ($codedString =~ /^(.*?)\\(\\|n|t|r)/) {
1235 : parrello 1.12 # Here we found it. The text preceding the sequence is in $1. The sequence
1236 :     # itself is in $2. First, move the clear text to the return variable.
1237 :     $retVal .= $1;
1238 :     $codedString = substr $codedString, (2 + length $1);
1239 : parrello 1.28 # Get the escape value.
1240 : parrello 1.12 my $char = $2;
1241 : parrello 1.28 # If we have a "\r", we are done.
1242 :     if ($char ne 'r') {
1243 :     # Here it's not an 'r', so we convert it.
1244 :     $char =~ tr/\\tn/\\\t\n/;
1245 :     $retVal .= $char;
1246 :     }
1247 : parrello 1.12 } else {
1248 :     # Here there are no more escape sequences. The rest of the string is
1249 :     # transferred unmodified.
1250 :     $retVal .= $codedString;
1251 :     $codedString = "";
1252 :     }
1253 :     }
1254 :     }
1255 :     # Return the result.
1256 :     return $retVal;
1257 : olson 1.1 }
1258 :    
1259 :     =head3 ParseRecord
1260 :    
1261 :     C<< my @fields = Tracer::ParseRecord($line); >>
1262 :    
1263 :     Parse a tab-delimited data line. The data line is split into field values. Embedded tab
1264 :     and new-line characters in the data line must be represented as C<\t> and C<\n>, respectively.
1265 :     These will automatically be converted.
1266 :    
1267 :     =over 4
1268 :    
1269 :     =item line
1270 :    
1271 :     Line of data containing the tab-delimited fields.
1272 :    
1273 :     =item RETURN
1274 :    
1275 :     Returns a list of the fields found in the data line.
1276 :    
1277 :     =back
1278 :    
1279 :     =cut
1280 :    
1281 :     sub ParseRecord {
1282 : parrello 1.12 # Get the parameter.
1283 :     my ($line) = @_;
1284 :     # Remove the trailing new-line, if any.
1285 :     chomp $line;
1286 :     # Split the line read into pieces using the tab character.
1287 :     my @retVal = split /\t/, $line;
1288 :     # Trim and fix the escapes in each piece.
1289 :     for my $value (@retVal) {
1290 :     # Trim leading whitespace.
1291 :     $value =~ s/^\s+//;
1292 :     # Trim trailing whitespace.
1293 :     $value =~ s/\s+$//;
1294 :     # Delete the carriage returns.
1295 :     $value =~ s/\r//g;
1296 :     # Convert the escapes into their real values.
1297 :     $value =~ s/\\t/"\t"/ge;
1298 :     $value =~ s/\\n/"\n"/ge;
1299 :     }
1300 :     # Return the result.
1301 :     return @retVal;
1302 : olson 1.1 }
1303 :    
1304 :     =head3 Merge
1305 :    
1306 :     C<< my @mergedList = Tracer::Merge(@inputList); >>
1307 :    
1308 :     Sort a list of strings and remove duplicates.
1309 :    
1310 :     =over 4
1311 :    
1312 :     =item inputList
1313 :    
1314 :     List of scalars to sort and merge.
1315 :    
1316 :     =item RETURN
1317 :    
1318 :     Returns a list containing the same elements sorted in ascending order with duplicates
1319 :     removed.
1320 :    
1321 :     =back
1322 :    
1323 :     =cut
1324 :    
1325 :     sub Merge {
1326 : parrello 1.12 # Get the input list in sort order.
1327 :     my @inputList = sort @_;
1328 :     # Only proceed if the list has at least two elements.
1329 :     if (@inputList > 1) {
1330 :     # Now we want to move through the list splicing out duplicates.
1331 :     my $i = 0;
1332 :     while ($i < @inputList) {
1333 :     # Get the current entry.
1334 :     my $thisEntry = $inputList[$i];
1335 :     # Find out how many elements duplicate the current entry.
1336 :     my $j = $i + 1;
1337 :     my $dup1 = $i + 1;
1338 :     while ($j < @inputList && $inputList[$j] eq $thisEntry) { $j++; };
1339 :     # If the number is nonzero, splice out the duplicates found.
1340 :     if ($j > $dup1) {
1341 :     splice @inputList, $dup1, $j - $dup1;
1342 :     }
1343 :     # Now the element at position $dup1 is different from the element before it
1344 :     # at position $i. We push $i forward one position and start again.
1345 :     $i++;
1346 :     }
1347 :     }
1348 :     # Return the merged list.
1349 :     return @inputList;
1350 : olson 1.1 }
1351 :    
1352 : parrello 1.54 =head3 Percent
1353 :    
1354 :     C<< my $percent = Tracer::Percent($number, $base); >>
1355 :    
1356 :     Returns the percent of the base represented by the given number. If the base
1357 :     is zero, returns zero.
1358 :    
1359 :     =over 4
1360 :    
1361 :     =item number
1362 :    
1363 :     Percent numerator.
1364 :    
1365 :     =item base
1366 :    
1367 :     Percent base.
1368 :    
1369 :     =item RETURN
1370 :    
1371 :     Returns the percentage of the base represented by the numerator.
1372 :    
1373 :     =back
1374 :    
1375 :     =cut
1376 :    
1377 :     sub Percent {
1378 :     # Get the parameters.
1379 :     my ($number, $base) = @_;
1380 :     # Declare the return variable.
1381 :     my $retVal = 0;
1382 :     # Compute the percent.
1383 :     if ($base != 0) {
1384 :     $retVal = $number * 100 / $base;
1385 :     }
1386 :     # Return the result.
1387 :     return $retVal;
1388 :     }
1389 :    
1390 : olson 1.1 =head3 GetFile
1391 :    
1392 : parrello 1.6 C<< my @fileContents = Tracer::GetFile($fileName); >>
1393 : olson 1.1
1394 : parrello 1.35 or
1395 :    
1396 :     C<< my $fileContents = Tracer::GetFile($fileName); >>
1397 :    
1398 :     Return the entire contents of a file. In list context, line-ends are removed and
1399 :     each line is a list element. In scalar context, line-ends are replaced by C<\n>.
1400 : olson 1.1
1401 :     =over 4
1402 :    
1403 :     =item fileName
1404 :    
1405 :     Name of the file to read.
1406 :    
1407 :     =item RETURN
1408 :    
1409 : parrello 1.6 In a list context, returns the entire file as a list with the line terminators removed.
1410 : parrello 1.39 In a scalar context, returns the entire file as a string. If an error occurs opening
1411 :     the file, an empty list will be returned.
1412 : olson 1.1
1413 :     =back
1414 :    
1415 :     =cut
1416 :    
1417 :     sub GetFile {
1418 : parrello 1.12 # Get the parameters.
1419 :     my ($fileName) = @_;
1420 :     # Declare the return variable.
1421 :     my @retVal = ();
1422 :     # Open the file for input.
1423 : parrello 1.60 my $handle = Open(undef, "<$fileName");
1424 :     # Read the whole file into the return variable, stripping off any terminator
1425 :     # characters.
1426 :     my $lineCount = 0;
1427 :     while (my $line = <$handle>) {
1428 :     $lineCount++;
1429 :     $line = Strip($line);
1430 :     push @retVal, $line;
1431 :     }
1432 :     # Close it.
1433 :     close $handle;
1434 :     my $actualLines = @retVal;
1435 : parrello 1.12 # Return the file's contents in the desired format.
1436 : parrello 1.9 if (wantarray) {
1437 : parrello 1.12 return @retVal;
1438 : parrello 1.6 } else {
1439 :     return join "\n", @retVal;
1440 :     }
1441 : olson 1.1 }
1442 :    
1443 : parrello 1.60 =head3 PutFile
1444 :    
1445 :     C<< Tracer::PutFile($fileName, \@lines); >>
1446 :    
1447 :     Write out a file from a list of lines of text.
1448 :    
1449 :     =over 4
1450 :    
1451 :     =item fileName
1452 :    
1453 :     Name of the output file.
1454 :    
1455 :     =item lines
1456 :    
1457 :     Reference to a list of text lines. The lines will be written to the file in order, with trailing
1458 :     new-line characters.
1459 :    
1460 :     =back
1461 :    
1462 :     =cut
1463 :    
1464 :     sub PutFile {
1465 :     # Get the parameters.
1466 :     my ($fileName, $lines) = @_;
1467 :     # Open the output file.
1468 :     my $handle = Open(undef, ">$fileName");
1469 :     # Write the lines.
1470 :     for my $line (@{$lines}) {
1471 :     print $handle "$line\n";
1472 :     }
1473 :     # Close the output file.
1474 :     close $handle;
1475 :     }
1476 :    
1477 : olson 1.1 =head3 QTrace
1478 :    
1479 :     C<< my $data = QTrace($format); >>
1480 :    
1481 :     Return the queued trace data in the specified format.
1482 :    
1483 :     =over 4
1484 :    
1485 :     =item format
1486 :    
1487 :     C<html> to format the data as an HTML list, C<text> to format it as straight text.
1488 :    
1489 :     =back
1490 :    
1491 :     =cut
1492 :    
1493 :     sub QTrace {
1494 : parrello 1.12 # Get the parameter.
1495 :     my ($format) = @_;
1496 :     # Create the return variable.
1497 :     my $retVal = "";
1498 : parrello 1.14 # Only proceed if there is an actual queue.
1499 :     if (@Queue) {
1500 :     # Process according to the format.
1501 :     if ($format =~ m/^HTML$/i) {
1502 :     # Convert the queue into an HTML list.
1503 :     $retVal = "<ul>\n";
1504 :     for my $line (@Queue) {
1505 :     my $escapedLine = CGI::escapeHTML($line);
1506 :     $retVal .= "<li>$escapedLine</li>\n";
1507 :     }
1508 :     $retVal .= "</ul>\n";
1509 :     } elsif ($format =~ m/^TEXT$/i) {
1510 :     # Convert the queue into a list of text lines.
1511 :     $retVal = join("\n", @Queue) . "\n";
1512 :     }
1513 :     # Clear the queue.
1514 :     @Queue = ();
1515 : parrello 1.12 }
1516 :     # Return the formatted list.
1517 :     return $retVal;
1518 : olson 1.1 }
1519 :    
1520 :     =head3 Confess
1521 :    
1522 :     C<< Confess($message); >>
1523 :    
1524 : parrello 1.22 Trace the call stack and abort the program with the specified message. When used with
1525 : parrello 1.9 the OR operator and the L</Assert> method, B<Confess> can function as a debugging assert.
1526 : parrello 1.6 So, for example
1527 : olson 1.1
1528 : parrello 1.6 C<< Assert($recNum >= 0) || Confess("Invalid record number $recNum."); >>
1529 : olson 1.1
1530 :     Will abort the program with a stack trace if the value of C<$recNum> is negative.
1531 :    
1532 :     =over 4
1533 :    
1534 :     =item message
1535 :    
1536 :     Message to include in the trace.
1537 :    
1538 :     =back
1539 :    
1540 :     =cut
1541 :    
1542 :     sub Confess {
1543 : parrello 1.12 # Get the parameters.
1544 :     my ($message) = @_;
1545 :     # Trace the call stack.
1546 : parrello 1.22 Cluck($message);
1547 : parrello 1.12 # Abort the program.
1548 :     croak(">>> $message");
1549 : olson 1.1 }
1550 :    
1551 : parrello 1.6 =head3 Assert
1552 :    
1553 :     C<< Assert($condition1, $condition2, ... $conditionN); >>
1554 :    
1555 :     Return TRUE if all the conditions are true. This method can be used in conjunction with
1556 : parrello 1.29 the OR operator and the L</Confess> method as a debugging assert.
1557 : parrello 1.6 So, for example
1558 :    
1559 :     C<< Assert($recNum >= 0) || Confess("Invalid record number $recNum."); >>
1560 :    
1561 :     Will abort the program with a stack trace if the value of C<$recNum> is negative.
1562 :    
1563 :     =cut
1564 :     sub Assert {
1565 :     my $retVal = 1;
1566 :     LOOP: for my $condition (@_) {
1567 :     if (! $condition) {
1568 :     $retVal = 0;
1569 :     last LOOP;
1570 :     }
1571 :     }
1572 :     return $retVal;
1573 :     }
1574 :    
1575 : olson 1.1 =head3 Cluck
1576 :    
1577 :     C<< Cluck($message); >>
1578 :    
1579 :     Trace the call stack. Note that for best results, you should qualify the call with a
1580 :     trace condition. For example,
1581 :    
1582 :     C<< Cluck("Starting record parse.") if T(3); >>
1583 :    
1584 :     will only trace the stack if the trace level for the package is 3 or more.
1585 :    
1586 :     =over 4
1587 :    
1588 :     =item message
1589 :    
1590 :     Message to include in the trace.
1591 :    
1592 :     =back
1593 :    
1594 :     =cut
1595 :    
1596 :     sub Cluck {
1597 : parrello 1.12 # Get the parameters.
1598 :     my ($message) = @_;
1599 : parrello 1.5 # Trace what's happening.
1600 :     Trace("Stack trace for event: $message");
1601 : parrello 1.12 my $confession = longmess($message);
1602 :     # Convert the confession to a series of trace messages. Note we skip any
1603 : parrello 1.5 # messages relating to calls into Tracer.
1604 : parrello 1.12 for my $line (split /\s*\n/, $confession) {
1605 :     Trace($line) if ($line !~ /Tracer\.pm/);
1606 :     }
1607 : olson 1.1 }
1608 :    
1609 : parrello 1.5 =head3 Min
1610 :    
1611 :     C<< my $min = Min($value1, $value2, ... $valueN); >>
1612 :    
1613 :     Return the minimum argument. The arguments are treated as numbers.
1614 :    
1615 :     =over 4
1616 :    
1617 :     =item $value1, $value2, ... $valueN
1618 :    
1619 :     List of numbers to compare.
1620 :    
1621 :     =item RETURN
1622 :    
1623 :     Returns the lowest number in the list.
1624 :    
1625 :     =back
1626 :    
1627 :     =cut
1628 :    
1629 :     sub Min {
1630 : parrello 1.12 # Get the parameters. Note that we prime the return value with the first parameter.
1631 :     my ($retVal, @values) = @_;
1632 :     # Loop through the remaining parameters, looking for the lowest.
1633 :     for my $value (@values) {
1634 :     if ($value < $retVal) {
1635 :     $retVal = $value;
1636 :     }
1637 :     }
1638 :     # Return the minimum found.
1639 :     return $retVal;
1640 : parrello 1.5 }
1641 :    
1642 :     =head3 Max
1643 :    
1644 :     C<< my $max = Max($value1, $value2, ... $valueN); >>
1645 :    
1646 :     Return the maximum argument. The arguments are treated as numbers.
1647 :    
1648 :     =over 4
1649 :    
1650 :     =item $value1, $value2, ... $valueN
1651 :    
1652 :     List of numbers to compare.
1653 :    
1654 :     =item RETURN
1655 :    
1656 :     Returns the highest number in the list.
1657 :    
1658 :     =back
1659 :    
1660 :     =cut
1661 :    
1662 :     sub Max {
1663 : parrello 1.12 # Get the parameters. Note that we prime the return value with the first parameter.
1664 :     my ($retVal, @values) = @_;
1665 :     # Loop through the remaining parameters, looking for the highest.
1666 :     for my $value (@values) {
1667 :     if ($value > $retVal) {
1668 :     $retVal = $value;
1669 :     }
1670 :     }
1671 :     # Return the maximum found.
1672 :     return $retVal;
1673 : parrello 1.5 }
1674 :    
1675 :     =head3 AddToListMap
1676 :    
1677 : parrello 1.53 C<< Tracer::AddToListMap(\%hash, $key, $value1, $value2, ... valueN); >>
1678 : parrello 1.5
1679 :     Add a key-value pair to a hash of lists. If no value exists for the key, a singleton list
1680 :     is created for the key. Otherwise, the new value is pushed onto the list.
1681 :    
1682 :     =over 4
1683 :    
1684 :     =item hash
1685 :    
1686 :     Reference to the target hash.
1687 :    
1688 :     =item key
1689 :    
1690 :     Key for which the value is to be added.
1691 :    
1692 : parrello 1.53 =item value1, value2, ... valueN
1693 : parrello 1.5
1694 : parrello 1.53 List of values to add to the key's value list.
1695 : parrello 1.5
1696 :     =back
1697 :    
1698 :     =cut
1699 :    
1700 :     sub AddToListMap {
1701 :     # Get the parameters.
1702 : parrello 1.53 my ($hash, $key, @values) = @_;
1703 : parrello 1.5 # Process according to whether or not the key already has a value.
1704 :     if (! exists $hash->{$key}) {
1705 : parrello 1.53 $hash->{$key} = [@values];
1706 : parrello 1.5 } else {
1707 : parrello 1.53 push @{$hash->{$key}}, @values;
1708 : parrello 1.5 }
1709 :     }
1710 : olson 1.1
1711 : parrello 1.7 =head3 DebugMode
1712 :    
1713 :     C<< if (Tracer::DebugMode) { ...code... } >>
1714 :    
1715 : parrello 1.22 Return TRUE if debug mode has been turned on, else output an error
1716 :     page and return FALSE.
1717 : parrello 1.7
1718 :     Certain CGI scripts are too dangerous to exist in the production
1719 :     environment. This method provides a simple way to prevent them
1720 : parrello 1.21 from working unless they are explicitly turned on by creating a password
1721 :     cookie via the B<SetPassword> script. If debugging mode
1722 : parrello 1.22 is not turned on, an error web page will be output directing the
1723 :     user to enter in the correct password.
1724 : parrello 1.7
1725 :     =cut
1726 :    
1727 :     sub DebugMode {
1728 : parrello 1.12 # Declare the return variable.
1729 : parrello 1.21 my $retVal = 0;
1730 : parrello 1.12 # Check the debug configuration.
1731 : parrello 1.21 my $password = CGI::cookie("DebugMode");
1732 :     my $encrypted = Digest::MD5::md5_hex($password);
1733 :     if ($encrypted eq "252dec43280e0c0d6a75ffcec486e61d") {
1734 : parrello 1.12 $retVal = 1;
1735 :     } else {
1736 :     # Here debug mode is off, so we generate an error page.
1737 : parrello 1.61 my $pageString = PageBuilder::Build("<<Html/ErrorPage.html", {}, "Html");
1738 : parrello 1.12 print $pageString;
1739 :     }
1740 :     # Return the determination indicator.
1741 : parrello 1.18 return $retVal;
1742 : parrello 1.9 }
1743 :    
1744 :     =head3 Strip
1745 :    
1746 :     C<< my $string = Tracer::Strip($line); >>
1747 :    
1748 :     Strip all line terminators off a string. This is necessary when dealing with files
1749 :     that may have been transferred back and forth several times among different
1750 :     operating environments.
1751 :    
1752 :     =over 4
1753 :    
1754 :     =item line
1755 :    
1756 :     Line of text to be stripped.
1757 :    
1758 :     =item RETURN
1759 :    
1760 :     The same line of text with all the line-ending characters chopped from the end.
1761 :    
1762 :     =back
1763 :    
1764 :     =cut
1765 :    
1766 :     sub Strip {
1767 : parrello 1.12 # Get a copy of the parameter string.
1768 :     my ($string) = @_;
1769 : parrello 1.29 my $retVal = (defined $string ? $string : "");
1770 : parrello 1.9 # Strip the line terminator characters.
1771 :     $retVal =~ s/(\r|\n)+$//g;
1772 : parrello 1.12 # Return the result.
1773 :     return $retVal;
1774 : parrello 1.9 }
1775 :    
1776 :     =head3 Pad
1777 :    
1778 :     C<< my $paddedString = Tracer::Pad($string, $len, $left, $padChar); >>
1779 :    
1780 :     Pad a string to a specified length. The pad character will be a
1781 :     space, and the padding will be on the right side unless specified
1782 :     in the third parameter.
1783 :    
1784 :     =over 4
1785 :    
1786 :     =item string
1787 :    
1788 :     String to be padded.
1789 :    
1790 :     =item len
1791 :    
1792 :     Desired length of the padded string.
1793 :    
1794 :     =item left (optional)
1795 :    
1796 :     TRUE if the string is to be left-padded; otherwise it will be padded on the right.
1797 :    
1798 :     =item padChar (optional)
1799 :    
1800 : parrello 1.22 Character to use for padding. The default is a space.
1801 :    
1802 : parrello 1.9 =item RETURN
1803 :    
1804 : parrello 1.22 Returns a copy of the original string with the pad character added to the
1805 :     specified end so that it achieves the desired length.
1806 : parrello 1.9
1807 :     =back
1808 :    
1809 :     =cut
1810 :    
1811 :     sub Pad {
1812 : parrello 1.12 # Get the parameters.
1813 :     my ($string, $len, $left, $padChar) = @_;
1814 :     # Compute the padding character.
1815 :     if (! defined $padChar) {
1816 :     $padChar = " ";
1817 :     }
1818 :     # Compute the number of spaces needed.
1819 :     my $needed = $len - length $string;
1820 :     # Copy the string into the return variable.
1821 :     my $retVal = $string;
1822 :     # Only proceed if padding is needed.
1823 :     if ($needed > 0) {
1824 :     # Create the pad string.
1825 :     my $pad = $padChar x $needed;
1826 :     # Affix it to the return value.
1827 :     if ($left) {
1828 :     $retVal = $pad . $retVal;
1829 :     } else {
1830 :     $retVal .= $pad;
1831 :     }
1832 :     }
1833 :     # Return the result.
1834 :     return $retVal;
1835 : parrello 1.7 }
1836 :    
1837 : parrello 1.29 =head3 EOF
1838 :    
1839 :     This is a constant that is lexically greater than any useful string.
1840 :    
1841 :     =cut
1842 :    
1843 :     sub EOF {
1844 :     return "\xFF\xFF\xFF\xFF\xFF";
1845 :     }
1846 :    
1847 : parrello 1.15 =head3 TICK
1848 :    
1849 :     C<< my @results = TICK($commandString); >>
1850 :    
1851 :     Perform a back-tick operation on a command. If this is a Windows environment, any leading
1852 :     dot-slash (C<./> will be removed. So, for example, if you were doing
1853 :    
1854 :     `./protein.cgi`
1855 :    
1856 :     from inside a CGI script, it would work fine in Unix, but would issue an error message
1857 :     in Windows complaining that C<'.'> is not a valid command. If instead you code
1858 :    
1859 :     TICK("./protein.cgi")
1860 :    
1861 :     it will work correctly in both environments.
1862 :    
1863 :     =over 4
1864 :    
1865 :     =item commandString
1866 :    
1867 :     The command string to pass to the system.
1868 :    
1869 :     =item RETURN
1870 :    
1871 :     Returns the standard output from the specified command, as a list.
1872 :    
1873 :     =back
1874 :    
1875 :     =cut
1876 :     #: Return Type @;
1877 :     sub TICK {
1878 :     # Get the parameters.
1879 :     my ($commandString) = @_;
1880 :     # Chop off the dot-slash if this is Windows.
1881 :     if ($FIG_Config::win_mode) {
1882 :     $commandString =~ s!^\./!!;
1883 :     }
1884 :     # Activate the command and return the result.
1885 :     return `$commandString`;
1886 :     }
1887 :    
1888 : parrello 1.35 =head3 ScriptSetup
1889 :    
1890 :     C<< my ($query, $varHash) = ScriptSetup(); >>
1891 :    
1892 :     Perform standard tracing and debugging setup for scripts. The value returned is
1893 :     the CGI object followed by a pre-built variable hash.
1894 :    
1895 :     The C<Trace> query parameter is used to determine whether or not tracing is active and
1896 :     which trace modules (other than C<Tracer> and C<FIG>) should be turned on. Specifying
1897 :     the C<CGI> trace module will trace parameter and environment information. Parameters are
1898 :     traced at level 3 and environment variables at level 4. At the end of the script, the
1899 :     client should call L</ScriptFinish> to output the web page.
1900 :    
1901 :     =cut
1902 :    
1903 :     sub ScriptSetup {
1904 :     # Get the CGI query object.
1905 :     my $query = CGI->new();
1906 :     # Check for tracing. Set it up if the user asked for it.
1907 :     if ($query->param('Trace')) {
1908 :     # Set up tracing to be queued for display at the bottom of the web page.
1909 :     TSetup($query->param('Trace') . " FIG Tracer", "QUEUE");
1910 :     # Trace the parameter and environment data.
1911 :     if (T(CGI => 3)) {
1912 :     # Here we want to trace the parameter data.
1913 :     my @names = $query->param;
1914 :     for my $parmName (sort @names) {
1915 :     # Note we skip "Trace", which is for our use only.
1916 :     if ($parmName ne 'Trace') {
1917 :     my @values = $query->param($parmName);
1918 :     Trace("CGI: $parmName = " . join(", ", @values));
1919 :     }
1920 :     }
1921 :     }
1922 :     if (T(CGI => 4)) {
1923 :     # Here we want the environment data too.
1924 :     for my $envName (sort keys %ENV) {
1925 :     Trace("ENV: $envName = $ENV{$envName}");
1926 :     }
1927 :     }
1928 :     } else {
1929 :     # Here tracing is to be turned off. All we allow is errors traced into the
1930 :     # error log.
1931 :     TSetup("0", "WARN");
1932 :     }
1933 :     # Create the variable hash.
1934 :     my $varHash = { DebugData => '' };
1935 :     # If we're in DEBUG mode, set up the debug mode data for forms.
1936 :     if (Tracer::DebugMode) {
1937 :     $varHash->{DebugData} = GetFile("Html/DebugFragment.html");
1938 :     }
1939 :     # Return the query object and variable hash.
1940 :     return ($query, $varHash);
1941 :     }
1942 :    
1943 :     =head3 ScriptFinish
1944 :    
1945 :     C<< ScriptFinish($webData, $varHash); >>
1946 :    
1947 :     Output a web page at the end of a script. Either the string to be output or the
1948 :     name of a template file can be specified. If the second parameter is omitted,
1949 :     it is assumed we have a string to be output; otherwise, it is assumed we have the
1950 :     name of a template file. The template should have the variable C<DebugData>
1951 :     specified in any form that invokes a standard script. If debugging mode is turned
1952 :     on, a form field will be put in that allows the user to enter tracing data.
1953 :     Trace messages will be placed immediately before the terminal C<BODY> tag in
1954 :     the output, formatted as a list.
1955 :    
1956 :     A typical standard script would loook like the following.
1957 :    
1958 :     BEGIN {
1959 :     # Print the HTML header.
1960 :     print "CONTENT-TYPE: text/html\n\n";
1961 :     }
1962 :     use Tracer;
1963 :     use CGI;
1964 :     use FIG;
1965 :     # ... more uses ...
1966 : parrello 1.61
1967 : parrello 1.35 my ($query, $varHash) = ScriptSetup();
1968 :     eval {
1969 :     # ... get data from $query, put it in $varHash ...
1970 :     };
1971 :     if ($@) {
1972 :     Trace("Script Error: $@") if T(0);
1973 :     }
1974 :     ScriptFinish("Html/MyTemplate.html", $varHash);
1975 :    
1976 :     The idea here is that even if the script fails, you'll see trace messages and
1977 :     useful output.
1978 :    
1979 :     =over 4
1980 :    
1981 :     =item webData
1982 :    
1983 :     A string containing either the full web page to be written to the output or the
1984 :     name of a template file from which the page is to be constructed. If the name
1985 :     of a template file is specified, then the second parameter must be present;
1986 :     otherwise, it must be absent.
1987 :    
1988 :     =item varHash (optional)
1989 :    
1990 :     If specified, then a reference to a hash mapping variable names for a template
1991 :     to their values. The template file will be read into memory, and variable markers
1992 :     will be replaced by data in this hash reference.
1993 :    
1994 : parrello 1.37 =back
1995 :    
1996 : parrello 1.35 =cut
1997 :    
1998 :     sub ScriptFinish {
1999 :     # Get the parameters.
2000 :     my ($webData, $varHash) = @_;
2001 :     # Check for a template file situation.
2002 :     my $outputString;
2003 :     if (defined $varHash) {
2004 :     # Here we have a template file. We need to apply the variables to the template.
2005 : parrello 1.61 $outputString = PageBuilder::Build("<<$webData", $varHash, "Html");
2006 : parrello 1.35 } else {
2007 :     # Here the user gave us a raw string.
2008 :     $outputString = $webData;
2009 :     }
2010 :     # Check for trace messages.
2011 :     if ($Destination eq "QUEUE") {
2012 :     # We have trace messages, so we want to put them at the end of the body. This
2013 :     # is either at the end of the whole string or at the beginning of the BODY
2014 :     # end-tag.
2015 :     my $pos = length $outputString;
2016 :     if ($outputString =~ m#</body>#gi) {
2017 :     $pos = (pos $outputString) - 7;
2018 :     }
2019 :     substr $outputString, $pos, 0, QTrace('Html');
2020 :     }
2021 :     # Write the output string.
2022 :     print $outputString;
2023 :     }
2024 :    
2025 : parrello 1.37 =head3 Insure
2026 :    
2027 :     C<< Insure($dirName); >>
2028 :    
2029 :     Insure a directory is present.
2030 :    
2031 :     =over 4
2032 :    
2033 :     =item dirName
2034 :    
2035 :     Name of the directory to check. If it does not exist, it will be created.
2036 :    
2037 :     =back
2038 :    
2039 :     =cut
2040 :    
2041 :     sub Insure {
2042 :     my ($dirName) = @_;
2043 :     if (! -d $dirName) {
2044 :     Trace("Creating $dirName directory.") if T(2);
2045 : parrello 1.43 eval { mkpath $dirName; };
2046 :     if ($@) {
2047 :     Confess("Error creating $dirName: $@");
2048 :     }
2049 :     }
2050 :     }
2051 :    
2052 :     =head3 ChDir
2053 :    
2054 :     C<< ChDir($dirName); >>
2055 :    
2056 :     Change to the specified directory.
2057 :    
2058 :     =over 4
2059 :    
2060 :     =item dirName
2061 :    
2062 :     Name of the directory to which we want to change.
2063 :    
2064 :     =back
2065 :    
2066 :     =cut
2067 :    
2068 :     sub ChDir {
2069 :     my ($dirName) = @_;
2070 :     if (! -d $dirName) {
2071 :     Confess("Cannot change to directory $dirName: no such directory.");
2072 :     } else {
2073 :     Trace("Changing to directory $dirName.") if T(4);
2074 :     my $okFlag = chdir $dirName;
2075 :     if (! $okFlag) {
2076 :     Confess("Error switching to directory $dirName.");
2077 :     }
2078 : parrello 1.37 }
2079 :     }
2080 :    
2081 : parrello 1.59 =head3 SendSMS
2082 :    
2083 :     C<< my $msgID = Tracer::SendSMS($phoneNumber, $msg); >>
2084 :    
2085 :     Send a text message to a phone number using Clickatell. The FIG_Config file must contain the
2086 :     user name, password, and API ID for the relevant account in the hash reference variable
2087 :     I<$FIG_Config::phone>, using the keys C<user>, C<password>, and C<api_id>. For
2088 :     example, if the user name is C<BruceTheHumanPet>, the password is C<silly>, and the API ID
2089 :     is C<2561022>, then the FIG_Config file must contain
2090 :    
2091 :     $phone = { user => 'BruceTheHumanPet',
2092 :     password => 'silly',
2093 :     api_id => '2561022' };
2094 :    
2095 :     The original purpose of this method was to insure Bruce would be notified immediately when the
2096 :     Sprout Load terminates. Care should be taken if you do not wish Bruce to be notified immediately
2097 :     when you call this method.
2098 :    
2099 :     The message ID will be returned if successful, and C<undef> if an error occurs.
2100 :    
2101 :     =over 4
2102 :    
2103 :     =item phoneNumber
2104 :    
2105 :     Phone number to receive the message, in international format. A United States phone number
2106 :     would be prefixed by "1". A British phone number would be prefixed by "44".
2107 :    
2108 :     =item msg
2109 :    
2110 :     Message to send to the specified phone.
2111 :    
2112 :     =item RETURN
2113 :    
2114 :     Returns the message ID if successful, and C<undef> if the message could not be sent.
2115 :    
2116 :     =back
2117 :    
2118 :     =cut
2119 :    
2120 :     sub SendSMS {
2121 :     # Get the parameters.
2122 :     my ($phoneNumber, $msg) = @_;
2123 :     # Declare the return variable. If we do not change it, C<undef> will be returned.
2124 :     my $retVal;
2125 :     # Only proceed if we have phone support.
2126 :     if (! defined $FIG_Config::phone) {
2127 :     Trace("Phone support not present in FIG_Config.") if T(1);
2128 :     } else {
2129 :     # Get the phone data.
2130 :     my $parms = $FIG_Config::phone;
2131 :     # Get the Clickatell URL.
2132 :     my $url = "http://api.clickatell.com/http/";
2133 :     # Create the user agent.
2134 :     my $ua = LWP::UserAgent->new;
2135 :     # Request a Clickatell session.
2136 :     my $resp = $ua->post("$url/sendmsg", { user => $parms->{user},
2137 :     password => $parms->{password},
2138 :     api_id => $parms->{api_id},
2139 :     to => $phoneNumber,
2140 :     text => $msg});
2141 :     # Check for an error.
2142 :     if (! $resp->is_success) {
2143 :     Trace("Alert failed.") if T(1);
2144 :     } else {
2145 :     # Get the message ID.
2146 :     my $rstring = $resp->content;
2147 :     if ($rstring =~ /^ID:\s+(.*)$/) {
2148 :     $retVal = $1;
2149 :     } else {
2150 :     Trace("Phone attempt failed with $rstring") if T(1);
2151 :     }
2152 :     }
2153 :     }
2154 :     # Return the result.
2155 :     return $retVal;
2156 :     }
2157 :    
2158 : parrello 1.55 =head3 CommaFormat
2159 :    
2160 :     C<< my $formatted = Tracer::CommaFormat($number); >>
2161 :    
2162 :     Insert commas into a number.
2163 :    
2164 :     =over 4
2165 :    
2166 :     =item number
2167 :    
2168 :     A sequence of digits.
2169 :    
2170 :     =item RETURN
2171 :    
2172 :     Returns the same digits with commas strategically inserted.
2173 :    
2174 :     =back
2175 :    
2176 :     =cut
2177 :    
2178 :     sub CommaFormat {
2179 :     # Get the parameters.
2180 :     my ($number) = @_;
2181 :     # Pad the length up to a multiple of three.
2182 :     my $padded = "$number";
2183 :     $padded = " " . $padded while length($padded) % 3 != 0;
2184 :     # This is a fancy PERL trick. The parentheses in the SPLIT pattern
2185 :     # cause the delimiters to be included in the output stream. The
2186 :     # GREP removes the empty strings in between the delimiters.
2187 :     my $retVal = join(",", grep { $_ ne '' } split(/(...)/, $padded));
2188 :     # Clean out the spaces.
2189 :     $retVal =~ s/ //g;
2190 :     # Return the result.
2191 :     return $retVal;
2192 :     }
2193 : parrello 1.46 =head3 SetPermissions
2194 :    
2195 : parrello 1.49 C<< Tracer::SetPermissions($dirName, $group, $mask, %otherMasks); >>
2196 : parrello 1.46
2197 :     Set the permissions for a directory and all the files and folders inside it.
2198 :     In addition, the group ownership will be changed to the specified value.
2199 :    
2200 :     This method is more vulnerable than most to permission and compatability
2201 :     problems, so it does internal error recovery.
2202 :    
2203 :     =over 4
2204 :    
2205 :     =item dirName
2206 :    
2207 :     Name of the directory to process.
2208 :    
2209 :     =item group
2210 :    
2211 :     Name of the group to be assigned.
2212 :    
2213 :     =item mask
2214 :    
2215 :     Permission mask. Bits that are C<1> in this mask will be ORed into the
2216 :     permission bits of any file or directory that does not already have them
2217 :     set to 1.
2218 :    
2219 : parrello 1.49 =item otherMasks
2220 :    
2221 :     Map of search patterns to permission masks. If a directory name matches
2222 :     one of the patterns, that directory and all its members and subdirectories
2223 :     will be assigned the new pattern. For example, the following would
2224 :     assign 01664 to most files, but would use 01777 for directories named C<tmp>.
2225 :    
2226 :     Tracer::SetPermissions($dirName, 'fig', 01664, '^tmp$' => 01777);
2227 :    
2228 :     The list is ordered, so the following would use 0777 for C<tmp1> and
2229 :     0666 for C<tmp>, C<tmp2>, or C<tmp3>.
2230 :    
2231 :     Tracer::SetPermissions($dirName, 'fig', 01664, '^tmp1' => 0777,
2232 :     '^tmp' => 0666);
2233 :    
2234 :     Note that the pattern matches are all case-insensitive, and only directory
2235 :     names are matched, not file names.
2236 :    
2237 : parrello 1.46 =back
2238 :    
2239 :     =cut
2240 :    
2241 :     sub SetPermissions {
2242 :     # Get the parameters.
2243 : parrello 1.49 my ($dirName, $group, $mask, @otherMasks) = @_;
2244 : parrello 1.46 # Set up for error recovery.
2245 :     eval {
2246 : parrello 1.49 # Switch to the specified directory.
2247 : parrello 1.46 ChDir($dirName);
2248 :     # Get the group ID.
2249 :     my $gid = getgrnam($group);
2250 : parrello 1.50 # Get the mask for tracing.
2251 : parrello 1.51 my $traceMask = sprintf("%04o", $mask) . "($mask)";
2252 : parrello 1.50 Trace("Fixing permissions for directory $dirName using group $group($gid) and mask $traceMask.") if T(2);
2253 : parrello 1.46 my $fixCount = 0;
2254 :     my $lookCount = 0;
2255 :     # @dirs will be a stack of directories to be processed.
2256 :     my @dirs = (getcwd());
2257 :     while (scalar(@dirs) > 0) {
2258 :     # Get the current directory.
2259 :     my $dir = pop @dirs;
2260 : parrello 1.49 # Check for a match to one of the specified directory names. To do
2261 :     # that, we need to pull the individual part of the name off of the
2262 :     # whole path.
2263 :     my $simpleName = $dir;
2264 : parrello 1.57 if ($dir =~ m!/([^/]+)$!) {
2265 : parrello 1.49 $simpleName = $1;
2266 :     }
2267 : parrello 1.56 Trace("Simple directory name for $dir is $simpleName.") if T(4);
2268 : parrello 1.49 # Search for a match.
2269 :     my $match = 0;
2270 :     my $i;
2271 :     for ($i = 0; $i < $#otherMasks && ! $match; $i += 2) {
2272 :     my $pattern = $otherMasks[$i];
2273 :     if ($simpleName =~ /$pattern/i) {
2274 :     $match = 1;
2275 : parrello 1.46 }
2276 : parrello 1.49 }
2277 : parrello 1.52 # Check for a match. Note we use $i-1 because the loop added 2
2278 :     # before terminating due to the match.
2279 :     if ($match && $otherMasks[$i-1] != $mask) {
2280 : parrello 1.49 # This directory matches one of the incoming patterns, and it's
2281 :     # a different mask, so we process it recursively with that mask.
2282 : parrello 1.52 SetPermissions($dir, $group, $otherMasks[$i-1], @otherMasks);
2283 : parrello 1.49 } else {
2284 :     # Here we can process normally. Get all of the non-hidden members.
2285 :     my @submems = OpenDir($dir, 1);
2286 :     for my $submem (@submems) {
2287 :     # Get the full name.
2288 :     my $thisMem = "$dir/$submem";
2289 :     Trace("Checking member $thisMem.") if T(4);
2290 :     $lookCount++;
2291 :     if ($lookCount % 1000 == 0) {
2292 : parrello 1.50 Trace("$lookCount members examined. Current is $thisMem. Mask is $traceMask") if T(3);
2293 : parrello 1.49 }
2294 :     # Fix the group.
2295 :     chown -1, $gid, $thisMem;
2296 :     # Insure this member is not a symlink.
2297 :     if (! -l $thisMem) {
2298 :     # Get its info.
2299 :     my $fileInfo = stat $thisMem;
2300 :     # Only proceed if we got the info. Otherwise, it's a hard link
2301 :     # and we want to skip it anyway.
2302 :     if ($fileInfo) {
2303 :     my $fileMode = $fileInfo->mode;
2304 : parrello 1.58 if (($fileMode & $mask) != $mask) {
2305 : parrello 1.49 # Fix this member.
2306 :     $fileMode |= $mask;
2307 :     chmod $fileMode, $thisMem;
2308 :     $fixCount++;
2309 :     }
2310 :     # If it's a subdirectory, stack it.
2311 :     if (-d $thisMem) {
2312 :     push @dirs, $thisMem;
2313 :     }
2314 : parrello 1.46 }
2315 :     }
2316 :     }
2317 :     }
2318 :     }
2319 :     Trace("$lookCount files and directories processed, $fixCount fixed.") if T(2);
2320 :     };
2321 :     # Check for an error.
2322 :     if ($@) {
2323 :     Confess("SetPermissions error: $@");
2324 :     }
2325 :     }
2326 :    
2327 : parrello 1.62 =head3 CompareLists
2328 :    
2329 :     C<< my ($inserted, $deleted) = Tracer::CompareLists(\@newList, \@oldList, $keyIndex); >>
2330 :    
2331 :     Compare two lists of tuples, and return a hash analyzing the differences. The lists
2332 :     are presumed to be sorted alphabetically by the value in the $keyIndex column.
2333 :     The return value contains a list of items that are only in the new list
2334 :     (inserted) and only in the old list (deleted).
2335 :    
2336 :     =over 4
2337 :    
2338 :     =item newList
2339 :    
2340 :     Reference to a list of new tuples.
2341 :    
2342 :     =item oldList
2343 :    
2344 :     Reference to a list of old tuples.
2345 :    
2346 :     =item keyIndex (optional)
2347 :    
2348 :     Index into each tuple of its key field. The default is 0.
2349 :    
2350 :     =item RETURN
2351 :    
2352 :     Returns a 2-tuple consisting of a reference to the list of items that are only in the new
2353 :     list (inserted) followed by a reference to the list of items that are only in the old
2354 :     list (deleted).
2355 :    
2356 :     =back
2357 :    
2358 :     =cut
2359 :    
2360 :     sub CompareLists {
2361 :     # Get the parameters.
2362 :     my ($newList, $oldList, $keyIndex) = @_;
2363 :     if (! defined $keyIndex) {
2364 :     $keyIndex = 0;
2365 :     }
2366 :     # Declare the return variables.
2367 :     my ($inserted, $deleted) = ([], []);
2368 :     # Loop through the two lists simultaneously.
2369 :     my ($newI, $oldI) = (0, 0);
2370 :     my ($newN, $oldN) = (scalar @{$newList}, scalar @{$oldList});
2371 :     while ($newI < $newN || $oldI < $oldN) {
2372 :     # Get the current object in each list. Note that if one
2373 :     # of the lists is past the end, we'll get undef.
2374 :     my $newItem = $newList->[$newI];
2375 :     my $oldItem = $oldList->[$oldI];
2376 : parrello 1.63 if (! defined($newItem) || defined($oldItem) && $newItem->[$keyIndex] gt $oldItem->[$keyIndex]) {
2377 : parrello 1.62 # The old item is not in the new list, so mark it deleted.
2378 :     push @{$deleted}, $oldItem;
2379 :     $oldI++;
2380 :     } elsif (! defined($oldItem) || $oldItem->[$keyIndex] gt $newItem->[$keyIndex]) {
2381 :     # The new item is not in the old list, so mark it inserted.
2382 :     push @{$inserted}, $newItem;
2383 :     $newI++;
2384 :     } else {
2385 :     # The item is in both lists, so push forward.
2386 :     $oldI++;
2387 :     $newI++;
2388 :     }
2389 :     }
2390 :     # Return the result.
2391 :     return ($inserted, $deleted);
2392 :     }
2393 :    
2394 :    
2395 : redwards 1.8 1;

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3