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

Diff of /FigKernelPackages/Tracer.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.73, Tue Oct 3 12:04:50 2006 UTC revision 1.129, Tue Jan 5 17:25:48 2010 UTC
# Line 1  Line 1 
1  #  # -*- perl -*-
2    ########################################################################
3  # Copyright (c) 2003-2006 University of Chicago and Fellowship  # Copyright (c) 2003-2006 University of Chicago and Fellowship
4  # for Interpretations of Genomes. All Rights Reserved.  # for Interpretations of Genomes. All Rights Reserved.
5  #  #
# Line 13  Line 14 
14  # at info@ci.uchicago.edu or the Fellowship for Interpretation of  # at info@ci.uchicago.edu or the Fellowship for Interpretation of
15  # Genomes at veronika@thefig.info or download a copy from  # Genomes at veronika@thefig.info or download a copy from
16  # http://www.theseed.org/LICENSE.TXT.  # http://www.theseed.org/LICENSE.TXT.
17  #  ########################################################################
18    
19  package Tracer;  package Tracer;
20    
     require Exporter;  
     @ISA = ('Exporter');  
     @EXPORT = qw(Trace T TSetup QTrace Confess Cluck Min Max Assert Open OpenDir TICK StandardSetup EmergencyKey ETracing ScriptSetup ScriptFinish Insure ChDir Emergency);  
     @EXPORT_OK = qw(GetFile GetOptions Merge MergeOptions ParseCommand ParseRecord UnEscape Escape);  
21      use strict;      use strict;
22      use Carp qw(longmess croak);      use base qw(Exporter);
23        use vars qw(@EXPORT @EXPORT_OK);
24        @EXPORT = qw(Trace T TSetup QTrace Confess MemTrace Cluck Min Max Assert Open OpenDir TICK StandardSetup EmergencyKey ETracing Constrain Insure ChDir Emergency Warn TraceDump IDHASH);
25        @EXPORT_OK = qw(GetFile GetOptions Merge MergeOptions ParseCommand ParseRecord UnEscape Escape PrintLine PutLine);
26        use Carp qw(longmess croak carp);
27      use CGI;      use CGI;
28      use Cwd;      use Cwd;
29      use FIG_Config;      use FIG_Config;
# Line 34  Line 35 
35      use LWP::UserAgent;      use LWP::UserAgent;
36      use Time::HiRes 'gettimeofday';      use Time::HiRes 'gettimeofday';
37      use URI::Escape;      use URI::Escape;
38        use Time::Local;
39        use POSIX qw(strftime);
40        use Time::Zone;
41        use Fcntl qw(:DEFAULT :flock);
42        use Data::Dumper;
43    
44    
45  =head1 Tracing and Debugging Helpers  =head1 Tracing and Debugging Helpers
46    
# Line 158  Line 165 
165    
166  Sometimes, you need a way for tracing to happen automatically without putting parameters  Sometimes, you need a way for tracing to happen automatically without putting parameters
167  in a form or on the command line. Emergency tracing does this. You invoke emergency tracing  in a form or on the command line. Emergency tracing does this. You invoke emergency tracing
168  from the debug form, which is accessed from I<MySeedInstance>C</FIG/Html/SetPassword.html>.  from the debug form, which is accessed from the [[DebugConsole]]. Emergency tracing requires
169  Emergency tracing requires you specify a tracing key. For command-line tools, the key is  that you specify a tracing key. For command-line tools, the key is
170  taken from the C<TRACING> environment variable. For web services, the key is taken from  taken from the C<TRACING> environment variable. For web services, the key is taken from
171  a cookie. Either way, the key tells the tracing facility who you are, so that you control  a cookie. Either way, the key tells the tracing facility who you are, so that you control
172  the tracing in your environment without stepping on other users.  the tracing in your environment without stepping on other users.
# Line 182  Line 189 
189    
190  The web script will look for the tracing key in the cookies, and the command-line  The web script will look for the tracing key in the cookies, and the command-line
191  script will look for it in the C<TRACING> environment variable. If you are  script will look for it in the C<TRACING> environment variable. If you are
192  using the L</StandardScript> or L</StandardSetup> methods, emergency tracing  using the L</StandardSetup> method or a [[WebApplication]], emergency tracing
193  will be configured automatically.  will be configured automatically.
194    
 =head3 Debugging Control Panel  
   
 The debugging control panel provides several tools to assist in development of  
 SEED and Sprout software. You access the debugging control panel from the URL  
 C</FIG/Html/SetPassword.html> in whichever seed instance you're using. (So,  
 for example, the panel access point for the development NMPDR system is  
 C<http://web-1.nmpdr.org/next/FIG/Html/SetPassword.html>. Contact Bruce to  
 find out what the password is. From this page, you can also specify a tracing  
 key. If you don't specify a key, one will be generated for you.  
   
 =head4 Emergency Tracing Form  
   
 At the bottom of the debugging control panel is a form that allows you to  
 specify a trace level and tracing categories. Special and common categories  
 are listed with check boxes. You can hold your mouse over a check box to see  
 what its category does. In general, however, a category name is the same as  
 the name of the package in which the trace message occurs.  
   
 Additional categories can be entered in an input box, delimited by spaces or commas.  
   
 The B<Activate> button turns on Emergency tracing at the level you specify with the  
 specified categories active. The B<Terminate> button turns tracing off. The  
 B<Show File> button displays the current contents of the trace file. The tracing  
 form at the bottom of the control panel is designed for emergency tracing, so it  
 will only affect programs that call L</ETracing>, L</StandardScript>,  
 or L</StandardSetup>.  
   
 =head4 Script Form  
   
 The top form of the debugging control panel allows you to enter a tiny script and  
 have the output generated in a formatted table. Certain object variables are  
 predefined in the script, including a FIG object (C<$fig>), a CGI object (C<$cgi>),  
 and-- if Sprout is active-- Sprout (C<$sprout>) and SFXlate (C<$sfx>) objects.  
   
 The last line of the script must be a scalar, but it can be a reference to a hash,  
 a list, a list of lists, and various other combinations. If you select the appropriate  
 data type in the dropdown box, the output will be formatted accordingly. The form  
 also has controls for specifying tracing. These controls override any emergency  
 tracing in effect.  
   
 =head4 Database Query Forms  
   
 The forms between the script form and the emergency tracing form allow you to  
 make queries against the database. The FIG query form allows simple queries against  
 a single FIG table. The Sprout query form uses the B<GetAll> method to do a  
 multi-table query against the Sprout database. B<GetAll> is located in the B<ERDB>  
 package, and it takes five parameters.  
   
     GetAll(\@objectNames, $filterClause, \@parameters, \@fields, $count);  
   
 Each of the five parameters corresponds to a text box on the query form:  
   
 =over 4  
   
 =item Objects  
   
 Comma-separated list containing the names of the entity and relationship objects to be retrieved.  
   
 =item Filter  
   
 WHERE/ORDER BY clause (without the WHERE) to be used to filter and sort the query. The WHERE clause can  
 be parameterized with parameter markers (C<?>). Each field used must be specified in the standard form  
 B<I<objectName>(I<fieldName>)> or B<$I<number>(I<fieldName>)> where I<fieldName> is the name of a  
 field, I<objectName> is the name of the entity or relationship object containing the field, and  
 I<number> is the 1-based position of the object in the object list. Any parameters  
 specified in the filter clause should be specified in the B<Params> field.  
 The fields in a filter clause can come from primary entity relations,  
 relationship relations, or secondary entity relations; however, all of the  
 entities and relationships involved must be included in the list of object names.  
   
 =item Params  
   
 List of the parameters to be substituted in for the parameters marks in the filter clause. This  
 is a comma-separated list without any quoting or escaping.  
   
 =item fields  
   
 Comma-separated list of the fields to be returned in each element of the list returned. Fields  
 are specified in the same manner as in the filter clause.  
   
 =item count  
   
 Maximum number of records to return. If omitted or 0, all available records will be returned.  
   
 =back  
   
 B<GetAll> automatically joins together the entities and relationships listed in the object  
 names. This simplifies the coding of the filter clause, but it means that some queries are  
 not possible, since they cannot be expressed in a linear sequence of joins. This is a limitation  
 that has yet to be addressed.  
   
