[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.26, Wed Sep 14 13:09:53 2005 UTC revision 1.101, Thu May 8 18:03:10 2008 UTC
# Line 1  Line 1 
1    # -*- perl -*-
2    ########################################################################
3    # Copyright (c) 2003-2006 University of Chicago and Fellowship
4    # for Interpretations of Genomes. All Rights Reserved.
5    #
6    # This file is part of the SEED Toolkit.
7    #
8    # The SEED Toolkit is free software. You can redistribute
9    # it and/or modify it under the terms of the SEED Toolkit
10    # Public License.
11    #
12    # You should have received a copy of the SEED Toolkit Public License
13    # along with this program; if not write to the University of Chicago
14    # at info@ci.uchicago.edu or the Fellowship for Interpretation of
15    # Genomes at veronika@thefig.info or download a copy from
16    # http://www.theseed.org/LICENSE.TXT.
17    ########################################################################
18    
19  package Tracer;  package Tracer;
20    
21      require Exporter;      require Exporter;
22      @ISA = ('Exporter');      @ISA = ('Exporter');
23      @EXPORT = qw(Trace T TSetup QTrace Confess Cluck Min Max Assert Open OpenDir TICK);      @EXPORT = qw(Trace T TSetup QTrace Confess Cluck Min Max Assert Open OpenDir TICK StandardSetup EmergencyKey ETracing Constrain Insure ChDir Emergency Warn);
24      @EXPORT_OK = qw(GetFile GetOptions Merge MergeOptions ParseCommand ParseRecord UnEscape Escape);      @EXPORT_OK = qw(GetFile GetOptions Merge MergeOptions ParseCommand ParseRecord UnEscape Escape PrintLine PutLine);
25      use strict;      use strict;
26      use Carp qw(longmess croak);      use Carp qw(longmess croak carp);
27      use CGI;      use CGI;
28        use Cwd;
29      use FIG_Config;      use FIG_Config;
30      use PageBuilder;      use PageBuilder;
31      use Digest::MD5;      use Digest::MD5;
32        use File::Basename;
33        use File::Path;
34        use File::stat;
35        use LWP::UserAgent;
36        use Time::HiRes 'gettimeofday';
37        use URI::Escape;
38        use Time::Local;
39        use POSIX qw(strftime);
40        use Time::Zone;
41    
42    
43  =head1 Tracing and Debugging Helpers  =head1 Tracing and Debugging Helpers
44    
45  =head2 Introduction  =head2 Tracing
46    
47  This package provides simple tracing for debugging and reporting purposes. To use it simply call the  This package provides simple tracing for debugging and reporting purposes. To use it simply call the
48  L</TSetup> method to set the options and call L</Trace> to write out trace messages. Each trace  L</TSetup> or L</ETracing> method to set the options and call L</Trace> to write out trace messages.
49  message has a I<trace level> and I<category> associated with it. In addition, the tracing package itself  L</TSetup> and L</ETracing> both establish a I<trace level> and a list of I<categories>. Similarly,
50  has a list of categories and a single trace level set by the B<TSetup> method. Only messages whose trace  each trace message has a I<trace level> and I<category> associated with it. Only messages whose trace
51  level is less than or equal to this package's trace level and whose category is activated will  level is less than or equal to the setup trace level and whose category is activated will
52  be written. Thus, a higher trace level on a message indicates that the message  be written. Thus, a higher trace level on a message indicates that the message
53  is less likely to be seen. A higher trace level passed to B<TSetup> means more trace messages will  is less likely to be seen, while a higher trace level passed to B<TSetup> means more trace messages will
54  appear. To generate a trace message, use the following syntax.  appear.
55    
56    =head3 Putting Trace Messages in Your Code
57    
58    To generate a trace message, use the following syntax.
59    
60  C<< Trace($message) if T(errors => 4); >>      Trace($message) if T(errors => 4);
61    
62  This statement will produce a trace message if the trace level is 4 or more and the C<errors>  This statement will produce a trace message if the trace level is 4 or more and the C<errors>
63  category is active. Note that the special category C<main> is always active, so  category is active. There is a special category C<main> that is always active, so
64    
65  C<< Trace($message) if T(main => 4); >>      Trace($message) if T(main => 4);
66    
67  will trace if the trace level is 4 or more.  will trace if the trace level is 4 or more.
68    
# Line 37  Line 70 
70  following call is made in the B<Sprout> package, it will appear if the C<Sprout> category is  following call is made in the B<Sprout> package, it will appear if the C<Sprout> category is
71  active and the trace level is 2 or more.  active and the trace level is 2 or more.
72    
73  C<< Trace($message) if T(2); >>      Trace($message) if T(2);
74    
75  To set up tracing, you call the L</TSetup> method. The method takes as input a trace level, a list  In scripts, where no package name is available, the category defaults to C<main>.
76  of category names, and a set of options. The trace level and list of category names are  
77    =head3 Custom Tracing
78    
79    Many programs have customized tracing configured using the L</TSetup> method. This is no longer
80    the preferred method, but a knowledge of how custom tracing works can make the more modern
81    L</Emergency Tracing> easier to understand.
82    
83    To set up custom tracing, you call the L</TSetup> method. The method takes as input a trace level,
84    a list of category names, and a destination. The trace level and list of category names are
85  specified as a space-delimited string. Thus  specified as a space-delimited string. Thus
86    
87  C<< TSetup('3 errors Sprout ERDB', 'HTML'); >>      TSetup('3 errors Sprout ERDB', 'TEXT');
88    
89  sets the trace level to 3, activates the C<errors>, C<Sprout>, and C<ERDB> categories, and  sets the trace level to 3, activates the C<errors>, C<Sprout>, and C<ERDB> categories, and
90  specifies that messages should be output as HTML paragraphs.  specifies that messages should be sent to the standard output.
91    
92  To turn on tracing for ALL categories, use an asterisk. The call below sets every category to  To turn on tracing for ALL categories, use an asterisk. The call below sets every category to
93  level 3 and writes the output to the standard error output. This sort of thing might be  level 3 and writes the output to the standard error output. This sort of thing might be
94  useful in a CGI environment.  useful in a CGI environment.
95    
96  C<< TSetup('3 *', 'WARN'); >>      TSetup('3 *', 'WARN');
97    
98  In addition to HTML and file output for trace messages, you can specify that the trace messages  In addition standard error and file output for trace messages, you can specify that the trace messages
99  be queued. The messages can then be retrieved by calling the L</QTrace> method. This approach  be queued. The messages can then be retrieved by calling the L</QTrace> method. This approach
100  is useful if you are building a web page. Instead of having the trace messages interspersed with  is useful if you are building a web page. Instead of having the trace messages interspersed with
101  the page output, they can be gathered together and displayed at the end of the page. This makes  the page output, they can be gathered together and displayed at the end of the page. This makes
102  it easier to debug page formatting problems.  it easier to debug page formatting problems.
103    
104  Finally, you can specify that all trace messages be emitted as warnings.  Finally, you can specify that all trace messages be emitted to a file, or the standard output and
105    a file at the same time. To trace to a file, specify the filename with an output character in front
106    of it.
107    
108        TSetup('4 SQL', ">$fileName");
109    
110    To trace to the standard output and a file at the same time, put a C<+> in front of the angle
111    bracket.
112    
113        TSetup('3 *', "+>$fileName");
114    
115  The flexibility of tracing makes it superior to simple use of directives like C<die> and C<warn>.  The flexibility of tracing makes it superior to simple use of directives like C<die> and C<warn>.
116  Tracer calls can be left in the code with minimal overhead and then turned on only when needed.  Tracer calls can be left in the code with minimal overhead and then turned on only when needed.
117  Thus, debugging information is available and easily retrieved even when the application is  Thus, debugging information is available and easily retrieved even when the application is
118  being used out in the field.  being used out in the field.
119    
120    =head3 Trace Levels
121    
122  There is no hard and fast rule on how to use trace levels. The following is therefore only  There is no hard and fast rule on how to use trace levels. The following is therefore only
123  a suggestion.  a suggestion.
124    
125  =over 4  =over 4
126    
127  =item 0 Error  =item Error 0
128    
129  Message indicates an error that may lead to incorrect results or that has stopped the  Message indicates an error that may lead to incorrect results or that has stopped the
130  application entirely.  application entirely.
131    
132  =item 1 Warning  =item Warning 1
133    
134  Message indicates something that is unexpected but that probably did not interfere  Message indicates something that is unexpected but that probably did not interfere
135  with program execution.  with program execution.
136    
137  =item 2 Notice  =item Notice 2
138    
139  Message indicates the beginning or end of a major task.  Message indicates the beginning or end of a major task.
140    
141  =item 3 Information  =item Information 3
142    
143  Message indicates a subtask. In the FIG system, a subtask generally relates to a single  Message indicates a subtask. In the FIG system, a subtask generally relates to a single
144  genome. This would be a big loop that is not expected to execute more than 500 times or so.  genome. This would be a big loop that is not expected to execute more than 500 times or so.
145    
146  =item 4 Detail  =item Detail 4
147    
148  Message indicates a low-level loop iteration.  Message indicates a low-level loop iteration.
149    
150  =back  =back
151    
152    The format of trace messages is important because some utilities analyze trace files.
153    There are three fields-- the time stamp, the category name, and the text.
154    The time stamp is between square brackets and the category name between angle brackets.
155    After the category name there is a colon (C<:>) followed by the message text.
156    If the square brackets or angle brackets are missing, then the trace management
157    utilities assume that they are encountering a set of pre-formatted lines.
158    
159    Note, however, that this formatting is done automatically by the tracing functions. You
160    only need to know about it if you want to parse a trace file.
161    
162    =head3 Emergency Tracing
163    
164    Sometimes, you need a way for tracing to happen automatically without putting parameters
165    in a form or on the command line. Emergency tracing does this. You invoke emergency tracing
166    from the debug form, which is accessed from the [[DebugConsole]]. Emergency tracing requires
167    that you specify a tracing key. For command-line tools, the key is
168    taken from the C<TRACING> environment variable. For web services, the key is taken from
169    a cookie. Either way, the key tells the tracing facility who you are, so that you control
170    the tracing in your environment without stepping on other users.
171    
172    The key can be anything you want. If you don't have a key, the C<SetPassword> page will
173    generate one for you.
174    
175    You can activate and de-activate emergency tracing from the debugging control panel, as
176    well as display the trace file itself.
177    
178    To enable emergency tracing in your code, call
179    
180        ETracing($cgi)
181    
182    from a web script and
183    
184        ETracing()
185    
186    from a command-line script.
187    
188    The web script will look for the tracing key in the cookies, and the command-line
189    script will look for it in the C<TRACING> environment variable. If you are
190    using the L</StandardSetup> method or a [[WebApplication]], emergency tracing
191    will be configured automatically.
192    
193  =cut  =cut
194    
195  # Declare the configuration variables.  # Declare the configuration variables.
196    
197  my $Destination = "NONE";   # Description of where to send the trace output.  my $Destination = "WARN";   # Description of where to send the trace output.
198  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
199                              # standard output                              # standard output
200  my %Categories = ( main => 1 );  my %Categories = ( main => 1 );
201                              # hash of active category names                              # hash of active category names
202    my @LevelNames = qw(error warn notice info detail);
203  my $TraceLevel = 0;         # trace level; a higher trace level produces more  my $TraceLevel = 0;         # trace level; a higher trace level produces more
204                              # messages                              # messages
205  my @Queue = ();             # queued list of trace messages.  my @Queue = ();             # queued list of trace messages.
206  my $LastCategory = "main";  # name of the last category interrogated  my $LastCategory = "main";  # name of the last category interrogated
207    my $LastLevel = 0;          # level of the last test call
208  my $SetupCount = 0;         # number of times TSetup called  my $SetupCount = 0;         # number of times TSetup called
209  my $AllTrace = 0;           # TRUE if we are tracing all categories.  my $AllTrace = 0;           # TRUE if we are tracing all categories.
210    my $SavedCGI;               # CGI object passed to ETracing
211    umask 2;                    # Fix the damn umask so everything is group-writable.
212    
213    =head2 Tracing Methods
214    
215    =head3 Setups
216    
217        my $count = Tracer::Setups();
218    
219    Return the number of times L</TSetup> has been called.
220    
221    This method allows for the creation of conditional tracing setups where, for example, we
222    may want to set up tracing if nobody else has done it before us.
223    
224  =head2 Public Methods  =cut
225    
226    sub Setups {
227        return $SetupCount;
228    }
229    
230  =head3 TSetup  =head3 TSetup
231    
232  C<< TSetup($categoryList, $target); >>      TSetup($categoryList, $target);
233    
234  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
235  and interrogated by the L</Trace> and L</T> methods.  and interrogated by the L</Trace> and L</T> methods.
# Line 157  Line 269 
269      # Presume category-based tracing until we learn otherwise.      # Presume category-based tracing until we learn otherwise.
270      $AllTrace = 0;      $AllTrace = 0;
271      # Build the category hash. Note that if we find a "*", we turn on non-category      # Build the category hash. Note that if we find a "*", we turn on non-category
272      # tracing.      # tracing. We must also clear away any pre-existing data.
273        %Categories = ( main => 1 );
274      for my $category (@categoryData) {      for my $category (@categoryData) {
275          if ($category eq '*') {          if ($category eq '*') {
276              $AllTrace = 1;              $AllTrace = 1;
# Line 166  Line 279 
279          }          }
280      }      }
281      # Now we need to process the destination information. The most important special      # Now we need to process the destination information. The most important special
282      # 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
283      # "+" prefix which indicates a double echo.      # ">>" (append). A leading "+" for either indicates that we are also writing to
284        # the standard output (tee mode).
285      if ($target =~ m/^\+?>>?/) {      if ($target =~ m/^\+?>>?/) {
286          if ($target =~ m/^\+/) {          if ($target =~ m/^\+/) {
287              $TeeFlag = 1;              $TeeFlag = 1;
288              $target = substr($target, 1);              $target = substr($target, 1);
289          }          }
290          if ($target =~ m/^>[^>]/) {          if ($target =~ m/^>[^>]/) {
291                # We need to initialize the file (which clears it).
292              open TRACEFILE, $target;              open TRACEFILE, $target;
293              print TRACEFILE Now() . " Tracing initialized.\n";              print TRACEFILE "[" . Now() . "] [notice] [Tracer] Tracing initialized.\n";
294              close TRACEFILE;              close TRACEFILE;
295                # Set to append mode now that the file has been cleared.
296              $Destination = ">$target";              $Destination = ">$target";
297          } else {          } else {
298              $Destination = $target;              $Destination = $target;
# Line 188  Line 304 
304      $SetupCount++;      $SetupCount++;
305  }  }
306    
307  =head3 Setups  =head3 SetLevel
   
 C<< my $count = Tracer::Setups(); >>  
   
 Return the number of times L</TSetup> has been called.  
   
 This method allows for the creation of conditional tracing setups where, for example, we  
 may want to set up tracing if nobody else has done it before us.  
   
 =cut  
308    
309  sub Setups {      Tracer::SetLevel($newLevel);
     return $SetupCount;  
 }  
310    
311  =head3 Open  Modify the trace level. A higher trace level will cause more messages to appear.
312    
313  C<< my $handle = Open($fileHandle, $fileSpec, $message); >>  =over 4
314    
315  Open a file.  =item newLevel
316    
317  The I<$fileSpec> is essentially the second argument of the PERL C<open>  Proposed new trace level.
 function. The mode is specified using Unix-like shell information. So, for  
 example,  
318    
319      Open(\*LOGFILE, '>>/usr/spool/news/twitlog', "Could not open twit log.");  =back
320    
321  would open for output appended to the specified file, and  =cut
322    
323      Open(\*DATASTREAM, "| sort -u >$outputFile", "Could not open $outputFile.");  sub SetLevel {
324        $TraceLevel = $_[0];
325    }
326    
327  would open a pipe that sorts the records written and removes duplicates. Note  =head3 ParseDate
 the use of file handle syntax in the Open call. To use anonymous file handles,  
 code as follows.  
328    
329      my $logFile = Open(undef, '>>/usr/spool/news/twitlog', "Could not open twit log.");      my $time = Tracer::ParseDate($dateString);
330    
331  The I<$message> parameter is used if the open fails. If it is set to C<0>, then  Convert a date into a PERL time number. This method expects a date-like string
332  the open returns TRUE if successful and FALSE if an error occurred. Otherwise, a  and parses it into a number. The string must be vaguely date-like or it will
333  failed open will throw an exception and the third parameter will be used to construct  return an undefined value. Our requirement is that a month and day be
334  an error message. If the parameter is omitted, a standard message is constructed  present and that three pieces of the date string (time of day, month and day,
335  using the file spec.  year) be separated by likely delimiters, such as spaces, commas, and such-like.
336    
337      Could not open "/usr/spool/news/twitlog"  If a time of day is present, it must be in military time with two digits for
338    everything but the hour.
339    
340  Note that the mode characters are automatically cleaned from the file name.  The year must be exactly four digits.
 The actual error message from the file system will be captured and appended to the  
 message in any case.  
341    
342      Could not open "/usr/spool/news/twitlog": file not found.  Additional stuff can be in the string. We presume it's time zones or weekdays or something
343    equally innocuous. This means, however, that a sufficiently long sentence with date-like
344    parts in it may be interpreted as a date. Hopefully this will not be a problem.
345    
346  In some versions of PERL the only error message we get is a number, which  It should be guaranteed that this method will parse the output of the L</Now> function.
 corresponds to the C++ C<errno> value.  
347    
348      Could not open "/usr/spool/news/twitlog": 6.  The parameters are as follows.
349    
350  =over 4  =over 4
351    
352  =item fileHandle  =item dateString
   
 File handle. If this parameter is C<undef>, a file handle will be generated  
 and returned as the value of this method.  
   
 =item fileSpec  
   
 File name and mode, as per the PERL C<open> function.  
   
 =item message (optional)  
353    
354  Error message to use if the open fails. If omitted, a standard error message  The date string to convert.
 will be generated. In either case, the error information from the file system  
 is appended to the message. To specify a conditional open that does not throw  
 an error if it fails, use C<0>.  
355    
356  =item RETURN  =item RETURN
357    
358  Returns the name of the file handle assigned to the file, or C<undef> if the  Returns a PERL time, that is, a number of seconds since the epoch, or C<undef> if
359  open failed.  the date string is invalid. A valid date string must contain a month and day.
360    
361  =back  =back
362    
363  =cut  =cut
364    
365  sub Open {  # Universal month conversion table.
366    use constant MONTHS => {    Jan =>  0, January   =>  0, '01' =>  0,  '1' =>  0,
367                                Feb =>  1, February  =>  1, '02' =>  1,  '2' =>  1,
368                                Mar =>  2, March     =>  2, '03' =>  2,  '3' =>  2,
369                                Apr =>  3, April     =>  3, '04' =>  3,  '4' =>  3,
370                                May =>  4, May       =>  4, '05' =>  4,  '5' =>  4,
371                                Jun =>  5, June      =>  5, '06' =>  5,  '6' =>  5,
372                                Jul =>  6, July      =>  6, '07' =>  6,  '7' =>  6,
373                                Aug =>  7, August    =>  7, '08' =>  7,  '8' =>  7,
374                                Sep =>  8, September =>  8, '09' =>  8,  '9' =>  8,
375                                Oct =>  9, October  =>   9, '10' =>  9,
376                                Nov => 10, November =>  10, '11' => 10,
377                                Dec => 11, December =>  11, '12' => 11
378                            };
379    
380    sub ParseDate {
381      # Get the parameters.      # Get the parameters.
382      my ($fileHandle, $fileSpec, $message) = @_;      my ($dateString) = @_;
383      # Attempt to open the file.      # Declare the return variable.
384      my $rv = open $fileHandle, $fileSpec;      my $retVal;
385      # If the open failed, generate an error message.      # Find the month and day of month. There are two ways that can happen. We check for the
386      if (! $rv) {      # numeric style first. That way, if the user's done something like "Sun 12/22", then we
387          # Save the system error message.      # won't be fooled into thinking the month is Sunday.
388          my $sysMessage = $!;      if ($dateString =~ m#\b(\d{1,2})/(\d{1,2})\b# || $dateString =~ m#\b(\w+)\s(\d{1,2})\b#) {
389          # See if we need a default message.          my ($mon, $mday) = (MONTHS->{$1}, $2);
390          if (!$message) {          # Insist that the month and day are valid.
391              # Clean any obvious mode characters and leading spaces from the          if (defined($mon) && $2 >= 1 && $2 <= 31) {
392              # filename.              # Find the time.
393              my ($fileName) = FindNamePart($fileSpec);              my ($hour, $min, $sec) = (0, 0, 0);
394              $message = "Could not open \"$fileName\"";              if ($dateString =~ /\b(\d{1,2}):(\d{2}):(\d{2})\b/) {
395                    ($hour, $min, $sec) = ($1, $2, $3);
396                }
397                # Find the year.
398                my $year;
399                if ($dateString =~ /\b(\d{4})\b/) {
400                    $year = $1;
401                } else {
402                    # Get the default year, which is this one. Note we must convert it to
403                    # the four-digit value expected by "timelocal".
404                    (undef, undef, undef, undef, undef, $year) = localtime();
405                    $year += 1900;
406          }          }
407          # Terminate with an error using the supplied message and the              $retVal = timelocal($sec, $min, $hour, $mday, $mon, $year);
         # error message from the file system.  
         Confess("$message: $!");  
408      }      }
409      # Return the file handle.      }
410      return $fileHandle;      # Return the result.
411        return $retVal;
412  }  }
413    
414  =head3 FindNamePart  =head3 LogErrors
   
 C<< my ($fileName, $start, $len) = Tracer::FindNamePart($fileSpec); >>  
   
 Extract the portion of a file specification that contains the file name.  
   
 A file specification is the string passed to an C<open> call. It specifies the file  
 mode and name. In a truly complex situation, it can specify a pipe sequence. This  
 method assumes that the file name is whatever follows the first angle bracket  
 sequence.  So, for example, in the following strings the file name is  
 C</usr/fig/myfile.txt>.  
415    
416      >>/usr/fig/myfile.txt      Tracer::LogErrors($fileName);
     </usr/fig/myfile.txt  
     | sort -u > /usr/fig/myfile.txt  
417    
418  If the method cannot find a file name using its normal methods, it will return the  Route the standard error output to a log file.
 whole incoming string.  
419    
420  =over 4  =over 4
421    
422  =item fileSpec  =item fileName
   
 File specification string from which the file name is to be extracted.  
   
 =item RETURN  
423    
424  Returns a three-element list. The first element contains the file name portion of  Name of the file to receive the error output.
 the specified string, or the whole string if a file name cannot be found via normal  
 methods. The second element contains the start position of the file name portion and  
 the third element contains the length.  
425    
426  =back  =back
427    
428  =cut  =cut
 #: Return Type $;  
 sub FindNamePart {  
     # Get the parameters.  
     my ($fileSpec) = @_;  
     # Default to the whole input string.  
     my ($retVal, $pos, $len) = ($fileSpec, 0, length $fileSpec);  
     # Parse out the file name if we can.  
     if ($fileSpec =~ m/(<|>>?)(.+?)(\s*)$/) {  
         $retVal = $2;  
         $len = length $retVal;  
         $pos = (length $fileSpec) - (length $3) - $len;  
     }  
     # Return the result.  
     return ($retVal, $pos, $len);  
 }  
   
 =head3 OpenDir  
   
 C<< my @files = OpenDir($dirName, $filtered); >>  
   
 Open a directory and return all the file names. This function essentially performs  
 the functions of an C<opendir> and C<readdir>. If the I<$filtered> parameter is  
 set to TRUE, all filenames beginning with a period (C<.>) will be filtered out of  
 the return list. If the directory does not open, an exception is thrown. So,  
 for example,  
   
     my @files = OpenDir("/Volumes/fig/contigs", 1);  
429    
430  is effectively the same as  sub LogErrors {
431        # Get the file name.
432      opendir(TMP, "/Volumes/fig/contigs") || Confess("Could not open /Volumes/fig/contigs.");      my ($fileName) = @_;
433      my @files = grep { $_ !~ /^\./ } readdir(TMP);      # Open the file as the standard error output.
434        open STDERR, '>', $fileName;
435    }
436    
437  Similarly, the following code  =head3 Trace
438    
439      my @files = grep { $_ =~ /^\d/ } OpenDir("/Volumes/fig/orgs");      Trace($message);
440    
441  Returns the names of all files in C</Volumes/fig/orgs> that begin with digits and  Write a trace message to the target location specified in L</TSetup>. If there has not been
442  automatically throws an error if the directory fails to open.  any prior call to B<TSetup>.
443    
444  =over 4  =over 4
445    
446  =item dirName  =item message
   
 Name of the directory to open.  
   
 =item filtered  
447    
448  TRUE if files whose names begin with a period (C<.>) should be automatically removed  Message to write.
 from the list, else FALSE.  
449    
450  =back  =back
451    
452  =cut  =cut
453  #: Return Type @;  
454  sub OpenDir {  sub Trace {
455      # Get the parameters.      # Get the parameters.
456      my ($dirName, $filtered) = @_;      my ($message) = @_;
457      # Declare the return variable.      # Strip off any line terminators at the end of the message. We will add
458      my @retVal;      # new-line stuff ourselves.
459      # Open the directory.      my $stripped = Strip($message);
460      if (opendir(my $dirHandle, $dirName)) {      # Compute the caller information.
461          # The directory opened successfully. Get the appropriate list according to the      my ($callPackage, $callFile, $callLine) = caller();
462          # strictures of the filter parameter.      my $callFileTitle = basename($callFile);
463          if ($filtered) {      # Check the caller.
464              @retVal = grep { $_ !~ /^\./ } readdir $dirHandle;      my $callerInfo = ($callFileTitle ne "Tracer.pm" ? " [$callFileTitle $callLine]" : "");
465          } else {      # Get the timestamp.
466              @retVal = readdir $dirHandle;      my $timeStamp = Now();
467        # Build the prefix.
468        my $level = $LevelNames[$LastLevel] || "($LastLevel)";
469        my $prefix = "[$timeStamp] [$level] [$LastCategory]$callerInfo";
470        # Format the message.
471        my $formatted = "$prefix $stripped";
472        # Process according to the destination.
473        if ($Destination eq "TEXT") {
474            # Write the message to the standard output.
475            print "$formatted\n";
476        } elsif ($Destination eq "ERROR") {
477            # Write the message to the error output. Here, we want our prefix fields.
478            print STDERR "$formatted\n";
479        } elsif ($Destination eq "WARN") {
480            # Emit the message to the standard error output. It is presumed that the
481            # error logger will add its own prefix fields, the notable exception being
482            # the caller info.
483            print STDERR "$callerInfo$stripped\n";
484        } elsif ($Destination eq "QUEUE") {
485            # Push the message into the queue.
486            push @Queue, "$formatted";
487        } elsif ($Destination eq "HTML") {
488            # Convert the message to HTML and write it to the standard output.
489            my $escapedMessage = CGI::escapeHTML($stripped);
490            print "<p>$timeStamp $LastCategory $LastLevel: $escapedMessage</p>\n";
491        } elsif ($Destination =~ m/^>>/) {
492            # Write the trace message to an output file.
493            open(TRACING, $Destination) || die "Tracing open for \"$Destination\" failed: $!";
494            print TRACING "$formatted\n";
495            close TRACING;
496            # If the Tee flag is on, echo it to the standard output.
497            if ($TeeFlag) {
498                print "$formatted\n";
499          }          }
     } else {  
         # Here the directory would not open.  
         Confess("Could not open directory $dirName.");  
500      }      }
     # Return the result.  
     return @retVal;  
501  }  }
502    
503  =head3 SetLevel  =head3 T
504    
505  C<< Tracer::SetLevel($newLevel); >>      my $switch = T($category, $traceLevel);
506    
507  Modify the trace level. A higher trace level will cause more messages to appear.      or
508    
509  =over 4      my $switch = T($traceLevel);
510    
511  =item newLevel  Return TRUE if the trace level is at or above a specified value and the specified category
512    is active, else FALSE. If no category is specified, the caller's package name is used.
513    
514  Proposed new trace level.  =over 4
515    
516  =back  =item category
517    
518  =cut  Category to which the message belongs. If not specified, the caller's package name is
519    used.
520    
521  sub SetLevel {  =item traceLevel
     $TraceLevel = $_[0];  
 }  
522    
523  =head3 Now  Relevant tracing level.
524    
525    =item RETURN
526    
527    TRUE if a message at the specified trace level would appear in the trace, else FALSE.
528    
529    =back
530    
531    =cut
532    
533    sub T {
534        # Declare the return variable.
535        my $retVal = 0;
536        # Only proceed if tracing is turned on.
537        if ($Destination ne "NONE") {
538            # Get the parameters.
539            my ($category, $traceLevel) = @_;
540            if (!defined $traceLevel) {
541                # Here we have no category, so we need to get the calling package.
542                # The calling package is normally the first parameter. If it is
543                # omitted, the first parameter will be the tracelevel. So, the
544                # first thing we do is shift the so-called category into the
545                # $traceLevel variable where it belongs.
546                $traceLevel = $category;
547                my ($package, $fileName, $line) = caller;
548                # If there is no calling package, we default to "main".
549                if (!$package) {
550                    $category = "main";
551                } else {
552                    my @cats = split /::/, $package;
553                    $category = $cats[$#cats];
554                }
555            }
556            # Save the category name and level.
557            $LastCategory = $category;
558            $LastLevel = $traceLevel;
559            # Convert it to lower case before we hash it.
560            $category = lc $category;
561            # Validate the trace level.
562            if (ref $traceLevel) {
563                Confess("Bad trace level.");
564            } elsif (ref $TraceLevel) {
565                Confess("Bad trace config.");
566            }
567            # Make the check. Note that level 0 shows even if the category is turned off.
568            $retVal = ($traceLevel <= $TraceLevel && ($traceLevel == 0 || $AllTrace || exists $Categories{$category}));
569        }
570        # Return the computed result.
571        return $retVal;
572    }
573    
574    =head3 QTrace
575    
576        my $data = QTrace($format);
577    
578    Return the queued trace data in the specified format.
579    
580    =over 4
581    
582    =item format
583    
584    C<html> to format the data as an HTML list, C<text> to format it as straight text.
585    
586    =back
587    
588    =cut
589    
590    sub QTrace {
591        # Get the parameter.
592        my ($format) = @_;
593        # Create the return variable.
594        my $retVal = "";
595        # Only proceed if there is an actual queue.
596        if (@Queue) {
597            # Process according to the format.
598            if ($format =~ m/^HTML$/i) {
599                # Convert the queue into an HTML list.
600                $retVal = "<ul>\n";
601                for my $line (@Queue) {
602                    my $escapedLine = CGI::escapeHTML($line);
603                    $retVal .= "<li>$escapedLine</li>\n";
604                }
605                $retVal .= "</ul>\n";
606            } elsif ($format =~ m/^TEXT$/i) {
607                # Convert the queue into a list of text lines.
608                $retVal = join("\n", @Queue) . "\n";
609            }
610            # Clear the queue.
611            @Queue = ();
612        }
613        # Return the formatted list.
614        return $retVal;
615    }
616    
617    =head3 Confess
618    
619        Confess($message);
620    
621    Trace the call stack and abort the program with the specified message. When used with
622    the OR operator and the L</Assert> method, B<Confess> can function as a debugging assert.
623    So, for example
624    
625        Assert($recNum >= 0) || Confess("Invalid record number $recNum.");
626    
627    Will abort the program with a stack trace if the value of C<$recNum> is negative.
628    
629    =over 4
630    
631    =item message
632    
633    Message to include in the trace.
634    
635    =back
636    
637    =cut
638    
639    sub Confess {
640        # Get the parameters.
641        my ($message) = @_;
642        # Set up the category and level.
643        $LastCategory = "(confess)";
644        $LastLevel = 0;
645        if (! defined($FIG_Config::no_tool_hdr)) {
646            # Here we have a tool header. Display its length so that the user can adjust the line numbers.
647            my $toolHeaderFile = "$FIG_Config::fig_disk/dist/releases/current/$FIG_Config::arch/tool_hdr";
648            # Only proceed if the tool header file is actually present.
649            if (-f $toolHeaderFile) {
650                my $fh;
651                if (open $fh, "<$toolHeaderFile") {
652                    my @lines = <$fh>;
653                    Trace("Tool header has " . scalar(@lines) . " lines.");
654                }
655            }
656        }
657        # Trace the call stack.
658        Cluck($message);
659        # Abort the program.
660        croak(">>> $message");
661    }
662    
663    =head3 Warn
664    
665        Warn($message);
666    
667    This method traces an important message. If an RSS feed is configured
668    (via I<FIG_Config::error_feed>) and the tracing destination is C<WARN>,
669    then the message will be echoed to the feed. In general, a tracing
670    destination of C<WARN> indicates that the caller is running as a web
671    service in a production environment; however, this is not a requirement.
672    
673    The L</Cluck> method calls this one for its final message. Since
674    L</Confess> calls L</Cluck>, this means that any error which is caught
675    and confessed will put something in the feed. This insures that someone
676    will be alerted relatively quickly when a failure occurs.
677    
678    =over 4
679    
680    =item message
681    
682    Message to be traced.
683    
684    =back
685    
686    =cut
687    
688    sub Warn {
689        # Get the parameters.
690        my ($message) = @_;
691        # Trace the message.
692        Trace($message);
693        # An error here would be disastrous. Note, however, that we aren't too worried
694        # about losing events. The error log is always available for the occasions where
695        # we mess up.
696        eval {
697            # Do we need to put this in the RSS feed?
698            if ($FIG_Config::error_feed && $Destination eq 'WARN') {
699                # Yes. We now need to compute the date, the link, and the title.
700                # First, the date, in a very specific format.
701                my $date = strftime("%a, %02e %b %H:%M:%S %Y ", localtime) .
702                    (tz_local_offset() / 30);
703                # Environment data goes in here.
704                my $environment;
705                # HTML-escape the message and remove excess space.
706                my $title = CGI::escapeHTML($message);
707                $title =~ s/\s+/ /gs;
708                # Compute the title from the message. If it's too long, we have to
709                # split it up.
710                if (length $title > 60) {
711                    $title = substr($title, 0, 50) . "...";
712                }
713                # We'll put the link in here.
714                my $link;
715                # If we have a CGI object, then this is a web error. Otherwise, it's
716                # command-line.
717                if (defined $SavedCGI) {
718                    # We're in a web service. The environment is the user's IP, and the link
719                    # is the URL that got us here.
720                    my $key = $ENV{HTTP_X_FORWARDED_FOR} || $ENV{REMOTE_ADDR};
721                    $environment = "Event Reported at IP address $key.";
722                    $link = $SavedCGI->url(-full => 1, -query => 1);
723                    # We need the user agent string and (if available) the referrer.
724                    $environment .= "User Agent $ENV{HTTP_USER_AGENT}";
725                    if ($ENV{HTTP_REFERER}) {
726                        $environment .= " referred from $ENV{HTTP_REFERER}"
727                    }
728                    # Close off the sentence.
729                    $environment .= ". ";
730                } else {
731                    # No CGI object, so we're a command-line tool. Use the tracing
732                    # key and the PID as the user identifier, and add the command.
733                    my $key = EmergencyKey();
734                    $environment = "Event Reported by $key Process $$. Command $ENV{_}.";
735                    # Set the link to the development NMPDR. There is really no good
736                    # choice here.
737                    $link = "http://$FIG_Config::dev_server";
738                }
739                # Build a GUID. We use the current time, the title, and the process ID,
740                # then digest the result.
741                my $guid = Digest::MD5::md5_base64(gettimeofday(), $title, $$);
742                # Finally, the description. This is a stack trace plus various environmental stuff.
743                # We have a goofy thing here in that we need to HTML-escape some sections of the description
744                # twice. They will be unescaped once when processed by the RSS reader. First, the stack
745                # trace.
746                my $stackTrace = "";
747                my @trace = LongMess();
748                # Only proceed if we got something back.
749                if (scalar(@trace) > 0) {
750                    $trace[0] =~ s/Tracer::Warn.+?called/Event occurred/;
751                    $stackTrace = "Stack trace:<pre>" . join("\n", @trace, "</pre>");
752                }
753                my $basicDescription = CGI::escapeHTML($message) . "<br /><br />" .
754                        CGI::escapeHTML($environment) . $stackTrace;
755                # Okay, we have all the pieces. Create a hash of the new event.
756                my $newItem = { title => $title,
757                                description => CGI::escapeHTML($basicDescription),
758                                link => $link,
759                                category => $LastCategory,
760                                pubDate => $date,
761                                guid => $guid,
762                               };
763                # We need XML capability for this.
764                require XML::Simple;
765                # The RSS document goes in here.
766                my $rss;
767                # Get the name of the RSS file. It's in the FIG temporary directory.
768                my $fileName = "$FIG_Config::temp/$FIG_Config::error_feed";
769                # Does it exist?
770                if (-s $fileName) {
771                    # Slurp it in.
772                    $rss = XML::Simple::XMLin($fileName, ForceArray => ['item']);
773                } else {
774                    my $size = -s $fileName;
775                    # Create an empty channel.
776                    $rss = {
777                        channel => {
778                            title => 'NMPDR Warning Feed',
779                            link => "$FIG_Config::temp_url/$FIG_Config::error_feed",
780                            description => "Important messages regarding the status of the NMPDR.",
781                            generator => "NMPDR Trace Facility",
782                            docs => "http://blogs.law.harvard.edu/tech/rss",
783                            item => []
784                        },
785                    };
786                }
787                # Get the channel object.
788                my $channel = $rss->{channel};
789                # Update the last-build date.
790                $channel->{lastBuildDate} = $date;
791                # Get the item array.
792                my $items = $channel->{item};
793                # Insure it has only 100 entries.
794                while (scalar @{$items} > 100) {
795                    pop @{$items};
796                }
797                # Add our new item at the front.
798                unshift @{$items}, $newItem;
799                # Replace the file.
800                my $xml = XML::Simple::XMLout($rss, NoAttr => 1, RootName => 'rss version="2.0"', XmlDecl => '<?xml version="1.0" encoding="utf-8"?>',
801                                              NoEscape => 1);
802                # We don't use Open here because we can't afford an error.
803                if (open XMLOUT, ">$fileName") {
804                    print XMLOUT $xml;
805                    close XMLOUT;
806                }
807            }
808        };
809        # There's no "if ($@)" here, because putting an error message in the log
810        # saying that we missed putting an error message in the feed of messages
811        # in the log is not going to help anybody.
812    }
813    
814    =head3 Assert
815    
816        Assert($condition1, $condition2, ... $conditionN);
817    
818    Return TRUE if all the conditions are true. This method can be used in conjunction with
819    the OR operator and the L</Confess> method as a debugging assert.
820    So, for example
821    
822        Assert($recNum >= 0) || Confess("Invalid record number $recNum.");
823    
824    Will abort the program with a stack trace if the value of C<$recNum> is negative.
825    
826    =cut
827    sub Assert {
828        my $retVal = 1;
829        LOOP: for my $condition (@_) {
830            if (! $condition) {
831                $retVal = 0;
832                last LOOP;
833            }
834        }
835        return $retVal;
836    }
837    
838    =head3 Cluck
839    
840        Cluck($message);
841    
842    Trace the call stack. Note that for best results, you should qualify the call with a
843    trace condition. For example,
844    
845        Cluck("Starting record parse.") if T(3);
846    
847    will only trace the stack if the trace level for the package is 3 or more.
848    
849    =over 4
850    
851    =item message
852    
853    Message to include in the trace.
854    
855    =back
856    
857    =cut
858    
859    sub Cluck {
860        # Get the parameters.
861        my ($message) = @_;
862        # Trace what's happening.
863        Trace("Stack trace for event: $message");
864        # Get the stack trace.
865        my @trace = LongMess();
866        # Convert the trace to a series of messages.
867        for my $line (@trace) {
868            # Replace the tab at the beginning with spaces.
869            $line =~ s/^\t/    /;
870            # Trace the line.
871            Trace($line);
872        }
873        # Issue a warning. This displays the event message and inserts it into the RSS error feed.
874        Warn($message);
875    }
876    
877    =head3 LongMess
878    
879        my @lines = Tracer::LongMess();
880    
881    Return a stack trace with all tracing methods removed. The return will be in the form of a list
882    of message strings.
883    
884    =cut
885    
886    sub LongMess {
887        # Declare the return variable.
888        my @retVal = ();
889        my $confession = longmess("");
890        for my $line (split /\s*\n/, $confession) {
891            unless ($line =~ /Tracer\.pm/) {
892                # Here we have a line worth keeping. Push it onto the result list.
893                push @retVal, $line;
894            }
895        }
896        # Return the result.
897        return @retVal;
898    }
899    
900    =head3 ScriptSetup (deprecated)
901    
902        my ($cgi, $varHash) = ScriptSetup($noTrace);
903    
904    Perform standard tracing and debugging setup for scripts. The value returned is
905    the CGI object followed by a pre-built variable hash. At the end of the script,
906    the client should call L</ScriptFinish> to output the web page.
907    
908    This method calls L</ETracing> to configure tracing, which allows the tracing
909    to be configured via the emergency tracing form on the debugging control panel.
910    Tracing will then be turned on automatically for all programs that use the L</ETracing>
911    method, which includes every program that uses this method or L</StandardSetup>.
912    
913    =over 4
914    
915    =item noTrace (optional)
916    
917    If specified, tracing will be suppressed. This is useful if the script wants to set up
918    tracing manually.
919    
920    =item RETURN
921    
922    Returns a two-element list consisting of a CGI query object and a variable hash for
923    the output page.
924    
925    =back
926    
927    =cut
928    
929    sub ScriptSetup {
930        # Get the parameters.
931        my ($noTrace) = @_;
932        # Get the CGI query object.
933        my $cgi = CGI->new();
934        # Set up tracing if it's not suppressed.
935        ETracing($cgi) unless $noTrace;
936        # Create the variable hash.
937        my $varHash = { results => '' };
938        # Return the query object and variable hash.
939        return ($cgi, $varHash);
940    }
941    
942    =head3 ETracing
943    
944        ETracing($parameter);
945    
946    Set up emergency tracing. Emergency tracing is tracing that is turned
947    on automatically for any program that calls this method. The emergency
948    tracing parameters are stored in a a file identified by a tracing key.
949    If this method is called with a CGI object, then the tracing key is
950    taken from a cookie. If it is called with no parameters, then the tracing
951    key is taken from an environment variable. If it is called with a string,
952    the tracing key is that string.
953    
954    =over 4
955    
956    =item parameter
957    
958    A parameter from which the tracing key is computed. If it is a scalar,
959    that scalar is used as the tracing key. If it is a CGI object, the
960    tracing key is taken from the C<IP> cookie. If it is omitted, the
961    tracing key is taken from the C<TRACING> environment variable. If it
962    is a CGI object and emergency tracing is not on, the C<Trace> and
963    C<TF> parameters will be used to determine the type of tracing.
964    
965    =back
966    
967    =cut
968    
969    sub ETracing {
970        # Get the parameter.
971        my ($parameter) = @_;
972        # Check for CGI mode.
973        if (defined $parameter && ref $parameter eq 'CGI') {
974            $SavedCGI = $parameter;
975        } else {
976            $SavedCGI = undef;
977        }
978        # Default to no tracing except errors.
979        my ($tracing, $dest) = ("0", "WARN");
980        # Check for emergency tracing.
981        my $tkey = EmergencyKey($parameter);
982        my $emergencyFile = EmergencyFileName($tkey);
983        if (-e $emergencyFile) {
984            # We have the file. Read in the data.
985            my @tracing = GetFile($emergencyFile);
986            # Pull off the time limit.
987            my $expire = shift @tracing;
988            # Convert it to seconds.
989            $expire *= 3600;
990            # Check the file data.
991            my $stat = stat($emergencyFile);
992            my ($now) = gettimeofday;
993            if ($now - $stat->mtime > $expire) {
994                # Delete the expired file.
995                unlink $emergencyFile;
996            } else {
997                # Emergency tracing is on. Pull off the destination and
998                # the trace level;
999                $dest = shift @tracing;
1000                my $level = shift @tracing;
1001                # Convert the destination to a real tracing destination.
1002                # temp directory.
1003                $dest = EmergencyTracingDest($tkey, $dest);
1004                # Insure Tracer is specified.
1005                my %moduleHash = map { $_ => 1 } @tracing;
1006                $moduleHash{Tracer} = 1;
1007                # Set the trace parameter.
1008                $tracing = join(" ", $level, sort keys %moduleHash);
1009            }
1010        } elsif (defined $SavedCGI) {
1011            # There's no emergency tracing, but we have a CGI object, so check
1012            # for tracing from the form parameters.
1013            if ($SavedCGI->param('Trace')) {
1014                # Here the user has requested tracing via a form.
1015                $dest = ($SavedCGI->param('TF') ? ">$FIG_Config::temp/Trace$$.log" : "QUEUE");
1016                $tracing = $SavedCGI->param('Trace') . " Tracer";
1017            }
1018        }
1019        # Setup the tracing we've determined from all the stuff above.
1020        TSetup($tracing, $dest);
1021        # Check to see if we're a web script.
1022        if (defined $SavedCGI) {
1023            # Yes we are. Trace the form and environment data.
1024            TraceParms($SavedCGI);
1025            # Check for RAW mode. In raw mode, we print a fake header so that we see everything
1026            # emitted by the script in its raw form.
1027            if (T(Raw => 3)) {
1028                print CGI::header(-type => 'text/plain', -tracing => 'Raw');
1029            }
1030        }
1031    }
1032    
1033    =head3 EmergencyFileName
1034    
1035        my $fileName = Tracer::EmergencyFileName($tkey);
1036    
1037    Return the emergency tracing file name. This is the file that specifies
1038    the tracing information.
1039    
1040    =over 4
1041    
1042    =item tkey
1043    
1044    Tracing key for the current program.
1045    
1046    =item RETURN
1047    
1048    Returns the name of the file to contain the emergency tracing information.
1049    
1050    =back
1051    
1052    =cut
1053    
1054    sub EmergencyFileName {
1055        # Get the parameters.
1056        my ($tkey) = @_;
1057        # Compute the emergency tracing file name.
1058        return "$FIG_Config::temp/Emergency$tkey.txt";
1059    }
1060    
1061    =head3 EmergencyFileTarget
1062    
1063        my $fileName = Tracer::EmergencyFileTarget($tkey);
1064    
1065    Return the emergency tracing target file name. This is the file that receives
1066    the tracing output for file-based tracing.
1067    
1068    =over 4
1069    
1070    =item tkey
1071    
1072    Tracing key for the current program.
1073    
1074    =item RETURN
1075    
1076    Returns the name of the file to contain the trace output.
1077    
1078    =back
1079    
1080    =cut
1081    
1082    sub EmergencyFileTarget {
1083        # Get the parameters.
1084        my ($tkey) = @_;
1085        # Compute the emergency tracing file name.
1086        return "$FIG_Config::temp/trace$tkey.log";
1087    }
1088    
1089    =head3 EmergencyTracingDest
1090    
1091        my $dest = Tracer::EmergencyTracingDest($tkey, $myDest);
1092    
1093    This method converts an emergency tracing destination to a real
1094    tracing destination. The main difference is that if the
1095    destination is C<FILE> or C<APPEND>, we convert it to file
1096    output. If the destination is C<DUAL>, we convert it to file
1097    and standard output.
1098    
1099    =over 4
1100    
1101    =item tkey
1102    
1103    Tracing key for this environment.
1104    
1105    =item myDest
1106    
1107    Destination from the emergency tracing file.
1108    
1109    =item RETURN
1110    
1111    Returns a destination that can be passed into L</TSetup>.
1112    
1113    =back
1114    
1115    =cut
1116    
1117    sub EmergencyTracingDest {
1118        # Get the parameters.
1119        my ($tkey, $myDest) = @_;
1120        # Declare the return variable.
1121        my $retVal = $myDest;
1122        # Process according to the destination value.
1123        if ($myDest eq 'FILE') {
1124            $retVal = ">" . EmergencyFileTarget($tkey);
1125        } elsif ($myDest eq 'APPEND') {
1126            $retVal = ">>" . EmergencyFileTarget($tkey);
1127        } elsif ($myDest eq 'DUAL') {
1128            $retVal = "+>" . EmergencyFileTarget($tkey);
1129        } elsif ($myDest eq 'WARN') {
1130            $retVal = "WARN";
1131        }
1132        # Return the result.
1133        return $retVal;
1134    }
1135    
1136    =head3 Emergency
1137    
1138        Emergency($key, $hours, $dest, $level, @modules);
1139    
1140    Turn on emergency tracing. This method is normally invoked over the web from
1141    a debugging console, but it can also be called by the C<trace.pl> script.
1142    The caller specifies the duration of the emergency in hours, the desired tracing
1143    destination, the trace level, and a list of the trace modules to activate.
1144    For the length of the duration, when a program in an environment with the
1145    specified tracing key active invokes a Sprout CGI script, tracing will be
1146    turned on automatically. See L</TSetup> for more about tracing setup and
1147    L</ETracing> for more about emergency tracing.
1148    
1149    =over 4
1150    
1151    =item tkey
1152    
1153    The tracing key. This is used to identify the control file and the trace file.
1154    
1155    =item hours
1156    
1157    Number of hours to keep emergency tracing alive.
1158    
1159    =item dest
1160    
1161    Tracing destination. If no path information is specified for a file
1162    destination, it is put in the FIG temporary directory.
1163    
1164    =item level
1165    
1166    Tracing level. A higher level means more trace messages.
1167    
1168    =item modules
1169    
1170    A list of the tracing modules to activate.
1171    
1172    =back
1173    
1174    =cut
1175    
1176    sub Emergency {
1177        # Get the parameters.
1178        my ($tkey, $hours, $dest, $level, @modules) = @_;
1179        # Create the emergency file.
1180        my $specFile = EmergencyFileName($tkey);
1181        my $outHandle = Open(undef, ">$specFile");
1182        print $outHandle join("\n", $hours, $dest, $level, @modules, "");
1183    }
1184    
1185    =head3 EmergencyKey
1186    
1187        my $tkey = EmergencyKey($parameter);
1188    
1189    Return the Key to be used for emergency tracing. This could be an IP address,
1190     a session ID, or a user name, depending on the environment.
1191    
1192    =over 4
1193    
1194    =item parameter
1195    
1196    Parameter defining the method for finding the tracing key. If it is a scalar,
1197    then it is presumed to be the tracing key itself. If it is a CGI object, then
1198    the tracing key is taken from the C<IP> cookie. Otherwise, the tracing key is
1199    taken from the C<TRACING> environment variable.
1200    
1201    =item RETURN
1202    
1203    Returns the key to be used for labels in emergency tracing.
1204    
1205    =back
1206    
1207    =cut
1208    
1209    sub EmergencyKey {
1210        # Get the parameters.
1211        my ($parameter) = @_;
1212        # Declare the return variable.
1213        my $retVal;
1214        # Determine the parameter type.
1215        if (! defined $parameter) {
1216            # Here we're supposed to check the environment. If that fails, we
1217            # get the effective login ID.
1218            $retVal = $ENV{TRACING} || scalar getpwuid($<);
1219        } else {
1220            my $ptype = ref $parameter;
1221            if ($ptype eq 'CGI') {
1222                # Here we were invoked from a web page. Look for a cookie.
1223                $retVal = $parameter->cookie('IP');
1224            } elsif (! $ptype) {
1225                # Here the key was passed in.
1226                $retVal = $parameter;
1227            }
1228        }
1229        # If no luck finding a key, use the PID.
1230        if (! defined $retVal) {
1231            $retVal = $$;
1232        }
1233        # Return the result.
1234        return $retVal;
1235    }
1236    
1237    
1238    =head3 TraceParms
1239    
1240        Tracer::TraceParms($cgi);
1241    
1242    Trace the CGI parameters at trace level CGI => 3 and the environment variables
1243    at level CGI => 4. A self-referencing URL is traced at level CGI => 2.
1244    
1245    =over 4
1246    
1247    =item cgi
1248    
1249    CGI query object containing the parameters to trace.
1250    
1251    =back
1252    
1253    =cut
1254    
1255    sub TraceParms {
1256        # Get the parameters.
1257        my ($cgi) = @_;
1258        if (T(CGI => 2)) {
1259            # Here we trace the GET-style URL for the script.
1260            Trace("[URL] " . $cgi->url(-relative => 1, -query => 1));
1261        }
1262        if (T(CGI => 3)) {
1263            # Here we want to trace the parameter data.
1264            my @names = $cgi->param;
1265            for my $parmName (sort @names) {
1266                # Note we skip the Trace parameters, which are for our use only.
1267                if ($parmName ne 'Trace' && $parmName ne 'TF') {
1268                    my @values = $cgi->param($parmName);
1269                    Trace("[CGI] $parmName = " . join(", ", @values));
1270                }
1271            }
1272            # Display the request method.
1273            my $method = $cgi->request_method();
1274            Trace("Method: $method");
1275        }
1276        if (T(CGI => 4)) {
1277            # Here we want the environment data too.
1278            for my $envName (sort keys %ENV) {
1279                Trace("[ENV] $envName = $ENV{$envName}");
1280            }
1281        }
1282    }
1283    
1284    =head3 TraceImages
1285    
1286        Tracer::TraceImages($htmlString);
1287    
1288    Trace information about all of an html document's images. The tracing
1289    will be for type "IMG" at level 3. The image's source string
1290    will be displayed. This is generally either the URL of the image or
1291    raw data for the image itself. If the source is too long, only the first 300
1292    characters will be shown at trace level 3. The entire source will be shown,
1293    however, at trace level 4. This method is not very smart, and might catch
1294    Javascript code, but it is still useful when debugging the arcane
1295    behavior of images in multiple browser environments.
1296    
1297    =over 4
1298    
1299    =item htmlString
1300    
1301    HTML text for an outgoing web page.
1302    
1303    =back
1304    
1305    =cut
1306    
1307    sub TraceImages {
1308        # Only proceed if we're at the proper trace level.
1309        if (T(IMG => 3)) {
1310            # For performance reasons we're manipulating $_[0] instead of retrieving the string
1311            # into a variable called "$htmlString". This is because we expect html strings to be
1312            # long, and don't want to copy them any more than we have to.
1313            Trace(length($_[0]) . " characters in web page.");
1314            # Loop through the HTML, culling image tags.
1315            while ($_[0] =~ /<img\s+[^>]+?src="([^"]+)"/sgi) {
1316                # Extract the source string and determine whether or not it's too long.
1317                my $srcString = $1;
1318                my $pos = pos($_[0]) - length($srcString);
1319                my $excess = length($srcString) - 300;
1320                # We'll put the display string in here.
1321                my $srcDisplay = $srcString;
1322                # If it's a data string, split it at the comma.
1323                $srcDisplay =~ s/^(data[^,]+,)/$1\n/;
1324                # If there's no excess or we're at trace level 4, we're done. At level 3 with
1325                # a long string, however, we only show the first 300 characters.
1326                if ($excess > 0 && ! T(IMG => 4)) {
1327                    $srcDisplay = substr($srcDisplay,0,300) . "\nplus $excess characters.";
1328                }
1329                # Output the trace message.
1330                Trace("Image tag at position $pos:\n$srcDisplay");
1331            }
1332        }
1333    }
1334    
1335    
1336    =head3 ScriptFinish (deprecated)
1337    
1338        ScriptFinish($webData, $varHash);
1339    
1340    Output a web page at the end of a script. Either the string to be output or the
1341    name of a template file can be specified. If the second parameter is omitted,
1342    it is assumed we have a string to be output; otherwise, it is assumed we have the
1343    name of a template file. The template should have the variable C<DebugData>
1344    specified in any form that invokes a standard script. If debugging mode is turned
1345    on, a form field will be put in that allows the user to enter tracing data.
1346    Trace messages will be placed immediately before the terminal C<BODY> tag in
1347    the output, formatted as a list.
1348    
1349    A typical standard script would loook like the following.
1350    
1351        BEGIN {
1352            # Print the HTML header.
1353            print "CONTENT-TYPE: text/html\n\n";
1354        }
1355        use Tracer;
1356        use CGI;
1357        use FIG;
1358        # ... more uses ...
1359    
1360        my ($cgi, $varHash) = ScriptSetup();
1361        eval {
1362            # ... get data from $cgi, put it in $varHash ...
1363        };
1364        if ($@) {
1365            Trace("Script Error: $@") if T(0);
1366        }
1367        ScriptFinish("Html/MyTemplate.html", $varHash);
1368    
1369    The idea here is that even if the script fails, you'll see trace messages and
1370    useful output.
1371    
1372    =over 4
1373    
1374    =item webData
1375    
1376    A string containing either the full web page to be written to the output or the
1377    name of a template file from which the page is to be constructed. If the name
1378    of a template file is specified, then the second parameter must be present;
1379    otherwise, it must be absent.
1380    
1381    =item varHash (optional)
1382    
1383    If specified, then a reference to a hash mapping variable names for a template
1384    to their values. The template file will be read into memory, and variable markers
1385    will be replaced by data in this hash reference.
1386    
1387    =back
1388    
1389    =cut
1390    
1391    sub ScriptFinish {
1392        # Get the parameters.
1393        my ($webData, $varHash) = @_;
1394        # Check for a template file situation.
1395        my $outputString;
1396        if (defined $varHash) {
1397            # Here we have a template file. We need to determine the template type.
1398            my $template;
1399            if ($FIG_Config::template_url && $webData =~ /\.php$/) {
1400                $template = "$FIG_Config::template_url/$webData";
1401            } else {
1402                $template = "<<$webData";
1403            }
1404            $outputString = PageBuilder::Build($template, $varHash, "Html");
1405        } else {
1406            # Here the user gave us a raw string.
1407            $outputString = $webData;
1408        }
1409        # Check for trace messages.
1410        if ($Destination ne "NONE" && $TraceLevel > 0) {
1411            # We have trace messages, so we want to put them at the end of the body. This
1412            # is either at the end of the whole string or at the beginning of the BODY
1413            # end-tag.
1414            my $pos = length $outputString;
1415            if ($outputString =~ m#</body>#gi) {
1416                $pos = (pos $outputString) - 7;
1417            }
1418            # If the trace messages were queued, we unroll them. Otherwise, we display the
1419            # destination.
1420            my $traceHtml;
1421            if ($Destination eq "QUEUE") {
1422                $traceHtml = QTrace('Html');
1423            } elsif ($Destination =~ /^>>(.+)$/) {
1424                # Here the tracing output it to a file. We code it as a hyperlink so the user
1425                # can copy the file name into the clipboard easily.
1426                my $actualDest = $1;
1427                $traceHtml = "<p>Tracing output to $actualDest.</p>\n";
1428            } else {
1429                # Here we have one of the special destinations.
1430                $traceHtml = "<P>Tracing output type is $Destination.</p>\n";
1431            }
1432            substr $outputString, $pos, 0, $traceHtml;
1433        }
1434        # Write the output string.
1435        print $outputString;
1436    }
1437    
1438    =head2 Command-Line Utility Methods
1439    
1440    =head3 SendSMS
1441    
1442        my $msgID = Tracer::SendSMS($phoneNumber, $msg);
1443    
1444    Send a text message to a phone number using Clickatell. The FIG_Config file must contain the
1445    user name, password, and API ID for the relevant account in the hash reference variable
1446    I<$FIG_Config::phone>, using the keys C<user>, C<password>, and C<api_id>. For
1447    example, if the user name is C<BruceTheHumanPet>, the password is C<silly>, and the API ID
1448    is C<2561022>, then the FIG_Config file must contain
1449    
1450        $phone =  { user => 'BruceTheHumanPet',
1451                    password => 'silly',
1452                    api_id => '2561022' };
1453    
1454    The original purpose of this method was to insure Bruce would be notified immediately when the
1455    Sprout Load terminates. Care should be taken if you do not wish Bruce to be notified immediately
1456    when you call this method.
1457    
1458    The message ID will be returned if successful, and C<undef> if an error occurs.
1459    
1460    =over 4
1461    
1462    =item phoneNumber
1463    
1464    Phone number to receive the message, in international format. A United States phone number
1465    would be prefixed by "1". A British phone number would be prefixed by "44".
1466    
1467    =item msg
1468    
1469    Message to send to the specified phone.
1470    
1471    =item RETURN
1472    
1473    Returns the message ID if successful, and C<undef> if the message could not be sent.
1474    
1475    =back
1476    
1477    =cut
1478    
1479    sub SendSMS {
1480        # Get the parameters.
1481        my ($phoneNumber, $msg) = @_;
1482        # Declare the return variable. If we do not change it, C<undef> will be returned.
1483        my $retVal;
1484        # Only proceed if we have phone support.
1485        if (! defined $FIG_Config::phone) {
1486            Trace("Phone support not present in FIG_Config.") if T(1);
1487        } else {
1488            # Get the phone data.
1489            my $parms = $FIG_Config::phone;
1490            # Get the Clickatell URL.
1491            my $url = "http://api.clickatell.com/http/";
1492            # Create the user agent.
1493            my $ua = LWP::UserAgent->new;
1494            # Request a Clickatell session.
1495            my $resp = $ua->post("$url/sendmsg", { user => $parms->{user},
1496                                         password => $parms->{password},
1497                                         api_id => $parms->{api_id},
1498                                         to => $phoneNumber,
1499                                         text => $msg});
1500            # Check for an error.
1501            if (! $resp->is_success) {
1502                Trace("Alert failed.") if T(1);
1503            } else {
1504                # Get the message ID.
1505                my $rstring = $resp->content;
1506                if ($rstring =~ /^ID:\s+(.*)$/) {
1507                    $retVal = $1;
1508                } else {
1509                    Trace("Phone attempt failed with $rstring") if T(1);
1510                }
1511            }
1512        }
1513        # Return the result.
1514        return $retVal;
1515    }
1516    
1517    =head3 StandardSetup
1518    
1519        my ($options, @parameters) = StandardSetup(\@categories, \%options, $parmHelp, @ARGV);
1520    
1521    This method performs standard command-line parsing and tracing setup. The return
1522    values are a hash of the command-line options and a list of the positional
1523    parameters. Tracing is automatically set up and the command-line options are
1524    validated.
1525    
1526    This is a complex method that does a lot of grunt work. The parameters can
1527    be more easily understood, however, once they are examined individually.
1528    
1529    The I<categories> parameter is the most obtuse. It is a reference to a list of
1530    special-purpose tracing categories. Most tracing categories are PERL package
1531    names. So, for example, if you wanted to turn on tracing inside the B<Sprout>,
1532    B<ERDB>, and B<SproutLoad> packages, you would specify the categories
1533    
1534        ["Sprout", "SproutLoad", "ERDB"]
1535    
1536    This would cause trace messages in the specified three packages to appear in
1537    the output. There are two special tracing categories that are automatically
1538    handled by this method. In other words, if you used L</TSetup> you would need
1539    to include these categories manually, but if you use this method they are turned
1540    on automatically.
1541    
1542    =over 4
1543    
1544    =item SQL
1545    
1546    Traces SQL commands and activity.
1547    
1548    =item Tracer
1549    
1550    Traces error messages and call stacks.
1551    
1552    =back
1553    
1554    C<SQL> is only turned on if the C<-sql> option is specified in the command line.
1555    The trace level is specified using the C<-trace> command-line option. For example,
1556    the following command line for C<TransactFeatures> turns on SQL tracing and runs
1557    all tracing at level 3.
1558    
1559        TransactFeatures -trace=3 -sql register ../xacts IDs.tbl
1560    
1561    Standard tracing is output to the standard output and echoed to the file
1562    C<trace>I<$$>C<.log> in the FIG temporary directory, where I<$$> is the
1563    process ID. You can also specify the C<user> parameter to put a user ID
1564    instead of a process ID in the trace file name. So, for example
1565    
1566    The default trace level is 2. To get all messages, specify a trace level of 4.
1567    For a genome-by-genome update, use 3.
1568    
1569        TransactFeatures -trace=3 -sql -user=Bruce register ../xacts IDs.tbl
1570    
1571    would send the trace output to C<traceBruce.log> in the temporary directory.
1572    
1573    The I<options> parameter is a reference to a hash containing the command-line
1574    options, their default values, and an explanation of what they mean. Command-line
1575    options may be in the form of switches or keywords. In the case of a switch, the
1576    option value is 1 if it is specified and 0 if it is not specified. In the case
1577    of a keyword, the value is separated from the option name by an equal sign. You
1578    can see this last in the command-line example above.
1579    
1580    You can specify a different default trace level by setting C<$options->{trace}>
1581    prior to calling this method.
1582    
1583    An example at this point would help. Consider, for example, the command-line utility
1584    C<TransactFeatures>. It accepts a list of positional parameters plus the options
1585    C<safe>, C<noAlias>, C<start>, and C<tblFiles>. To start up this command, we execute
1586    the following code.
1587    
1588        my ($options, @parameters) = Tracer::StandardSetup(["DocUtils"],
1589                            { safe => [0, "use database transactions"],
1590                              noAlias => [0, "do not expect aliases in CHANGE transactions"],
1591                              start => [' ', "start with this genome"],
1592                              tblFiles => [0, "output TBL files containing the corrected IDs"] },
1593                            "<command> <transactionDirectory> <IDfile>",
1594                          @ARGV);
1595    
1596    
1597    The call to C<ParseCommand> specifies the default values for the options and
1598    stores the actual options in a hash that is returned as C<$options>. The
1599    positional parameters are returned in C<@parameters>.
1600    
1601    The following is a sample command line for C<TransactFeatures>.
1602    
1603        TransactFeatures -trace=2 -noAlias register ../xacts IDs.tbl
1604    
1605    Single and double hyphens are equivalent. So, you could also code the
1606    above command as
1607    
1608        TransactFeatures --trace=2 --noAlias register ../xacts IDs.tbl
1609    
1610    In this case, C<register>, C<../xacts>, and C<IDs.tbl> are the positional
1611    parameters, and would find themselves in I<@parameters> after executing the
1612    above code fragment. The tracing would be set to level 2, and the categories
1613    would be C<Tracer>, and <DocUtils>. C<Tracer> is standard,
1614    and C<DocUtils> was included because it came in within the first parameter
1615    to this method. The I<$options> hash would be
1616    
1617        { trace => 2, sql => 0, safe => 0,
1618          noAlias => 1, start => ' ', tblFiles => 0 }
1619    
1620    Use of C<StandardSetup> in this way provides a simple way of performing
1621    standard tracing setup and command-line parsing. Note that the caller is
1622    not even aware of the command-line switches C<-trace> and C<-sql>, which
1623    are used by this method to control the tracing. If additional tracing features
1624    need to be added in the future, they can be processed by this method without
1625    upsetting the command-line utilities.
1626    
1627    If the C<background> option is specified on the command line, then the
1628    standard and error outputs will be directed to files in the temporary
1629    directory, using the same suffix as the trace file. So, if the command
1630    line specified
1631    
1632        -user=Bruce -background
1633    
1634    then the trace output would go to C<traceBruce.log>, the standard output to
1635    C<outBruce.log>, and the error output to C<errBruce.log>. This is designed to
1636    simplify starting a command in the background.
1637    
1638    The user name is also used as the tracing key for L</Emergency Tracing>.
1639    Specifying a value of C<E> for the trace level causes emergency tracing to
1640    be used instead of custom tracing. If the user name is not specified,
1641    the tracing key is taken from the C<Tracing> environment variable. If there
1642    is no value for that variable, the tracing key will be computed from the PID.
1643    
1644    Finally, if the special option C<-help> is specified, the option
1645    names will be traced at level 0 and the program will exit without processing.
1646    This provides a limited help capability. For example, if the user enters
1647    
1648        TransactFeatures -help
1649    
1650    he would see the following output.
1651    
1652        TransactFeatures [options] <command> <transactionDirectory> <IDfile>
1653            -trace    tracing level (default E)
1654            -sql      trace SQL commands
1655            -safe     use database transactions
1656            -noAlias  do not expect aliases in CHANGE transactions
1657            -start    start with this genome
1658            -tblFiles output TBL files containing the corrected IDs
1659    
1660    The caller has the option of modifying the tracing scheme by placing a value
1661    for C<trace> in the incoming options hash. The default value can be overridden,
1662    or the tracing to the standard output can be turned off by suffixing a minus
1663    sign to the trace level. So, for example,
1664    
1665        { trace => [0, "tracing level (default 0)"],
1666           ...
1667    
1668    would set the default trace level to 0 instead of E, while
1669    
1670        { trace => ["2-", "tracing level (default 2)"],
1671           ...
1672    
1673    would set the default to 2, but trace only to the log file, not to the
1674    standard output.
1675    
1676    The parameters to this method are as follows.
1677    
1678    =over 4
1679    
1680    =item categories
1681    
1682    Reference to a list of tracing category names. These should be names of
1683    packages whose internal workings will need to be debugged to get the
1684    command working.
1685    
1686    =item options
1687    
1688    Reference to a hash containing the legal options for the current command mapped
1689    to their default values and descriptions. The user can override the defaults
1690    by specifying the options as command-line switches prefixed by a hyphen.
1691    Tracing-related options may be added to this hash. If the C<-h> option is
1692    specified on the command line, the option descriptions will be used to
1693    explain the options. To turn off tracing to the standard output, add a
1694    minus sign to the value for C<trace> (see above).
1695    
1696    =item parmHelp
1697    
1698    A string that vaguely describes the positional parameters. This is used
1699    if the user specifies the C<-h> option.
1700    
1701    =item argv
1702    
1703    List of command line parameters, including the option switches, which must
1704    precede the positional parameters and be prefixed by a hyphen.
1705    
1706    =item RETURN
1707    
1708    Returns a list. The first element of the list is the reference to a hash that
1709    maps the command-line option switches to their values. These will either be the
1710    default values or overrides specified on the command line. The remaining
1711    elements of the list are the position parameters, in order.
1712    
1713    =back
1714    
1715    =cut
1716    
1717    sub StandardSetup {
1718        # Get the parameters.
1719        my ($categories, $options, $parmHelp, @argv) = @_;
1720        # Get the default tracing key.
1721        my $tkey = EmergencyKey();
1722        # Add the tracing options.
1723        if (! exists $options->{trace}) {
1724            $options->{trace} = ['2', "tracing level (E for emergency tracing)"];
1725        }
1726        $options->{sql} = [0, "turn on SQL tracing"];
1727        $options->{help} = [0, "display command-line options"];
1728        $options->{user} = [$tkey, "tracing key"];
1729        $options->{background} = [0, "spool standard and error output"];
1730        # Create a parsing hash from the options hash. The parsing hash
1731        # contains the default values rather than the default value
1732        # and the description. While we're at it, we'll memorize the
1733        # length of the longest option name.
1734        my $longestName = 0;
1735        my %parseOptions = ();
1736        for my $key (keys %{$options}) {
1737            if (length $key > $longestName) {
1738                $longestName = length $key;
1739            }
1740            $parseOptions{$key} = $options->{$key}->[0];
1741        }
1742        # Parse the command line.
1743        my ($retOptions, @retParameters) = ParseCommand(\%parseOptions, @argv);
1744        # Get the logfile suffix.
1745        my $suffix = $retOptions->{user};
1746        # Check for background mode.
1747        if ($retOptions->{background}) {
1748            my $outFileName = "$FIG_Config::temp/out$suffix.log";
1749            my $errFileName = "$FIG_Config::temp/err$suffix.log";
1750            open STDOUT, ">$outFileName";
1751            open STDERR, ">$errFileName";
1752            # Check for phone support. If we have phone support and a phone number,
1753            # we want to turn it on.
1754            if ($ENV{PHONE} && defined($FIG_Config::phone)) {
1755                $retOptions->{phone} = $ENV{PHONE};
1756            }
1757        }
1758        # Now we want to set up tracing. First, we need to know if the user
1759        # wants emergency tracing.
1760        if ($retOptions->{trace} eq 'E') {
1761            ETracing($retOptions->{user});
1762        } else {
1763            # Here the tracing is controlled from the command line.
1764            my @cats = @{$categories};
1765            if ($retOptions->{sql}) {
1766                push @cats, "SQL";
1767            }
1768            # Add the default categories.
1769            push @cats, "Tracer";
1770            # Next, we create the category string by joining the categories.
1771            my $cats = join(" ", @cats);
1772            # Check to determine whether or not the caller wants to turn off tracing
1773            # to the standard output.
1774            my $traceLevel = $retOptions->{trace};
1775            my $textOKFlag = 1;
1776            if ($traceLevel =~ /^(.)-/) {
1777                $traceLevel = $1;
1778                $textOKFlag = 0;
1779            }
1780            # Now we set up the trace mode.
1781            my $traceMode;
1782            # Verify that we can open a file in the FIG temporary directory.
1783            my $traceFileName = "$FIG_Config::temp/trace$suffix.log";
1784            if (open TESTTRACE, ">$traceFileName") {
1785                # Here we can trace to a file.
1786                $traceMode = ">$traceFileName";
1787                if ($textOKFlag) {
1788                    # Echo to standard output if the text-OK flag is set.
1789                    $traceMode = "+$traceMode";
1790                }
1791                # Close the test file.
1792                close TESTTRACE;
1793            } else {
1794                # Here we can't trace to a file. Complain about this.
1795                warn "Could not open trace file $traceFileName: $!\n";
1796                # We trace to the standard output if it's
1797                # okay, and the error log otherwise.
1798                if ($textOKFlag) {
1799                    $traceMode = "TEXT";
1800                } else {
1801                    $traceMode = "WARN";
1802                }
1803            }
1804            # Now set up the tracing.
1805            TSetup("$traceLevel $cats", $traceMode);
1806        }
1807        # Check for the "help" option. If it is specified, dump the command-line
1808        # options and exit the program.
1809        if ($retOptions->{help}) {
1810            $0 =~ m#[/\\](\w+)(\.pl)?$#i;
1811            print "$1 [options] $parmHelp\n";
1812            for my $key (sort keys %{$options}) {
1813                my $name = Pad($key, $longestName, 0, ' ');
1814                my $desc = $options->{$key}->[1];
1815                if ($options->{$key}->[0]) {
1816                    $desc .= " (default " . $options->{$key}->[0] . ")";
1817                }
1818                print "  $name $desc\n";
1819            }
1820            exit(0);
1821        }
1822        # Trace the options, if applicable.
1823        if (T(3)) {
1824            my @parms = grep { $retOptions->{$_} } keys %{$retOptions};
1825            Trace("Selected options: " . join(", ", sort @parms) . ".");
1826        }
1827        # Return the parsed parameters.
1828        return ($retOptions, @retParameters);
1829    }
1830    
1831    =head3 ReadOptions
1832    
1833        my %options = Tracer::ReadOptions($fileName);
1834    
1835    Read a set of options from a file. Each option is encoded in a line of text that has the
1836    format
1837    
1838    I<optionName>C<=>I<optionValue>C<; >I<comment>
1839    
1840    The option name must consist entirely of letters, digits, and the punctuation characters
1841    C<.> and C<_>, and is case sensitive. Blank lines and lines in which the first nonblank
1842    character is a semi-colon will be ignored. The return hash will map each option name to
1843    the corresponding option value.
1844    
1845    =over 4
1846    
1847    =item fileName
1848    
1849    Name of the file containing the option data.
1850    
1851    =item RETURN
1852    
1853    Returns a hash mapping the option names specified in the file to their corresponding option
1854    value.
1855    
1856    =back
1857    
1858    =cut
1859    
1860    sub ReadOptions {
1861        # Get the parameters.
1862        my ($fileName) = @_;
1863        # Open the file.
1864        (open CONFIGFILE, "<$fileName") || Confess("Could not open option file $fileName.");
1865        # Count the number of records read.
1866        my ($records, $comments) = 0;
1867        # Create the return hash.
1868        my %retVal = ();
1869        # Loop through the file, accumulating key-value pairs.
1870        while (my $line = <CONFIGFILE>) {
1871            # Denote we've read a line.
1872            $records++;
1873            # Determine the line type.
1874            if ($line =~ /^\s*[\n\r]/) {
1875                # A blank line is a comment.
1876                $comments++;
1877            } elsif ($line =~ /^\s*([A-Za-z0-9_\.]+)=([^;]*);/) {
1878                # Here we have an option assignment.
1879                retVal{$1} = $2;
1880            } elsif ($line =~ /^\s*;/) {
1881                # Here we have a text comment.
1882                $comments++;
1883            } else {
1884                # Here we have an invalid line.
1885                Trace("Invalid option statement in record $records.") if T(0);
1886            }
1887        }
1888        # Return the hash created.
1889        return %retVal;
1890    }
1891    
1892    =head3 GetOptions
1893    
1894        Tracer::GetOptions(\%defaults, \%options);
1895    
1896    Merge a specified set of options into a table of defaults. This method takes two hash references
1897    as input and uses the data from the second to update the first. If the second does not exist,
1898    there will be no effect. An error will be thrown if one of the entries in the second hash does not
1899    exist in the first.
1900    
1901    Consider the following example.
1902    
1903        my $optionTable = GetOptions({ dbType => 'mySQL', trace => 0 }, $options);
1904    
1905    In this example, the variable B<$options> is expected to contain at most two options-- B<dbType> and
1906    B<trace>. The default database type is C<mySQL> and the default trace level is C<0>. If the value of
1907    B<$options> is C<< {dbType => 'Oracle'} >>, then the database type will be changed to C<Oracle> and
1908    the trace level will remain at 0. If B<$options> is undefined, then the database type and trace level
1909    will remain C<mySQL> and C<0>. If, on the other hand, B<$options> is defined as
1910    
1911        {databaseType => 'Oracle'}
1912    
1913    an error will occur because the B<databaseType> option does not exist.
1914    
1915    =over 4
1916    
1917    =item defaults
1918    
1919    Table of default option values.
1920    
1921    =item options
1922    
1923    Table of overrides, if any.
1924    
1925    =item RETURN
1926    
1927    Returns a reference to the default table passed in as the first parameter.
1928    
1929    =back
1930    
1931    =cut
1932    
1933    sub GetOptions {
1934        # Get the parameters.
1935        my ($defaults, $options) = @_;
1936        # Check for overrides.
1937        if ($options) {
1938            # Loop through the overrides.
1939            while (my ($option, $setting) = each %{$options}) {
1940                # Insure this override exists.
1941                if (!exists $defaults->{$option}) {
1942                    croak "Unrecognized option $option encountered.";
1943                } else {
1944                    # Apply the override.
1945                    $defaults->{$option} = $setting;
1946                }
1947            }
1948        }
1949        # Return the merged table.
1950        return $defaults;
1951    }
1952    
1953    =head3 MergeOptions
1954    
1955        Tracer::MergeOptions(\%table, \%defaults);
1956    
1957    Merge default values into a hash table. This method looks at the key-value pairs in the
1958    second (default) hash, and if a matching key is not found in the first hash, the default
1959    pair is copied in. The process is similar to L</GetOptions>, but there is no error-
1960    checking and no return value.
1961    
1962    =over 4
1963    
1964    =item table
1965    
1966    Hash table to be updated with the default values.
1967    
1968    =item defaults
1969    
1970    Default values to be merged into the first hash table if they are not already present.
1971    
1972    =back
1973    
1974    =cut
1975    
1976    sub MergeOptions {
1977        # Get the parameters.
1978        my ($table, $defaults) = @_;
1979        # Loop through the defaults.
1980        while (my ($key, $value) = each %{$defaults}) {
1981            if (!exists $table->{$key}) {
1982                $table->{$key} = $value;
1983            }
1984        }
1985    }
1986    
1987    =head3 ParseCommand
1988    
1989        my ($options, @arguments) = Tracer::ParseCommand(\%optionTable, @inputList);
1990    
1991    Parse a command line consisting of a list of parameters. The initial parameters may be option
1992    specifiers of the form C<->I<option> or C<->I<option>C<=>I<value>. The options are stripped
1993    off and merged into a table of default options. The remainder of the command line is
1994    returned as a list of positional arguments. For example, consider the following invocation.
1995    
1996        my ($options, @arguments) = ParseCommand({ errors => 0, logFile => 'trace.log'}, @words);
1997    
1998    In this case, the list @words will be treated as a command line and there are two options available,
1999    B<errors> and B<logFile>. If @words has the following format
2000    
2001        -logFile=error.log apple orange rutabaga
2002    
2003    then at the end of the invocation, C<$options> will be
2004    
2005        { errors => 0, logFile => 'error.log' }
2006    
2007    and C<@arguments> will contain
2008    
2009        apple orange rutabaga
2010    
2011    The parser allows for some escape sequences. See L</UnEscape> for a description. There is no
2012    support for quote characters. Options can be specified with single or double hyphens.
2013    
2014    =over 4
2015    
2016    =item optionTable
2017    
2018    Table of default options.
2019    
2020    =item inputList
2021    
2022    List of words on the command line.
2023    
2024    =item RETURN
2025    
2026    Returns a reference to the option table and a list of the positional arguments.
2027    
2028    =back
2029    
2030    =cut
2031    
2032    sub ParseCommand {
2033        # Get the parameters.
2034        my ($optionTable, @inputList) = @_;
2035        # Process any options in the input list.
2036        my %overrides = ();
2037        while ((@inputList > 0) && ($inputList[0] =~ /^--?/)) {
2038            # Get the current option.
2039            my $arg = shift @inputList;
2040            # Pull out the option name.
2041            $arg =~ /^--?([^=]*)/g;
2042            my $name = $1;
2043            # Check for an option value.
2044            if ($arg =~ /\G=(.*)$/g) {
2045                # Here we have a value for the option.
2046                $overrides{$name} = UnEscape($1);
2047            } else {
2048                # Here there is no value, so we use 1.
2049                $overrides{$name} = 1;
2050            }
2051        }
2052        # Merge the options into the defaults.
2053        GetOptions($optionTable, \%overrides);
2054        # Translate the remaining parameters.
2055        my @retVal = ();
2056        for my $inputParm (@inputList) {
2057            push @retVal, UnEscape($inputParm);
2058        }
2059        # Return the results.
2060        return ($optionTable, @retVal);
2061    }
2062    
2063    
2064    =head2 File Utility Methods
2065    
2066    =head3 GetFile
2067    
2068        my @fileContents = Tracer::GetFile($fileName);
2069    
2070        or
2071    
2072        my $fileContents = Tracer::GetFile($fileName);
2073    
2074    Return the entire contents of a file. In list context, line-ends are removed and
2075    each line is a list element. In scalar context, line-ends are replaced by C<\n>.
2076    
2077    =over 4
2078    
2079    =item fileName
2080    
2081    Name of the file to read.
2082    
2083    =item RETURN
2084    
2085    In a list context, returns the entire file as a list with the line terminators removed.
2086    In a scalar context, returns the entire file as a string. If an error occurs opening
2087    the file, an empty list will be returned.
2088    
2089    =back
2090    
2091    =cut
2092    
2093    sub GetFile {
2094        # Get the parameters.
2095        my ($fileName) = @_;
2096        # Declare the return variable.
2097        my @retVal = ();
2098        # Open the file for input.
2099        my $handle = Open(undef, "<$fileName");
2100        # Read the whole file into the return variable, stripping off any terminator
2101        # characters.
2102        my $lineCount = 0;
2103        while (my $line = <$handle>) {
2104            $lineCount++;
2105            $line = Strip($line);
2106            push @retVal, $line;
2107        }
2108        # Close it.
2109        close $handle;
2110        my $actualLines = @retVal;
2111        Trace("$actualLines lines read from file $fileName.") if T(File => 2);
2112        # Return the file's contents in the desired format.
2113        if (wantarray) {
2114            return @retVal;
2115        } else {
2116            return join "\n", @retVal;
2117        }
2118    }
2119    
2120    =head3 PutFile
2121    
2122        Tracer::PutFile($fileName, \@lines);
2123    
2124    Write out a file from a list of lines of text.
2125    
2126    =over 4
2127    
2128    =item fileName
2129    
2130    Name of the output file.
2131    
2132    =item lines
2133    
2134    Reference to a list of text lines. The lines will be written to the file in order, with trailing
2135    new-line characters. Alternatively, may be a string, in which case the string will be written without
2136    modification.
2137    
2138    =back
2139    
2140    =cut
2141    
2142    sub PutFile {
2143        # Get the parameters.
2144        my ($fileName, $lines) = @_;
2145        # Open the output file.
2146        my $handle = Open(undef, ">$fileName");
2147        # Count the lines written.
2148        if (ref $lines ne 'ARRAY') {
2149            # Here we have a scalar, so we write it raw.
2150            print $handle $lines;
2151            Trace("Scalar put to file $fileName.") if T(File => 3);
2152        } else {
2153            # Write the lines one at a time.
2154            my $count = 0;
2155            for my $line (@{$lines}) {
2156                print $handle "$line\n";
2157                $count++;
2158            }
2159            Trace("$count lines put to file $fileName.") if T(File => 3);
2160        }
2161        # Close the output file.
2162        close $handle;
2163    }
2164    
2165    =head3 ParseRecord
2166    
2167        my @fields = Tracer::ParseRecord($line);
2168    
2169    Parse a tab-delimited data line. The data line is split into field values. Embedded tab
2170    and new-line characters in the data line must be represented as C<\t> and C<\n>, respectively.
2171    These will automatically be converted.
2172    
2173    =over 4
2174    
2175    =item line
2176    
2177    Line of data containing the tab-delimited fields.
2178    
2179    =item RETURN
2180    
2181    Returns a list of the fields found in the data line.
2182    
2183    =back
2184    
2185    =cut
2186    
2187    sub ParseRecord {
2188        # Get the parameter.
2189        my ($line) = @_;
2190        # Remove the trailing new-line, if any.
2191        chomp $line;
2192        # Split the line read into pieces using the tab character.
2193        my @retVal = split /\t/, $line;
2194        # Trim and fix the escapes in each piece.
2195        for my $value (@retVal) {
2196            # Trim leading whitespace.
2197            $value =~ s/^\s+//;
2198            # Trim trailing whitespace.
2199            $value =~ s/\s+$//;
2200            # Delete the carriage returns.
2201            $value =~ s/\r//g;
2202            # Convert the escapes into their real values.
2203            $value =~ s/\\t/"\t"/ge;
2204            $value =~ s/\\n/"\n"/ge;
2205        }
2206        # Return the result.
2207        return @retVal;
2208    }
2209    
2210    =head3 Merge
2211    
2212        my @mergedList = Tracer::Merge(@inputList);
2213    
2214    Sort a list of strings and remove duplicates.
2215    
2216    =over 4
2217    
2218    =item inputList
2219    
2220    List of scalars to sort and merge.
2221    
2222    =item RETURN
2223    
2224    Returns a list containing the same elements sorted in ascending order with duplicates
2225    removed.
2226    
2227    =back
2228    
2229    =cut
2230    
2231    sub Merge {
2232        # Get the input list in sort order.
2233        my @inputList = sort @_;
2234        # Only proceed if the list has at least two elements.
2235        if (@inputList > 1) {
2236            # Now we want to move through the list splicing out duplicates.
2237            my $i = 0;
2238            while ($i < @inputList) {
2239                # Get the current entry.
2240                my $thisEntry = $inputList[$i];
2241                # Find out how many elements duplicate the current entry.
2242                my $j = $i + 1;
2243                my $dup1 = $i + 1;
2244                while ($j < @inputList && $inputList[$j] eq $thisEntry) { $j++; };
2245                # If the number is nonzero, splice out the duplicates found.
2246                if ($j > $dup1) {
2247                    splice @inputList, $dup1, $j - $dup1;
2248                }
2249                # Now the element at position $dup1 is different from the element before it
2250                # at position $i. We push $i forward one position and start again.
2251                $i++;
2252            }
2253        }
2254        # Return the merged list.
2255        return @inputList;
2256    }
2257    
2258    =head3 Open
2259    
2260        my $handle = Open($fileHandle, $fileSpec, $message);
2261    
2262    Open a file.
2263    
2264    The I<$fileSpec> is essentially the second argument of the PERL C<open>
2265    function. The mode is specified using Unix-like shell information. So, for
2266    example,
2267    
2268        Open(\*LOGFILE, '>>/usr/spool/news/twitlog', "Could not open twit log.");
2269    
2270    would open for output appended to the specified file, and
2271    
2272        Open(\*DATASTREAM, "| sort -u >$outputFile", "Could not open $outputFile.");
2273    
2274    would open a pipe that sorts the records written and removes duplicates. Note
2275    the use of file handle syntax in the Open call. To use anonymous file handles,
2276    code as follows.
2277    
2278        my $logFile = Open(undef, '>>/usr/spool/news/twitlog', "Could not open twit log.");
2279    
2280    The I<$message> parameter is used if the open fails. If it is set to C<0>, then
2281    the open returns TRUE if successful and FALSE if an error occurred. Otherwise, a
2282    failed open will throw an exception and the third parameter will be used to construct
2283    an error message. If the parameter is omitted, a standard message is constructed
2284    using the file spec.
2285    
2286        Could not open "/usr/spool/news/twitlog"
2287    
2288    Note that the mode characters are automatically cleaned from the file name.
2289    The actual error message from the file system will be captured and appended to the
2290    message in any case.
2291    
2292        Could not open "/usr/spool/news/twitlog": file not found.
2293    
2294    In some versions of PERL the only error message we get is a number, which
2295    corresponds to the C++ C<errno> value.
2296    
2297        Could not open "/usr/spool/news/twitlog": 6.
2298    
2299    =over 4
2300    
2301    =item fileHandle
2302    
2303    File handle. If this parameter is C<undef>, a file handle will be generated
2304    and returned as the value of this method.
2305    
2306    =item fileSpec
2307    
2308    File name and mode, as per the PERL C<open> function.
2309    
2310    =item message (optional)
2311    
2312    Error message to use if the open fails. If omitted, a standard error message
2313    will be generated. In either case, the error information from the file system
2314    is appended to the message. To specify a conditional open that does not throw
2315    an error if it fails, use C<0>.
2316    
2317    =item RETURN
2318    
2319    Returns the name of the file handle assigned to the file, or C<undef> if the
2320    open failed.
2321    
2322    =back
2323    
2324    =cut
2325    
2326    sub Open {
2327        # Get the parameters.
2328        my ($fileHandle, $fileSpec, $message) = @_;
2329        # Attempt to open the file.
2330        my $rv = open $fileHandle, $fileSpec;
2331        # If the open failed, generate an error message.
2332        if (! $rv) {
2333            # Save the system error message.
2334            my $sysMessage = $!;
2335            # See if we need a default message.
2336            if (!$message) {
2337                # Clean any obvious mode characters and leading spaces from the
2338                # filename.
2339                my ($fileName) = FindNamePart($fileSpec);
2340                $message = "Could not open \"$fileName\"";
2341            }
2342            # Terminate with an error using the supplied message and the
2343            # error message from the file system.
2344            Confess("$message: $!");
2345        }
2346        # Return the file handle.
2347        return $fileHandle;
2348    }
2349    
2350    =head3 FindNamePart
2351    
2352        my ($fileName, $start, $len) = Tracer::FindNamePart($fileSpec);
2353    
2354    Extract the portion of a file specification that contains the file name.
2355    
2356    A file specification is the string passed to an C<open> call. It specifies the file
2357    mode and name. In a truly complex situation, it can specify a pipe sequence. This
2358    method assumes that the file name is whatever follows the first angle bracket
2359    sequence.  So, for example, in the following strings the file name is
2360    C</usr/fig/myfile.txt>.
2361    
2362        >>/usr/fig/myfile.txt
2363        </usr/fig/myfile.txt
2364        | sort -u > /usr/fig/myfile.txt
2365    
2366    If the method cannot find a file name using its normal methods, it will return the
2367    whole incoming string.
2368    
2369    =over 4
2370    
2371    =item fileSpec
2372    
2373    File specification string from which the file name is to be extracted.
2374    
2375    =item RETURN
2376    
2377    Returns a three-element list. The first element contains the file name portion of
2378    the specified string, or the whole string if a file name cannot be found via normal
2379    methods. The second element contains the start position of the file name portion and
2380    the third element contains the length.
2381    
2382    =back
2383    
2384    =cut
2385    #: Return Type $;
2386    sub FindNamePart {
2387        # Get the parameters.
2388        my ($fileSpec) = @_;
2389        # Default to the whole input string.
2390        my ($retVal, $pos, $len) = ($fileSpec, 0, length $fileSpec);
2391        # Parse out the file name if we can.
2392        if ($fileSpec =~ m/(<|>>?)(.+?)(\s*)$/) {
2393            $retVal = $2;
2394            $len = length $retVal;
2395            $pos = (length $fileSpec) - (length $3) - $len;
2396        }
2397        # Return the result.
2398        return ($retVal, $pos, $len);
2399    }
2400    
2401    =head3 OpenDir
2402    
2403  C<< my $string = Tracer::Now(); >>      my @files = OpenDir($dirName, $filtered, $flag);
2404    
2405  Return a displayable time stamp containing the local time.  Open a directory and return all the file names. This function essentially performs
2406    the functions of an C<opendir> and C<readdir>. If the I<$filtered> parameter is
2407    set to TRUE, all filenames beginning with a period (C<.>), dollar sign (C<$>),
2408    or pound sign (C<#>) and all filenames ending with a tilde C<~>) will be
2409    filtered out of the return list. If the directory does not open and I<$flag> is not
2410    set, an exception is thrown. So, for example,
2411    
2412  =cut      my @files = OpenDir("/Volumes/fig/contigs", 1);
2413    
2414  sub Now {  is effectively the same as
     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;  
 }  
2415    
2416  # Pad a number to 2 digits.      opendir(TMP, "/Volumes/fig/contigs") || Confess("Could not open /Volumes/fig/contigs.");
2417  sub _p2 {      my @files = grep { $_ !~ /^[\.\$\#]/ && $_ !~ /~$/ } readdir(TMP);
     my ($value) = @_;  
     $value = "0$value" if ($value < 10);  
     return $value;  
 }  
2418    
2419  =head3 LogErrors  Similarly, the following code
2420    
2421  C<< Tracer::LogErrors($fileName); >>      my @files = grep { $_ =~ /^\d/ } OpenDir("/Volumes/fig/orgs", 0, 1);
2422    
2423  Route the standard error output to a log file.  Returns the names of all files in C</Volumes/fig/orgs> that begin with digits and
2424    automatically returns an empty list if the directory fails to open.
2425    
2426  =over 4  =over 4
2427    
2428  =item fileName  =item dirName
2429    
2430  Name of the file to receive the error output.  Name of the directory to open.
2431    
2432    =item filtered
2433    
2434    TRUE if files whose names begin with a period (C<.>) should be automatically removed
2435    from the list, else FALSE.
2436    
2437    =item flag
2438    
2439    TRUE if a failure to open is okay, else FALSE
2440    
2441  =back  =back
2442    
2443  =cut  =cut
2444    #: Return Type @;
2445  sub LogErrors {  sub OpenDir {
2446      # Get the file name.      # Get the parameters.
2447      my ($fileName) = @_;      my ($dirName, $filtered, $flag) = @_;
2448      # Open the file as the standard error output.      # Declare the return variable.
2449      open STDERR, '>', $fileName;      my @retVal = ();
2450        # Open the directory.
2451        if (opendir(my $dirHandle, $dirName)) {
2452            # The directory opened successfully. Get the appropriate list according to the
2453            # strictures of the filter parameter.
2454            if ($filtered) {
2455                @retVal = grep { $_ !~ /^[\.\$\#]/ && $_ !~ /~$/ } readdir $dirHandle;
2456            } else {
2457                @retVal = readdir $dirHandle;
2458            }
2459        } elsif (! $flag) {
2460            # Here the directory would not open and it's considered an error.
2461            Confess("Could not open directory $dirName.");
2462        }
2463        # Return the result.
2464        return @retVal;
2465  }  }
2466    
 =head3 ReadOptions  
   
 C<< my %options = Tracer::ReadOptions($fileName); >>  
2467    
2468  Read a set of options from a file. Each option is encoded in a line of text that has the  =head3 Insure
 format  
2469    
2470  I<optionName>C<=>I<optionValue>C<; >I<comment>      Insure($dirName, $chmod);
2471    
2472  The option name must consist entirely of letters, digits, and the punctuation characters  Insure a directory is present.
 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.  
2473    
2474  =over 4  =over 4
2475    
2476  =item fileName  =item dirName
2477    
2478  Name of the file containing the option data.  Name of the directory to check. If it does not exist, it will be created.
2479    
2480  =item RETURN  =item chmod (optional)
2481    
2482  Returns a hash mapping the option names specified in the file to their corresponding option  Security privileges to be given to the directory if it is created.
 value.  
2483    
2484  =back  =back
2485    
2486  =cut  =cut
2487    
2488  sub ReadOptions {  sub Insure {
2489      # Get the parameters.      my ($dirName, $chmod) = @_;
2490      my ($fileName) = @_;      if (! -d $dirName) {
2491      # Open the file.          Trace("Creating $dirName directory.") if T(2);
2492      (open CONFIGFILE, "<$fileName") || Confess("Could not open option file $fileName.");          eval {
2493      # Count the number of records read.              mkpath $dirName;
2494      my ($records, $comments) = 0;              # If we have permissions specified, set them here.
2495      # Create the return hash.              if (defined($chmod)) {
2496      my %retVal = ();                  chmod $chmod, $dirName;
2497      # Loop through the file, accumulating key-value pairs.              }
2498      while (my $line = <CONFIGFILE>) {          };
2499          # Denote we've read a line.          if ($@) {
2500          $records++;              Confess("Error creating $dirName: $@");
         # 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);  
2501          }          }
2502      }      }
     # Return the hash created.  
     return %retVal;  
2503  }  }
2504    
2505  =head3 GetOptions  =head3 ChDir
2506    
2507  C<< Tracer::GetOptions(\%defaults, \%options); >>      ChDir($dirName);
2508    
2509  Merge a specified set of options into a table of defaults. This method takes two hash references  Change to the specified directory.
 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.  
2510    
2511  Consider the following example.  =over 4
2512    
2513  C<< my $optionTable = GetOptions({ dbType => 'mySQL', trace => 0 }, $options); >>  =item dirName
2514    
2515  In this example, the variable B<$options> is expected to contain at most two options-- B<dbType> and  Name of the directory to which we want to change.
 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  
2516    
2517  C<< {databaseType => 'Oracle'} >>  =back
2518    
2519  an error will occur because the B<databaseType> option does not exist.  =cut
2520    
2521  =over 4  sub ChDir {
2522        my ($dirName) = @_;
2523        if (! -d $dirName) {
2524            Confess("Cannot change to directory $dirName: no such directory.");
2525        } else {
2526            Trace("Changing to directory $dirName.") if T(File => 4);
2527            my $okFlag = chdir $dirName;
2528            if (! $okFlag) {
2529                Confess("Error switching to directory $dirName.");
2530            }
2531        }
2532    }
2533    
2534  =item defaults  =head3 SetPermissions
2535    
2536  Table of default option values.      Tracer::SetPermissions($dirName, $group, $mask, %otherMasks);
2537    
2538  =item options  Set the permissions for a directory and all the files and folders inside it.
2539    In addition, the group ownership will be changed to the specified value.
2540    
2541  Table of overrides, if any.  This method is more vulnerable than most to permission and compatability
2542    problems, so it does internal error recovery.
2543    
2544  =item RETURN  =over 4
2545    
2546  Returns a reference to the default table passed in as the first parameter.  =item dirName
2547    
2548  =back  Name of the directory to process.
2549    
2550  =cut  =item group
2551    
2552  sub GetOptions {  Name of the group to be assigned.
     # 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;  
 }  
2553    
2554  =head3 MergeOptions  =item mask
2555    
2556  C<< Tracer::MergeOptions(\%table, \%defaults); >>  Permission mask. Bits that are C<1> in this mask will be ORed into the
2557    permission bits of any file or directory that does not already have them
2558    set to 1.
2559    
2560  Merge default values into a hash table. This method looks at the key-value pairs in the  =item otherMasks
 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.  
2561    
2562  =over 4  Map of search patterns to permission masks. If a directory name matches
2563    one of the patterns, that directory and all its members and subdirectories
2564    will be assigned the new pattern. For example, the following would
2565    assign 01664 to most files, but would use 01777 for directories named C<tmp>.
2566    
2567  =item table      Tracer::SetPermissions($dirName, 'fig', 01664, '^tmp$' => 01777);
2568    
2569  Hash table to be updated with the default values.  The list is ordered, so the following would use 0777 for C<tmp1> and
2570    0666 for C<tmp>, C<tmp2>, or C<tmp3>.
2571    
2572  =item defaults      Tracer::SetPermissions($dirName, 'fig', 01664, '^tmp1' => 0777,
2573                                                       '^tmp' => 0666);
2574    
2575  Default values to be merged into the first hash table if they are not already present.  Note that the pattern matches are all case-insensitive, and only directory
2576    names are matched, not file names.
2577    
2578  =back  =back
2579    
2580  =cut  =cut
2581    
2582  sub MergeOptions {  sub SetPermissions {
2583      # Get the parameters.      # Get the parameters.
2584      my ($table, $defaults) = @_;      my ($dirName, $group, $mask, @otherMasks) = @_;
2585      # Loop through the defaults.      # Set up for error recovery.
2586      while (my ($key, $value) = each %{$defaults}) {      eval {
2587          if (!exists $table->{$key}) {          # Switch to the specified directory.
2588              $table->{$key} = $value;          ChDir($dirName);
2589            # Get the group ID.
2590            my $gid = getgrnam($group);
2591            # Get the mask for tracing.
2592            my $traceMask = sprintf("%04o", $mask) . "($mask)";
2593            Trace("Fixing permissions for directory $dirName using group $group($gid) and mask $traceMask.") if T(File => 2);
2594            my $fixCount = 0;
2595            my $lookCount = 0;
2596            # @dirs will be a stack of directories to be processed.
2597            my @dirs = (getcwd());
2598            while (scalar(@dirs) > 0) {
2599                # Get the current directory.
2600                my $dir = pop @dirs;
2601                # Check for a match to one of the specified directory names. To do
2602                # that, we need to pull the individual part of the name off of the
2603                # whole path.
2604                my $simpleName = $dir;
2605                if ($dir =~ m!/([^/]+)$!) {
2606                    $simpleName = $1;
2607                }
2608                Trace("Simple directory name for $dir is $simpleName.") if T(File => 4);
2609                # Search for a match.
2610                my $match = 0;
2611                my $i;
2612                for ($i = 0; $i < $#otherMasks && ! $match; $i += 2) {
2613                    my $pattern = $otherMasks[$i];
2614                    if ($simpleName =~ /$pattern/i) {
2615                        $match = 1;
2616                    }
2617                }
2618                # Check for a match. Note we use $i-1 because the loop added 2
2619                # before terminating due to the match.
2620                if ($match && $otherMasks[$i-1] != $mask) {
2621                    # This directory matches one of the incoming patterns, and it's
2622                    # a different mask, so we process it recursively with that mask.
2623                    SetPermissions($dir, $group, $otherMasks[$i-1], @otherMasks);
2624                } else {
2625                    # Here we can process normally. Get all of the non-hidden members.
2626                    my @submems = OpenDir($dir, 1);
2627                    for my $submem (@submems) {
2628                        # Get the full name.
2629                        my $thisMem = "$dir/$submem";
2630                        Trace("Checking member $thisMem.") if T(4);
2631                        $lookCount++;
2632                        if ($lookCount % 1000 == 0) {
2633                            Trace("$lookCount members examined. Current is $thisMem. Mask is $traceMask") if T(File => 3);
2634                        }
2635                        # Fix the group.
2636                        chown -1, $gid, $thisMem;
2637                        # Insure this member is not a symlink.
2638                        if (! -l $thisMem) {
2639                            # Get its info.
2640                            my $fileInfo = stat $thisMem;
2641                            # Only proceed if we got the info. Otherwise, it's a hard link
2642                            # and we want to skip it anyway.
2643                            if ($fileInfo) {
2644                                my $fileMode = $fileInfo->mode;
2645                                if (($fileMode & $mask) != $mask) {
2646                                    # Fix this member.
2647                                    $fileMode |= $mask;
2648                                    chmod $fileMode, $thisMem;
2649                                    $fixCount++;
2650                                }
2651                                # If it's a subdirectory, stack it.
2652                                if (-d $thisMem) {
2653                                    push @dirs, $thisMem;
2654                                }
2655          }          }
2656      }      }
2657  }  }
2658                }
2659            }
2660            Trace("$lookCount files and directories processed, $fixCount fixed.") if T(File => 2);
2661        };
2662        # Check for an error.
2663        if ($@) {
2664            Confess("SetPermissions error: $@");
2665        }
2666    }
2667    
2668  =head3 Trace  =head3 GetLine
2669    
2670  C<< Trace($message); >>      my @data = Tracer::GetLine($handle);
2671    
2672  Write a trace message to the target location specified in L</TSetup>. If there has not been  Read a line of data from a tab-delimited file.
 any prior call to B<TSetup>.  
2673    
2674  =over 4  =over 4
2675    
2676  =item message  =item handle
2677    
2678  Message to write.  Open file handle from which to read.
2679    
2680    =item RETURN
2681    
2682    Returns a list of the fields in the record read. The fields are presumed to be
2683    tab-delimited. If we are at the end of the file, then an empty list will be
2684    returned. If an empty line is read, a single list item consisting of a null
2685    string will be returned.
2686    
2687  =back  =back
2688    
2689  =cut  =cut
2690    
2691  sub Trace {  sub GetLine {
2692      # Get the parameters.      # Get the parameters.
2693      my ($message) = @_;      my ($handle) = @_;
2694      # Get the timestamp.      # Declare the return variable.
2695      my $timeStamp = Now();      my @retVal = ();
2696      # Format the message. Note we strip off any line terminators at the end.      Trace("File position is " . tell($handle) . ". EOF flag is " . eof($handle) . ".") if T(File => 4);
2697      my $formatted = "$timeStamp <$LastCategory>: " . Strip($message);      # Read from the file.
2698      # Process according to the destination.      my $line = <$handle>;
2699      if ($Destination eq "TEXT") {      # Only proceed if we found something.
2700          # Write the message to the standard output.      if (defined $line) {
2701          print "$formatted\n";          # Remove the new-line. We are a bit over-cautious here because the file may be coming in via an
2702      } elsif ($Destination eq "ERROR") {          # upload control and have a nonstandard EOL combination.
2703          # Write the message to the error output.          $line =~ s/(\r|\n)+$//;
2704          print STDERR "$formatted\n";          # Here we do some fancy tracing to help in debugging complicated EOL marks.
2705      } elsif ($Destination eq "QUEUE") {          if (T(File => 4)) {
2706          # Push the message into the queue.              my $escapedLine = $line;
2707          push @Queue, "$formatted";              $escapedLine =~ s/\n/\\n/g;
2708      } elsif ($Destination eq "HTML") {              $escapedLine =~ s/\r/\\r/g;
2709          # Convert the message to HTML and write it to the standard output.              $escapedLine =~ s/\t/\\t/g;
2710          my $escapedMessage = CGI::escapeHTML($message);              Trace("Line read: -->$escapedLine<--");
2711          print "<p>$formatted</p>\n";          }
2712      } elsif ($Destination eq "WARN") {          # If the line is empty, return a single empty string; otherwise, parse
2713         # Emit the message as a warning.          # it into fields.
2714         warn $message;          if ($line eq "") {
2715      } elsif ($Destination =~ m/^>>/) {              push @retVal, "";
2716          # Write the trace message to an output file.          } else {
2717          (open TRACING, $Destination) || die "Tracing open for \"$Destination\" failed: $!";              push @retVal, split /\t/,$line;
         print TRACING "$formatted\n";  
         close TRACING;  
         # If the Tee flag is on, echo it to the standard output.  
         if ($TeeFlag) {  
             print "$formatted\n";  
2718          }          }
2719        } else {
2720            # Trace the reason the read failed.
2721            Trace("End of file: $!") if T(File => 3);
2722      }      }
2723        # Return the result.
2724        return @retVal;
2725  }  }
2726    
2727  =head3 T  =head3 PutLine
   
 C<< my $switch = T($category, $traceLevel); >>  
   
     or  
2728    
2729  C<< my $switch = T($traceLevel); >>      Tracer::PutLine($handle, \@fields, $eol);
2730    
2731  Return TRUE if the trace level is at or above a specified value and the specified category  Write a line of data to a tab-delimited file. The specified field values will be
2732  is active, else FALSE. If no category is specified, the caller's package name is used.  output in tab-separated form, with a trailing new-line.
2733    
2734  =over 4  =over 4
2735    
2736  =item category  =item handle
2737    
2738  Category to which the message belongs. If not specified, the caller's package name is  Output file handle.
 used.  
2739    
2740  =item traceLevel  =item fields
2741    
2742  Relevant tracing level.  List of field values.
2743    
2744  =item RETURN  =item eol (optional)
2745    
2746  TRUE if a message at the specified trace level would appear in the trace, else FALSE.  End-of-line character (default is "\n").
2747    
2748  =back  =back
2749    
2750  =cut  =cut
2751    
2752  sub T {  sub PutLine {
     # Declare the return variable.  
     my $retVal = 0;  
     # Only proceed if tracing is turned on.  
     if ($Destination ne "NONE") {  
2753          # Get the parameters.          # Get the parameters.
2754          my ($category, $traceLevel) = @_;      my ($handle, $fields, $eol) = @_;
2755          if (!defined $traceLevel) {      # Write the data.
2756              # Here we have no category, so we need to get the calling package.      print $handle join("\t", @{$fields}) . ($eol || "\n");
             # 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";  
             } else {  
                 $category = $package;  
2757              }              }
2758          }  
2759          # Save the category name.  
2760          $LastCategory = $category;  =head3 PrintLine
2761          # Convert it to lower case before we hash it.  
2762          $category = lc $category;      Tracer::PrintLine($line);
2763          # Use the category and tracelevel to compute the result.  
2764          $retVal = ($traceLevel <= $TraceLevel && ($AllTrace || exists $Categories{$category}));  Print a line of text with a trailing new-line.
2765      }  
2766      # Return the computed result.  =over 4
2767      return $retVal;  
2768    =item line
2769    
2770    Line of text to print.
2771    
2772    =back
2773    
2774    =cut
2775    
2776    sub PrintLine {
2777        # Get the parameters.
2778        my ($line) = @_;
2779        # Print the line.
2780        print "$line\n";
2781  }  }
2782    
 =head3 ParseCommand  
2783    
2784  C<< my ($options, @arguments) = Tracer::ParseCommand(\%optionTable, @inputList); >>  =head2 Other Useful Methods
2785    
2786  Parse a command line consisting of a list of parameters. The initial parameters may be option  =head3 ParseParm
 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.  
2787    
2788  C<< my ($options, @arguments) = ParseCommand({ errors => 0, logFile => 'trace.log'}, @words); >>      my $listValue = Tracer::ParseParm($string);
2789    
2790  In this case, the list @words will be treated as a command line. There are two options available,  Convert a parameter into a list reference. If the parameter is undefined,
2791  B<errors> and B<logFile>. If @words has the following format  an undefined value will be returned. Otherwise, it will be parsed as a
2792    comma-separated list of values.
2793    
2794  C<< -logFile=error.log apple orange rutabaga >>  =over 4
2795    
2796  then at the end of the invocation, C<$options> will be  =item string
2797    
2798  C<< { errors => 0, logFile => 'error.log' } >>  Incoming string.
2799    
2800  and C<@arguments> will contain  =item RETURN
2801    
2802  C<< apple orange rutabaga >>  Returns a reference to a list of values, or C<undef> if the incoming value
2803    was undefined.
2804    
2805  The parser allows for some escape sequences. See L</UnEscape> for a description. There is no  =back
 support for quote characters.  
2806    
2807  =over 4  =cut
2808    
2809  =item optionTable  sub ParseParm {
2810        # Get the parameters.
2811        my ($string) = @_;
2812        # Declare the return variable.
2813        my $retVal;
2814        # Check for data.
2815        if (defined $string) {
2816            # We have some, so split it into a list.
2817            $retVal = [ split /\s*,\s*/, $string];
2818        }
2819        # Return the result.
2820        return $retVal;
2821    }
2822    
2823  Table of default options.  =head3 Now
2824    
2825  =item inputList      my $string = Tracer::Now();
2826    
2827  List of words on the command line.  Return a displayable time stamp containing the local time. Whatever format this
2828    method produces must be parseable by L</ParseDate>.
2829    
2830    =cut
2831    
2832    sub Now {
2833        return DisplayTime(time);
2834    }
2835    
2836    =head3 DisplayTime
2837    
2838        my $string = Tracer::DisplayTime($time);
2839    
2840    Convert a time value to a displayable time stamp. Whatever format this
2841    method produces must be parseable by L</ParseDate>.
2842    
2843    =over 4
2844    
2845    =item time
2846    
2847    Time to display, in seconds since the epoch, or C<undef> if the time is unknown.
2848    
2849  =item RETURN  =item RETURN
2850    
2851  Returns a reference to the option table and a list of the positional arguments.  Returns a displayable time, or C<(n/a)> if the incoming time is undefined.
2852    
2853  =back  =back
2854    
2855  =cut  =cut
2856    
2857  sub ParseCommand {  sub DisplayTime {
2858      # Get the parameters.      my ($time) = @_;
2859      my ($optionTable, @inputList) = @_;      my $retVal = "(n/a)";
2860      # Process any options in the input list.      if (defined $time) {
2861      my %overrides = ();          my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($time);
2862      while ((@inputList > 0) && ($inputList[0] =~ /^-/)) {          $retVal = _p2($mon+1) . "/" . _p2($mday) . "/" . ($year + 1900) . " " .
2863          # Get the current option.                    _p2($hour) . ":" . _p2($min) . ":" . _p2($sec);
         my $arg = shift @inputList;  
         # Pull out the option name.  
         $arg =~ /^-([^=]*)/g;  
         my $name = $1;  
         # Check for an option value.  
         if ($arg =~ /\G=(.*)$/g) {  
             # Here we have a value for the option.  
             $overrides{$name} = UnEscape($1);  
         } else {  
             # Here there is no value, so we use 1.  
             $overrides{$name} = 1;  
         }  
2864      }      }
2865      # Merge the options into the defaults.      return $retVal;
     GetOptions($optionTable, \%overrides);  
     # Translate the remaining parameters.  
     my @retVal = ();  
     for my $inputParm (@inputList) {  
         push @retVal, UnEscape($inputParm);  
2866      }      }
2867      # Return the results.  
2868      return ($optionTable, @retVal);  # Pad a number to 2 digits.
2869    sub _p2 {
2870        my ($value) = @_;
2871        $value = "0$value" if ($value < 10);
2872        return $value;
2873  }  }
2874    
2875  =head3 Escape  =head3 Escape
2876    
2877  C<< my $codedString = Tracer::Escape($realString); >>      my $codedString = Tracer::Escape($realString);
2878    
2879  Escape a string for use in a command length. Tabs will be replaced by C<\t>, new-lines  Escape a string for use in a command. Tabs will be replaced by C<\t>, new-lines
2880  replaced by C<\n>, and backslashes will be doubled. The effect is to exactly reverse the  replaced by C<\n>, carriage returns will be deleted, and backslashes will be doubled. The
2881  effect of L</UnEscape>.  result is to reverse the effect of L</UnEscape>.
2882    
2883  =over 4  =over 4
2884    
# Line 850  Line 2902 
2902      # Loop through the parameter string, looking for sequences to escape.      # Loop through the parameter string, looking for sequences to escape.
2903      while (length $realString > 0) {      while (length $realString > 0) {
2904          # Look for the first sequence to escape.          # Look for the first sequence to escape.
2905          if ($realString =~ /^(.*?)([\n\t\\])/) {          if ($realString =~ /^(.*?)([\n\t\r\\])/) {
2906              # Here we found it. The text preceding the sequence is in $1. The sequence              # Here we found it. The text preceding the sequence is in $1. The sequence
2907              # itself is in $2. First, move the clear text to the return variable.              # itself is in $2. First, move the clear text to the return variable.
2908              $retVal .= $1;              $retVal .= $1;
2909              # Strip the processed section off the real string.              # Strip the processed section off the real string.
2910              $realString = substr $realString, (length $2) + (length $1);              $realString = substr $realString, (length $2) + (length $1);
2911              # Encode the escape sequence.              # Get the matched character.
2912              my $char = $2;              my $char = $2;
2913                # If we have a CR, we are done.
2914                if ($char ne "\r") {
2915                    # It's not a CR, so encode the escape sequence.
2916              $char =~ tr/\t\n/tn/;              $char =~ tr/\t\n/tn/;
2917              $retVal .= "\\" . $char;              $retVal .= "\\" . $char;
2918                }
2919          } else {          } else {
2920              # Here there are no more escape sequences. The rest of the string is              # Here there are no more escape sequences. The rest of the string is
2921              # transferred unmodified.              # transferred unmodified.
# Line 873  Line 2929 
2929    
2930  =head3 UnEscape  =head3 UnEscape
2931    
2932  C<< my $realString = Tracer::UnEscape($codedString); >>      my $realString = Tracer::UnEscape($codedString);
2933    
2934  Replace escape sequences with their actual equivalents. C<\t> will be replaced by  Replace escape sequences with their actual equivalents. C<\t> will be replaced by
2935  a tab, C<\n> by a new-line character, and C<\\> by a back-slash.  a tab, C<\n> by a new-line character, and C<\\> by a backslash. C<\r> codes will
2936    be deleted.
2937    
2938  =over 4  =over 4
2939    
# Line 905  Line 2962 
2962          # "\<tab>" no matter what we do.)          # "\<tab>" no matter what we do.)
2963          while (length $codedString > 0) {          while (length $codedString > 0) {
2964              # Look for the first escape sequence.              # Look for the first escape sequence.
2965              if ($codedString =~ /^(.*?)\\(\\|n|t)/) {              if ($codedString =~ /^(.*?)\\(\\|n|t|r)/) {
2966                  # Here we found it. The text preceding the sequence is in $1. The sequence                  # Here we found it. The text preceding the sequence is in $1. The sequence
2967                  # itself is in $2. First, move the clear text to the return variable.                  # itself is in $2. First, move the clear text to the return variable.
2968                  $retVal .= $1;                  $retVal .= $1;
2969                  $codedString = substr $codedString, (2 + length $1);                  $codedString = substr $codedString, (2 + length $1);
2970                  # Decode the escape sequence.                  # Get the escape value.
2971                  my $char = $2;                  my $char = $2;
2972                    # If we have a "\r", we are done.
2973                    if ($char ne 'r') {
2974                        # Here it's not an 'r', so we convert it.
2975                  $char =~ tr/\\tn/\\\t\n/;                  $char =~ tr/\\tn/\\\t\n/;
2976                  $retVal .= $char;                  $retVal .= $char;
2977                    }
2978              } else {              } else {
2979                  # Here there are no more escape sequences. The rest of the string is                  # Here there are no more escape sequences. The rest of the string is
2980                  # transferred unmodified.                  # transferred unmodified.
# Line 922  Line 2983 
2983              }              }
2984          }          }
2985      }      }
2986      # Return the result.      # Return the result.
     return $retVal;  
 }  
   
 =head3 ParseRecord  
   
 C<< my @fields = Tracer::ParseRecord($line); >>  
   
 Parse a tab-delimited data line. The data line is split into field values. Embedded tab  
 and new-line characters in the data line must be represented as C<\t> and C<\n>, respectively.  
 These will automatically be converted.  
   
 =over 4  
   
 =item line  
   
 Line of data containing the tab-delimited fields.  
   
 =item RETURN  
   
 Returns a list of the fields found in the data line.  
   
 =back  
   
 =cut  
   
 sub ParseRecord {  
     # Get the parameter.  
     my ($line) = @_;  
     # Remove the trailing new-line, if any.  
     chomp $line;  
     # 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;  
     }  
     # Return the result.  
     return @retVal;  
 }  
   
 =head3 Merge  
   
 C<< my @mergedList = Tracer::Merge(@inputList); >>  
   
 Sort a list of strings and remove duplicates.  
   
 =over 4  
   
 =item inputList  
   
 List of scalars to sort and merge.  
   
 =item RETURN  
   
 Returns a list containing the same elements sorted in ascending order with duplicates  
 removed.  
   
 =back  
   
 =cut  
   
 sub Merge {  
     # Get the input list in sort order.  
     my @inputList = sort @_;  
     # Only proceed if the list has at least two elements.  
     if (@inputList > 1) {  
         # Now we want to move through the list splicing out duplicates.  
         my $i = 0;  
         while ($i < @inputList) {  
             # Get the current entry.  
             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++;  
         }  
     }  
     # Return the merged list.  
     return @inputList;  
 }  
   
 =head3 GetFile  
   
 C<< my @fileContents = Tracer::GetFile($fileName); >>  
   
 Return the entire contents of a file.  
   
 =over 4  
   
 =item fileName  
   
 Name of the file to read.  
   
 =item RETURN  
   
 In a list context, returns the entire file as a list with the line terminators removed.  
 In a scalar context, returns the entire file as a string.  
   
 =back  
   
 =cut  
   
 sub GetFile {  
     # Get the parameters.  
     my ($fileName) = @_;  
     # Declare the return variable.  
     my @retVal = ();  
     # Open the file for input.  
     my $ok = open INPUTFILE, "<$fileName";  
     if (!$ok) {  
         # If we had an error, trace it. We will automatically return a null value.  
         Trace("Could not open \"$fileName\" for input: $!") if T(0);  
     } else {  
         # Read the whole file into the return variable, stripping off any terminator  
         # characters.  
         my $lineCount = 0;  
         while (my $line = <INPUTFILE>) {  
             $lineCount++;  
             $line = Strip($line);  
             push @retVal, $line;  
         }  
         # Close it.  
         close INPUTFILE;  
         my $actualLines = @retVal;  
     }  
     # Return the file's contents in the desired format.  
     if (wantarray) {  
         return @retVal;  
     } else {  
         return join "\n", @retVal;  
     }  
 }  
   
 =head3 QTrace  
   
 C<< my $data = QTrace($format); >>  
   
 Return the queued trace data in the specified format.  
   
 =over 4  
   
 =item format  
   
 C<html> to format the data as an HTML list, C<text> to format it as straight text.  
   
 =back  
   
 =cut  
   
 sub QTrace {  
     # Get the parameter.  
     my ($format) = @_;  
     # Create the return variable.  
     my $retVal = "";  
     # Only proceed if there is an actual queue.  
     if (@Queue) {  
         # Process according to the format.  
         if ($format =~ m/^HTML$/i) {  
             # Convert the queue into an HTML list.  
             $retVal = "<ul>\n";  
             for my $line (@Queue) {  
                 my $escapedLine = CGI::escapeHTML($line);  
                 $retVal .= "<li>$escapedLine</li>\n";  
             }  
             $retVal .= "</ul>\n";  
         } elsif ($format =~ m/^TEXT$/i) {  
             # Convert the queue into a list of text lines.  
             $retVal = join("\n", @Queue) . "\n";  
         }  
         # Clear the queue.  
         @Queue = ();  
     }  
     # Return the formatted list.  
2987      return $retVal;      return $retVal;
2988  }  }
2989    
2990  =head3 Confess  =head3 Percent
2991    
2992  C<< Confess($message); >>      my $percent = Tracer::Percent($number, $base);
2993    
2994  Trace the call stack and abort the program with the specified message. When used with  Returns the percent of the base represented by the given number. If the base
2995  the OR operator and the L</Assert> method, B<Confess> can function as a debugging assert.  is zero, returns zero.
 So, for example  
2996    
2997  C<< Assert($recNum >= 0) || Confess("Invalid record number $recNum."); >>  =over 4
2998    
2999  Will abort the program with a stack trace if the value of C<$recNum> is negative.  =item number
3000    
3001  =over 4  Percent numerator.
3002    
3003  =item message  =item base
3004    
3005  Message to include in the trace.  Percent base.
3006    
3007    =item RETURN
3008    
3009    Returns the percentage of the base represented by the numerator.
3010    
3011  =back  =back
3012    
3013  =cut  =cut
3014    
3015  sub Confess {  sub Percent {
3016      # Get the parameters.      # Get the parameters.
3017      my ($message) = @_;      my ($number, $base) = @_;
3018      # Trace the call stack.      # Declare the return variable.
3019      Cluck($message);      my $retVal = 0;
3020      # Abort the program.      # Compute the percent.
3021      croak(">>> $message");      if ($base != 0) {
3022            $retVal = $number * 100 / $base;
3023        }
3024        # Return the result.
3025        return $retVal;
3026  }  }
3027    
3028  =head3 Assert  =head3 Constrain
   
 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, B<Assert> can function as a debugging assert.  
 So, for example  
3029    
3030  C<< Assert($recNum >= 0) || Confess("Invalid record number $recNum."); >>      my $constrained = Constrain($value, $min, $max);
3031    
3032  Will abort the program with a stack trace if the value of C<$recNum> is negative.  Modify a numeric value to bring it to a point in between a maximum and a minimum.
3033    
3034  =cut  =over 4
 sub Assert {  
     my $retVal = 1;  
     LOOP: for my $condition (@_) {  
         if (! $condition) {  
             $retVal = 0;  
             last LOOP;  
         }  
     }  
     return $retVal;  
 }  
3035    
3036  =head3 Cluck  =item value
3037    
3038  C<< Cluck($message); >>  Value to constrain.
3039    
3040  Trace the call stack. Note that for best results, you should qualify the call with a  =item min (optional)
 trace condition. For example,  
3041    
3042  C<< Cluck("Starting record parse.") if T(3); >>  Minimum permissible value. If this parameter is undefined, no minimum constraint will be applied.
3043    
3044  will only trace the stack if the trace level for the package is 3 or more.  =item max (optional)
3045    
3046  =over 4  Maximum permissible value. If this parameter is undefined, no maximum constraint will be applied.
3047    
3048  =item message  =item RETURN
3049    
3050  Message to include in the trace.  Returns the incoming value, constrained according to the other parameters.
3051    
3052  =back  =back
3053    
3054  =cut  =cut
3055    
3056  sub Cluck {  sub Constrain {
3057      # Get the parameters.      # Get the parameters.
3058      my ($message) = @_;      my ($value, $min, $max) = @_;
3059      # Trace what's happening.      # Declare the return variable.
3060      Trace("Stack trace for event: $message");      my $retVal = $value;
3061      my $confession = longmess($message);      # Apply the minimum constraint.
3062      # Convert the confession to a series of trace messages. Note we skip any      if (defined $min && $retVal < $min) {
3063      # messages relating to calls into Tracer.          $retVal = $min;
3064      for my $line (split /\s*\n/, $confession) {      }
3065          Trace($line) if ($line !~ /Tracer\.pm/);      # Apply the maximum constraint.
3066        if (defined $max && $retVal > $max) {
3067            $retVal = $max;
3068      }      }
3069        # Return the result.
3070        return $retVal;
3071  }  }
3072    
3073  =head3 Min  =head3 Min
3074    
3075  C<< my $min = Min($value1, $value2, ... $valueN); >>      my $min = Min($value1, $value2, ... $valueN);
3076    
3077  Return the minimum argument. The arguments are treated as numbers.  Return the minimum argument. The arguments are treated as numbers.
3078    
# Line 1238  Line 3105 
3105    
3106  =head3 Max  =head3 Max
3107    
3108  C<< my $max = Max($value1, $value2, ... $valueN); >>      my $max = Max($value1, $value2, ... $valueN);
3109    
3110  Return the maximum argument. The arguments are treated as numbers.  Return the maximum argument. The arguments are treated as numbers.
3111    
# Line 1269  Line 3136 
3136      return $retVal;      return $retVal;
3137  }  }
3138    
 =head3 AddToListMap  
   
 C<< Tracer::AddToListMap(\%hash, $key, $value); >>  
   
 Add a key-value pair to a hash of lists. If no value exists for the key, a singleton list  
 is created for the key. Otherwise, the new value is pushed onto the list.  
   
 =over 4  
   
 =item hash  
   
 Reference to the target hash.  
   
 =item key  
   
 Key for which the value is to be added.  
   
 =item value  
   
 Value to add to the key's value list.  
   
 =back  
   
 =cut  
   
 sub AddToListMap {  
     # Get the parameters.  
     my ($hash, $key, $value) = @_;  
     # Process according to whether or not the key already has a value.  
     if (! exists $hash->{$key}) {  
         $hash->{$key} = [$value];  
     } else {  
         push @{$hash->{$key}}, $value;  
     }  
 }  
   
 =head3 DebugMode  
   
 C<< if (Tracer::DebugMode) { ...code... } >>  
   
 Return TRUE if debug mode has been turned on, else output an error  
 page and return FALSE.  
   
 Certain CGI scripts are too dangerous to exist in the production  
 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 web page will be output directing the  
 user to enter in the correct password.  
   
 =cut  
   
 sub DebugMode {  
     # Declare the return variable.  
     my $retVal = 0;  
     # Check the debug configuration.  
     my $password = CGI::cookie("DebugMode");  
     my $encrypted = Digest::MD5::md5_hex($password);  
     if ($encrypted eq "252dec43280e0c0d6a75ffcec486e61d") {  
         $retVal = 1;  
     } else {  
         # Here debug mode is off, so we generate an error page.  
         my $pageString = PageBuilder::Build("<Html/ErrorPage.html", {}, "Html");  
         print $pageString;  
     }  
     # Return the determination indicator.  
     return $retVal;  
 }  