195  =cut  =cut
196    
197  # Declare the configuration variables.  # Declare the configuration variables.
198    
199  my $Destination = "NONE";   # Description of where to send the trace output.  my $Destination = "WARN";   # Description of where to send the trace output.
200  my $TeeFlag = 0;            # TRUE if output is going to a file and to the  my $TeeFlag = 0;            # TRUE if output is going to a file and to the
201                              # standard output                              # standard output
202  my %Categories = ( main => 1 );  my %Categories = ( main => 1 );
203                              # hash of active category names                              # hash of active category names
204    my @LevelNames = qw(error warn notice info detail);
205  my $TraceLevel = 0;         # trace level; a higher trace level produces more  my $TraceLevel = 0;         # trace level; a higher trace level produces more
206                              # messages                              # messages
207  my @Queue = ();             # queued list of trace messages.  my @Queue = ();             # queued list of trace messages.
208  my $LastCategory = "main";  # name of the last category interrogated  my $LastCategory = "main";  # name of the last category interrogated
209    my $LastLevel = 0;          # level of the last test call
210  my $SetupCount = 0;         # number of times TSetup called  my $SetupCount = 0;         # number of times TSetup called
211  my $AllTrace = 0;           # TRUE if we are tracing all categories.  my $AllTrace = 0;           # TRUE if we are tracing all categories.
212    my $SavedCGI;               # CGI object passed to ETracing
213    my $CommandLine;            # Command line passed to StandardSetup
214    my $Confessions = 0;        # confession count
215    umask 2;                    # Fix the damn umask so everything is group-writable.
216    
217  =head2 Public Methods  =head2 Tracing Methods
218    
219    =head3 Setups
220    
221        my $count = Tracer::Setups();
222    
223    Return the number of times L</TSetup> has been called.
224    
225    This method allows for the creation of conditional tracing setups where, for example, we
226    may want to set up tracing if nobody else has done it before us.
227    
228    =cut
229    
230    sub Setups {
231        return $SetupCount;
232    }
233    
234  =head3 TSetup  =head3 TSetup
235    
236  C<< TSetup($categoryList, $target); >>      TSetup($categoryList, $target);
237    
238  This method is used to specify the trace options. The options are stored as package data  This method is used to specify the trace options. The options are stored as package data
239  and interrogated by the L</Trace> and L</T> methods.  and interrogated by the L</Trace> and L</T> methods.
# Line 346  Line 283 
283          }          }
284      }      }
285      # Now we need to process the destination information. The most important special      # Now we need to process the destination information. The most important special
286      # cases are the single ">", which requires we clear the file first, and the      # case is when we're writing to a file. This is indicated by ">" (overwrite) and
287      # "+" prefix which indicates a double echo.      # ">>" (append). A leading "+" for either indicates that we are also writing to
288        # the standard output (tee mode).
289      if ($target =~ m/^\+?>>?/) {      if ($target =~ m/^\+?>>?/) {
290          if ($target =~ m/^\+/) {          if ($target =~ m/^\+/) {
291              $TeeFlag = 1;              $TeeFlag = 1;
292              $target = substr($target, 1);              $target = substr($target, 1);
293          }          }
294          if ($target =~ m/^>[^>]/) {          if ($target =~ m/^>[^>]/) {
295                # We need to initialize the file (which clears it).
296              open TRACEFILE, $target;              open TRACEFILE, $target;
297              print TRACEFILE "[" . Now() . "] <Tracer>: Tracing initialized.\n";              print TRACEFILE "[" . Now() . "] [notice] [Tracer] Tracing initialized.\n";
298              close TRACEFILE;              close TRACEFILE;
299                # Set to append mode now that the file has been cleared.
300              $Destination = ">$target";              $Destination = ">$target";
301          } else {          } else {
302              $Destination = $target;              $Destination = $target;
# Line 368  Line 308 
308      $SetupCount++;      $SetupCount++;
309  }  }
310    
311  =head3 StandardSetup  =head3 SetLevel
   
 C<< my ($options, @parameters) = StandardSetup(\@categories, \%options, $parmHelp, @ARGV); >>  
   
 This method performs standard command-line parsing and tracing setup. The return  
 values are a hash of the command-line options and a list of the positional  
 parameters. Tracing is automatically set up and the command-line options are  
 validated.  
   
 This is a complex method that does a lot of grunt work. The parameters can  
 be more easily understood, however, once they are examined individually.  
   
 The I<categories> parameter is the most obtuse. It is a reference to a list of  
 special-purpose tracing categories. Most tracing categories are PERL package  
 names. So, for example, if you wanted to turn on tracing inside the B<Sprout>,  
 B<ERDB>, and B<SproutLoad> packages, you would specify the categories  
312    
313      ["Sprout", "SproutLoad", "ERDB"]      Tracer::SetLevel($newLevel);
314    
315  This would cause trace messages in the specified three packages to appear in  Modify the trace level. A higher trace level will cause more messages to appear.
 the output. There are two special tracing categories that are automatically  
 handled by this method. In other words, if you used L</TSetup> you would need  
 to include these categories manually, but if you use this method they are turned  
 on automatically.  
316    
317  =over 4  =over 4
318    
319  =item SQL  =item newLevel
   
 Traces SQL commands and activity.  
   
 =item Tracer  
320    
321  Traces error messages and call stacks.  Proposed new trace level.
322    
323  =back  =back
324    
325  C<SQL> is only turned on if the C<-sql> option is specified in the command line.  =cut
 The trace level is specified using the C<-trace> command-line option. For example,  
 the following command line for C<TransactFeatures> turns on SQL tracing and runs  
 all tracing at level 3.  
326    
327      TransactFeatures -trace=3 -sql register ../xacts IDs.tbl  sub SetLevel {
328        $TraceLevel = $_[0];
329    }
330    
331  Standard tracing is output to the standard output and echoed to the file  =head3 ParseDate
 C<trace>I<$$>C<.log> in the FIG temporary directory, where I<$$> is the  
 process ID. You can also specify the C<user> parameter to put a user ID  
 instead of a process ID in the trace file name. So, for example  
332    
333  The default trace level is 2. To get all messages, specify a trace level of 4.      my $time = Tracer::ParseDate($dateString);
 For a genome-by-genome update, use 3.  
334    
335      TransactFeatures -trace=3 -sql -user=Bruce register ../xacts IDs.tbl  Convert a date into a PERL time number. This method expects a date-like string
336    and parses it into a number. The string must be vaguely date-like or it will
337    return an undefined value. Our requirement is that a month and day be
338    present and that three pieces of the date string (time of day, month and day,
339    year) be separated by likely delimiters, such as spaces, commas, and such-like.
340    
341  would send the trace output to C<traceBruce.log> in the temporary directory.  If a time of day is present, it must be in military time with two digits for
342    everything but the hour.
343    
344  The I<options> parameter is a reference to a hash containing the command-line  The year must be exactly four digits.
 options, their default values, and an explanation of what they mean. Command-line  
 options may be in the form of switches or keywords. In the case of a switch, the  
 option value is 1 if it is specified and 0 if it is not specified. In the case  
 of a keyword, the value is separated from the option name by an equal sign. You  
 can see this last in the command-line example above.  
345    
346  You can specify a different default trace level by setting C<$options->{trace}>  Additional stuff can be in the string. We presume it's time zones or weekdays or something
347  prior to calling this method.  equally innocuous. This means, however, that a sufficiently long sentence with date-like
348    parts in it may be interpreted as a date. Hopefully this will not be a problem.
349    
350  An example at this point would help. Consider, for example, the command-line utility  It should be guaranteed that this method will parse the output of the L</Now> function.
 C<TransactFeatures>. It accepts a list of positional parameters plus the options  
 C<safe>, C<noAlias>, C<start>, and C<tblFiles>. To start up this command, we execute  
 the following code.  
351    
352      my ($options, @parameters) = Tracer::StandardSetup(["DocUtils"],  The parameters are as follows.
                         { safe => [0, "use database transactions"],  
                           noAlias => [0, "do not expect aliases in CHANGE transactions"],  
                           start => [' ', "start with this genome"],  
                           tblFiles => [0, "output TBL files containing the corrected IDs"] },  
                         "command transactionDirectory IDfile",  
                       @ARGV);  
353    
354    =over 4
355    
356  The call to C<ParseCommand> specifies the default values for the options and  =item dateString
 stores the actual options in a hash that is returned as C<$options>. The  
 positional parameters are returned in C<@parameters>.  
357    
358  The following is a sample command line for C<TransactFeatures>.  The date string to convert.
359    
360      TransactFeatures -trace=2 -noAlias register ../xacts IDs.tbl  =item RETURN
361    
362  In this case, C<register>, C<../xacts>, and C<IDs.tbl> are the positional  Returns a PERL time, that is, a number of seconds since the epoch, or C<undef> if
363  parameters, and would find themselves in I<@parameters> after executing the  the date string is invalid. A valid date string must contain a month and day.
 above code fragment. The tracing would be set to level 2, and the categories  
 would be C<Tracer>, and <DocUtils>. C<Tracer> is standard,  
 and C<DocUtils> was included because it came in within the first parameter  
 to this method. The I<$options> hash would be  
364    
365      { trace => 2, sql => 0, safe => 0,  =back
       noAlias => 1, start => ' ', tblFiles => 0 }  
366    
367  Use of C<StandardSetup> in this way provides a simple way of performing  =cut
 standard tracing setup and command-line parsing. Note that the caller is  
 not even aware of the command-line switches C<-trace> and C<-sql>, which  
 are used by this method to control the tracing. If additional tracing features  
 need to be added in the future, they can be processed by this method without  
 upsetting the command-line utilities.  
368    
369  If the C<background> option is specified on the command line, then the  # Universal month conversion table.
370  standard and error outputs will be directed to files in the temporary  use constant MONTHS => {    Jan =>  0, January   =>  0, '01' =>  0,  '1' =>  0,
371  directory, using the same suffix as the trace file. So, if the command                              Feb =>  1, February  =>  1, '02' =>  1,  '2' =>  1,
372  line specified                              Mar =>  2, March     =>  2, '03' =>  2,  '3' =>  2,
373                                Apr =>  3, April     =>  3, '04' =>  3,  '4' =>  3,
374                                May =>  4, May       =>  4, '05' =>  4,  '5' =>  4,
375                                Jun =>  5, June      =>  5, '06' =>  5,  '6' =>  5,
376                                Jul =>  6, July      =>  6, '07' =>  6,  '7' =>  6,
377                                Aug =>  7, August    =>  7, '08' =>  7,  '8' =>  7,
378                                Sep =>  8, September =>  8, '09' =>  8,  '9' =>  8,
379                                Oct =>  9, October  =>   9, '10' =>  9,
380                                Nov => 10, November =>  10, '11' => 10,
381                                Dec => 11, December =>  11, '12' => 11
382                            };
383    
384      -user=Bruce -background  sub ParseDate {
385        # Get the parameters.
386        my ($dateString) = @_;
387        # Declare the return variable.
388        my $retVal;
389        # Find the month and day of month. There are two ways that can happen. We check for the
390        # numeric style first. That way, if the user's done something like "Sun 12/22", then we
391        # won't be fooled into thinking the month is Sunday.
392        if ($dateString =~ m#\b(\d{1,2})/(\d{1,2})\b# || $dateString =~ m#\b(\w+)\s(\d{1,2})\b#) {
393            my ($mon, $mday) = (MONTHS->{$1}, $2);
394            # Insist that the month and day are valid.
395            if (defined($mon) && $2 >= 1 && $2 <= 31) {
396                # Find the time.
397                my ($hour, $min, $sec) = (0, 0, 0);
398                if ($dateString =~ /\b(\d{1,2}):(\d{2}):(\d{2})\b/) {
399                    ($hour, $min, $sec) = ($1, $2, $3);
400                }
401                # Find the year.
402                my $year;
403                if ($dateString =~ /\b(\d{4})\b/) {
404                    $year = $1;
405                } else {
406                    # Get the default year, which is this one. Note we must convert it to
407                    # the four-digit value expected by "timelocal".
408                    (undef, undef, undef, undef, undef, $year) = localtime();
409                    $year += 1900;
410                }
411                $retVal = timelocal($sec, $min, $hour, $mday, $mon, $year);
412            }
413        }
414        # Return the result.
415        return $retVal;
416    }
417    
418  then the trace output would go to C<traceBruce.log>, the standard output to  =head3 LogErrors
 C<outBruce.log>, and the error output to C<errBruce.log>. This is designed to  
 simplify starting a command in the background.  
419    
420  The user name is also used as the tracing key for L</Emergency Tracing>.      Tracer::LogErrors($fileName);
 Specifying a value of C<E> for the trace level causes emergency tracing to  
 be used instead of custom tracing. If the user name is not specified,  
 the tracing key is taken from the C<Tracing> environment variable. If there  
 is no value for that variable, the tracing key will be computed from the PID.  
421    
422  Finally, if the special option C<-h> is specified, the option names will  Route the standard error output to a log file.
 be traced at level 0 and the program will exit without processing.  
 This provides a limited help capability. For example, if the user enters  
423    
424      TransactFeatures -h  =over 4
425    
426  he would see the following output.  =item fileName
427    
428      TransactFeatures [options] command transactionDirectory IDfile  Name of the file to receive the error output.
         -trace    tracing level (default E)  
         -sql      trace SQL commands  
         -safe     use database transactions  
         -noAlias  do not expect aliases in CHANGE transactions  
         -start    start with this genome  
         -tblFiles output TBL files containing the corrected IDs  
429    
430  The caller has the option of modifying the tracing scheme by placing a value  =back
 for C<trace> in the incoming options hash. The default value can be overridden,  
 or the tracing to the standard output can be turned off by suffixing a minus  
 sign to the trace level. So, for example,  
431    
432      { trace => [0, "tracing level (default 0)"],  =cut
        ...  
433    
434  would set the default trace level to 0 instead of E, while  sub LogErrors {
435        # Get the file name.
436        my ($fileName) = @_;
437        # Open the file as the standard error output.
438        open STDERR, '>', $fileName;
439    }
440    
441      { trace => ["2-", "tracing level (default 2)"],  =head3 Trace
        ...  
442    
443  would set the default to 2, but trace only to the log file, not to the      Trace($message);
 standard output.  
444    
445  The parameters to this method are as follows.  Write a trace message to the target location specified in L</TSetup>. If there has not been
446    any prior call to B<TSetup>.
447    
448  =over 4  =over 4
449    
450  =item categories  =item message
451    
452  Reference to a list of tracing category names. These should be names of  Message to write.
 packages whose internal workings will need to be debugged to get the  
 command working.  
453    
454  =item options  =back
455    
456  Reference to a hash containing the legal options for the current command mapped  =cut
 to their default values and descriptions. The user can override the defaults  
 by specifying the options as command-line switches prefixed by a hyphen.  
 Tracing-related options may be added to this hash. If the C<-h> option is  
 specified on the command line, the option descriptions will be used to  
 explain the options. To turn off tracing to the standard output, add a  
 minus sign to the value for C<trace> (see above).  
457    
458  =item parmHelp  sub Trace {
459        # Get the parameters.
460        my ($message) = @_;
461        # Strip off any line terminators at the end of the message. We will add
462        # new-line stuff ourselves.
463        my $stripped = Strip($message);
464        # Compute the caller information.
465        my ($callPackage, $callFile, $callLine) = caller();
466        my $callFileTitle = basename($callFile);
467        # Check the caller.
468        my $callerInfo = ($callFileTitle ne "Tracer.pm" ? " [$callFileTitle $callLine]" : "");
469        # Get the timestamp.
470        my $timeStamp = Now();
471        # Build the prefix.
472        my $level = $LevelNames[$LastLevel] || "($LastLevel)";
473        my $prefix = "[$timeStamp] [$level] [$LastCategory]$callerInfo";
474        # Format the message.
475        my $formatted = "$prefix $stripped";
476        # Process according to the destination.
477        if ($Destination eq "TEXT") {
478            # Write the message to the standard output.
479            print "$formatted\n";
480        } elsif ($Destination eq "ERROR") {
481            # Write the message to the error output. Here, we want our prefix fields.
482            print STDERR "$formatted\n";
483        } elsif ($Destination eq "WARN") {
484            # Emit the message to the standard error output. It is presumed that the
485            # error logger will add its own prefix fields, the notable exception being
486            # the caller info.
487            print STDERR "$callerInfo$stripped\n";
488        } elsif ($Destination eq "QUEUE") {
489            # Push the message into the queue.
490            push @Queue, "$formatted";
491        } elsif ($Destination eq "HTML") {
492            # Convert the message to HTML.
493            my $escapedMessage = CGI::escapeHTML($stripped);
494            # The stuff after the first line feed should be pre-formatted.
495            my @lines = split /\s*\n/, $escapedMessage;
496            # Get the normal portion.
497            my $line1 = shift @lines;
498            print "<p>$timeStamp $LastCategory $LastLevel: $line1</p>\n";
499            if (@lines) {
500                print "<pre>" . join("\n", @lines, "</pre>");
501            }
502        } elsif ($Destination =~ m/^>>/) {
503            # Write the trace message to an output file.
504            open(TRACING, $Destination) || die "Tracing open for \"$Destination\" failed: $!";
505            # Lock the file.
506            flock TRACING, LOCK_EX;
507            print TRACING "$formatted\n";
508            close TRACING;
509            # If the Tee flag is on, echo it to the standard output.
510            if ($TeeFlag) {
511                print "$formatted\n";
512            }
513        }
514    }
515    
516  A string that vaguely describes the positional parameters. This is used  =head3 MemTrace
 if the user specifies the C<-h> option.  
517    
518  =item argv      MemTrace($message);
519    
520  List of command line parameters, including the option switches, which must  Output a trace message that includes memory size information.
 precede the positional parameters and be prefixed by a hyphen.  
521    
522  =item RETURN  =over 4
523    
524  Returns a list. The first element of the list is the reference to a hash that  =item message
525  maps the command-line option switches to their values. These will either be the  
526  default values or overrides specified on the command line. The remaining  Message to display. The message will be followed by a sentence about the memory size.
 elements of the list are the position parameters, in order.  
527    
528  =back  =back
529    
530  =cut  =cut
531    
532  sub StandardSetup {  sub MemTrace {
533      # Get the parameters.      # Get the parameters.
534      my ($categories, $options, $parmHelp, @argv) = @_;      my ($message) = @_;
535      # Get the default tracing key.      my $memory = GetMemorySize();
536      my $tkey = EmergencyKey();      Trace("$message $memory in use.");
     # Add the tracing options.  
     if (! exists $options->{trace}) {  
         $options->{trace} = ['E', "tracing level (E for emergency tracing)"];  
537      }      }
538      $options->{sql} = [0, "turn on SQL tracing"];  
539      $options->{h} = [0, "display command-line options"];  
540      $options->{user} = [$tkey, "tracing key"];  =head3 TraceDump
541      $options->{background} = [0, "spool standard and error output"];  
542      # Create a parsing hash from the options hash. The parsing hash      TraceDump($title, $object);
543      # contains the default values rather than the default value  
544      # and the description. While we're at it, we'll memorize the  Dump an object to the trace log. This method simply calls the C<Dumper>
545      # length of the longest option name.  function, but routes the output to the trace log instead of returning it
546      my $longestName = 0;  as a string. The output is arranged so that it comes out monospaced when
547      my %parseOptions = ();  it appears in an HTML trace dump.
548      for my $key (keys %{$options}) {  
549          if (length $key > $longestName) {  =over 4
550              $longestName = length $key;  
551    =item title
552    
553    Title to give to the object being dumped.
554    
555    =item object
556    
557    Reference to a list, hash, or object to dump.
558    
559    =back
560    
561    =cut
562    
563    sub TraceDump {
564        # Get the parameters.
565        my ($title, $object) = @_;
566        # Trace the object.
567        Trace("Object dump for $title:\n" . Dumper($object));
568          }          }
569          $parseOptions{$key} = $options->{$key}->[0];  
570    =head3 T
571    
572        my $switch = T($category, $traceLevel);
573    
574        or
575    
576        my $switch = T($traceLevel);
577    
578    Return TRUE if the trace level is at or above a specified value and the specified category
579    is active, else FALSE. If no category is specified, the caller's package name is used.
580    
581    =over 4
582    
583    =item category
584    
585    Category to which the message belongs. If not specified, the caller's package name is
586    used.
587    
588    =item traceLevel
589    
590    Relevant tracing level.
591    
592    =item RETURN
593    
594    TRUE if a message at the specified trace level would appear in the trace, else FALSE.
595    
596    =back
597    
598    =cut
599    
600    sub T {
601        # Declare the return variable.
602        my $retVal = 0;
603        # Only proceed if tracing is turned on.
604        if ($Destination ne "NONE") {
605            # Get the parameters.
606            my ($category, $traceLevel) = @_;
607            if (!defined $traceLevel) {
608                # Here we have no category, so we need to get the calling package.
609                # The calling package is normally the first parameter. If it is
610                # omitted, the first parameter will be the tracelevel. So, the
611                # first thing we do is shift the so-called category into the
612                # $traceLevel variable where it belongs.
613                $traceLevel = $category;
614                my ($package, $fileName, $line) = caller;
615                # If there is no calling package, we default to "main".
616                if (!$package) {
617                    $category = "main";
618                } else {
619                    my @cats = split /::/, $package;
620                    $category = $cats[$#cats];
621      }      }
     # Parse the command line.  
     my ($retOptions, @retParameters) = ParseCommand(\%parseOptions, @argv);  
     # Get the logfile suffix.  
     my $suffix = $retOptions->{user};  
     # Check for background mode.  
     if ($retOptions->{background}) {  
         my $outFileName = "$FIG_Config::temp/out$suffix.log";  
         my $errFileName = "$FIG_Config::temp/err$suffix.log";  
         open STDOUT, ">$outFileName";  
         open STDERR, ">$errFileName";  
622      }      }
623      # Now we want to set up tracing. First, we need to know if the user          # Save the category name and level.
624      # wants emergency tracing.          $LastCategory = $category;
625      if ($retOptions->{trace} eq 'E') {          $LastLevel = $traceLevel;
626          ETracing($retOptions->{user});          # Convert it to lower case before we hash it.
627      } else {          $category = lc $category;
628          # Here the tracing is controlled from the command line.          # Validate the trace level.
629          my @cats = @{$categories};          if (ref $traceLevel) {
630          if ($retOptions->{sql}) {              Confess("Bad trace level.");
631              push @cats, "SQL";          } elsif (ref $TraceLevel) {
632                Confess("Bad trace config.");
633          }          }
634          # Add the default categories.          # Make the check. Note that level 0 shows even if the category is turned off.
635          push @cats, "Tracer";          $retVal = ($traceLevel <= $TraceLevel && ($traceLevel == 0 || $AllTrace || exists $Categories{$category}));
         # Next, we create the category string by joining the categories.  
         my $cats = join(" ", @cats);  
         # Check to determine whether or not the caller wants to turn off tracing  
         # to the standard output.  
         my $traceLevel = $retOptions->{trace};  
         my $textOKFlag = 1;  
         if ($traceLevel =~ /^(.)-/) {  
             $traceLevel = $1;  
             $textOKFlag = 0;  
636          }          }
637          # Now we set up the trace mode.      # Return the computed result.
638          my $traceMode;      return $retVal;
         # Verify that we can open a file in the FIG temporary directory.  
         my $traceFileName = "$FIG_Config::temp/trace$suffix.log";  
         if (open TESTTRACE, ">$traceFileName") {  
             # Here we can trace to a file.  
             $traceMode = ">$traceFileName";  
             if ($textOKFlag) {  
                 # Echo to standard output if the text-OK flag is set.  
                 $traceMode = "+$traceMode";  
639              }              }
640              # Close the test file.  
641              close TESTTRACE;  =head3 QTrace
642          } else {  
643              # Here we can't trace to a file. We trace to the standard output if it's      my $data = QTrace($format);
644              # okay, and the error log otherwise.  
645              if ($textOKFlag) {  Return the queued trace data in the specified format.
646                  $traceMode = "TEXT";  
647              } else {  =over 4
648                  $traceMode = "WARN";  
649    =item format
650    
651    C<html> to format the data as an HTML list, C<text> to format it as straight text.
652    
653    =back
654    
655    =cut
656    
657    sub QTrace {
658        # Get the parameter.
659        my ($format) = @_;
660        # Create the return variable.
661        my $retVal = "";
662        # Only proceed if there is an actual queue.
663        if (@Queue) {
664            # Process according to the format.
665            if ($format =~ m/^HTML$/i) {
666                # Convert the queue into an HTML list.
667                $retVal = "<ul>\n";
668                for my $line (@Queue) {
669                    my $escapedLine = CGI::escapeHTML($line);
670                    $retVal .= "<li>$escapedLine</li>\n";
671              }              }
672                $retVal .= "</ul>\n";
673            } elsif ($format =~ m/^TEXT$/i) {
674                # Convert the queue into a list of text lines.
675                $retVal = join("\n", @Queue) . "\n";
676          }          }
677          # Now set up the tracing.          # Clear the queue.
678          TSetup("$traceLevel $cats", $traceMode);          @Queue = ();
679      }      }
680      # Check for the "h" option. If it is specified, dump the command-line      # Return the formatted list.
681      # options and exit the program.      return $retVal;
     if ($retOptions->{h}) {  
         $0 =~ m#[/\\](\w+)(\.pl)?$#i;  
         print "$1 [options] $parmHelp\n";  
         for my $key (sort keys %{$options}) {  
             my $name = Pad($key, $longestName, 0, ' ');  
             my $desc = $options->{$key}->[1];  
             if ($options->{$key}->[0]) {  
                 $desc .= " (default " . $options->{$key}->[0] . ")";  
682              }              }
683              print "  $name $desc\n";  
684    =head3 Confess
685    
686        Confess($message);
687    
688    Trace the call stack and abort the program with the specified message. When used with
689    the OR operator and the L</Assert> method, B<Confess> can function as a debugging assert.
690    So, for example
691    
692        Assert($recNum >= 0) || Confess("Invalid record number $recNum.");
693    
694    Will abort the program with a stack trace if the value of C<$recNum> is negative.
695    
696    =over 4
697    
698    =item message
699    
700    Message to include in the trace.
701    
702    =back
703    
704    =cut
705    
706    sub Confess {
707        # Get the parameters.
708        my ($message) = @_;
709        # Set up the category and level.
710        $LastCategory = "(confess)";
711        $LastLevel = 0;
712        # Trace the call stack.
713        Cluck($message);
714        # Increment the confession count.
715        $Confessions++;
716        # Abort the program.
717        croak(">>> $message");
718          }          }
719          exit(0);  
720    =head3 Confessions
721    
722        my $count = Tracer::Confessions();
723    
724    Return the number of calls to L</Confess> by the current task.
725    
726    =cut
727    
728    sub Confessions {
729        return $Confessions;
730      }      }
731      # Return the parsed parameters.  
732      return ($retOptions, @retParameters);  
733    =head3 SaveCGI
734    
735        Tracer::SaveCGI($cgi);
736    
737    This method saves the CGI object but does not activate emergency tracing.
738    It is used to allow L</Warn> to work in situations where emergency
739    tracing is contra-indicated (e.g. the wiki).
740    
741    =over 4
742    
743    =item cgi
744    
745    Active CGI query object.
746    
747    =back
748    
749    =cut
750    
751    sub SaveCGI {
752        $SavedCGI = $_[0];
753  }  }
754    
755  =head3 Setups  =head3 Warn
756    
757        Warn($message, @options);
758    
759    This method traces an important message. If an RSS feed is configured
760    (via I<FIG_Config::error_feed>) and the tracing destination is C<WARN>,
761    then the message will be echoed to the feed. In general, a tracing
762    destination of C<WARN> indicates that the caller is running as a web
763    service in a production environment; however, this is not a requirement.
764    
765    To force warnings into the RSS feed even when the tracing destination
766    is not C<WARN>, simply specify the C<Feed> tracing module. This can be
767    configured automatically when L</StandardSetup> is used.
768    
769    The L</Cluck> method calls this one for its final message. Since
770    L</Confess> calls L</Cluck>, this means that any error which is caught
771    and confessed will put something in the feed. This insures that someone
772    will be alerted relatively quickly when a failure occurs.
773    
774    =over 4
775    
776    =item message
777    
778    Message to be traced.
779    
780    =item options
781    
782    A list containing zero or more options.
783    
784    =back
785    
786    The permissible options are as follows.
787    
788    =over 4
789    
790    =item noStack
791    
792    If specified, then the stack trace is not included in the output.
793    
794    =back
795    
796    =cut
797    
798    sub Warn {
799        # Get the parameters.
800        my $message = shift @_;
801        my %options = map { $_ => 1 } @_;
802        # Save $@;
803        my $savedError = $@;
804        # Trace the message.
805        Trace($message);
806        # This will contain the lock handle. If it's defined, it means we need to unlock.
807        my $lock;
808        # Check for feed forcing.
809        my $forceFeed = exists $Categories{feed};
810        # An error here would be disastrous. Note that if debug mode is specified,
811        # we do this stuff even in a test environment.
812        eval {
813            # Do we need to put this in the RSS feed?
814            if ($FIG_Config::error_feed && ($Destination eq 'WARN' || $forceFeed)) {
815                # Probably. We need to check first, however, to see if it's from an
816                # ignored IP. For non-CGI situations, we default the IP to the self-referent.
817                my $key = "127.0.0.1";
818                if (defined $SavedCGI) {
819                    # Get the IP address.
820                    $key = $ENV{HTTP_X_FORWARDED_FOR} || $ENV{REMOTE_ADDR};
821                }
822                # Is the IP address in the ignore list?
823                my $found = scalar(grep { $_ eq $key } @FIG_Config::error_ignore_ips);
824                if (! $found) {
825                    # No. We're good. We now need to compute the date, the link, and the title.
826                    # First, the date, in a very specific format.
827                    my $date = strftime("%a, %02e %b %H:%M:%S %Y ", localtime) .
828                        (tz_local_offset() / 30);
829                    # Environment data goes in here. We start with the date.
830                    my $environment = "$date.  ";
831                    # If we need to recap the message (because it's too long to be a title), we'll
832                    # put it in here.
833                    my $recap;
834                    # Copy the message and remove excess space.
835                    my $title = $message;
836                    $title =~ s/\s+/ /gs;
837                    # If it's too long, we have to split it up.
838                    if (length $title > 60) {
839                        # Put the full message in the environment string.
840                        $recap = $title;
841                        # Excerpt it as the title.
842                        $title = substr($title, 0, 50) . "...";
843                    }
844                    # If we have a CGI object, then this is a web error. Otherwise, it's
845                    # command-line.
846                    if (defined $SavedCGI) {
847                        # We're in a web service. The environment is the user's IP, and the link
848                        # is the URL that got us here.
849                        $environment .= "Event Reported at IP address $key process $$.";
850                        my $url = $SavedCGI->self_url();
851                        # We need the user agent string and (if available) the referrer.
852                        # The referrer will be the link.
853                        $environment .= " User Agent $ENV{HTTP_USER_AGENT}";
854                        if ($ENV{HTTP_REFERER}) {
855                            my $link = $ENV{HTTP_REFERER};
856                            $environment .= " referred from <a href=\"$link\">$link</a>.";
857                        } else {
858                            $environment .= " referrer unknown.";
859                        }
860                        # Close off the sentence with the original link.
861                        $environment .= " URL of event is <a href=\"$url\">$url</a>.";
862                    } else {
863                        # No CGI object, so we're a command-line tool. Use the tracing
864                        # key and the PID as the user identifier, and add the command.
865                        my $key = EmergencyKey();
866                        $environment .= "Event Reported by $key process $$.";
867                        if ($CommandLine) {
868                            # We're in a StandardSetup script, so we have the real command line.
869                            $environment .= "\n<pre>" . CGI::escapeHTML($CommandLine) . "</pre>\n";
870                        } elsif ($ENV{_}) {
871                            # We're in a BASH script, so the command has been stored in the _ variable.
872                            $environment .= "  Command = " . CGI::escapeHTML($ENV{_}) . "\n";
873                        }
874                    }
875                    # Build a GUID. We use the current time, the title, and the process ID,
876                    # then digest the result.
877                    my $guid = Digest::MD5::md5_base64(gettimeofday(), $title, $$);
878                    # Finally, the description. This is a stack trace plus various environmental stuff.
879                    # The trace is optional.
880                    my $stackTrace;
881                    if ($options{noStack}) {
882                        $stackTrace = "";
883                    } else {
884                        my @trace = LongMess();
885                        # Only proceed if we got something back.
886                        if (scalar(@trace) > 0) {
887                            $trace[0] =~ s/Tracer::Warn.+?called/Event occurred/;
888                            $stackTrace = "Stack trace:<pre>" . join("\n", @trace, "</pre>");
889                        }
890                    }
891                    # We got the stack trace. Now it's time to put it all together.
892                    # We have a goofy thing here in that we need to HTML-escape some sections of the description
893                    # twice. They will be escaped once here, and then once when written by XML::Simple. They are
894                    # unescaped once when processed by the RSS reader, and stuff in the description is treated as
895                    # HTML. So, anything escaped here is treated as a literal when viewed in the RSS reader, but
896                    # our <br>s and <pre>s are used to format the description.
897                    $recap = (defined $recap ? "<em>" . CGI::escapeHTML($recap) . "</em><br /><br />" : "");
898                    my $description = "$recap$environment  $stackTrace";
899                    # Okay, we have all the pieces. Create a hash of the new event.
900                    my $newItem = { title => $title,
901                                    description => $description,
902                                    category => $LastCategory,
903                                    pubDate => $date,
904                                    guid => $guid,
905                                  };
906                    # We need XML capability for this.
907                    require XML::Simple;
908                    # The RSS document goes in here.
909                    my $rss;
910                    # Get the name of the RSS file. It's in the FIG temporary directory.
911                    my $fileName = "$FIG_Config::temp/$FIG_Config::error_feed";
912                    # Open the config file and lock it.
913                    $lock = Open(undef, "<$FIG_Config::fig_disk/config/FIG_Config.pm");
914                    flock $lock, LOCK_EX;
915                    # Does it exist?
916                    if (-s $fileName) {
917                        # Slurp it in.
918                        $rss = XML::Simple::XMLin($fileName, ForceArray => ['item']);
919                    } else {
920                        my $size = -s $fileName;
921                        # Create an empty channel.
922                        $rss = {
923                            channel => {
924                                title => 'NMPDR Warning Feed',
925                                link => "$FIG_Config::temp_url/$FIG_Config::error_feed",
926                                description => "Important messages regarding the status of the NMPDR.",
927                                generator => "NMPDR Trace Facility",
928                                docs => "http://blogs.law.harvard.edu/tech/rss",
929                                item => []
930                            },
931                        };
932                    }
933                    # Get the channel object.
934                    my $channel = $rss->{channel};
935                    # Update the last-build date.
936                    $channel->{lastBuildDate} = $date;
937                    # Get the item array.
938                    my $items = $channel->{item};
939                    # Insure it has only 100 entries.
940                    while (scalar @{$items} > 100) {
941                        pop @{$items};
942                    }
943                    # Add our new item at the front.
944                    unshift @{$items}, $newItem;
945                    # Create the XML. Note we do not include the root or the declaration. XML Simple can't handle
946                    # the requirements for those.
947                    my $xml = XML::Simple::XMLout($channel, NoAttr => 1, RootName => 'channel', XmlDecl => '');
948                    # Here we put in the root and declaration. The problem is that the root has to have the version attribute
949                    # in it. So, we suppress the root and do it by hand, and that requires suppressing the declaration, too.
950                    $xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<rss version=\"2.0\">$xml\n</rss>";
951                    # We don't use Open here because we can't afford an error.
952                    if (open XMLOUT, ">$fileName") {
953                        print XMLOUT $xml;
954                        close XMLOUT;
955                    }
956                }
957            }
958        };
959        if ($@) {
960            # If the feed failed, we need to know why. The error will be traced, but this method will not be involved
961            # (which is a good thing).
962            my $error = $@;
963            Trace("Feed Error: $error") if T(Feed => 0);
964        }
965        # Be sure to unlock.
966        if ($lock) {
967            flock $lock, LOCK_UN;
968            undef $lock;
969        }
970        # Restore the error message.
971        $@ = $savedError;
972    }
973    
974    
975    
976    
977    =head3 Assert
978    
979        Assert($condition1, $condition2, ... $conditionN);
980    
981    Return TRUE if all the conditions are true. This method can be used in conjunction with
982    the OR operator and the L</Confess> method as a debugging assert.
983    So, for example
984    
985        Assert($recNum >= 0) || Confess("Invalid record number $recNum.");
986    
987    Will abort the program with a stack trace if the value of C<$recNum> is negative.
988    
989    =cut
990    sub Assert {
991        my $retVal = 1;
992        LOOP: for my $condition (@_) {
993            if (! $condition) {
994                $retVal = 0;
995                last LOOP;
996            }
997        }
998        return $retVal;
999    }
1000    
1001    =head3 Cluck
1002    
1003        Cluck($message);
1004    
1005    Trace the call stack. Note that for best results, you should qualify the call with a
1006    trace condition. For example,
1007    
1008        Cluck("Starting record parse.") if T(3);
1009    
1010    will only trace the stack if the trace level for the package is 3 or more.
1011    
1012    =over 4
1013    
1014    =item message
1015    
1016    Message to include in the trace.
1017    
1018    =back
1019    
1020    =cut
1021    
1022    sub Cluck {
1023        # Get the parameters.
1024        my ($message) = @_;
1025        # Trace what's happening.
1026        Trace("Stack trace for event: $message");
1027        # Get the stack trace.
1028        my @trace = LongMess();
1029        # Convert the trace to a series of messages.
1030        for my $line (@trace) {
1031            # Replace the tab at the beginning with spaces.
1032            $line =~ s/^\t/    /;
1033            # Trace the line.
1034            Trace($line);
1035        }
1036        # Issue a warning. This displays the event message and inserts it into the RSS error feed.
1037        Warn($message);
1038    }
1039    
1040    =head3 LongMess
1041    
1042        my @lines = Tracer::LongMess();
1043    
1044    Return a stack trace with all tracing methods removed. The return will be in the form of a list
1045    of message strings.
1046    
1047    =cut
1048    
1049    sub LongMess {
1050        # Declare the return variable.
1051        my @retVal = ();
1052        my $confession = longmess("");
1053        for my $line (split /\s*\n/, $confession) {
1054            unless ($line =~ /Tracer\.pm/) {
1055                # Here we have a line worth keeping. Push it onto the result list.
1056                push @retVal, $line;
1057            }
1058        }
1059        # Return the result.
1060        return @retVal;
1061    }
1062    
1063    =head3 ETracing
1064    
1065        ETracing($parameter, %options);
1066    
1067    Set up emergency tracing. Emergency tracing is tracing that is turned
1068    on automatically for any program that calls this method. The emergency
1069    tracing parameters are stored in a a file identified by a tracing key.
1070    If this method is called with a CGI object, then the tracing key is
1071    taken from a cookie. If it is called with no parameters, then the tracing
1072    key is taken from an environment variable. If it is called with a string,
1073    the tracing key is that string.
1074    
1075    =over 4
1076    
1077    =item parameter
1078    
1079    A parameter from which the tracing key is computed. If it is a scalar,
1080    that scalar is used as the tracing key. If it is a CGI object, the
1081    tracing key is taken from the C<IP> cookie. If it is omitted, the
1082    tracing key is taken from the C<TRACING> environment variable. If it
1083    is a CGI object and emergency tracing is not on, the C<Trace> and
1084    C<TF> parameters will be used to determine the type of tracing.
1085    
1086    =item options
1087    
1088    Hash of options. The permissible options are given below.
1089    
1090    =over 8
1091    
1092    =item destType
1093    
1094    Emergency tracing destination type to use if no tracing file is found. The
1095    default is C<WARN>.
1096    
1097    =item noParms
1098    
1099    If TRUE, then display of the saved CGI parms is suppressed. The default is FALSE.
1100    
1101    =item level
1102    
1103    The trace level to use if no tracing file is found. The default is C<0>.
1104    
1105    =back
1106    
1107    =cut
1108    
1109    sub ETracing {
1110        # Get the parameter.
1111        my ($parameter, %options) = @_;
1112        # Check for CGI mode.
1113        if (defined $parameter && ref $parameter eq 'CGI') {
1114            $SavedCGI = $parameter;
1115        } else {
1116            $SavedCGI = undef;
1117        }
1118        # Check for the noParms option.
1119        my $noParms = $options{noParms} || 0;
1120        # Get the default tracing information.
1121        my $tracing = $options{level} || 0;
1122        my $dest = $options{destType} || "WARN";
1123        # Check for emergency tracing.
1124        my $tkey = EmergencyKey($parameter);
1125        my $emergencyFile = EmergencyFileName($tkey);
1126        if (-e $emergencyFile && (my $stat = stat($emergencyFile))) {
1127            # We have the file. Read in the data.
1128            my @tracing = GetFile($emergencyFile);
1129            # Pull off the time limit.
1130            my $expire = shift @tracing;
1131            # Convert it to seconds.
1132            $expire *= 3600;
1133            # Check the file data.
1134            my ($now) = gettimeofday;
1135            if ($now - $stat->mtime <= $expire) {
1136                # Emergency tracing is on. Pull off the destination and
1137                # the trace level;
1138                $dest = shift @tracing;
1139                my $level = shift @tracing;
1140                # Insure Tracer is specified.
1141                my %moduleHash = map { $_ => 1 } @tracing;
1142                $moduleHash{Tracer} = 1;
1143                # Set the trace parameter.
1144                $tracing = join(" ", $level, sort keys %moduleHash);
1145            }
1146        }
1147        # Convert the destination to a real tracing destination.
1148        $dest = EmergencyTracingDest($tkey, $dest);
1149        # Setup the tracing we've determined from all the stuff above.
1150        TSetup($tracing, $dest);
1151        # Check to see if we're a web script.
1152        if (defined $SavedCGI) {
1153            # Yes we are. Trace the form and environment data if it's not suppressed.
1154            if (! $noParms) {
1155                TraceParms($SavedCGI);
1156            }
1157            # Check for RAW mode. In raw mode, we print a fake header so that we see everything
1158            # emitted by the script in its raw form.
1159            if (T(Raw => 3)) {
1160                print CGI::header(-type => 'text/plain', -tracing => 'Raw');
1161            }
1162        }
1163    }
1164    
1165    =head3 EmergencyFileName
1166    
1167        my $fileName = Tracer::EmergencyFileName($tkey);
1168    
1169    Return the emergency tracing file name. This is the file that specifies
1170    the tracing information.
1171    
1172    =over 4
1173    
1174    =item tkey
1175    
1176    Tracing key for the current program.
1177    
1178    =item RETURN
1179    
1180    Returns the name of the file to contain the emergency tracing information.
1181    
1182    =back
1183    
1184    =cut
1185    
1186    sub EmergencyFileName {
1187        # Get the parameters.
1188        my ($tkey) = @_;
1189        # Compute the emergency tracing file name.
1190        return "$FIG_Config::temp/Emergency$tkey.txt";
1191    }
1192    
1193    =head3 EmergencyFileTarget
1194    
1195        my $fileName = Tracer::EmergencyFileTarget($tkey);
1196    
1197    Return the emergency tracing target file name. This is the file that receives
1198    the tracing output for file-based tracing.
1199    
1200    =over 4
1201    
1202    =item tkey
1203    
1204    Tracing key for the current program.
1205    
1206    =item RETURN
1207    
1208    Returns the name of the file to contain the trace output.
1209    
1210    =back
1211    
1212    =cut
1213    
1214    sub EmergencyFileTarget {
1215        # Get the parameters.
1216        my ($tkey) = @_;
1217        # Compute the emergency tracing file name.
1218        return "$FIG_Config::temp/trace$tkey.log";
1219    }
1220    
1221    =head3 EmergencyTracingDest
1222    
1223        my $dest = Tracer::EmergencyTracingDest($tkey, $myDest);
1224    
1225    This method converts an emergency tracing destination to a real
1226    tracing destination. The main difference is that if the
1227    destination is C<FILE> or C<APPEND>, we convert it to file
1228    output. If the destination is C<DUAL>, we convert it to file
1229    and standard output.
1230    
1231    =over 4
1232    
1233    =item tkey
1234    
1235    Tracing key for this environment.
1236    
1237    =item myDest
1238    
1239    Destination from the emergency tracing file.
1240    
1241    =item RETURN
1242    
1243    Returns a destination that can be passed into L</TSetup>.
1244    
1245    =back
1246    
1247    =cut
1248    
1249    sub EmergencyTracingDest {
1250        # Get the parameters.
1251        my ($tkey, $myDest) = @_;
1252        # Declare the return variable.
1253        my $retVal = $myDest;
1254        # Process according to the destination value.
1255        if ($myDest eq 'FILE') {
1256            $retVal = ">" . EmergencyFileTarget($tkey);
1257        } elsif ($myDest eq 'APPEND') {
1258            $retVal = ">>" . EmergencyFileTarget($tkey);
1259        } elsif ($myDest eq 'DUAL') {
1260            $retVal = "+>" . EmergencyFileTarget($tkey);
1261        } elsif ($myDest eq 'WARN') {
1262            $retVal = "WARN";
1263        }
1264        # Return the result.
1265        return $retVal;
1266    }
1267    
1268    =head3 Emergency
1269    
1270        Emergency($key, $hours, $dest, $level, @modules);
1271    
1272    Turn on emergency tracing. This method is normally invoked over the web from
1273    a debugging console, but it can also be called by the C<trace.pl> script.
1274    The caller specifies the duration of the emergency in hours, the desired tracing
1275    destination, the trace level, and a list of the trace modules to activate.
1276    For the length of the duration, when a program in an environment with the
1277    specified tracing key active invokes a Sprout CGI script, tracing will be
1278    turned on automatically. See L</TSetup> for more about tracing setup and
1279    L</ETracing> for more about emergency tracing.
1280    
1281    =over 4
1282    
1283    =item tkey
1284    
1285    The tracing key. This is used to identify the control file and the trace file.
1286    
1287    =item hours
1288    
1289    Number of hours to keep emergency tracing alive.
1290    
1291    =item dest
1292    
1293    Tracing destination. If no path information is specified for a file
1294    destination, it is put in the FIG temporary directory.
1295    
1296    =item level
1297    
1298    Tracing level. A higher level means more trace messages.
1299    
1300    =item modules
1301    
1302    A list of the tracing modules to activate.
1303    
1304    =back
1305    
1306    =cut
1307    
1308    sub Emergency {
1309        # Get the parameters.
1310        my ($tkey, $hours, $dest, $level, @modules) = @_;
1311        # Create the emergency file.
1312        my $specFile = EmergencyFileName($tkey);
1313        my $outHandle = Open(undef, ">$specFile");
1314        print $outHandle join("\n", $hours, $dest, $level, @modules, "");
1315    }
1316    
1317    =head3 EmergencyKey
1318    
1319        my $tkey = EmergencyKey($parameter);
1320    
1321    Return the Key to be used for emergency tracing. This could be an IP address,
1322     a session ID, or a user name, depending on the environment.
1323    
1324    =over 4
1325    
1326    =item parameter
1327    
1328    Parameter defining the method for finding the tracing key. If it is a scalar,
1329    then it is presumed to be the tracing key itself. If it is a CGI object, then
1330    the tracing key is taken from the C<IP> cookie. Otherwise, the tracing key is
1331    taken from the C<TRACING> environment variable.
1332    
1333    =item RETURN
1334    
1335    Returns the key to be used for labels in emergency tracing.
1336    
1337    =back
1338    
1339    =cut
1340    
1341    sub EmergencyKey {
1342        # Get the parameters.
1343        my ($parameter) = @_;
1344        # Declare the return variable.
1345        my $retVal;
1346        # Determine the parameter type.
1347        if (! defined $parameter) {
1348            # Here we're supposed to check the environment. If that fails, we
1349            # get the effective login ID.
1350            $retVal = $ENV{TRACING} || scalar getpwuid($<);
1351        } else {
1352            my $ptype = ref $parameter;
1353            if ($ptype eq 'CGI') {
1354                # Here we were invoked from a web page. Look for a cookie.
1355                $retVal = $parameter->cookie('IP');
1356            } elsif (! $ptype) {
1357                # Here the key was passed in.
1358                $retVal = $parameter;
1359            }
1360        }
1361        # If no luck finding a key, use the PID.
1362        if (! defined $retVal) {
1363            $retVal = $$;
1364        }
1365        # Return the result.
1366        return $retVal;
1367    }
1368    
1369    
1370    =head3 TraceParms
1371    
1372        Tracer::TraceParms($cgi);
1373    
1374    Trace the CGI parameters at trace level CGI => 3 and the environment variables
1375    at level CGI => 4. A self-referencing URL is traced at level CGI => 2.
1376    
1377    =over 4
1378    
1379    =item cgi
1380    
1381    CGI query object containing the parameters to trace.
1382    
1383    =back
1384    
1385    =cut
1386    
1387    sub TraceParms {
1388        # Get the parameters.
1389        my ($cgi) = @_;
1390        if (T(CGI => 2)) {
1391            # Here we trace the GET-style URL for the script, but only if it's
1392            # relatively small.
1393            my $url = $cgi->url(-relative => 1, -query => 1);
1394            my $len = length($url);
1395            if ($len < 500) {
1396                Trace("[URL] $url");
1397            } elsif ($len > 2048) {
1398                Trace("[URL] URL is too long to use with GET ($len characters).");
1399            } else {
1400                Trace("[URL] URL length is $len characters.");
1401            }
1402        }
1403        if (T(CGI => 3)) {
1404            # Here we want to trace the parameter data.
1405            my @names = $cgi->param;
1406            for my $parmName (sort @names) {
1407                # Note we skip the Trace parameters, which are for our use only.
1408                if ($parmName ne 'Trace' && $parmName ne 'TF') {
1409                    my @values = $cgi->param($parmName);
1410                    Trace("[CGI] $parmName = " . join(", ", @values));
1411                }
1412            }
1413            # Display the request method.
1414            my $method = $cgi->request_method();
1415            Trace("Method: $method");
1416        }
1417        if (T(CGI => 4)) {
1418            # Here we want the environment data too.
1419            for my $envName (sort keys %ENV) {
1420                Trace("[ENV] $envName = $ENV{$envName}");
1421            }
1422        }
1423    }
1424    
1425    =head3 TraceImages
1426    
1427        Tracer::TraceImages($htmlString);
1428    
1429    Trace information about all of an html document's images. The tracing
1430    will be for type "IMG" at level 3. The image's source string
1431    will be displayed. This is generally either the URL of the image or
1432    raw data for the image itself. If the source is too long, only the first 300
1433    characters will be shown at trace level 3. The entire source will be shown,
1434    however, at trace level 4. This method is not very smart, and might catch
1435    Javascript code, but it is still useful when debugging the arcane
1436    behavior of images in multiple browser environments.
1437    
1438    =over 4
1439    
1440    =item htmlString
1441    
1442    HTML text for an outgoing web page.
1443    
1444    =back
1445    
1446    =cut
1447    
1448    sub TraceImages {
1449        # Only proceed if we're at the proper trace level.
1450        if (T(IMG => 3)) {
1451            # For performance reasons we're manipulating $_[0] instead of retrieving the string
1452            # into a variable called "$htmlString". This is because we expect html strings to be
1453            # long, and don't want to copy them any more than we have to.
1454            Trace(length($_[0]) . " characters in web page.");
1455            # Loop through the HTML, culling image tags.
1456            while ($_[0] =~ /<img\s+[^>]+?src="([^"]+)"/sgi) {
1457                # Extract the source string and determine whether or not it's too long.
1458                my $srcString = $1;
1459                my $pos = pos($_[0]) - length($srcString);
1460                my $excess = length($srcString) - 300;
1461                # We'll put the display string in here.
1462                my $srcDisplay = $srcString;
1463                # If it's a data string, split it at the comma.
1464                $srcDisplay =~ s/^(data[^,]+,)/$1\n/;
1465                # If there's no excess or we're at trace level 4, we're done. At level 3 with
1466                # a long string, however, we only show the first 300 characters.
1467                if ($excess > 0 && ! T(IMG => 4)) {
1468                    $srcDisplay = substr($srcDisplay,0,300) . "\nplus $excess characters.";
1469                }
1470                # Output the trace message.
1471                Trace("Image tag at position $pos:\n$srcDisplay");
1472            }
1473        }
1474    }
1475    
1476    =head2 Command-Line Utility Methods
1477    
1478    =head3 SendSMS
1479    
1480        my $msgID = Tracer::SendSMS($phoneNumber, $msg);
1481    
1482    Send a text message to a phone number using Clickatell. The FIG_Config file must contain the
1483    user name, password, and API ID for the relevant account in the hash reference variable
1484    I<$FIG_Config::phone>, using the keys C<user>, C<password>, and C<api_id>. For
1485    example, if the user name is C<BruceTheHumanPet>, the password is C<silly>, and the API ID
1486    is C<2561022>, then the FIG_Config file must contain
1487    
1488        $phone =  { user => 'BruceTheHumanPet',
1489                    password => 'silly',
1490                    api_id => '2561022' };
1491    
1492    The original purpose of this method was to insure Bruce would be notified immediately when the
1493    Sprout Load terminates. Care should be taken if you do not wish Bruce to be notified immediately
1494    when you call this method.
1495    
1496    The message ID will be returned if successful, and C<undef> if an error occurs.
1497    
1498    =over 4
1499    
1500    =item phoneNumber
1501    
1502    Phone number to receive the message, in international format. A United States phone number
1503    would be prefixed by "1". A British phone number would be prefixed by "44".
1504    
1505    =item msg
1506    
1507    Message to send to the specified phone.
1508    
1509    =item RETURN
1510    
1511    Returns the message ID if successful, and C<undef> if the message could not be sent.
1512    
1513    =back
1514    
1515    =cut
1516    
1517    sub SendSMS {
1518        # Get the parameters.
1519        my ($phoneNumber, $msg) = @_;
1520        # Declare the return variable. If we do not change it, C<undef> will be returned.
1521        my $retVal;
1522        # Only proceed if we have phone support.
1523        if (! defined $FIG_Config::phone) {
1524            Trace("Phone support not present in FIG_Config.") if T(1);
1525        } else {
1526            # Get the phone data.
1527            my $parms = $FIG_Config::phone;
1528            # Get the Clickatell URL.
1529            my $url = "http://api.clickatell.com/http/";
1530            # Create the user agent.
1531            my $ua = LWP::UserAgent->new;
1532            # Request a Clickatell session.
1533            my $resp = $ua->post("$url/sendmsg", { user => $parms->{user},
1534                                         password => $parms->{password},
1535                                         api_id => $parms->{api_id},
1536                                         to => $phoneNumber,
1537                                         text => $msg});
1538            # Check for an error.
1539            if (! $resp->is_success) {
1540                Trace("Alert failed.") if T(1);
1541            } else {
1542                # Get the message ID.
1543                my $rstring = $resp->content;
1544                if ($rstring =~ /^ID:\s+(.*)$/) {
1545                    $retVal = $1;
1546                } else {
1547                    Trace("Phone attempt failed with $rstring") if T(1);
1548                }
1549            }
1550        }
1551        # Return the result.
1552        return $retVal;
1553    }
1554    
1555    =head3 StandardSetup
1556    
1557        my ($options, @parameters) = StandardSetup(\@categories, \%options, $parmHelp, @ARGV);
1558    
1559    This method performs standard command-line parsing and tracing setup. The return
1560    values are a hash of the command-line options and a list of the positional
1561    parameters. Tracing is automatically set up and the command-line options are
1562    validated.
1563    
1564    This is a complex method that does a lot of grunt work. The parameters can
1565    be more easily understood, however, once they are examined individually.
1566    
1567    The I<categories> parameter is the most obtuse. It is a reference to a list of
1568    special-purpose tracing categories. Most tracing categories are PERL package
1569    names. So, for example, if you wanted to turn on tracing inside the B<Sprout>,
1570    B<ERDB>, and B<SproutLoad> packages, you would specify the categories
1571    
1572        ["Sprout", "SproutLoad", "ERDB"]
1573    
1574    This would cause trace messages in the specified three packages to appear in
1575    the output. There are two special tracing categories that are automatically
1576    handled by this method. In other words, if you used L</TSetup> you would need
1577    to include these categories manually, but if you use this method they are turned
1578    on automatically.
1579    
1580    =over 4
1581    
1582    =item SQL
1583    
1584    Traces SQL commands and activity.
1585    
1586    =item Tracer
1587    
1588    Traces error messages and call stacks.
1589    
1590    =back
1591    
1592    C<SQL> is only turned on if the C<-sql> option is specified in the command line.
1593    The trace level is specified using the C<-trace> command-line option. For example,
1594    the following command line for C<TransactFeatures> turns on SQL tracing and runs
1595    all tracing at level 3.
1596    
1597        TransactFeatures -trace=3 -sql register ../xacts IDs.tbl
1598    
1599    Standard tracing is output to the standard output and echoed to the file
1600    C<trace>I<$$>C<.log> in the FIG temporary directory, where I<$$> is the
1601    process ID. You can also specify the C<user> parameter to put a user ID
1602    instead of a process ID in the trace file name. So, for example
1603    
1604    The default trace level is 2. To get all messages, specify a trace level of 4.
1605    For a genome-by-genome update, use 3.
1606    
1607        TransactFeatures -trace=3 -sql -user=Bruce register ../xacts IDs.tbl
1608    
1609    would send the trace output to C<traceBruce.log> in the temporary directory.
1610    
1611    The I<options> parameter is a reference to a hash containing the command-line
1612    options, their default values, and an explanation of what they mean. Command-line
1613    options may be in the form of switches or keywords. In the case of a switch, the
1614    option value is 1 if it is specified and 0 if it is not specified. In the case
1615    of a keyword, the value is separated from the option name by an equal sign. You
1616    can see this last in the command-line example above.
1617    
1618    You can specify a different default trace level by setting C<$options->{trace}>
1619    prior to calling this method.
1620    
1621    An example at this point would help. Consider, for example, the command-line utility
1622    C<TransactFeatures>. It accepts a list of positional parameters plus the options
1623    C<safe>, C<noAlias>, C<start>, and C<tblFiles>. To start up this command, we execute
1624    the following code.
1625    
1626        my ($options, @parameters) = Tracer::StandardSetup(["DocUtils"],
1627                            { safe => [0, "use database transactions"],
1628                              noAlias => [0, "do not expect aliases in CHANGE transactions"],
1629                              start => [' ', "start with this genome"],
1630                              tblFiles => [0, "output TBL files containing the corrected IDs"] },
1631                            "<command> <transactionDirectory> <IDfile>",
1632                          @ARGV);
1633    
1634    
1635    The call to C<ParseCommand> specifies the default values for the options and
1636    stores the actual options in a hash that is returned as C<$options>. The
1637    positional parameters are returned in C<@parameters>.
1638    
1639    The following is a sample command line for C<TransactFeatures>.
1640    
1641        TransactFeatures -trace=2 -noAlias register ../xacts IDs.tbl
1642    
1643    Single and double hyphens are equivalent. So, you could also code the
1644    above command as
1645    
1646        TransactFeatures --trace=2 --noAlias register ../xacts IDs.tbl
1647    
1648    In this case, C<register>, C<../xacts>, and C<IDs.tbl> are the positional
1649    parameters, and would find themselves in I<@parameters> after executing the
1650    above code fragment. The tracing would be set to level 2, and the categories
1651    would be C<Tracer>, and <DocUtils>. C<Tracer> is standard,
1652    and C<DocUtils> was included because it came in within the first parameter
1653    to this method. The I<$options> hash would be
1654    
1655        { trace => 2, sql => 0, safe => 0,
1656          noAlias => 1, start => ' ', tblFiles => 0 }
1657    
1658    Use of C<StandardSetup> in this way provides a simple way of performing
1659    standard tracing setup and command-line parsing. Note that the caller is
1660    not even aware of the command-line switches C<-trace> and C<-sql>, which
1661    are used by this method to control the tracing. If additional tracing features
1662    need to be added in the future, they can be processed by this method without
1663    upsetting the command-line utilities.
1664    
1665    If the C<background> option is specified on the command line, then the
1666    standard and error outputs will be directed to files in the temporary
1667    directory, using the same suffix as the trace file. So, if the command
1668    line specified
1669    
1670        -user=Bruce -background
1671    
1672    then the trace output would go to C<traceBruce.log>, the standard output to
1673    C<outBruce.log>, and the error output to C<errBruce.log>. This is designed to
1674    simplify starting a command in the background.
1675    
1676    The user name is also used as the tracing key for L</Emergency Tracing>.
1677    Specifying a value of C<E> for the trace level causes emergency tracing to
1678    be used instead of custom tracing. If the user name is not specified,
1679    the tracing key is taken from the C<Tracing> environment variable. If there
1680    is no value for that variable, the tracing key will be computed from the active
1681    login ID.
1682    
1683    Since the default situation in StandardSetup is to trace to the standard
1684    output, errors that occur in command-line scripts will not generate
1685    RSS events. To force the events, use the C<warn> option.
1686    
1687        TransactFeatures -background -warn register ../xacts IDs.tbl
1688    
1689    Finally, if the special option C<-help> is specified, the option
1690    names will be traced at level 0 and the program will exit without processing.
1691    This provides a limited help capability. For example, if the user enters
1692    
1693        TransactFeatures -help
1694    
1695    he would see the following output.
1696    
1697        TransactFeatures [options] <command> <transactionDirectory> <IDfile>
1698            -trace    tracing level (default E)
1699            -sql      trace SQL commands
1700            -safe     use database transactions
1701            -noAlias  do not expect aliases in CHANGE transactions
1702            -start    start with this genome
1703            -tblFiles output TBL files containing the corrected IDs
1704            -forked   do not erase the trace file before tracing
1705    
1706    The caller has the option of modifying the tracing scheme by placing a value
1707    for C<trace> in the incoming options hash. The default value can be overridden,
1708    or the tracing to the standard output can be turned off by suffixing a minus
1709    sign to the trace level. So, for example,
1710    
1711        { trace => [0, "tracing level (default 0)"],
1712           ...
1713    
1714    would set the default trace level to 0 instead of E, while
1715    
1716        { trace => ["2-", "tracing level (default 2)"],
1717           ...
1718    
1719    would set the default to 2, but trace only to the log file, not to the
1720    standard output.
1721    
1722    The parameters to this method are as follows.
1723    
1724    =over 4
1725    
1726    =item categories
1727    
1728    Reference to a list of tracing category names. These should be names of
1729    packages whose internal workings will need to be debugged to get the
1730    command working.
1731    
1732    =item options
1733    
1734    Reference to a hash containing the legal options for the current command mapped
1735    to their default values and descriptions. The user can override the defaults
1736    by specifying the options as command-line switches prefixed by a hyphen.
1737    Tracing-related options may be added to this hash. If the C<-h> option is
1738    specified on the command line, the option descriptions will be used to
1739    explain the options. To turn off tracing to the standard output, add a
1740    minus sign to the value for C<trace> (see above).
1741    
1742    =item parmHelp
1743    
1744    A string that vaguely describes the positional parameters. This is used
1745    if the user specifies the C<-h> option.
1746    
1747    =item argv
1748    
1749    List of command line parameters, including the option switches, which must
1750    precede the positional parameters and be prefixed by a hyphen.
1751    
1752    =item RETURN
1753    
1754    Returns a list. The first element of the list is the reference to a hash that
1755    maps the command-line option switches to their values. These will either be the
1756    default values or overrides specified on the command line. The remaining
1757    elements of the list are the position parameters, in order.
1758    
1759    =back
1760    
1761    =cut
1762    
1763    sub StandardSetup {
1764        # Get the parameters.
1765        my ($categories, $options, $parmHelp, @argv) = @_;
1766        # Get the default tracing key.
1767        my $tkey = EmergencyKey();
1768        # Save the command line.
1769        $CommandLine = join(" ", $0, map { $_ =~ /\s/ ? "\"$_\"" : $_ } @argv);
1770        # Add the tracing options.
1771        if (! exists $options->{trace}) {
1772            $options->{trace} = ['2', "tracing level (E for emergency tracing)"];
1773        }
1774        if (! exists $options->{forked}) {
1775            $options->{forked} = [0, "keep old trace file"];
1776        }
1777        $options->{sql} = [0, "turn on SQL tracing"];
1778        $options->{help} = [0, "display command-line options"];
1779        $options->{user} = [$tkey, "tracing key"];
1780        $options->{background} = [0, "spool standard and error output"];
1781        $options->{warn} = [0, "send errors to RSS feed"];
1782        $options->{moreTracing} = ["", "comma-delimited list of additional trace modules for debugging"];
1783        # Create a parsing hash from the options hash. The parsing hash
1784        # contains the default values rather than the default value
1785        # and the description. While we're at it, we'll memorize the
1786        # length of the longest option name.
1787        my $longestName = 0;
1788        my %parseOptions = ();
1789        for my $key (keys %{$options}) {
1790            if (length $key > $longestName) {
1791                $longestName = length $key;
1792            }
1793            $parseOptions{$key} = $options->{$key}->[0];
1794        }
1795        # Parse the command line.
1796        my ($retOptions, @retParameters) = ParseCommand(\%parseOptions, @argv);
1797        # Get the logfile suffix.
1798        my $suffix = $retOptions->{user};
1799        # We'll put the trace file name in here. We need it later if background
1800        # mode is on.
1801        my $traceFileName;
1802        # Now we want to set up tracing. First, we need to know if the user
1803        # wants emergency tracing.
1804        if ($retOptions->{trace} eq 'E') {
1805            ETracing($retOptions->{user});
1806        } else {
1807            # Here the tracing is controlled from the command line.
1808            my @cats = @{$categories};
1809            if ($retOptions->{sql}) {
1810                push @cats, "SQL";
1811            }
1812            if ($retOptions->{warn}) {
1813                push @cats, "Feed";
1814            }
1815            # Add the default categories.
1816            push @cats, "Tracer";
1817            # Check for more tracing groups.
1818            if ($retOptions->{moreTracing}) {
1819                push @cats, split /,/, $retOptions->{moreTracing};
1820            }
1821            # Next, we create the category string by joining the categories.
1822            my $cats = join(" ", @cats);
1823            # Check to determine whether or not the caller wants to turn off tracing
1824            # to the standard output.
1825            my $traceLevel = $retOptions->{trace};
1826            my $textOKFlag = 1;
1827            if ($traceLevel =~ /^(.)-/) {
1828                $traceLevel = $1;
1829                $textOKFlag = 0;
1830            }
1831            # Now we set up the trace mode.
1832            my $traceMode;
1833            # Verify that we can open a file in the FIG temporary directory.
1834            my $traceFileName = "$FIG_Config::temp/trace$suffix.log";
1835            my $traceFileSpec = ($retOptions->{forked} ? ">>$traceFileName" : ">$traceFileName");
1836            if (open TESTTRACE, "$traceFileSpec") {
1837                # Here we can trace to a file.
1838                $traceMode = ">>$traceFileName";
1839                if ($textOKFlag) {
1840                    # Echo to standard output if the text-OK flag is set.
1841                    $traceMode = "+$traceMode";
1842                }
1843                # Close the test file.
1844                close TESTTRACE;
1845            } else {
1846                # Here we can't trace to a file. Complain about this.
1847                warn "Could not open trace file $traceFileName: $!\n";
1848                # We trace to the standard output if it's
1849                # okay, and the error log otherwise.
1850                if ($textOKFlag) {
1851                    $traceMode = "TEXT";
1852                } else {
1853                    $traceMode = "WARN";
1854                }
1855            }
1856            # Now set up the tracing.
1857            TSetup("$traceLevel $cats", $traceMode);
1858        }
1859        # Check for background mode.
1860        if ($retOptions->{background}) {
1861            my $outFileName = "$FIG_Config::temp/out$suffix$$.log";
1862            my $errFileName = "$FIG_Config::temp/err$suffix$$.log";
1863            # Spool the output.
1864            open STDOUT, ">$outFileName";
1865            # If we have a trace file, trace the errors to the log. Otherwise,
1866            # spool the errors.
1867            if (defined $traceFileName) {
1868                open STDERR, "| Tracer $traceFileName";
1869            } else {
1870                open STDERR, ">$errFileName";
1871            }
1872            # Check for phone support. If we have phone support and a phone number,
1873            # we want to turn it on.
1874            if ($ENV{PHONE} && defined($FIG_Config::phone)) {
1875                $retOptions->{phone} = $ENV{PHONE};
1876            }
1877        }
1878        # Check for the "help" option. If it is specified, dump the command-line
1879        # options and exit the program.
1880        if ($retOptions->{help}) {
1881            $0 =~ m#[/\\](\w+)(\.pl)?$#i;
1882            print "$1 [options] $parmHelp\n";
1883            for my $key (sort keys %{$options}) {
1884                my $name = Pad($key, $longestName, 0, ' ');
1885                my $desc = $options->{$key}->[1];
1886                if ($options->{$key}->[0]) {
1887                    $desc .= " (default " . $options->{$key}->[0] . ")";
1888                }
1889                print "  $name $desc\n";
1890            }
1891            exit(0);
1892        }
1893        # Trace the options, if applicable.
1894        if (T(3)) {
1895            my @parms = grep { $retOptions->{$_} } keys %{$retOptions};
1896            Trace("Selected options: " . join(", ", sort @parms) . ".");
1897        }
1898        # Return the parsed parameters.
1899        return ($retOptions, @retParameters);
1900    }
1901    
1902    =head3 ReadOptions
1903    
1904        my %options = Tracer::ReadOptions($fileName);
1905    
1906    Read a set of options from a file. Each option is encoded in a line of text that has the
1907    format
1908    
1909    I<optionName>C<=>I<optionValue>C<; >I<comment>
1910    
1911    The option name must consist entirely of letters, digits, and the punctuation characters
1912    C<.> and C<_>, and is case sensitive. Blank lines and lines in which the first nonblank
1913    character is a semi-colon will be ignored. The return hash will map each option name to
1914    the corresponding option value.
1915    
1916    =over 4
1917    
1918    =item fileName
1919    
1920    Name of the file containing the option data.
1921    
1922    =item RETURN
1923    
1924    Returns a hash mapping the option names specified in the file to their corresponding option
1925    value.
1926    
1927    =back
1928    
1929    =cut
1930    
1931    sub ReadOptions {
1932        # Get the parameters.
1933        my ($fileName) = @_;
1934        # Open the file.
1935        (open CONFIGFILE, "<$fileName") || Confess("Could not open option file $fileName.");
1936        # Count the number of records read.
1937        my ($records, $comments) = 0;
1938        # Create the return hash.
1939        my %retVal = ();
1940        # Loop through the file, accumulating key-value pairs.
1941        while (my $line = <CONFIGFILE>) {
1942            # Denote we've read a line.
1943            $records++;
1944            # Determine the line type.
1945            if ($line =~ /^\s*[\n\r]/) {
1946                # A blank line is a comment.
1947                $comments++;
1948            } elsif ($line =~ /^\s*([A-Za-z0-9_\.]+)=([^;]*);/) {
1949                # Here we have an option assignment.
1950                retVal{$1} = $2;
1951            } elsif ($line =~ /^\s*;/) {
1952                # Here we have a text comment.
1953                $comments++;
1954            } else {
1955                # Here we have an invalid line.
1956                Trace("Invalid option statement in record $records.") if T(0);
1957            }
1958        }
1959        # Return the hash created.
1960        return %retVal;
1961    }
1962    
1963    =head3 GetOptions
1964    
1965        Tracer::GetOptions(\%defaults, \%options);
1966    
1967    Merge a specified set of options into a table of defaults. This method takes two hash references
1968    as input and uses the data from the second to update the first. If the second does not exist,
1969    there will be no effect. An error will be thrown if one of the entries in the second hash does not
1970    exist in the first.
1971    
1972    Consider the following example.
1973    
1974        my $optionTable = GetOptions({ dbType => 'mySQL', trace => 0 }, $options);
1975    
1976    In this example, the variable B<$options> is expected to contain at most two options-- B<dbType> and
1977    B<trace>. The default database type is C<mySQL> and the default trace level is C<0>. If the value of
1978    B<$options> is C<< {dbType => 'Oracle'} >>, then the database type will be changed to C<Oracle> and
1979    the trace level will remain at 0. If B<$options> is undefined, then the database type and trace level
1980    will remain C<mySQL> and C<0>. If, on the other hand, B<$options> is defined as
1981    
1982        {databaseType => 'Oracle'}
1983    
1984    an error will occur because the B<databaseType> option does not exist.
1985    
1986    =over 4
1987    
1988    =item defaults
1989    
1990    Table of default option values.
1991    
1992    =item options
1993    
1994    Table of overrides, if any.
1995    
1996    =item RETURN
1997    
1998    Returns a reference to the default table passed in as the first parameter.
1999    
2000    =back
2001    
2002    =cut
2003    
2004    sub GetOptions {
2005        # Get the parameters.
2006        my ($defaults, $options) = @_;
2007        # Check for overrides.
2008        if ($options) {
2009            # Loop through the overrides.
2010            while (my ($option, $setting) = each %{$options}) {
2011                # Insure this override exists.
2012                if (!exists $defaults->{$option}) {
2013                    croak "Unrecognized option $option encountered.";
2014                } else {
2015                    # Apply the override.
2016                    $defaults->{$option} = $setting;
2017                }
2018            }
2019        }
2020        # Return the merged table.
2021        return $defaults;
2022    }
2023    
2024    =head3 MergeOptions
2025    
2026        Tracer::MergeOptions(\%table, \%defaults);
2027    
2028    Merge default values into a hash table. This method looks at the key-value pairs in the
2029    second (default) hash, and if a matching key is not found in the first hash, the default
2030    pair is copied in. The process is similar to L</GetOptions>, but there is no error-
2031    checking and no return value.
2032    
2033    =over 4
2034    
2035    =item table
2036    
2037    Hash table to be updated with the default values.
2038    
2039    =item defaults
2040    
2041    Default values to be merged into the first hash table if they are not already present.
2042    
2043    =back
2044    
2045    =cut
2046    
2047    sub MergeOptions {
2048        # Get the parameters.
2049        my ($table, $defaults) = @_;
2050        # Loop through the defaults.
2051        while (my ($key, $value) = each %{$defaults}) {
2052            if (!exists $table->{$key}) {
2053                $table->{$key} = $value;
2054            }
2055        }
2056    }
2057    
2058    =head3 UnparseOptions
2059    
2060        my $optionString = Tracer::UnparseOptions(\%options);
2061    
2062    Convert an option hash into a command-line string. This will not
2063    necessarily be the same text that came in, but it will nonetheless
2064    produce the same ultimate result when parsed by L</StandardSetup>.
2065    
2066    =over 4
2067    
2068    =item options
2069    
2070    Reference to a hash of options to convert into an option string.
2071    
2072    =item RETURN
2073    
2074    Returns a string that will parse to the same set of options when
2075    parsed by L</StandardSetup>.
2076    
2077    =back
2078    
2079    =cut
2080    
2081    sub UnparseOptions {
2082        # Get the parameters.
2083        my ($options) = @_;
2084        # The option segments will be put in here.
2085        my @retVal = ();
2086        # Loop through the options.
2087        for my $key (keys %$options) {
2088            # Get the option value.
2089            my $value = $options->{$key};
2090            # Only use it if it's nonempty.
2091            if (defined $value && $value ne "") {
2092                my $segment = "--$key=$value";
2093                # Quote it if necessary.
2094                if ($segment =~ /[ |<>*]/) {
2095                    $segment = '"' . $segment . '"';
2096                }
2097                # Add it to the return list.
2098                push @retVal, $segment;
2099            }
2100        }
2101        # Return the result.
2102        return join(" ", @retVal);
2103    }
2104    
2105    =head3 ParseCommand
2106    
2107        my ($options, @arguments) = Tracer::ParseCommand(\%optionTable, @inputList);
2108    
2109    Parse a command line consisting of a list of parameters. The initial parameters may be option
2110    specifiers of the form C<->I<option> or C<->I<option>C<=>I<value>. The options are stripped
2111    off and merged into a table of default options. The remainder of the command line is
2112    returned as a list of positional arguments. For example, consider the following invocation.
2113    
2114        my ($options, @arguments) = ParseCommand({ errors => 0, logFile => 'trace.log'}, @words);
2115    
2116    In this case, the list @words will be treated as a command line and there are two options available,
2117    B<errors> and B<logFile>. If @words has the following format
2118    
2119        -logFile=error.log apple orange rutabaga
2120    
2121    then at the end of the invocation, C<$options> will be
2122    
2123        { errors => 0, logFile => 'error.log' }
2124    
2125    and C<@arguments> will contain
2126    
2127        apple orange rutabaga
2128    
2129    The parser allows for some escape sequences. See L</UnEscape> for a description. There is no
2130    support for quote characters. Options can be specified with single or double hyphens.
2131    
2132    =over 4
2133    
2134    =item optionTable
2135    
2136    Table of default options.
2137    
2138    =item inputList
2139    
2140    List of words on the command line.
2141    
2142    =item RETURN
2143    
2144    Returns a reference to the option table and a list of the positional arguments.
2145    
2146    =back
2147    
2148    =cut
2149    
2150    sub ParseCommand {
2151        # Get the parameters.
2152        my ($optionTable, @inputList) = @_;
2153        # Process any options in the input list.
2154        my %overrides = ();
2155        while ((@inputList > 0) && ($inputList[0] =~ /^--?/)) {
2156            # Get the current option.
2157            my $arg = shift @inputList;
2158            # Pull out the option name.
2159            $arg =~ /^--?([^=]*)/g;
2160            my $name = $1;
2161            # Check for an option value.
2162            if ($arg =~ /\G=(.*)$/g) {
2163                # Here we have a value for the option.
2164                $overrides{$name} = UnEscape($1);
2165            } else {
2166                # Here there is no value, so we use 1.
2167                $overrides{$name} = 1;
2168            }
2169        }
2170        # Merge the options into the defaults.
2171        GetOptions($optionTable, \%overrides);
2172        # Translate the remaining parameters.
2173        my @retVal = ();
2174        for my $inputParm (@inputList) {
2175            push @retVal, UnEscape($inputParm);
2176        }
2177        # Return the results.
2178        return ($optionTable, @retVal);
2179    }
2180    
2181    
2182    =head2 File Utility Methods
2183    
2184    =head3 GetFile
2185    
2186        my @fileContents = Tracer::GetFile($fileName);
2187    
2188        or
2189    
2190        my $fileContents = Tracer::GetFile($fileName);
2191    
2192    Return the entire contents of a file. In list context, line-ends are removed and
2193    each line is a list element. In scalar context, line-ends are replaced by C<\n>.
2194    
2195    =over 4
2196    
2197    =item fileName
2198    
2199    Name of the file to read.
2200    
2201    =item RETURN
2202    
2203    In a list context, returns the entire file as a list with the line terminators removed.
2204    In a scalar context, returns the entire file as a string. If an error occurs opening
2205    the file, an empty list will be returned.
2206    
2207    =back
2208    
2209    =cut
2210    
2211    sub GetFile {
2212        # Get the parameters.
2213        my ($fileName) = @_;
2214        # Declare the return variable.
2215        my @retVal = ();
2216        # Open the file for input.
2217        my $handle = Open(undef, "<$fileName");
2218        # Read the whole file into the return variable, stripping off any terminator
2219        # characters.
2220        my $lineCount = 0;
2221        while (my $line = <$handle>) {
2222            $lineCount++;
2223            $line = Strip($line);
2224            push @retVal, $line;
2225        }
2226        # Close it.
2227        close $handle;
2228        my $actualLines = @retVal;
2229        Trace("$actualLines lines read from file $fileName.") if T(File => 2);
2230        # Return the file's contents in the desired format.
2231        if (wantarray) {
2232            return @retVal;
2233        } else {
2234            return join "\n", @retVal;
2235        }
2236    }
2237    
2238    =head3 PutFile
2239    
2240        Tracer::PutFile($fileName, \@lines);
2241    
2242    Write out a file from a list of lines of text.
2243    
2244    =over 4
2245    
2246    =item fileName
2247    
2248    Name of the output file.
2249    
2250    =item lines
2251    
2252    Reference to a list of text lines. The lines will be written to the file in order, with trailing
2253    new-line characters. Alternatively, may be a string, in which case the string will be written without
2254    modification.
2255    
2256    =back
2257    
2258    =cut
2259    
2260    sub PutFile {
2261        # Get the parameters.
2262        my ($fileName, $lines) = @_;
2263        # Open the output file.
2264        my $handle = Open(undef, ">$fileName");
2265        # Count the lines written.
2266        if (ref $lines ne 'ARRAY') {
2267            # Here we have a scalar, so we write it raw.
2268            print $handle $lines;
2269            Trace("Scalar put to file $fileName.") if T(File => 3);
2270        } else {
2271            # Write the lines one at a time.
2272            my $count = 0;
2273            for my $line (@{$lines}) {
2274                print $handle "$line\n";
2275                $count++;
2276            }
2277            Trace("$count lines put to file $fileName.") if T(File => 3);
2278        }
2279        # Close the output file.
2280        close $handle;
2281    }
2282    
2283    =head3 ParseRecord
2284    
2285        my @fields = Tracer::ParseRecord($line);
2286    
2287    Parse a tab-delimited data line. The data line is split into field values. Embedded tab
2288    and new-line characters in the data line must be represented as C<\t> and C<\n>, respectively.
2289    These will automatically be converted.
2290    
2291    =over 4
2292    
2293    =item line
2294    
2295    Line of data containing the tab-delimited fields.
2296    
2297    =item RETURN
2298    
2299    Returns a list of the fields found in the data line.
2300    
2301    =back
2302    
2303    =cut
2304    
2305    sub ParseRecord {
2306        # Get the parameter.
2307        my ($line) = @_;
2308        # Remove the trailing new-line, if any.
2309        chomp $line;
2310        # Split the line read into pieces using the tab character.
2311        my @retVal = split /\t/, $line;
2312        # Trim and fix the escapes in each piece.
2313        for my $value (@retVal) {
2314            # Trim leading whitespace.
2315            $value =~ s/^\s+//;
2316            # Trim trailing whitespace.
2317            $value =~ s/\s+$//;
2318            # Delete the carriage returns.
2319            $value =~ s/\r//g;
2320            # Convert the escapes into their real values.
2321            $value =~ s/\\t/"\t"/ge;
2322            $value =~ s/\\n/"\n"/ge;
2323        }
2324        # Return the result.
2325        return @retVal;
2326    }
2327    
2328    =head3 Merge
2329    
2330        my @mergedList = Tracer::Merge(@inputList);
2331    
2332    Sort a list of strings and remove duplicates.
2333    
2334    =over 4
2335    
2336    =item inputList
2337    
2338    List of scalars to sort and merge.
2339    
2340  C<< my $count = Tracer::Setups(); >>  =item RETURN
2341    
2342  Return the number of times L</TSetup> has been called.  Returns a list containing the same elements sorted in ascending order with duplicates
2343    removed.
2344    
2345  This method allows for the creation of conditional tracing setups where, for example, we  =back
 may want to set up tracing if nobody else has done it before us.  
2346    
2347  =cut  =cut
2348    
2349  sub Setups {  sub Merge {
2350      return $SetupCount;      # Get the input list in sort order.
2351        my @inputList = sort @_;
2352        # Only proceed if the list has at least two elements.
2353        if (@inputList > 1) {
2354            # Now we want to move through the list splicing out duplicates.
2355            my $i = 0;
2356            while ($i < @inputList) {
2357                # Get the current entry.
2358                my $thisEntry = $inputList[$i];
2359                # Find out how many elements duplicate the current entry.
2360                my $j = $i + 1;
2361                my $dup1 = $i + 1;
2362                while ($j < @inputList && $inputList[$j] eq $thisEntry) { $j++; };
2363                # If the number is nonzero, splice out the duplicates found.
2364                if ($j > $dup1) {
2365                    splice @inputList, $dup1, $j - $dup1;
2366                }
2367                # Now the element at position $dup1 is different from the element before it
2368                # at position $i. We push $i forward one position and start again.
2369                $i++;
2370            }
2371        }
2372        # Return the merged list.
2373        return @inputList;
2374  }  }
2375    
2376  =head3 Open  =head3 Open
2377    
2378  C<< my $handle = Open($fileHandle, $fileSpec, $message); >>      my $handle = Open($fileHandle, $fileSpec, $message);
2379    
2380  Open a file.  Open a file.
2381    
# Line 774  Line 2467 
2467    
2468  =head3 FindNamePart  =head3 FindNamePart
2469    
2470  C<< my ($fileName, $start, $len) = Tracer::FindNamePart($fileSpec); >>      my ($fileName, $start, $len) = Tracer::FindNamePart($fileSpec);
2471    
2472  Extract the portion of a file specification that contains the file name.  Extract the portion of a file specification that contains the file name.
2473    
# Line 825  Line 2518 
2518    
2519  =head3 OpenDir  =head3 OpenDir
2520    
2521  C<< my @files = OpenDir($dirName, $filtered, $flag); >>      my @files = OpenDir($dirName, $filtered, $flag);
2522    
2523  Open a directory and return all the file names. This function essentially performs  Open a directory and return all the file names. This function essentially performs
2524  the functions of an C<opendir> and C<readdir>. If the I<$filtered> parameter is  the functions of an C<opendir> and C<readdir>. If the I<$filtered> parameter is
# Line 866  Line 2559 
2559  =back  =back
2560    
2561  =cut  =cut
2562  #: Return Type @;  #: Return Type @;
2563  sub OpenDir {  sub OpenDir {
     # Get the parameters.  
     my ($dirName, $filtered, $flag) = @_;  
     # Declare the return variable.  
     my @retVal = ();  
     # Open the directory.  
     if (opendir(my $dirHandle, $dirName)) {  
         # The directory opened successfully. Get the appropriate list according to the  
         # strictures of the filter parameter.  
         if ($filtered) {  
             @retVal = grep { $_ !~ /^[\.\$\#]/ && $_ !~ /~$/ } readdir $dirHandle;  
         } else {  
             @retVal = readdir $dirHandle;  
         }  
     } elsif (! $flag) {  
         # Here the directory would not open and it's considered an error.  
         Confess("Could not open directory $dirName.");  
     }  
     # Return the result.  
     return @retVal;  
 }  
   
 =head3 SetLevel  
   
 C<< Tracer::SetLevel($newLevel); >>  
   
 Modify the trace level. A higher trace level will cause more messages to appear.  
   
 =over 4  
   
 =item newLevel  
   
 Proposed new trace level.  
   
 =back  
   
 =cut  
   
 sub SetLevel {  
     $TraceLevel = $_[0];  
 }  
   
 =head3 Now  
   
 C<< my $string = Tracer::Now(); >>  
   
 Return a displayable time stamp containing the local time.  
   
 =cut  
   
 sub Now {  
     my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);  
     my $retVal = _p2($mon+1) . "/" . _p2($mday) . "/" . ($year + 1900) . " " .  
                  _p2($hour) . ":" . _p2($min) . ":" . _p2($sec);  
     return $retVal;  
 }  
   
 # Pad a number to 2 digits.  
 sub _p2 {  
     my ($value) = @_;  
     $value = "0$value" if ($value < 10);  
     return $value;  
 }  
   
 =head3 LogErrors  
   
 C<< Tracer::LogErrors($fileName); >>  
   
 Route the standard error output to a log file.  
   
 =over 4  
   
 =item fileName  
   
 Name of the file to receive the error output.  
   
 =back  
   
 =cut  
   
 sub LogErrors {  
     # Get the file name.  
     my ($fileName) = @_;  
     # Open the file as the standard error output.  
     open STDERR, '>', $fileName;  
 }  
   
 =head3 ReadOptions  
   
 C<< my %options = Tracer::ReadOptions($fileName); >>  
   
 Read a set of options from a file. Each option is encoded in a line of text that has the  
 format  
   
 I<optionName>C<=>I<optionValue>C<; >I<comment>  
   
 The option name must consist entirely of letters, digits, and the punctuation characters  
 C<.> and C<_>, and is case sensitive. Blank lines and lines in which the first nonblank  
 character is a semi-colon will be ignored. The return hash will map each option name to  
 the corresponding option value.  
   
 =over 4  
   
 =item fileName  
   
 Name of the file containing the option data.  
   
 =item RETURN  
   
 Returns a hash mapping the option names specified in the file to their corresponding option  
 value.  
   
 =back  
   
 =cut  
   
 sub ReadOptions {  
     # Get the parameters.  
     my ($fileName) = @_;  
     # Open the file.  
     (open CONFIGFILE, "<$fileName") || Confess("Could not open option file $fileName.");  
     # Count the number of records read.  
     my ($records, $comments) = 0;  
     # Create the return hash.  
     my %retVal = ();  
     # Loop through the file, accumulating key-value pairs.  
     while (my $line = <CONFIGFILE>) {  
         # Denote we've read a line.  
         $records++;  
         # Determine the line type.  
         if ($line =~ /^\s*[\n\r]/) {  
             # A blank line is a comment.  
             $comments++;  
         } elsif ($line =~ /^\s*([A-Za-z0-9_\.]+)=([^;]*);/) {  
             # Here we have an option assignment.  
             retVal{$1} = $2;  
         } elsif ($line =~ /^\s*;/) {  
             # Here we have a text comment.  
             $comments++;  
         } else {  
             # Here we have an invalid line.  
             Trace("Invalid option statement in record $records.") if T(0);  
         }  
     }  
     # Return the hash created.  
     return %retVal;  
 }  
   
 =head3 GetOptions  
   
 C<< Tracer::GetOptions(\%defaults, \%options); >>  
   
 Merge a specified set of options into a table of defaults. This method takes two hash references  
 as input and uses the data from the second to update the first. If the second does not exist,  
 there will be no effect. An error will be thrown if one of the entries in the second hash does not  
 exist in the first.  
   
 Consider the following example.  
   
 C<< my $optionTable = GetOptions({ dbType => 'mySQL', trace => 0 }, $options); >>  
   
 In this example, the variable B<$options> is expected to contain at most two options-- B<dbType> and  
 B<trace>. The default database type is C<mySQL> and the default trace level is C<0>. If the value of  
 B<$options> is C<< {dbType => 'Oracle'} >>, then the database type will be changed to C<Oracle> and  
 the trace level will remain at 0. If B<$options> is undefined, then the database type and trace level  
 will remain C<mySQL> and C<0>. If, on the other hand, B<$options> is defined as  
   
 C<< {databaseType => 'Oracle'} >>  
   
 an error will occur because the B<databaseType> option does not exist.  
   
 =over 4  
   
 =item defaults  
   
 Table of default option values.  
   
 =item options  
   
 Table of overrides, if any.  
   
 =item RETURN  
   
 Returns a reference to the default table passed in as the first parameter.  
   
 =back  
   
 =cut  
   
 sub GetOptions {  
     # Get the parameters.  
     my ($defaults, $options) = @_;  
     # Check for overrides.  
     if ($options) {  
         # Loop through the overrides.  
         while (my ($option, $setting) = each %{$options}) {  
             # Insure this override exists.  
             if (!exists $defaults->{$option}) {  
                 croak "Unrecognized option $option encountered.";  
             } else {  
                 # Apply the override.  
                 $defaults->{$option} = $setting;  
             }  
         }  
     }  
     # Return the merged table.  
     return $defaults;  
 }  
   
 =head3 MergeOptions  
   
 C<< Tracer::MergeOptions(\%table, \%defaults); >>  
   
 Merge default values into a hash table. This method looks at the key-value pairs in the  
 second (default) hash, and if a matching key is not found in the first hash, the default  
 pair is copied in. The process is similar to L</GetOptions>, but there is no error-  
 checking and no return value.  
   
 =over 4  
   
 =item table  
   
 Hash table to be updated with the default values.  
   
 =item defaults  
   
 Default values to be merged into the first hash table if they are not already present.  
   
 =back  
   
 =cut  
   
 sub MergeOptions {  
2564      # Get the parameters.      # Get the parameters.
2565      my ($table, $defaults) = @_;      my ($dirName, $filtered, $flag) = @_;
2566      # Loop through the defaults.      # Declare the return variable.
2567      while (my ($key, $value) = each %{$defaults}) {      my @retVal = ();
2568          if (!exists $table->{$key}) {      # Open the directory.
2569              $table->{$key} = $value;      if (opendir(my $dirHandle, $dirName)) {
2570            # The directory opened successfully. Get the appropriate list according to the
2571            # strictures of the filter parameter.
2572            if ($filtered) {
2573                @retVal = grep { $_ !~ /^[\.\$\#]/ && $_ !~ /~$/ } readdir $dirHandle;
2574            } else {
2575                @retVal = readdir $dirHandle;
2576          }          }
2577            closedir $dirHandle;
2578        } elsif (! $flag) {
2579            # Here the directory would not open and it's considered an error.
2580            Confess("Could not open directory $dirName.");
2581      }      }
2582        # Return the result.
2583        return @retVal;
2584  }  }
2585    
 =head3 Trace  
2586    
2587  C<< Trace($message); >>  =head3 Insure
2588    
2589  Write a trace message to the target location specified in L</TSetup>. If there has not been      Insure($dirName, $chmod);
2590  any prior call to B<TSetup>.  
2591    Insure a directory is present.
2592    
2593  =over 4  =over 4
2594    
2595  =item message  =item dirName
2596    
2597  Message to write.  Name of the directory to check. If it does not exist, it will be created.
2598    
2599    =item chmod (optional)
2600    
2601    Security privileges to be given to the directory if it is created.
2602    
2603  =back  =back
2604    
2605  =cut  =cut
2606    
2607  sub Trace {  sub Insure {
2608      # Get the parameters.      my ($dirName, $chmod) = @_;
2609      my ($message) = @_;      if (! -d $dirName) {
2610      # Get the timestamp.          Trace("Creating $dirName directory.") if T(2);
2611      my $timeStamp = Now();          eval {
2612      # Format the message. Note we strip off any line terminators at the end.              mkpath $dirName;
2613      my $formatted = "[$timeStamp] <$LastCategory>: " . Strip($message);              # If we have permissions specified, set them here.
2614      # Process according to the destination.              if (defined($chmod)) {
2615      if ($Destination eq "TEXT") {                  chmod $chmod, $dirName;
2616          # Write the message to the standard output.              }
2617          print "$formatted\n";          };
2618      } elsif ($Destination eq "ERROR") {          if ($@) {
2619          # Write the message to the error output.              Confess("Error creating $dirName: $@");
         print STDERR "$formatted\n";  
     } elsif ($Destination eq "QUEUE") {  
         # Push the message into the queue.  
         push @Queue, "$formatted";  
     } elsif ($Destination eq "HTML") {  
         # Convert the message to HTML and write it to the standard output.  
         my $escapedMessage = CGI::escapeHTML($message);  
         print "<p>$formatted</p>\n";  
     } elsif ($Destination eq "WARN") {  
        # Emit the message as a warning.  
        warn $message;  
     } elsif ($Destination =~ m/^>>/) {  
         # Write the trace message to an output file.  
         (open TRACING, $Destination) || die "Tracing open for \"$Destination\" failed: $!";  
         print TRACING "$formatted\n";  
         close TRACING;  
         # If the Tee flag is on, echo it to the standard output.  
         if ($TeeFlag) {  
             print "$formatted\n";  
2620          }          }
2621      }      }
2622  }  }
2623    
2624  =head3 T  =head3 ChDir
   
 C<< my $switch = T($category, $traceLevel); >>  
   
     or  
2625    
2626  C<< my $switch = T($traceLevel); >>      ChDir($dirName);
2627    
2628  Return TRUE if the trace level is at or above a specified value and the specified category  Change to the specified directory.
 is active, else FALSE. If no category is specified, the caller's package name is used.  
2629    
2630  =over 4  =over 4
2631    
2632  =item category  =item dirName
   
 Category to which the message belongs. If not specified, the caller's package name is  
 used.  
   
 =item traceLevel  
   
 Relevant tracing level.  
   
 =item RETURN  
2633    
2634  TRUE if a message at the specified trace level would appear in the trace, else FALSE.  Name of the directory to which we want to change.
2635    
2636  =back  =back
2637    
2638  =cut  =cut
2639    
2640  sub T {  sub ChDir {
2641      # Declare the return variable.      my ($dirName) = @_;
2642      my $retVal = 0;      if (! -d $dirName) {
2643      # Only proceed if tracing is turned on.          Confess("Cannot change to directory $dirName: no such directory.");
     if ($Destination ne "NONE") {  
         # Get the parameters.  
         my ($category, $traceLevel) = @_;  
         if (!defined $traceLevel) {  
             # Here we have no category, so we need to get the calling package.  
             # The calling package is normally the first parameter. If it is  
             # omitted, the first parameter will be the tracelevel. So, the  
             # first thing we do is shift the so-called category into the  
             # $traceLevel variable where it belongs.  
             $traceLevel = $category;  
             my ($package, $fileName, $line) = caller;  
             # If there is no calling package, we default to "main".  
             if (!$package) {  
                 $category = "main";  
2644              } else {              } else {
2645                  $category = $package;          Trace("Changing to directory $dirName.") if T(File => 4);
2646              }          my $okFlag = chdir $dirName;
2647          }          if (! $okFlag) {
2648          # Save the category name.              Confess("Error switching to directory $dirName.");
         $LastCategory = $category;  
         # Convert it to lower case before we hash it.  
         $category = lc $category;  
         # Use the category and tracelevel to compute the result.  
         if (ref $traceLevel) {  
             Confess("Bad trace level.");  
         } elsif (ref $TraceLevel) {  
             Confess("Bad trace config.");  
2649          }          }
         $retVal = ($traceLevel <= $TraceLevel && ($AllTrace || exists $Categories{$category}));  
2650      }      }
     # Return the computed result.  
     return $retVal;  
2651  }  }
2652    
2653  =head3 ParseCommand  =head3 SetPermissions
   
 C<< my ($options, @arguments) = Tracer::ParseCommand(\%optionTable, @inputList); >>  
2654    
2655  Parse a command line consisting of a list of parameters. The initial parameters may be option      Tracer::SetPermissions($dirName, $group, $mask, %otherMasks);
 specifiers of the form C<->I<option> or C<->I<option>C<=>I<value>. The options are stripped  
 off and merged into a table of default options. The remainder of the command line is  
 returned as a list of positional arguments. For example, consider the following invocation.  
2656    
2657  C<< my ($options, @arguments) = ParseCommand({ errors => 0, logFile => 'trace.log'}, @words); >>  Set the permissions for a directory and all the files and folders inside it.
2658    In addition, the group ownership will be changed to the specified value.
2659    
2660  In this case, the list @words will be treated as a command line. There are two options available,  This method is more vulnerable than most to permission and compatability
2661  B<errors> and B<logFile>. If @words has the following format  problems, so it does internal error recovery.
2662    
2663  C<< -logFile=error.log apple orange rutabaga >>  =over 4
2664    
2665  then at the end of the invocation, C<$options> will be  =item dirName
2666    
2667  C<< { errors => 0, logFile => 'error.log' } >>  Name of the directory to process.
2668    
2669  and C<@arguments> will contain  =item group
2670    
2671  C<< apple orange rutabaga >>  Name of the group to be assigned.
2672    
2673  The parser allows for some escape sequences. See L</UnEscape> for a description. There is no  =item mask
 support for quote characters.  
2674    
2675  =over 4  Permission mask. Bits that are C<1> in this mask will be ORed into the
2676    permission bits of any file or directory that does not already have them
2677    set to 1.
2678    
2679  =item optionTable  =item otherMasks
2680    
2681  Table of default options.  Map of search patterns to permission masks. If a directory name matches
2682    one of the patterns, that directory and all its members and subdirectories
2683    will be assigned the new pattern. For example, the following would
2684    assign 0664 to most files, but would use 0777 for directories named C<tmp>.
2685    
2686  =item inputList      Tracer::SetPermissions($dirName, 'fig', 01664, '^tmp$' => 01777);
2687    
2688  List of words on the command line.  The list is ordered, so the following would use 0777 for C<tmp1> and
2689    0666 for C<tmp>, C<tmp2>, or C<tmp3>.
2690    
2691  =item RETURN      Tracer::SetPermissions($dirName, 'fig', 01664, '^tmp1' => 0777,
2692                                                       '^tmp' => 0666);
2693    
2694  Returns a reference to the option table and a list of the positional arguments.  Note that the pattern matches are all case-insensitive, and only directory
2695    names are matched, not file names.
2696    
2697  =back  =back
2698    
2699  =cut  =cut
2700    
2701  sub ParseCommand {  sub SetPermissions {
2702      # Get the parameters.      # Get the parameters.
2703      my ($optionTable, @inputList) = @_;      my ($dirName, $group, $mask, @otherMasks) = @_;
2704      # Process any options in the input list.      # Set up for error recovery.
2705      my %overrides = ();      eval {
2706      while ((@inputList > 0) && ($inputList[0] =~ /^-/)) {          # Switch to the specified directory.
2707          # Get the current option.          ChDir($dirName);
2708          my $arg = shift @inputList;          # Get the group ID.
2709          # Pull out the option name.          my $gid = getgrnam($group);
2710          $arg =~ /^-([^=]*)/g;          # Get the mask for tracing.
2711          my $name = $1;          my $traceMask = sprintf("%04o", $mask) . "($mask)";
2712          # Check for an option value.          Trace("Fixing permissions for directory $dirName using group $group($gid) and mask $traceMask.") if T(File => 2);
2713          if ($arg =~ /\G=(.*)$/g) {          my $fixCount = 0;
2714              # Here we have a value for the option.          my $lookCount = 0;
2715              $overrides{$name} = UnEscape($1);          # @dirs will be a stack of directories to be processed.
2716            my @dirs = (getcwd());
2717            while (scalar(@dirs) > 0) {
2718                # Get the current directory.
2719                my $dir = pop @dirs;
2720                # Check for a match to one of the specified directory names. To do
2721                # that, we need to pull the individual part of the name off of the
2722                # whole path.
2723                my $simpleName = $dir;
2724                if ($dir =~ m!/([^/]+)$!) {
2725                    $simpleName = $1;
2726                }
2727                Trace("Simple directory name for $dir is $simpleName.") if T(File => 4);
2728                # Search for a match.
2729                my $match = 0;
2730                my $i;
2731                for ($i = 0; $i < $#otherMasks && ! $match; $i += 2) {
2732                    my $pattern = $otherMasks[$i];
2733                    if ($simpleName =~ /$pattern/i) {
2734                        $match = 1;
2735                    }
2736                }
2737                # Find out if we have a match. Note we use $i-1 because the loop added 2
2738                # before terminating due to the match.
2739                if ($match && $otherMasks[$i-1] != $mask) {
2740                    # This directory matches one of the incoming patterns, and it's
2741                    # a different mask, so we process it recursively with that mask.
2742                    SetPermissions($dir, $group, $otherMasks[$i-1], @otherMasks);
2743          } else {          } else {
2744              # Here there is no value, so we use 1.                  # Here we can process normally. Get all of the non-hidden members.
2745              $overrides{$name} = 1;                  my @submems = OpenDir($dir, 1);
2746                    for my $submem (@submems) {
2747                        # Get the full name.
2748                        my $thisMem = "$dir/$submem";
2749                        Trace("Checking member $thisMem.") if T(4);
2750                        $lookCount++;
2751                        if ($lookCount % 1000 == 0) {
2752                            Trace("$lookCount members examined. Current is $thisMem. Mask is $traceMask") if T(File => 3);
2753                        }
2754                        # Fix the group.
2755                        chown -1, $gid, $thisMem;
2756                        # Insure this member is not a symlink.
2757                        if (! -l $thisMem) {
2758                            # Get its info.
2759                            my $fileInfo = stat $thisMem;
2760                            # Only proceed if we got the info. Otherwise, it's a hard link
2761                            # and we want to skip it anyway.
2762                            if ($fileInfo) {
2763                                my $fileMode = $fileInfo->mode;
2764                                if (($fileMode & $mask) != $mask) {
2765                                    # Fix this member.
2766                                    $fileMode |= $mask;
2767                                    chmod $fileMode, $thisMem;
2768                                    $fixCount++;
2769                                }
2770                                # If it's a subdirectory, stack it.
2771                                if (-d $thisMem) {
2772                                    push @dirs, $thisMem;
2773                                }
2774                            }
2775          }          }
2776      }      }
     # Merge the options into the defaults.  
     GetOptions($optionTable, \%overrides);  
     # Translate the remaining parameters.  
     my @retVal = ();  
     for my $inputParm (@inputList) {  
         push @retVal, UnEscape($inputParm);  
2777      }      }
2778      # Return the results.          }
2779      return ($optionTable, @retVal);          Trace("$lookCount files and directories processed, $fixCount fixed.") if T(File => 2);
2780        };
2781        # Check for an error.
2782        if ($@) {
2783            Confess("SetPermissions error: $@");
2784        }
2785  }  }
2786    
2787  =head3 Escape  =head3 GetLine
2788    
2789  C<< my $codedString = Tracer::Escape($realString); >>      my @data = Tracer::GetLine($handle);
2790    
2791  Escape a string for use in a command length. Tabs will be replaced by C<\t>, new-lines  Read a line of data from a tab-delimited file.
 replaced by C<\n>, carriage returns will be deleted, and backslashes will be doubled. The  
 result is to reverse the effect of L</UnEscape>.  
2792    
2793  =over 4  =over 4
2794    
2795  =item realString  =item handle
2796    
2797  String to escape.  Open file handle from which to read.
2798    
2799  =item RETURN  =item RETURN
2800    
2801  Escaped equivalent of the real string.  Returns a list of the fields in the record read. The fields are presumed to be
2802    tab-delimited. If we are at the end of the file, then an empty list will be
2803    returned. If an empty line is read, a single list item consisting of a null
2804    string will be returned.
2805    
2806  =back  =back
2807    
2808  =cut  =cut
2809    
2810  sub Escape {  sub GetLine {
2811      # Get the parameter.      # Get the parameters.
2812      my ($realString) = @_;      my ($handle) = @_;
2813      # Initialize the return variable.      # Declare the return variable.
2814      my $retVal = "";      my @retVal = ();
2815      # Loop through the parameter string, looking for sequences to escape.      Trace("File position is " . tell($handle) . ". EOF flag is " . eof($handle) . ".") if T(File => 4);
2816      while (length $realString > 0) {      # Read from the file.
2817          # Look for the first sequence to escape.      my $line = <$handle>;
2818          if ($realString =~ /^(.*?)([\n\t\r\\])/) {      # Only proceed if we found something.
2819              # Here we found it. The text preceding the sequence is in $1. The sequence      if (defined $line) {
2820              # itself is in $2. First, move the clear text to the return variable.          # Remove the new-line. We are a bit over-cautious here because the file may be coming in via an
2821              $retVal .= $1;          # upload control and have a nonstandard EOL combination.
2822              # Strip the processed section off the real string.          $line =~ s/(\r|\n)+$//;
2823              $realString = substr $realString, (length $2) + (length $1);          # Here we do some fancy tracing to help in debugging complicated EOL marks.
2824              # Get the matched character.          if (T(File => 4)) {
2825              my $char = $2;              my $escapedLine = $line;
2826              # If we have a CR, we are done.              $escapedLine =~ s/\n/\\n/g;
2827              if ($char ne "\r") {              $escapedLine =~ s/\r/\\r/g;
2828                  # It's not a CR, so encode the escape sequence.              $escapedLine =~ s/\t/\\t/g;
2829                  $char =~ tr/\t\n/tn/;              Trace("Line read: -->$escapedLine<--");
                 $retVal .= "\\" . $char;  
2830              }              }
2831            # If the line is empty, return a single empty string; otherwise, parse
2832            # it into fields.
2833            if ($line eq "") {
2834                push @retVal, "";
2835          } else {          } else {
2836              # Here there are no more escape sequences. The rest of the string is              push @retVal, split /\t/,$line;
             # transferred unmodified.  
             $retVal .= $realString;  
             $realString = "";  
2837          }          }
2838        } else {
2839            # Trace the reason the read failed.
2840            Trace("End of file: $!") if T(File => 3);
2841      }      }
2842      # Return the result.      # Return the result.
2843      return $retVal;      return @retVal;
2844  }  }
2845    
2846  =head3 UnEscape  =head3 PutLine
2847    
2848  C<< my $realString = Tracer::UnEscape($codedString); >>      Tracer::PutLine($handle, \@fields, $eol);
2849    
2850  Replace escape sequences with their actual equivalents. C<\t> will be replaced by  Write a line of data to a tab-delimited file. The specified field values will be
2851  a tab, C<\n> by a new-line character, and C<\\> by a backslash. C<\r> codes will  output in tab-separated form, with a trailing new-line.
 be deleted.  
2852    
2853  =over 4  =over 4
2854    
2855  =item codedString  =item handle
2856    
2857  String to un-escape.  Output file handle.
2858    
2859  =item RETURN  =item fields
2860    
2861  Returns a copy of the original string with the escape sequences converted to their actual  List of field values.
2862  values.  
2863    =item eol (optional)
2864    
2865    End-of-line character (default is "\n").
2866    
2867  =back  =back
2868    
2869  =cut  =cut
2870    
2871  sub UnEscape {  sub PutLine {
2872      # Get the parameter.      # Get the parameters.
2873      my ($codedString) = @_;      my ($handle, $fields, $eol) = @_;
2874      # Initialize the return variable.      # Write the data.
2875      my $retVal = "";      print $handle join("\t", @{$fields}) . ($eol || "\n");
     # Only proceed if the incoming string is nonempty.  
     if (defined $codedString) {  
         # Loop through the parameter string, looking for escape sequences. We can't do  
         # translating because it causes problems with the escaped slash. ("\\t" becomes  
         # "\<tab>" no matter what we do.)  
         while (length $codedString > 0) {  
             # Look for the first escape sequence.  
             if ($codedString =~ /^(.*?)\\(\\|n|t|r)/) {  
                 # Here we found it. The text preceding the sequence is in $1. The sequence  
                 # itself is in $2. First, move the clear text to the return variable.  
                 $retVal .= $1;  
                 $codedString = substr $codedString, (2 + length $1);  
                 # Get the escape value.  
                 my $char = $2;  
                 # If we have a "\r", we are done.  
                 if ($char ne 'r') {  
                     # Here it's not an 'r', so we convert it.  
                     $char =~ tr/\\tn/\\\t\n/;  
                     $retVal .= $char;  
                 }  
             } else {  
                 # Here there are no more escape sequences. The rest of the string is  
                 # transferred unmodified.  
                 $retVal .= $codedString;  
                 $codedString = "";  
             }  
         }  
     }  
     # Return the result.  
     return $retVal;  
2876  }  }
2877    
 =head3 ParseRecord  
2878    
2879  C<< my @fields = Tracer::ParseRecord($line); >>  =head3 PrintLine
2880    
2881  Parse a tab-delimited data line. The data line is split into field values. Embedded tab      Tracer::PrintLine($line);
2882  and new-line characters in the data line must be represented as C<\t> and C<\n>, respectively.  
2883  These will automatically be converted.  Print a line of text with a trailing new-line.
2884    
2885  =over 4  =over 4
2886    
2887  =item line  =item line
2888    
2889  Line of data containing the tab-delimited fields.  Line of text to print.
   
 =item RETURN  
   
 Returns a list of the fields found in the data line.  
2890    
2891  =back  =back
2892    
2893  =cut  =cut
2894    
2895  sub ParseRecord {  sub PrintLine {
2896      # Get the parameter.      # Get the parameters.
2897      my ($line) = @_;      my ($line) = @_;
2898      # Remove the trailing new-line, if any.      # Print the line.
2899      chomp $line;      print "$line\n";
     # Split the line read into pieces using the tab character.  
     my @retVal = split /\t/, $line;  
     # Trim and fix the escapes in each piece.  
     for my $value (@retVal) {  
         # Trim leading whitespace.  
         $value =~ s/^\s+//;  
         # Trim trailing whitespace.  
         $value =~ s/\s+$//;  
         # Delete the carriage returns.  
         $value =~ s/\r//g;  
         # Convert the escapes into their real values.  
         $value =~ s/\\t/"\t"/ge;  
         $value =~ s/\\n/"\n"/ge;  
2900      }      }
2901      # Return the result.  
2902      return @retVal;  
2903    =head2 Other Useful Methods
2904    
2905    =head3 IDHASH
2906    
2907        my $hash = SHTargetSearch::IDHASH(@keys);
2908    
2909    This is a dinky little method that converts a list of values to a reference
2910    to hash of values to labels. The values and labels are the same.
2911    
2912    =cut
2913    
2914    sub IDHASH {
2915        my %retVal = map { $_ => $_ } @_;
2916        return \%retVal;
2917  }  }
2918    
2919  =head3 Merge  =head3 Pluralize
2920    
2921  C<< my @mergedList = Tracer::Merge(@inputList); >>      my $plural = Tracer::Pluralize($word);
2922    
2923  Sort a list of strings and remove duplicates.  This is a very simple pluralization utility. It adds an C<s> at the end
2924    of the input word unless it already ends in an C<s>, in which case it
2925    adds C<es>.
2926    
2927  =over 4  =over 4
2928    
2929  =item inputList  =item word
2930    
2931  List of scalars to sort and merge.  Singular word to pluralize.
2932    
2933  =item RETURN  =item RETURN
2934    
2935  Returns a list containing the same elements sorted in ascending order with duplicates  Returns the probable plural form of the word.
 removed.  
2936    
2937  =back  =back
2938    
2939  =cut  =cut
2940    
2941  sub Merge {  sub Pluralize {
2942      # Get the input list in sort order.      # Get the parameters.
2943      my @inputList = sort @_;      my ($word) = @_;
2944      # Only proceed if the list has at least two elements.      # Declare the return variable.
2945      if (@inputList > 1) {      my $retVal;
2946          # Now we want to move through the list splicing out duplicates.      if ($word =~ /s$/) {
2947          my $i = 0;          $retVal = $word . 'es';
2948          while ($i < @inputList) {      } else {
2949              # Get the current entry.          $retVal = $word . 's';
             my $thisEntry = $inputList[$i];  
             # Find out how many elements duplicate the current entry.  
             my $j = $i + 1;  
             my $dup1 = $i + 1;  
             while ($j < @inputList && $inputList[$j] eq $thisEntry) { $j++; };  
             # If the number is nonzero, splice out the duplicates found.  
             if ($j > $dup1) {  
                 splice @inputList, $dup1, $j - $dup1;  
             }  
             # Now the element at position $dup1 is different from the element before it  
             # at position $i. We push $i forward one position and start again.  
             $i++;  
         }  
2950      }      }
2951      # Return the merged list.      # Return the result.
2952      return @inputList;      return $retVal;
2953  }  }
2954    
2955  =head3 Percent  =head3 Numeric
2956    
2957  C<< my $percent = Tracer::Percent($number, $base); >>      my $okFlag = Tracer::Numeric($string);
2958    
2959  Returns the percent of the base represented by the given number. If the base  Return the value of the specified string if it is numeric, or an undefined value
2960  is zero, returns zero.  if it is not numeric.
2961    
2962  =over 4  =over 4
2963    
2964  =item number  =item string
   
 Percent numerator.  
   
 =item base  
2965    
2966  Percent base.  String to check.
2967    
2968  =item RETURN  =item RETURN
2969    
2970  Returns the percentage of the base represented by the numerator.  Returns the numeric value of the string if successful, or C<undef> if the string
2971    is not numeric.
2972    
2973  =back  =back
2974    
2975  =cut  =cut
2976    
2977  sub Percent {  sub Numeric {
2978      # Get the parameters.      # Get the parameters.
2979      my ($number, $base) = @_;      my ($string) = @_;
2980      # Declare the return variable.      # We'll put the value in here if we succeed.
2981      my $retVal = 0;      my $retVal;
2982      # Compute the percent.      # Get a working copy of the string.
2983      if ($base != 0) {      my $copy = $string;
2984          $retVal = $number * 100 / $base;      # Trim leading and trailing spaces.
2985        $copy =~ s/^\s+//;
2986        $copy =~ s/\s+$//;
2987        # Check the result.
2988        if ($copy =~ /^[+-]?\d+$/) {
2989            $retVal = $copy;
2990        } elsif ($copy =~ /^([+-]\d+|\d*)[eE][+-]?\d+$/) {
2991            $retVal = $copy;
2992        } elsif ($copy =~ /^([+-]\d+|\d*)\.\d*([eE][+-]?\d+)?$/) {
2993            $retVal = $copy;
2994      }      }
2995      # Return the result.      # Return the result.
2996      return $retVal;      return $retVal;
2997  }  }
2998    
 =head3 GetFile  
   
 C<< my @fileContents = Tracer::GetFile($fileName); >>  
2999    
3000      or  =head3 ParseParm
3001    
3002  C<< my $fileContents = Tracer::GetFile($fileName); >>      my $listValue = Tracer::ParseParm($string);
3003    
3004  Return the entire contents of a file. In list context, line-ends are removed and  Convert a parameter into a list reference. If the parameter is undefined,
3005  each line is a list element. In scalar context, line-ends are replaced by C<\n>.  an undefined value will be returned. Otherwise, it will be parsed as a
3006    comma-separated list of values.
3007    
3008  =over 4  =over 4
3009    
3010  =item fileName  =item string
3011    
3012  Name of the file to read.  Incoming string.
3013    
3014  =item RETURN  =item RETURN
3015    
3016  In a list context, returns the entire file as a list with the line terminators removed.  Returns a reference to a list of values, or C<undef> if the incoming value
3017  In a scalar context, returns the entire file as a string. If an error occurs opening  was undefined.
 the file, an empty list will be returned.  
3018    
3019  =back  =back
3020    
3021  =cut  =cut
3022    
3023  sub GetFile {  sub ParseParm {
3024      # Get the parameters.      # Get the parameters.
3025      my ($fileName) = @_;      my ($string) = @_;
3026      # Declare the return variable.      # Declare the return variable.
3027      my @retVal = ();      my $retVal;
3028      # Open the file for input.      # Check for data.
3029      my $handle = Open(undef, "<$fileName");      if (defined $string) {
3030      # Read the whole file into the return variable, stripping off any terminator          # We have some, so split it into a list.
3031      # characters.          $retVal = [ split /\s*,\s*/, $string];
     my $lineCount = 0;  
     while (my $line = <$handle>) {  
         $lineCount++;  
         $line = Strip($line);  
         push @retVal, $line;  
3032      }      }
3033      # Close it.      # Return the result.
3034      close $handle;      return $retVal;
     my $actualLines = @retVal;  
     # Return the file's contents in the desired format.  
     if (wantarray) {  
         return @retVal;  
     } else {  
         return join "\n", @retVal;  
3035      }      }
3036    
3037    =head3 Now
3038    
3039        my $string = Tracer::Now();
3040    
3041    Return a displayable time stamp containing the local time. Whatever format this
3042    method produces must be parseable by L</ParseDate>.
3043    
3044    =cut
3045    
3046    sub Now {
3047        return DisplayTime(time);
3048  }  }
3049    
3050  =head3 PutFile  =head3 DisplayTime
3051    
3052  C<< Tracer::PutFile($fileName, \@lines); >>      my $string = Tracer::DisplayTime($time);
3053    
3054  Write out a file from a list of lines of text.  Convert a time value to a displayable time stamp. Whatever format this
3055    method produces must be parseable by L</ParseDate>.
3056    
3057  =over 4  =over 4
3058    
3059  =item fileName  =item time
3060    
3061  Name of the output file.  Time to display, in seconds since the epoch, or C<undef> if the time is unknown.
3062    
3063  =item lines  =item RETURN
3064    
3065  Reference to a list of text lines. The lines will be written to the file in order, with trailing  Returns a displayable time, or C<(n/a)> if the incoming time is undefined.
 new-line characters. Alternatively, may be a string, in which case the string will be written without  
 modification.  
3066    
3067  =back  =back
3068    
3069  =cut  =cut
3070    
3071  sub PutFile {  sub DisplayTime {
3072      # Get the parameters.      my ($time) = @_;
3073      my ($fileName, $lines) = @_;      my $retVal = "(n/a)";
3074      # Open the output file.      if (defined $time) {
3075      my $handle = Open(undef, ">$fileName");          my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($time);
3076      if (ref $lines ne 'ARRAY') {          $retVal = _p2($mon+1) . "/" . _p2($mday) . "/" . ($year + 1900) . " " .
3077          # Here we have a scalar, so we write it raw.                    _p2($hour) . ":" . _p2($min) . ":" . _p2($sec);
         print $handle $lines;  
     } else {  
         # Write the lines one at a time.  
         for my $line (@{$lines}) {  
             print $handle "$line\n";  
3078          }          }
3079        return $retVal;
3080      }      }
3081      # Close the output file.  
3082      close $handle;  # Pad a number to 2 digits.
3083    sub _p2 {
3084        my ($value) = @_;
3085        $value = "0$value" if ($value < 10);
3086        return $value;
3087  }  }
3088    
3089  =head3 QTrace  =head3 Escape
3090    
3091  C<< my $data = QTrace($format); >>      my $codedString = Tracer::Escape($realString);
3092    
3093  Return the queued trace data in the specified format.  Escape a string for use in a command. Tabs will be replaced by C<\t>, new-lines
3094    replaced by C<\n>, carriage returns will be deleted, and backslashes will be doubled. The
3095    result is to reverse the effect of L</UnEscape>.
3096    
3097  =over 4  =over 4
3098    
3099  =item format  =item realString
3100    
3101  C<html> to format the data as an HTML list, C<text> to format it as straight text.  String to escape.
3102    
3103    =item RETURN
3104    
3105    Escaped equivalent of the real string.
3106    
3107  =back  =back
3108    
3109  =cut  =cut
3110    
3111  sub QTrace {  sub Escape {
3112      # Get the parameter.      # Get the parameter.
3113      my ($format) = @_;      my ($realString) = @_;
3114      # Create the return variable.      # Initialize the return variable.
3115      my $retVal = "";      my $retVal = "";
3116      # Only proceed if there is an actual queue.      # Loop through the parameter string, looking for sequences to escape.
3117      if (@Queue) {      while (length $realString > 0) {
3118          # Process according to the format.          # Look for the first sequence to escape.
3119          if ($format =~ m/^HTML$/i) {          if ($realString =~ /^(.*?)([\n\t\r\\])/) {
3120              # Convert the queue into an HTML list.              # Here we found it. The text preceding the sequence is in $1. The sequence
3121              $retVal = "<ul>\n";              # itself is in $2. First, move the clear text to the return variable.
3122              for my $line (@Queue) {              $retVal .= $1;
3123                  my $escapedLine = CGI::escapeHTML($line);              # Strip the processed section off the real string.
3124                  $retVal .= "<li>$escapedLine</li>\n";              $realString = substr $realString, (length $2) + (length $1);
3125                # Get the matched character.
3126                my $char = $2;
3127                # If we have a CR, we are done.
3128                if ($char ne "\r") {
3129                    # It's not a CR, so encode the escape sequence.
3130                    $char =~ tr/\t\n/tn/;
3131                    $retVal .= "\\" . $char;
3132              }              }
3133              $retVal .= "</ul>\n";          } else {
3134          } elsif ($format =~ m/^TEXT$/i) {              # Here there are no more escape sequences. The rest of the string is
3135              # Convert the queue into a list of text lines.              # transferred unmodified.
3136              $retVal = join("\n", @Queue) . "\n";              $retVal .= $realString;
3137                $realString = "";
3138          }          }
         # Clear the queue.  
         @Queue = ();  
3139      }      }
3140      # Return the formatted list.      # Return the result.
3141      return $retVal;      return $retVal;
3142  }  }
3143    
3144  =head3 Confess  =head3 UnEscape
3145    
3146  C<< Confess($message); >>      my $realString = Tracer::UnEscape($codedString);
3147    
3148  Trace the call stack and abort the program with the specified message. When used with  Replace escape sequences with their actual equivalents. C<\t> will be replaced by
3149  the OR operator and the L</Assert> method, B<Confess> can function as a debugging assert.  a tab, C<\n> by a new-line character, and C<\\> by a backslash. C<\r> codes will
3150  So, for example  be deleted.
3151    
3152  C<< Assert($recNum >= 0) || Confess("Invalid record number $recNum."); >>  =over 4
3153    
3154  Will abort the program with a stack trace if the value of C<$recNum> is negative.  =item codedString
3155    
3156  =over 4  String to un-escape.
3157    
3158  =item message  =item RETURN
3159    
3160  Message to include in the trace.  Returns a copy of the original string with the escape sequences converted to their actual
3161    values.
3162    
3163  =back  =back
3164    
3165  =cut  =cut
3166    
3167  sub Confess {  sub UnEscape {
3168      # Get the parameters.      # Get the parameter.
3169      my ($message) = @_;      my ($codedString) = @_;
3170      # Trace the call stack.      # Initialize the return variable.
3171      Cluck($message);      my $retVal = "";
3172      # Abort the program.      # Only proceed if the incoming string is nonempty.
3173      croak(">>> $message");      if (defined $codedString) {
3174            # Loop through the parameter string, looking for escape sequences. We can't do
3175            # translating because it causes problems with the escaped slash. ("\\t" becomes
3176            # "\<tab>" no matter what we do.)
3177            while (length $codedString > 0) {
3178                # Look for the first escape sequence.
3179                if ($codedString =~ /^(.*?)\\(\\|n|t|r)/) {
3180                    # Here we found it. The text preceding the sequence is in $1. The sequence
3181                    # itself is in $2. First, move the clear text to the return variable.
3182                    $retVal .= $1;
3183                    $codedString = substr $codedString, (2 + length $1);
3184                    # Get the escape value.
3185                    my $char = $2;
3186                    # If we have a "\r", we are done.
3187                    if ($char ne 'r') {
3188                        # Here it's not an 'r', so we convert it.
3189                        $char =~ tr/\\tn/\\\t\n/;
3190                        $retVal .= $char;
3191                    }
3192                } else {
3193                    # Here there are no more escape sequences. The rest of the string is
3194                    # transferred unmodified.
3195                    $retVal .= $codedString;
3196                    $codedString = "";
3197  }  }
   
 =head3 Assert  
   
 C<< Assert($condition1, $condition2, ... $conditionN); >>  
   
 Return TRUE if all the conditions are true. This method can be used in conjunction with  
 the OR operator and the L</Confess> method as a debugging assert.  
 So, for example  
   
 C<< Assert($recNum >= 0) || Confess("Invalid record number $recNum."); >>  
   
 Will abort the program with a stack trace if the value of C<$recNum> is negative.  
   
 =cut  
 sub Assert {  
     my $retVal = 1;  
     LOOP: for my $condition (@_) {  
         if (! $condition) {  
             $retVal = 0;  
             last LOOP;  
3198          }          }
3199      }      }
3200        # Return the result.
3201      return $retVal;      return $retVal;
3202  }  }
3203    
3204  =head3 Cluck  =head3 Percent
3205    
3206  C<< Cluck($message); >>      my $percent = Tracer::Percent($number, $base);
3207    
3208  Trace the call stack. Note that for best results, you should qualify the call with a  Returns the percent of the base represented by the given number. If the base
3209  trace condition. For example,  is zero, returns zero.
3210    
3211  C<< Cluck("Starting record parse.") if T(3); >>  =over 4
3212    
3213  will only trace the stack if the trace level for the package is 3 or more.  =item number
3214    
3215  =over 4  Percent numerator.
3216    
3217  =item message  =item base
3218    
3219  Message to include in the trace.  Percent base.
3220    
3221    =item RETURN
3222    
3223    Returns the percentage of the base represented by the numerator.
3224    
3225  =back  =back
3226    
3227  =cut  =cut
3228    
3229  sub Cluck {  sub Percent {
3230      # Get the parameters.      # Get the parameters.
3231      my ($message) = @_;      my ($number, $base) = @_;
3232      # Trace what's happening.      # Declare the return variable.
3233      Trace("Stack trace for event: $message");      my $retVal = 0;
3234      my $confession = longmess($message);      # Compute the percent.
3235      # Convert the confession to a series of trace messages. Note we skip any      if ($base != 0) {
3236      # messages relating to calls into Tracer.          $retVal = $number * 100 / $base;
     for my $line (split /\s*\n/, $confession) {  
         Trace($line) if ($line !~ /Tracer\.pm/);  
3237      }      }
3238        # Return the result.
3239        return $retVal;
3240  }  }
3241    
3242  =head3 Min  =head3 In
3243    
3244  C<< my $min = Min($value1, $value2, ... $valueN); >>      my $flag = Tracer::In($value, $min, $max);
3245    
3246  Return the minimum argument. The arguments are treated as numbers.  Return TRUE if the value is between the minimum and the maximum, else FALSE.
3247    
3248    =cut
3249    
3250    sub In {
3251        return ($_[0] <= $_[2] && $_[0] >= $_[1]);
3252    }
3253    
3254    
3255    =head3 Constrain
3256    
3257        my $constrained = Constrain($value, $min, $max);
3258    
3259    Modify a numeric value to bring it to a point in between a maximum and a minimum.
3260    
3261  =over 4  =over 4
3262    
3263  =item $value1, $value2, ... $valueN  =item value
3264    
3265  List of numbers to compare.  Value to constrain.
3266    
3267    =item min (optional)
3268    
3269    Minimum permissible value. If this parameter is undefined, no minimum constraint will be applied.
3270    
3271    =item max (optional)
3272    
3273    Maximum permissible value. If this parameter is undefined, no maximum constraint will be applied.
3274    
3275  =item RETURN  =item RETURN
3276    
3277  Returns the lowest number in the list.  Returns the incoming value, constrained according to the other parameters.
3278    
3279  =back  =back
3280    
3281  =cut  =cut
3282    
3283  sub Min {  sub Constrain {
3284      # Get the parameters. Note that we prime the return value with the first parameter.      # Get the parameters.
3285      my ($retVal, @values) = @_;      my ($value, $min, $max) = @_;
3286      # Loop through the remaining parameters, looking for the lowest.      # Declare the return variable.
3287      for my $value (@values) {      my $retVal = $value;
3288          if ($value < $retVal) {      # Apply the minimum constraint.
3289              $retVal = $value;      if (defined $min && $retVal < $min) {
3290          }          $retVal = $min;
3291        }
3292        # Apply the maximum constraint.
3293        if (defined $max && $retVal > $max) {
3294            $retVal = $max;
3295      }      }
3296      # Return the minimum found.      # Return the result.
3297      return $retVal;      return $retVal;
3298  }  }
3299    
3300  =head3 Max  =head3 Min
3301    
3302  C<< my $max = Max($value1, $value2, ... $valueN); >>      my $min = Min($value1, $value2, ... $valueN);
3303    
3304  Return the maximum argument. The arguments are treated as numbers.  Return the minimum argument. The arguments are treated as numbers.
3305    
3306  =over 4  =over 4
3307    
# Line 1825  Line 3311 
3311    
3312  =item RETURN  =item RETURN
3313    
3314  Returns the highest number in the list.  Returns the lowest number in the list.
3315    
3316  =back  =back
3317    
3318  =cut  =cut
3319    
3320  sub Max {  sub Min {
3321      # Get the parameters. Note that we prime the return value with the first parameter.      # Get the parameters. Note that we prime the return value with the first parameter.
3322      my ($retVal, @values) = @_;      my ($retVal, @values) = @_;
3323      # Loop through the remaining parameters, looking for the highest.      # Loop through the remaining parameters, looking for the lowest.
3324      for my $value (@values) {      for my $value (@values) {
3325          if ($value > $retVal) {          if ($value < $retVal) {
3326              $retVal = $value;              $retVal = $value;
3327          }          }
3328      }      }
3329      # Return the maximum found.      # Return the minimum found.
3330      return $retVal;      return $retVal;
3331  }  }
3332    
3333  =head3 AddToListMap  =head3 Max
3334    
3335  C<< Tracer::AddToListMap(\%hash, $key, $value1, $value2, ... valueN); >>      my $max = Max($value1, $value2, ... $valueN);
3336    
3337  Add a key-value pair to a hash of lists. If no value exists for the key, a singleton list  Return the maximum argument. The arguments are treated as numbers.
 is created for the key. Otherwise, the new value is pushed onto the list.  
3338    
3339  =over 4  =over 4
3340    
3341  =item hash  =item $value1, $value2, ... $valueN
   
 Reference to the target hash.  
   
 =item key  
   
 Key for which the value is to be added.  
   
 =item value1, value2, ... valueN  
   
 List of values to add to the key's value list.  
   
 =back  
   
 =cut  
   
 sub AddToListMap {  
     # Get the parameters.  
     my ($hash, $key, @values) = @_;  
     # Process according to whether or not the key already has a value.  
     if (! exists $hash->{$key}) {  
         $hash->{$key} = [@values];  
     } else {  
         push @{$hash->{$key}}, @values;  
     }  
 }  
3342    
3343  =head3 DebugMode  List of numbers to compare.
3344    
3345  C<< if (Tracer::DebugMode) { ...code... } >>  =item RETURN
3346    
3347  Return TRUE if debug mode has been turned on, else abort.  Returns the highest number in the list.
3348    
3349  Certain CGI scripts are too dangerous to exist in the production  =back
 environment. This method provides a simple way to prevent them  
 from working unless they are explicitly turned on by creating a password  
 cookie via the B<SetPassword> script.  If debugging mode  
 is not turned on, an error will occur.  
3350    
3351  =cut  =cut
3352    
3353  sub DebugMode {  sub Max {
3354      # Declare the return variable.      # Get the parameters. Note that we prime the return value with the first parameter.
3355      my $retVal = 0;      my ($retVal, @values) = @_;
3356      # Check the debug configuration.      # Loop through the remaining parameters, looking for the highest.
3357      my $password = CGI::cookie("DebugMode");      for my $value (@values) {
3358      my $encrypted = Digest::MD5::md5_hex($password);          if ($value > $retVal) {
3359      if ($encrypted eq "252dec43280e0c0d6a75ffcec486e61d") {              $retVal = $value;
         $retVal = 1;  
     } else {  
         # Here debug mode is off, so we generate an error.  
         Confess("Cannot use this facility without logging in.");  
3360      }      }
3361      # Return the determination indicator.      }
3362        # Return the maximum found.
3363      return $retVal;      return $retVal;
3364  }  }
3365    
3366  =head3 Strip  =head3 Strip
3367    
3368  C<< my $string = Tracer::Strip($line); >>      my $string = Tracer::Strip($line);
3369    
3370  Strip all line terminators off a string. This is necessary when dealing with files  Strip all line terminators off a string. This is necessary when dealing with files
3371  that may have been transferred back and forth several times among different  that may have been transferred back and forth several times among different
# Line 1942  Line 3395 
3395      return $retVal;      return $retVal;
3396  }  }
3397    
3398    =head3 Trim
3399    
3400        my $string = Tracer::Trim($line);
3401    
3402    Trim all spaces from the beginning and ending of a string.
3403    
3404    =over 4
3405    
3406    =item line
3407    
3408    Line of text to be trimmed.
3409    
3410    =item RETURN
3411    
3412    The same line of text with all whitespace chopped off either end.
3413    
3414    =back
3415    
3416    =cut
3417    
3418    sub Trim {
3419        # Get a copy of the parameter string.
3420        my ($string) = @_;
3421        my $retVal = (defined $string ? $string : "");
3422        # Strip the front spaces.
3423        $retVal =~ s/^\s+//;
3424        # Strip the back spaces.
3425        $retVal =~ s/\s+$//;
3426        # Return the result.
3427        return $retVal;
3428    }
3429    
3430  =head3 Pad  =head3 Pad
3431    
3432  C<< my $paddedString = Tracer::Pad($string, $len, $left, $padChar); >>      my $paddedString = Tracer::Pad($string, $len, $left, $padChar);
3433    
3434  Pad a string to a specified length. The pad character will be a  Pad a string to a specified length. The pad character will be a
3435  space, and the padding will be on the right side unless specified  space, and the padding will be on the right side unless specified
# Line 2003  Line 3488 
3488      return $retVal;      return $retVal;
3489  }  }
3490    
3491    =head3 Quoted
3492    
3493        my $string = Tracer::Quoted($var);
3494    
3495    Convert the specified value to a string and enclose it in single quotes.
3496    If it's undefined, the string C<undef> in angle brackets will be used
3497    instead.
3498    
3499    =over 4
3500    
3501    =item var
3502    
3503    Value to quote.
3504    
3505    =item RETURN
3506    
3507    Returns a string enclosed in quotes, or an indication the value is undefined.
3508    
3509    =back
3510    
3511    =cut
3512    
3513    sub Quoted {
3514        # Get the parameters.
3515        my ($var) = @_;
3516        # Declare the return variable.
3517        my $retVal;
3518        # Are we undefined?
3519        if (! defined $var) {
3520            $retVal = "<undef>";
3521        } else {
3522            # No, so convert to a string and enclose in quotes.
3523            $retVal = $var;
3524            $retVal =~ s/'/\\'/;
3525            $retVal = "'$retVal'";
3526        }
3527        # Return the result.
3528        return $retVal;
3529    }
3530    
3531  =head3 EOF  =head3 EOF
3532    
3533  This is a constant that is lexically greater than any useful string.  This is a constant that is lexically greater than any useful string.
# Line 2015  Line 3540 
3540    
3541  =head3 TICK  =head3 TICK
3542    
3543  C<< my @results = TICK($commandString); >>      my @results = TICK($commandString);
3544    
3545  Perform a back-tick operation on a command. If this is a Windows environment, any leading  Perform a back-tick operation on a command. If this is a Windows environment, any leading
3546  dot-slash (C<./> will be removed. So, for example, if you were doing  dot-slash (C<./> will be removed. So, for example, if you were doing
# Line 2054  Line 3579 
3579      return `$commandString`;      return `$commandString`;
3580  }  }
3581    
 =head3 ScriptSetup  
   
 C<< my ($cgi, $varHash) = ScriptSetup($noTrace); >>  
3582    
3583  Perform standard tracing and debugging setup for scripts. The value returned is  =head3 CommaFormat
 the CGI object followed by a pre-built variable hash.  
3584    
3585  The C<Trace> form parameter is used to determine whether or not tracing is active and      my $formatted = Tracer::CommaFormat($number);
 which trace modules (other than C<Tracer> itself) should be turned on. Specifying  
 the C<CGI> trace module will trace parameter and environment information. Parameters are  
 traced at level 3 and environment variables at level 4. To trace to a file instead of to  
 the web page, set C<TF> to 1. At the end of the script, the client should call  
 L</ScriptFinish> to output the web page.  
3586    
3587  In some situations, it is not practical to invoke tracing via form parameters. For this  Insert commas into a number.
 situation, you can turn on emergency tracing from the debugging control panel.  
 Tracing will then be turned on automatically for all programs that use the L</ETracing>  
 method, which includes every program that uses this method or L</StandardSetup>.  
3588    
3589  =over 4  =over 4
3590    
3591  =item noTrace (optional)  =item number
3592    
3593  If specified, tracing will be suppressed. This is useful if the script wants to set up  A sequence of digits.
 tracing manually.  
3594    
3595  =item RETURN  =item RETURN
3596    
3597  Returns a two-element list consisting of a CGI query object and a variable hash for  Returns the same digits with commas strategically inserted.
 the output page.  
3598    
3599  =back  =back
3600    
3601  =cut  =cut
3602    
3603  sub ScriptSetup {  sub CommaFormat {
3604      # Get the parameters.      # Get the parameters.
3605      my ($noTrace) = @_;      my ($number) = @_;
3606      # Get the CGI query object.      # Pad the length up to a multiple of three.
3607      my $cgi = CGI->new();      my $padded = "$number";
3608      # Set up tracing if it's not suppressed.      $padded = " " . $padded while length($padded) % 3 != 0;
3609      ETracing($cgi) unless $noTrace;      # This is a fancy PERL trick. The parentheses in the SPLIT pattern
3610      # Create the variable hash.      # cause the delimiters to be included in the output stream. The
3611      my $varHash = { results => '' };      # GREP removes the empty strings in between the delimiters.
3612      # Return the query object and variable hash.      my $retVal = join(",", grep { $_ ne '' } split(/(...)/, $padded));
3613      return ($cgi, $varHash);      # Clean out the spaces.
3614        $retVal =~ s/ //g;
3615        # Return the result.
3616        return $retVal;
3617  }  }
3618    
 =head3 ETracing  
   
 C<< ETracing($parameter); >>  
   
 Set up emergency tracing. Emergency tracing is tracing that is turned  
 on automatically for any program that calls this method. The emergency  
 tracing parameters are stored in a a file identified by a tracing key.  
 If this method is called with a CGI object, then the tracing key is  
 taken from a cookie. If it is called with no parameters, then the tracing  
 key is taken from an environment variable. If it is called with a string,  
 the tracing key is that string.  
   
 =over 4  
3619    
3620  =item parameter  =head3 GetMemorySize
3621    
3622  A parameter from which the tracing key is computed. If it is a scalar,      my $string = Tracer::GetMemorySize();
 that scalar is used as the tracing key. If it is a CGI object, the  
 tracing key is taken from the C<IP> cookie. If it is omitted, the  
 tracing key is taken from the C<TRACING> environment variable. If it  
 is a CGI object and emergency tracing is not on, the C<Trace> and  
 C<TF> parameters will be used to determine the type of tracing.  
3623    
3624  =back  Return a memory size string for the current process. The string will be
3625    in comma format, with a size indicator (K, M, G) at the end.
3626    
3627  =cut  =cut
3628    
3629  sub ETracing {  sub GetMemorySize {
3630      # Get the parameter.      # Get the memory size from Unix.
3631      my ($parameter) = @_;      my ($retVal) = `ps h -o vsz $$`;
3632      # Check for CGI mode.      # Remove the ending new-line.
3633      my $cgi = (ref $parameter eq 'CGI' ? $parameter : undef);      chomp $retVal;
3634      # Default to no tracing except errors.      # Format and return the result.
3635      my ($tracing, $dest) = ("0", "WARN");      return CommaFormat($retVal) . "K";
     # Check for emergency tracing.  
     my $tkey = EmergencyKey($parameter);  
     my $emergencyFile = EmergencyFileName($tkey);  
     if (-e $emergencyFile) {  
         # We have the file. Read in the data.  
         my @tracing = GetFile($emergencyFile);  
         # Pull off the time limit.  
         my $expire = shift @tracing;  
         # Convert it to seconds.  
         $expire *= 3600;  
         # Check the file data.  
         my $stat = stat($emergencyFile);  
         my ($now) = gettimeofday;  
         if ($now - $stat->mtime > $expire) {  
             # Delete the expired file.  
             unlink $emergencyFile;  
         } else {  
             # Emergency tracing is on. Pull off the destination and  
             # the trace level;  
             $dest = shift @tracing;  
             my $level = shift @tracing;  
             # Convert the destination to a real tracing destination.  
             # temp directory.  
             $dest = EmergencyTracingDest($tkey, $dest);  
             # Insure Tracer is specified.  
             my %moduleHash = map { $_ => 1 } @tracing;  
             $moduleHash{Tracer} = 1;  
             # Set the trace parameter.  
             $tracing = join(" ", $level, sort keys %moduleHash);  
         }  
     } elsif (defined $cgi) {  
         # There's no emergency tracing, but we have a CGI object, so check  
         # for tracing from the form parameters.  
         if ($cgi->param('Trace')) {  
             # Here the user has requested tracing via a form.  
             $dest = ($cgi->param('TF') ? ">$FIG_Config::temp/Trace$$.log" : "QUEUE");  
             $tracing = $cgi->param('Trace') . " Tracer";  
         }  
     }  
     # Setup the tracing we've determined from all the stuff above.  
     TSetup($tracing, $dest);  
     # If we're a web script, trace the parameter and environment data.  
     if (defined $cgi) {  
         TraceParms($cgi);  
     }  
3636  }  }
3637    
3638  =head3 EmergencyFileName  =head3 CompareLists
3639    
3640  C<< my $fileName = Tracer::EmergencyFileName($tkey); >>      my ($inserted, $deleted) = Tracer::CompareLists(\@newList, \@oldList, $keyIndex);
3641    
3642  Return the emergency tracing file name. This is the file that specifies  Compare two lists of tuples, and return a hash analyzing the differences. The lists
3643  the tracing information.  are presumed to be sorted alphabetically by the value in the $keyIndex column.
3644    The return value contains a list of items that are only in the new list
3645    (inserted) and only in the old list (deleted).
3646    
3647  =over 4  =over 4
3648    
3649  =item tkey  =item newList
   
 Tracing key for the current program.  
   
 =item RETURN  
   
 Returns the name of the file to contain the emergency tracing information.  
   
 =back