3139    
3140  =head3 Strip  =head3 Strip
3141    
3142  C<< my $string = Tracer::Strip($line); >>      my $string = Tracer::Strip($line);
3143    
3144  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
3145  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 1363  Line 3162 
3162  sub Strip {  sub Strip {
3163      # Get a copy of the parameter string.      # Get a copy of the parameter string.
3164      my ($string) = @_;      my ($string) = @_;
3165      my $retVal = $string;      my $retVal = (defined $string ? $string : "");
3166      # Strip the line terminator characters.      # Strip the line terminator characters.
3167      $retVal =~ s/(\r|\n)+$//g;      $retVal =~ s/(\r|\n)+$//g;
3168      # Return the result.      # Return the result.
# Line 1372  Line 3171 
3171    
3172  =head3 Pad  =head3 Pad
3173    
3174  C<< my $paddedString = Tracer::Pad($string, $len, $left, $padChar); >>      my $paddedString = Tracer::Pad($string, $len, $left, $padChar);
3175    
3176  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
3177  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 1431  Line 3230 
3230      return $retVal;      return $retVal;
3231  }  }
3232    
3233    =head3 EOF
3234    
3235    This is a constant that is lexically greater than any useful string.
3236    
3237    =cut
3238    
3239    sub EOF {
3240        return "\xFF\xFF\xFF\xFF\xFF";
3241    }
3242    
3243  =head3 TICK  =head3 TICK
3244    
3245  C<< my @results = TICK($commandString); >>      my @results = TICK($commandString);
3246    
3247  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
3248  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 1472  Line 3281 
3281      return `$commandString`;      return `$commandString`;
3282  }  }
3283    
3284    
3285    =head3 CommaFormat
3286    
3287        my $formatted = Tracer::CommaFormat($number);
3288    
3289    Insert commas into a number.
3290    
3291    =over 4
3292    
3293    =item number
3294    
3295    A sequence of digits.
3296    
3297    =item RETURN
3298    
3299    Returns the same digits with commas strategically inserted.
3300    
3301    =back
3302    
3303    =cut
3304    
3305    sub CommaFormat {
3306        # Get the parameters.
3307        my ($number) = @_;
3308        # Pad the length up to a multiple of three.
3309        my $padded = "$number";
3310        $padded = " " . $padded while length($padded) % 3 != 0;
3311        # This is a fancy PERL trick. The parentheses in the SPLIT pattern
3312        # cause the delimiters to be included in the output stream. The
3313        # GREP removes the empty strings in between the delimiters.
3314        my $retVal = join(",", grep { $_ ne '' } split(/(...)/, $padded));
3315        # Clean out the spaces.
3316        $retVal =~ s/ //g;
3317        # Return the result.
3318        return $retVal;
3319    }
3320    
3321    
3322    =head3 CompareLists
3323    
3324        my ($inserted, $deleted) = Tracer::CompareLists(\@newList, \@oldList, $keyIndex);
3325    
3326    Compare two lists of tuples, and return a hash analyzing the differences. The lists
3327    are presumed to be sorted alphabetically by the value in the $keyIndex column.
3328    The return value contains a list of items that are only in the new list
3329    (inserted) and only in the old list (deleted).
3330    
3331    =over 4
3332    
3333    =item newList
3334    
3335    Reference to a list of new tuples.
3336    
3337    =item oldList
3338    
3339    Reference to a list of old tuples.
3340    
3341    =item keyIndex (optional)
3342    
3343    Index into each tuple of its key field. The default is 0.
3344    
3345    =item RETURN
3346    
3347    Returns a 2-tuple consisting of a reference to the list of items that are only in the new
3348    list (inserted) followed by a reference to the list of items that are only in the old
3349    list (deleted).
3350    
3351    =back
3352    
3353    =cut
3354    
3355    sub CompareLists {
3356        # Get the parameters.
3357        my ($newList, $oldList, $keyIndex) = @_;
3358        if (! defined $keyIndex) {
3359            $keyIndex = 0;
3360        }
3361        # Declare the return variables.
3362        my ($inserted, $deleted) = ([], []);
3363        # Loop through the two lists simultaneously.
3364        my ($newI, $oldI) = (0, 0);
3365        my ($newN, $oldN) = (scalar @{$newList}, scalar @{$oldList});
3366        while ($newI < $newN || $oldI < $oldN) {
3367            # Get the current object in each list. Note that if one
3368            # of the lists is past the end, we'll get undef.
3369            my $newItem = $newList->[$newI];
3370            my $oldItem = $oldList->[$oldI];
3371            if (! defined($newItem) || defined($oldItem) && $newItem->[$keyIndex] gt $oldItem->[$keyIndex]) {
3372                # The old item is not in the new list, so mark it deleted.
3373                push @{$deleted}, $oldItem;
3374                $oldI++;
3375            } elsif (! defined($oldItem) || $oldItem->[$keyIndex] gt $newItem->[$keyIndex]) {
3376                # The new item is not in the old list, so mark it inserted.
3377                push @{$inserted}, $newItem;
3378                $newI++;
3379            } else {
3380                # The item is in both lists, so push forward.
3381                $oldI++;
3382                $newI++;
3383            }
3384        }
3385        # Return the result.
3386        return ($inserted, $deleted);
3387    }
3388    
3389    =head3 GenerateURL
3390    
3391        my $queryUrl = Tracer::GenerateURL($page, %parameters);
3392    
3393    Generate a GET-style URL for the specified page with the specified parameter
3394    names and values. The values will be URL-escaped automatically. So, for
3395    example
3396    
3397        Tracer::GenerateURL("form.cgi", type => 1, string => "\"high pass\" or highway")
3398    
3399    would return
3400    
3401        form.cgi?type=1;string=%22high%20pass%22%20or%20highway
3402    
3403    =over 4
3404    
3405    =item page
3406    
3407    Page URL.
3408    
3409    =item parameters
3410    
3411    Hash mapping parameter names to parameter values.
3412    
3413    =item RETURN
3414    
3415    Returns a GET-style URL that goes to the specified page and passes in the
3416    specified parameters and values.
3417    
3418    =back
3419    
3420    =cut
3421    
3422    sub GenerateURL {
3423        # Get the parameters.
3424        my ($page, %parameters) = @_;
3425        # Prime the return variable with the page URL.
3426        my $retVal = $page;
3427        # Loop through the parameters, creating parameter elements in a list.
3428        my @parmList = map { "$_=" . uri_escape($parameters{$_}) } keys %parameters;
3429        # If the list is nonempty, tack it on.
3430        if (@parmList) {
3431            $retVal .= "?" . join(";", @parmList);
3432        }
3433        # Return the result.
3434        return $retVal;
3435    }
3436    
3437    =head3 ApplyURL
3438    
3439        Tracer::ApplyURL($table, $target, $url);
3440    
3441    Run through a two-dimensional table (or more accurately, a list of lists), converting the
3442    I<$target> column to HTML text having a hyperlink to a URL in the I<$url> column. The
3443    URL column will be deleted by this process and the target column will be HTML-escaped.
3444    
3445    This provides a simple way to process the results of a database query into something
3446    displayable by combining a URL with text.
3447    
3448    =over 4
3449    
3450    =item table
3451    
3452    Reference to a list of lists. The elements in the containing list will be updated by
3453    this method.
3454    
3455    =item target
3456    
3457    The index of the column to be converted into HTML.
3458    
3459    =item url
3460    
3461    The index of the column containing the URL. Note that the URL must have a recognizable
3462    C<http:> at the beginning.
3463    
3464    =back
3465    
3466    =cut
3467    
3468    sub ApplyURL {
3469        # Get the parameters.
3470        my ($table, $target, $url) = @_;
3471        # Loop through the table.
3472        for my $row (@{$table}) {
3473            # Apply the URL to the target cell.
3474            $row->[$target] = CombineURL($row->[$target], $row->[$url]);
3475            # Delete the URL from the row.
3476            delete $row->[$url];
3477        }
3478    }
3479    
3480    =head3 CombineURL
3481    
3482        my $combinedHtml = Tracer::CombineURL($text, $url);
3483    
3484    This method will convert the specified text into HTML hyperlinked to the specified
3485    URL. The hyperlinking will only take place if the URL looks legitimate: that is, it
3486    is defined and begins with an C<http:> header.
3487    
3488    =over 4
3489    
3490    =item text
3491    
3492    Text to return. This will be HTML-escaped automatically.
3493    
3494    =item url
3495    
3496    A URL to be hyperlinked to the text. If it does not look like a URL, then the text
3497    will be returned without any hyperlinking.
3498    
3499    =item RETURN
3500    
3501    Returns the original text, HTML-escaped, with the URL hyperlinked to it. If the URL
3502    doesn't look right, the HTML-escaped text will be returned without any further
3503    modification.
3504    
3505    =back
3506    
3507    =cut
3508    
3509    sub CombineURL {
3510        # Get the parameters.
3511        my ($text, $url) = @_;
3512        # Declare the return variable.
3513        my $retVal = CGI::escapeHTML($text);
3514        # Verify the URL.
3515        if (defined($url) && $url =~ m!http://!i) {
3516            # It's good, so we apply it to the text.
3517            $retVal = "<a href=\"$url\">$retVal</a>";
3518        }
3519        # Return the result.
3520        return $retVal;
3521    }
3522    
3523    =head3 Cmp
3524    
3525        my $cmp = Tracer::Cmp($a, $b);
3526    
3527    This method performs a universal sort comparison. Each value coming in is
3528    separated into a leading text part and a trailing number part. The text
3529    part is string compared, and if both parts are equal, then the number
3530    parts are compared numerically. A stream of just numbers or a stream of
3531    just strings will sort correctly, and a mixed stream will sort with the
3532    numbers first. Strings with a label and a number will sort in the
3533    expected manner instead of lexically.
3534    
3535    =over 4
3536    
3537    =item a
3538    
3539    First item to compare.
3540    
3541    =item b
3542    
3543    Second item to compare.
3544    
3545    =item RETURN
3546    
3547    Returns a negative number if the first item should sort first (is less), a positive
3548    number if the first item should sort second (is greater), and a zero if the items are
3549    equal.
3550    
3551    =back
3552    
3553    =cut
3554    
3555    sub Cmp {
3556        # Get the parameters.
3557        my ($a, $b) = @_;
3558        # Declare the return value.
3559        my $retVal;
3560        # Check for nulls.
3561        if (! defined($a)) {
3562            $retVal = (! defined($b) ? 0 : -1);
3563        } elsif (! defined($b)) {
3564            $retVal = 1;
3565        } else {
3566            # Here we have two real values. Parse the two strings.
3567            $a =~ /^(\D*)(\d*)$/;
3568            my $aParsed = [$1, $2];
3569            $b =~ /^(\D*)(\d*)$/;
3570            my $bParsed = [$1, $2];
3571            # Compare the string parts.
3572            $retVal = $aParsed->[0] cmp $bParse