[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.9, Wed May 4 03:05:12 2005 UTC revision 1.43, Mon Apr 24 21:05:17 2006 UTC
# Line 1  Line 1 
1    #
2    # Copyright (c) 2003-2006 University of Chicago and Fellowship
3    # for Interpretations of Genomes. All Rights Reserved.
4    #
5    # This file is part of the SEED Toolkit.
6    #
7    # The SEED Toolkit is free software. You can redistribute
8    # it and/or modify it under the terms of the SEED Toolkit
9    # Public License.
10    #
11    # You should have received a copy of the SEED Toolkit Public License
12    # along with this program; if not write to the University of Chicago
13    # at info@ci.uchicago.edu or the Fellowship for Interpretation of
14    # Genomes at veronika@thefig.info or download a copy from
15    # http://www.theseed.org/LICENSE.TXT.
16    #
17    
18  package Tracer;  package Tracer;
19    
20          require Exporter;          require Exporter;
21          @ISA = ('Exporter');          @ISA = ('Exporter');
22          @EXPORT = qw(Trace T TSetup QTrace Confess Cluck Min Max Assert);      @EXPORT = qw(Trace T TSetup QTrace Confess Cluck Min Max Assert Open OpenDir TICK StandardSetup ScriptSetup ScriptFinish Insure);
23          @EXPORT_OK = qw(GetFile GetOptions Merge MergeOptions ParseCommand ParseRecord UnEscape Escape);          @EXPORT_OK = qw(GetFile GetOptions Merge MergeOptions ParseCommand ParseRecord UnEscape Escape);
24          use strict;          use strict;
25          use Carp qw(longmess croak);          use Carp qw(longmess croak);
26          use CGI;          use CGI;
27          use FIG_Config;          use FIG_Config;
28      use PageBuilder;      use PageBuilder;
29        use Digest::MD5;
30        use File::Basename;
31        use File::Path;
32    
33  =head1 Tracing and Debugging Helpers  =head1 Tracing and Debugging Helpers
34    
# Line 20  Line 40 
40  has a list of categories and a single trace level set by the B<TSetup> method. Only messages whose trace  has a list of categories and a single trace level set by the B<TSetup> method. Only messages whose trace
41  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 this package's trace level and whose category is activated will
42  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
43  is less likely to be seen. A higher trace level passed to B<Setup> means more trace messages will  is less likely to be seen. A higher trace level passed to B<TSetup> means more trace messages will
44  appear. To generate a trace message, use the following syntax.  appear. To generate a trace message, use the following syntax.
45    
46  C<< Trace($message) if T(errors => 4); >>  C<< Trace($message) if T(errors => 4); >>
# Line 38  Line 58 
58    
59  C<< Trace($message) if T(2); >>  C<< Trace($message) if T(2); >>
60    
61  To set up tracing, you call the C</Setup> method. The method takes as input a trace level, a list  To set up tracing, you call the L</TSetup> method. The method takes as input a trace level, a list
62  of category names, and a set of options. The trace level and list of category names are  of category names, and a set of options. The trace level and list of category names are
63  specified as a space-delimited string. Thus  specified as a space-delimited string. Thus
64    
65  C<< TSetup('3 errors Sprout ERDB', 'HTML'); >>  C<< TSetup('3 errors Sprout ERDB', 'HTML'); >>
66    
67  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
68  specifies that messages should be output as HTML paragraphs. The parameters are formatted  specifies that messages should be output as HTML paragraphs.
69  to make it easier to input tracing configuration on a web form.  
70    To turn on tracing for ALL categories, use an asterisk. The call below sets every category to
71    level 3 and writes the output to the standard error output. This sort of thing might be
72    useful in a CGI environment.
73    
74    C<< TSetup('3 *', 'WARN'); >>
75    
76  In addition to HTML and file output for trace messages, you can specify that the trace messages  In addition to HTML and file output for trace messages, you can specify that the trace messages
77  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
# Line 61  Line 86 
86  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
87  being used out in the field.  being used out in the field.
88    
89    There is no hard and fast rule on how to use trace levels. The following is therefore only
90    a suggestion.
91    
92    =over 4
93    
94    =item Error 0
95    
96    Message indicates an error that may lead to incorrect results or that has stopped the
97    application entirely.
98    
99    =item Warning 1
100    
101    Message indicates something that is unexpected but that probably did not interfere
102    with program execution.
103    
104    =item Notice 2
105    
106    Message indicates the beginning or end of a major task.
107    
108    =item Information 3
109    
110    Message indicates a subtask. In the FIG system, a subtask generally relates to a single
111    genome. This would be a big loop that is not expected to execute more than 500 times or so.
112    
113    =item Detail 4
114    
115    Message indicates a low-level loop iteration.
116    
117    =back
118    
119  =cut  =cut
120    
121  # Declare the configuration variables.  # Declare the configuration variables.
122    
123  my $Destination = "NONE";       # Description of where to send the trace output.  my $Destination = "NONE";       # Description of where to send the trace output.
124    my $TeeFlag = 0;            # TRUE if output is going to a file and to the
125                                # standard output
126  my %Categories = ( main => 1 );  my %Categories = ( main => 1 );
127                                                          # hash of active category names                                                          # hash of active category names
128  my $TraceLevel = 0;                     # trace level; a higher trace level produces more  my $TraceLevel = 0;                     # trace level; a higher trace level produces more
129                                                          # messages                                                          # messages
130  my @Queue = ();                         # queued list of trace messages.  my @Queue = ();                         # queued list of trace messages.
131  my $LastCategory = "main";  # name of the last category interrogated  my $LastCategory = "main";  # name of the last category interrogated
132    my $SetupCount = 0;         # number of times TSetup called
133    my $AllTrace = 0;           # TRUE if we are tracing all categories.
134    
135  =head2 Public Methods  =head2 Public Methods
136    
# Line 93  Line 152 
152    
153  The destination for the trace output. To send the trace output to a file, specify the file  The destination for the trace output. To send the trace output to a file, specify the file
154  name preceded by a ">" symbol. If a double symbol is used (">>"), then the data is appended  name preceded by a ">" symbol. If a double symbol is used (">>"), then the data is appended
155  to the file. Otherwise the file is cleared before tracing begins. In addition to sending  to the file. Otherwise the file is cleared before tracing begins. Precede the first ">"
156  the trace messages to a file, you can specify a special destination. C<HTML> will cause  symbol with a C<+> to echo output to a file AND to the standard output. In addition to
157  tracing to the standard output with each line formatted as an HTML paragraph. C<TEXT>  sending the trace messages to a file, you can specify a special destination. C<HTML> will
158    cause tracing to the standard output with each line formatted as an HTML paragraph. C<TEXT>
159  will cause tracing to the standard output as ordinary text. C<ERROR> will cause trace  will cause tracing to the standard output as ordinary text. C<ERROR> will cause trace
160  messages to be sent to the standard error output as ordinary text. C<QUEUE> will cause trace  messages to be sent to the standard error output as ordinary text. C<QUEUE> will cause trace
161  messages to be stored in a queue for later retrieval by the L</QTrace> method. C<WARN> will  messages to be stored in a queue for later retrieval by the L</QTrace> method. C<WARN> will
# Line 113  Line 173 
173          my @categoryData = split /\s+/, $categoryList;          my @categoryData = split /\s+/, $categoryList;
174          # Extract the trace level.          # Extract the trace level.
175          $TraceLevel = shift @categoryData;          $TraceLevel = shift @categoryData;
176          # Build the category hash.      # Presume category-based tracing until we learn otherwise.
177        $AllTrace = 0;
178        # Build the category hash. Note that if we find a "*", we turn on non-category
179        # tracing. We must also clear away any pre-existing data.
180        %Categories = ( main => 1 );
181          for my $category (@categoryData) {          for my $category (@categoryData) {
182                  $Categories{$category} = 1;          if ($category eq '*') {
183                $AllTrace = 1;
184            } else {
185                $Categories{lc $category} = 1;
186            }
187          }          }
188          # Now we need to process the destination information. The most important special          # Now we need to process the destination information. The most important special
189          # case is the single ">", which requires we clear the file first. After doing      # cases are the single ">", which requires we clear the file first, and the
190          # so, we tack on another ">" sign so that future trace messages are appended.      # "+" prefix which indicates a double echo.
191        if ($target =~ m/^\+?>>?/) {
192            if ($target =~ m/^\+/) {
193                $TeeFlag = 1;
194                $target = substr($target, 1);
195            }
196          if ($target =~ m/^>[^>]/) {          if ($target =~ m/^>[^>]/) {
197                  open TRACEFILE, $target;                  open TRACEFILE, $target;
198                  print TRACEFILE Now() . " Tracing initialized.\n";                  print TRACEFILE Now() . " Tracing initialized.\n";
199                  close TRACEFILE;                  close TRACEFILE;
200                  $Destination = ">$target";                  $Destination = ">$target";
201          } else {          } else {
202                $Destination = $target;
203            }
204        } else {
205                  $Destination = uc($target);                  $Destination = uc($target);
206          }          }
207        # Increment the setup counter.
208        $SetupCount++;
209    }
210    
211    =head3 StandardSetup
212    
213    C<< my ($options, @parameters) = StandardSetup(\@categories, \%options, $parmHelp, @ARGV); >>
214    
215    This method performs standard command-line parsing and tracing setup. The return
216    values are a hash of the command-line options and a list of the positional
217    parameters. Tracing is automatically set up and the command-line options are
218    validated.
219    
220    This is a complex method that does a lot of grunt work. The parameters can
221    be more easily understood, however, once they are examined individually.
222    
223    The I<categories> parameter is the most obtuse. It is a reference to a list of
224    special-purpose tracing categories. Most tracing categories are PERL package
225    names. So, for example, if you wanted to turn on tracing inside the B<Sprout>,
226    B<ERDB>, and B<SproutLoad> packages, you would specify the categories
227    
228        ["Sprout", "SproutLoad", "ERDB"]
229    
230    This would cause trace messages in the specified three packages to appear in
231    the output. There are threer special tracing categories that are automatically
232    handled by this method. In other words, if you used L</TSetup> you would need
233    to include these categories manually, but if you use this method they are turned
234    on automatically.
235    
236    =over 4
237    
238    =item FIG
239    
240    Turns on trace messages inside the B<FIG> package.
241    
242    =item SQL
243    
244    Traces SQL commands and activity.
245    
246    =item Tracer
247    
248    Traces error messages and call stacks.
249    
250    =back
251    
252    C<SQL> is only turned on if the C<-sql> option is specified in the command line.
253    The trace level is specified using the C<-trace> command-line option. For example,
254    the following command line for C<TransactFeatures> turns on SQL tracing and runs
255    all tracing at level 3.
256    
257        TransactFeatures -trace=3 -sql register ../xacts IDs.tbl
258    
259    Standard tracing is output to the standard output and echoed to the file
260    C<trace>I<$$>C<.log> in the FIG temporary directory, where I<$$> is the
261    process ID. You can also specify the C<user> parameter to put a user ID
262    instead of a process ID in the trace file name. So, for example
263    
264    The default trace level is 2. To get all messages, specify a trace level of 4.
265    For a genome-by-genome update, use 3.
266    
267        TransactFeatures -trace=3 -sql -user=Bruce register ../xacts IDs.tbl
268    
269    would send the trace output to C<traceBruce.log> in the temporary directory.
270    
271    The I<options> parameter is a reference to a hash containing the command-line
272    options, their default values, and an explanation of what they mean. Command-line
273    options may be in the form of switches or keywords. In the case of a switch, the
274    option value is 1 if it is specified and 0 if it is not specified. In the case
275    of a keyword, the value is separated from the option name by an equal sign. You
276    can see this last in the command-line example above.
277    
278    You can specify a different default trace level by setting C<$options->{trace}>
279    prior to calling this method.
280    
281    An example at this point would help. Consider, for example, the command-line utility
282    C<TransactFeatures>. It accepts a list of positional parameters plus the options
283    C<safe>, C<noAlias>, C<start>, and C<tblFiles>. To start up this command, we execute
284    the following code.
285    
286        my ($options, @parameters) = Tracer::StandardSetup(["DocUtils"],
287                            { safe => [0, "use database transactions"],
288                              noAlias => [0, "do not expect aliases in CHANGE transactions"],
289                              start => [' ', "start with this genome"],
290                              tblFiles => [0, "output TBL files containing the corrected IDs"] },
291                            "command transactionDirectory IDfile",
292                          @ARGV);
293    
294    
295    The call to C<ParseCommand> specifies the default values for the options and
296    stores the actual options in a hash that is returned as C<$options>. The
297    positional parameters are returned in C<@parameters>.
298    
299    The following is a sample command line for C<TransactFeatures>.
300    
301        TransactFeatures -trace=2 -noAlias register ../xacts IDs.tbl
302    
303    In this case, C<register>, C<../xacts>, and C<IDs.tbl> are the positional
304    parameters, and would find themselves in I<@parameters> after executing the
305    above code fragment. The tracing would be set to level 2, and the categories
306    would be C<FIG>, C<Tracer>, and <DocUtils>. C<FIG> and C<Tracer> are standard,
307    and C<DocUtils> was included because it came in within the first parameter
308    to this method. The I<$options> hash would be
309    
310        { trace => 2, sql => 0, safe => 0,
311          noAlias => 1, start => ' ', tblFiles => 0 }
312    
313    Use of C<StandardSetup> in this way provides a simple way of performing
314    standard tracing setup and command-line parsing. Note that the caller is
315    not even aware of the command-line switches C<-trace> and C<-sql>, which
316    are used by this method to control the tracing. If additional tracing features
317    need to be added in the future, they can be processed by this method without
318    upsetting the command-line utilities.
319    
320    If the C<background> option is specified on the command line, then the
321    standard and error outputs will be directed to files in the temporary
322    directory, using the same suffix as the trace file. So, if the command
323    line specified
324    
325        -user=Bruce -background
326    
327    then the trace output would go to C<traceBruce.log>, the standard output to
328    C<outBruce.log>, and the error output to C<errBruce.log>. This is designed to
329    simplify starting a command in the background.
330    
331    Finally, if the special option C<-h> is specified, the option names will
332    be traced at level 0 and the program will exit without processing.
333    This provides a limited help capability. For example, if the user enters
334    
335        TransactFeatures -h
336    
337    he would see the following output.
338    
339        TransactFeatures [options] command transactionDirectory IDfile
340            -trace    tracing level (default 2)
341            -sql      trace SQL commands
342            -safe     use database transactions
343            -noAlias  do not expect aliases in CHANGE transactions
344            -start    start with this genome
345            -tblFiles output TBL files containing the corrected IDs
346    
347    The parameters to this method are as follows.
348    
349    =over 4
350    
351    =item categories
352    
353    Reference to a list of tracing category names. These should be names of
354    packages whose internal workings will need to be debugged to get the
355    command working.
356    
357    =item options
358    
359    Reference to a hash containing the legal options for the current command mapped
360    to their default values and descriptions. The user can override the defaults
361    by specifying the options as command-line switches prefixed by a hyphen.
362    Tracing-related options may be added to this hash. If the C<-h> option is
363    specified on the command line, the option descriptions will be used to
364    explain the options.
365    
366    =item parmHelp
367    
368    A string that vaguely describes the positional parameters. This is used
369    if the user specifies the C<-h> option.
370    
371    =item ARGV
372    
373    List of command line parameters, including the option switches, which must
374    precede the positional parameters and be prefixed by a hyphen.
375    
376    =item RETURN
377    
378    Returns a list. The first element of the list is the reference to a hash that
379    maps the command-line option switches to their values. These will either be the
380    default values or overrides specified on the command line. The remaining
381    elements of the list are the position parameters, in order.
382    
383    =back
384    
385    =cut
386    
387    sub StandardSetup {
388        # Get the parameters.
389        my ($categories, $options, $parmHelp, @argv) = @_;
390        # Add the tracing options.
391        if (! exists $options->{trace}) {
392            $options->{trace} = [2, "tracing level"];
393        }
394        $options->{sql} = [0, "turn on SQL tracing"];
395        $options->{h} = [0, "display command-line options"];
396        $options->{user} = [$$, "trace log file name suffix"];
397        $options->{background} = [0, "spool standard and error output"];
398        # Create a parsing hash from the options hash. The parsing hash
399        # contains the default values rather than the default value
400        # and the description. While we're at it, we'll memorize the
401        # length of the longest option name.
402        my $longestName = 0;
403        my %parseOptions = ();
404        for my $key (keys %{$options}) {
405            if (length $key > $longestName) {
406                $longestName = length $key;
407            }
408            $parseOptions{$key} = $options->{$key}->[0];
409        }
410        # Parse the command line.
411        my ($retOptions, @retParameters) = ParseCommand(\%parseOptions, @argv);
412        # Get the logfile suffix.
413        my $suffix = $retOptions->{user};
414        # Check for background mode.
415        if ($retOptions->{background}) {
416            my $outFileName = "$FIG_Config::temp/out$suffix.log";
417            my $errFileName = "$FIG_Config::temp/err$suffix.log";
418            open STDOUT, ">$outFileName";
419            open STDERR, ">$errFileName";
420        }
421        # Now we want to set up tracing. First, we need to know if SQL is to
422        # be traced.
423        my @cats = @{$categories};
424        if ($retOptions->{sql}) {
425            push @cats, "SQL";
426        }
427        # Add the default categories.
428        push @cats, "Tracer", "FIG";
429        # Next, we create the category string by prefixing the trace level
430        # and joining the categories.
431        my $cats = join(" ", $parseOptions{trace}, @cats);
432        # Verify that we can open a file in the temporary directory.
433        my $traceMode = "TEXT";
434        my $traceFileName = "$FIG_Config::temp/trace$suffix.log";
435        if (open TESTTRACE, ">$traceFileName") {
436            $traceMode = "+>$traceFileName";
437            close TESTTRACE;
438        }
439        # Now set up the tracing.
440        TSetup($cats, $traceMode);
441        # Check for the "h" option. If it is specified, dump the command-line
442        # options and exit the program.
443        if ($retOptions->{h}) {
444            $0 =~ m#[/\\](\w+)(\.pl)?$#i;
445            Trace("$1 [options] $parmHelp") if T(0);
446            for my $key (sort keys %{$options}) {
447                my $name = Pad($key, $longestName, 0, ' ');
448                my $desc = $options->{$key}->[1];
449                if ($options->{$key}->[0]) {
450                    $desc .= " (default " . $options->{$key}->[0] . ")";
451                }
452                Trace("  $name $desc") if T(0);
453            }
454            exit(0);
455        }
456        # Return the parsed parameters.
457        return ($retOptions, @retParameters);
458    }
459    
460    =head3 Setups
461    
462    C<< my $count = Tracer::Setups(); >>
463    
464    Return the number of times L</TSetup> has been called.
465    
466    This method allows for the creation of conditional tracing setups where, for example, we
467    may want to set up tracing if nobody else has done it before us.
468    
469    =cut
470    
471    sub Setups {
472        return $SetupCount;
473    }
474    
475    =head3 Open
476    
477    C<< my $handle = Open($fileHandle, $fileSpec, $message); >>
478    
479    Open a file.
480    
481    The I<$fileSpec> is essentially the second argument of the PERL C<open>
482    function. The mode is specified using Unix-like shell information. So, for
483    example,
484    
485        Open(\*LOGFILE, '>>/usr/spool/news/twitlog', "Could not open twit log.");
486    
487    would open for output appended to the specified file, and
488    
489        Open(\*DATASTREAM, "| sort -u >$outputFile", "Could not open $outputFile.");
490    
491    would open a pipe that sorts the records written and removes duplicates. Note
492    the use of file handle syntax in the Open call. To use anonymous file handles,
493    code as follows.
494    
495        my $logFile = Open(undef, '>>/usr/spool/news/twitlog', "Could not open twit log.");
496    
497    The I<$message> parameter is used if the open fails. If it is set to C<0>, then
498    the open returns TRUE if successful and FALSE if an error occurred. Otherwise, a
499    failed open will throw an exception and the third parameter will be used to construct
500    an error message. If the parameter is omitted, a standard message is constructed
501    using the file spec.
502    
503        Could not open "/usr/spool/news/twitlog"
504    
505    Note that the mode characters are automatically cleaned from the file name.
506    The actual error message from the file system will be captured and appended to the
507    message in any case.
508    
509        Could not open "/usr/spool/news/twitlog": file not found.
510    
511    In some versions of PERL the only error message we get is a number, which
512    corresponds to the C++ C<errno> value.
513    
514        Could not open "/usr/spool/news/twitlog": 6.
515    
516    =over 4
517    
518    =item fileHandle
519    
520    File handle. If this parameter is C<undef>, a file handle will be generated
521    and returned as the value of this method.
522    
523    =item fileSpec
524    
525    File name and mode, as per the PERL C<open> function.
526    
527    =item message (optional)
528    
529    Error message to use if the open fails. If omitted, a standard error message
530    will be generated. In either case, the error information from the file system
531    is appended to the message. To specify a conditional open that does not throw
532    an error if it fails, use C<0>.
533    
534    =item RETURN
535    
536    Returns the name of the file handle assigned to the file, or C<undef> if the
537    open failed.
538    
539    =back
540    
541    =cut
542    
543    sub Open {
544        # Get the parameters.
545        my ($fileHandle, $fileSpec, $message) = @_;
546        # Attempt to open the file.
547        my $rv = open $fileHandle, $fileSpec;
548        # If the open failed, generate an error message.
549        if (! $rv) {
550            # Save the system error message.
551            my $sysMessage = $!;
552            # See if we need a default message.
553            if (!$message) {
554                # Clean any obvious mode characters and leading spaces from the
555                # filename.
556                my ($fileName) = FindNamePart($fileSpec);
557                $message = "Could not open \"$fileName\"";
558            }
559            # Terminate with an error using the supplied message and the
560            # error message from the file system.
561            Confess("$message: $!");
562        }
563        # Return the file handle.
564        return $fileHandle;
565    }
566    
567    =head3 FindNamePart
568    
569    C<< my ($fileName, $start, $len) = Tracer::FindNamePart($fileSpec); >>
570    
571    Extract the portion of a file specification that contains the file name.
572    
573    A file specification is the string passed to an C<open> call. It specifies the file
574    mode and name. In a truly complex situation, it can specify a pipe sequence. This
575    method assumes that the file name is whatever follows the first angle bracket
576    sequence.  So, for example, in the following strings the file name is
577    C</usr/fig/myfile.txt>.
578    
579        >>/usr/fig/myfile.txt
580        </usr/fig/myfile.txt
581        | sort -u > /usr/fig/myfile.txt
582    
583    If the method cannot find a file name using its normal methods, it will return the
584    whole incoming string.
585    
586    =over 4
587    
588    =item fileSpec
589    
590    File specification string from which the file name is to be extracted.
591    
592    =item RETURN
593    
594    Returns a three-element list. The first element contains the file name portion of
595    the specified string, or the whole string if a file name cannot be found via normal
596    methods. The second element contains the start position of the file name portion and
597    the third element contains the length.
598    
599    =back
600    
601    =cut
602    #: Return Type $;
603    sub FindNamePart {
604        # Get the parameters.
605        my ($fileSpec) = @_;
606        # Default to the whole input string.
607        my ($retVal, $pos, $len) = ($fileSpec, 0, length $fileSpec);
608        # Parse out the file name if we can.
609        if ($fileSpec =~ m/(<|>>?)(.+?)(\s*)$/) {
610            $retVal = $2;
611            $len = length $retVal;
612            $pos = (length $fileSpec) - (length $3) - $len;
613        }
614        # Return the result.
615        return ($retVal, $pos, $len);
616    }
617    
618    =head3 OpenDir
619    
620    C<< my @files = OpenDir($dirName, $filtered, $flag); >>
621    
622    Open a directory and return all the file names. This function essentially performs
623    the functions of an C<opendir> and C<readdir>. If the I<$filtered> parameter is
624    set to TRUE, all filenames beginning with a period (C<.>), dollar sign (C<$>),
625    or pound sign (C<#>) and all filenames ending with a tilde C<~>) will be
626    filtered out of the return list. If the directory does not open and I<$flag> is not
627    set, an exception is thrown. So, for example,
628    
629        my @files = OpenDir("/Volumes/fig/contigs", 1);
630    
631    is effectively the same as
632    
633        opendir(TMP, "/Volumes/fig/contigs") || Confess("Could not open /Volumes/fig/contigs.");
634        my @files = grep { $_ !~ /^[\.\$\#]/ && $_ !~ /~$/ } readdir(TMP);
635    
636    Similarly, the following code
637    
638        my @files = grep { $_ =~ /^\d/ } OpenDir("/Volumes/fig/orgs", 0, 1);
639    
640    Returns the names of all files in C</Volumes/fig/orgs> that begin with digits and
641    automatically returns an empty list if the directory fails to open.
642    
643    =over 4
644    
645    =item dirName
646    
647    Name of the directory to open.
648    
649    =item filtered
650    
651    TRUE if files whose names begin with a period (C<.>) should be automatically removed
652    from the list, else FALSE.
653    
654    =item flag
655    
656    TRUE if a failure to open is okay, else FALSE
657    
658    =back
659    
660    =cut
661    #: Return Type @;
662    sub OpenDir {
663        # Get the parameters.
664        my ($dirName, $filtered, $flag) = @_;
665        # Declare the return variable.
666        my @retVal = ();
667        # Open the directory.
668        if (opendir(my $dirHandle, $dirName)) {
669            # The directory opened successfully. Get the appropriate list according to the
670            # strictures of the filter parameter.
671            if ($filtered) {
672                @retVal = grep { $_ !~ /^[\.\$\#]/ && $_ !~ /~$/ } readdir $dirHandle;
673            } else {
674                @retVal = readdir $dirHandle;
675            }
676        } elsif (! $flag) {
677            # Here the directory would not open and it's considered an error.
678            Confess("Could not open directory $dirName.");
679        }
680        # Return the result.
681        return @retVal;
682  }  }
683    
684  =head3 SetLevel  =head3 SetLevel
# Line 394  Line 945 
945         warn $message;         warn $message;
946          } elsif ($Destination =~ m/^>>/) {          } elsif ($Destination =~ m/^>>/) {
947                  # Write the trace message to an output file.                  # Write the trace message to an output file.
948                  open TRACING, $Destination;          (open TRACING, $Destination) || die "Tracing open for \"$Destination\" failed: $!";
949                  print TRACING "$formatted\n";                  print TRACING "$formatted\n";
950                  close TRACING;                  close TRACING;
951            # If the Tee flag is on, echo it to the standard output.
952            if ($TeeFlag) {
953                print "$formatted\n";
954            }
955          }          }
956  }  }
957    
# Line 439  Line 994 
994                  my ($category, $traceLevel) = @_;                  my ($category, $traceLevel) = @_;
995                  if (!defined $traceLevel) {                  if (!defined $traceLevel) {
996                          # Here we have no category, so we need to get the calling package.                          # Here we have no category, so we need to get the calling package.
997                # The calling package is normally the first parameter. If it is
998                # omitted, the first parameter will be the tracelevel. So, the
999                # first thing we do is shift the so-called category into the
1000                # $traceLevel variable where it belongs.
1001                          $traceLevel = $category;                          $traceLevel = $category;
1002                          my ($package, $fileName, $line) = caller;                          my ($package, $fileName, $line) = caller;
1003              # If there is no calling package, we default to "main".              # If there is no calling package, we default to "main".
# Line 450  Line 1009 
1009                  }                  }
1010          # Save the category name.          # Save the category name.
1011          $LastCategory = $category;          $LastCategory = $category;
1012            # Convert it to lower case before we hash it.
1013            $category = lc $category;
1014                  # Use the category and tracelevel to compute the result.                  # Use the category and tracelevel to compute the result.
1015                  $retVal = ($traceLevel <= $TraceLevel && exists $Categories{$category});          if (ref $traceLevel) {
1016                Confess("Bad trace level.");
1017            } elsif (ref $TraceLevel) {
1018                Confess("Bad trace config.");
1019            }
1020            $retVal = ($traceLevel <= $TraceLevel && ($AllTrace || exists $Categories{$category}));
1021      }      }
1022          # Return the computed result.          # Return the computed result.
1023      return $retVal;      return $retVal;
# Line 537  Line 1103 
1103    
1104  C<< my $codedString = Tracer::Escape($realString); >>  C<< my $codedString = Tracer::Escape($realString); >>
1105    
1106  Escape a string for use in a command length. Spaces will be replaced by C<\b>,  Escape a string for use in a command length. Tabs will be replaced by C<\t>, new-lines
1107  tabs replaced by C<\t>, new-lines replaced by C<\n>, and backslashes will be  replaced by C<\n>, carriage returns will be deleted, and backslashes will be doubled. The
1108  doubled. The effect is to exactly reverse the effect of L</UnEscape>.  result is to reverse the effect of L</UnEscape>.
1109    
1110  =over 4  =over 4
1111    
# Line 563  Line 1129 
1129          # Loop through the parameter string, looking for sequences to escape.          # Loop through the parameter string, looking for sequences to escape.
1130          while (length $realString > 0) {          while (length $realString > 0) {
1131                  # Look for the first sequence to escape.                  # Look for the first sequence to escape.
1132                  if ($realString =~ /^(.*?)([ \n\t\\])/) {          if ($realString =~ /^(.*?)([\n\t\r\\])/) {
1133                          # 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
1134                          # 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.
1135                          $retVal .= $1;                          $retVal .= $1;
1136                          $realString = substr $realString, (length $2 + length $1);              # Strip the processed section off the real string.
1137                          # Encode the escape sequence.              $realString = substr $realString, (length $2) + (length $1);
1138                # Get the matched character.
1139                          my $char = $2;                          my $char = $2;
1140                          $char =~ tr/ \t\n/btn/;              # If we have a CR, we are done.
1141                if ($char ne "\r") {
1142                    # It's not a CR, so encode the escape sequence.
1143                    $char =~ tr/\t\n/tn/;
1144                          $retVal .= "\\" . $char;                          $retVal .= "\\" . $char;
1145                }
1146                  } else {                  } else {
1147                          # 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
1148                          # transferred unmodified.                          # transferred unmodified.
# Line 587  Line 1158 
1158    
1159  C<< my $realString = Tracer::UnEscape($codedString); >>  C<< my $realString = Tracer::UnEscape($codedString); >>
1160    
1161  Replace escape sequences with their actual equivalents. C<\b> will be replaced by a space,  Replace escape sequences with their actual equivalents. C<\t> will be replaced by
1162  C<\t> by 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
1163    be deleted.
1164    
1165  =over 4  =over 4
1166    
# Line 613  Line 1185 
1185          # Only proceed if the incoming string is nonempty.          # Only proceed if the incoming string is nonempty.
1186          if (defined $codedString) {          if (defined $codedString) {
1187                  # Loop through the parameter string, looking for escape sequences. We can't do                  # Loop through the parameter string, looking for escape sequences. We can't do
1188                  # translating because it causes problems with the escaped slash. ("\\b" becomes          # translating because it causes problems with the escaped slash. ("\\t" becomes
1189                  # "\ " no matter what we do.)          # "\<tab>" no matter what we do.)
1190                  while (length $codedString > 0) {                  while (length $codedString > 0) {
1191                          # Look for the first escape sequence.                          # Look for the first escape sequence.
1192                          if ($codedString =~ /^(.*?)\\(\\|b|n|t)/) {              if ($codedString =~ /^(.*?)\\(\\|n|t|r)/) {
1193                                  # 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
1194                                  # 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.
1195                                  $retVal .= $1;                                  $retVal .= $1;
1196                                  $codedString = substr $codedString, (2 + length $1);                                  $codedString = substr $codedString, (2 + length $1);
1197                                  # Decode the escape sequence.                  # Get the escape value.
1198                                  my $char = $2;                                  my $char = $2;
1199                                  $char =~ tr/\\btn/\\ \t\n/;                  # If we have a "\r", we are done.
1200                    if ($char ne 'r') {
1201                        # Here it's not an 'r', so we convert it.
1202                        $char =~ tr/\\tn/\\\t\n/;
1203                                  $retVal .= $char;                                  $retVal .= $char;
1204                    }
1205                          } else {                          } else {
1206                                  # 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
1207                                  # transferred unmodified.                                  # transferred unmodified.
# Line 735  Line 1311 
1311    
1312  C<< my @fileContents = Tracer::GetFile($fileName); >>  C<< my @fileContents = Tracer::GetFile($fileName); >>
1313    
1314  Return the entire contents of a file.      or
1315    
1316    C<< my $fileContents = Tracer::GetFile($fileName); >>
1317    
1318    Return the entire contents of a file. In list context, line-ends are removed and
1319    each line is a list element. In scalar context, line-ends are replaced by C<\n>.
1320    
1321  =over 4  =over 4
1322    
# Line 746  Line 1327 
1327  =item RETURN  =item RETURN
1328    
1329  In a list context, returns the entire file as a list with the line terminators removed.  In a list context, returns the entire file as a list with the line terminators removed.
1330  In a scalar context, returns the entire file as a string.  In a scalar context, returns the entire file as a string. If an error occurs opening
1331    the file, an empty list will be returned.
1332    
1333  =back  =back
1334    
# Line 761  Line 1343 
1343          my $ok = open INPUTFILE, "<$fileName";          my $ok = open INPUTFILE, "<$fileName";
1344          if (!$ok) {          if (!$ok) {
1345                  # If we had an error, trace it. We will automatically return a null value.                  # If we had an error, trace it. We will automatically return a null value.
1346                  Trace("Could not open \"$fileName\" for input.") if T(0);          Trace("Could not open \"$fileName\" for input: $!") if T(0);
1347          } else {          } else {
1348                  # Read the whole file into the return variable, stripping off any terminator                  # Read the whole file into the return variable, stripping off any terminator
1349          # characters.          # characters.
# Line 774  Line 1356 
1356                  # Close it.                  # Close it.
1357                  close INPUTFILE;                  close INPUTFILE;
1358          my $actualLines = @retVal;          my $actualLines = @retVal;
         Trace("$lineCount lines read from $fileName. $actualLines processed.") if T(3);  
1359          }          }
1360          # Return the file's contents in the desired format.          # Return the file's contents in the desired format.
1361      if (wantarray) {      if (wantarray) {
# Line 805  Line 1386 
1386          my ($format) = @_;          my ($format) = @_;
1387          # Create the return variable.          # Create the return variable.
1388          my $retVal = "";          my $retVal = "";
1389        # Only proceed if there is an actual queue.
1390        if (@Queue) {
1391          # Process according to the format.          # Process according to the format.
1392          if ($format =~ m/^HTML$/i) {          if ($format =~ m/^HTML$/i) {
1393                  # Convert the queue into an HTML list.                  # Convert the queue into an HTML list.
# Line 820  Line 1403 
1403          }          }
1404          # Clear the queue.          # Clear the queue.
1405          @Queue = ();          @Queue = ();
1406        }
1407          # Return the formatted list.          # Return the formatted list.
1408          return $retVal;          return $retVal;
1409  }  }
# Line 828  Line 1412 
1412    
1413  C<< Confess($message); >>  C<< Confess($message); >>
1414    
1415  Trace the call stack and abort the program with the specified message. The stack  Trace the call stack and abort the program with the specified message. When used with
 trace will only appear if the trace level for this package is 1 or more. When used with  
1416  the OR operator and the L</Assert> method, B<Confess> can function as a debugging assert.  the OR operator and the L</Assert> method, B<Confess> can function as a debugging assert.
1417  So, for example  So, for example
1418    
# Line 851  Line 1434 
1434          # Get the parameters.          # Get the parameters.
1435          my ($message) = @_;          my ($message) = @_;
1436          # Trace the call stack.          # Trace the call stack.
1437          Cluck($message) if T(1);      Cluck($message);
1438          # Abort the program.          # Abort the program.
1439          croak(">>> $message");          croak(">>> $message");
1440  }  }
# Line 861  Line 1444 
1444  C<< Assert($condition1, $condition2, ... $conditionN); >>  C<< Assert($condition1, $condition2, ... $conditionN); >>
1445    
1446  Return TRUE if all the conditions are true. This method can be used in conjunction with  Return TRUE if all the conditions are true. This method can be used in conjunction with
1447  the OR operator and the L</Confess> method, B<Assert> can function as a debugging assert.  the OR operator and the L</Confess> method as a debugging assert.
1448  So, for example  So, for example
1449    
1450  C<< Assert($recNum >= 0) || Confess("Invalid record number $recNum."); >>  C<< Assert($recNum >= 0) || Confess("Invalid record number $recNum."); >>
# Line 1020  Line 1603 
1603    
1604  C<< if (Tracer::DebugMode) { ...code... } >>  C<< if (Tracer::DebugMode) { ...code... } >>
1605    
1606  Return TRUE if debug mode has been turned on in FIG_Config, else output  Return TRUE if debug mode has been turned on, else output an error
1607  an error page and return FALSE.  page and return FALSE.
1608    
1609  Certain CGI scripts are too dangerous to exist in the production  Certain CGI scripts are too dangerous to exist in the production
1610  environment. This method provides a simple way to prevent them  environment. This method provides a simple way to prevent them
1611  from working unless they are explicitly turned on in the configuration  from working unless they are explicitly turned on by creating a password
1612  file by setting C<$FIG_Config::debug_mode> to 1. If debugging mode  cookie via the B<SetPassword> script.  If debugging mode
1613  is not turned on, an error web page will be output.  is not turned on, an error web page will be output directing the
1614    user to enter in the correct password.
1615    
1616  =cut  =cut
1617    
1618  sub DebugMode {  sub DebugMode {
1619          # Declare the return variable.          # Declare the return variable.
1620          my $retVal;      my $retVal = 0;
1621          # Check the debug configuration.          # Check the debug configuration.
1622          if ($FIG_Config::debug_mode) {      my $password = CGI::cookie("DebugMode");
1623        my $encrypted = Digest::MD5::md5_hex($password);
1624        if ($encrypted eq "252dec43280e0c0d6a75ffcec486e61d") {
1625                  $retVal = 1;                  $retVal = 1;
1626          } else {          } else {
1627                  # Here debug mode is off, so we generate an error page.                  # Here debug mode is off, so we generate an error page.
# Line 1071  Line 1657 
1657  sub Strip {  sub Strip {
1658          # Get a copy of the parameter string.          # Get a copy of the parameter string.
1659          my ($string) = @_;          my ($string) = @_;
1660          my $retVal = $string;      my $retVal = (defined $string ? $string : "");
1661      # Strip the line terminator characters.      # Strip the line terminator characters.
1662      $retVal =~ s/(\r|\n)+$//g;      $retVal =~ s/(\r|\n)+$//g;
1663          # Return the result.          # Return the result.
# Line 1102  Line 1688 
1688    
1689  =item padChar (optional)  =item padChar (optional)
1690    
1691    Character to use for padding. The default is a space.
1692    
1693  =item RETURN  =item RETURN
1694    
1695  Returns a copy of the original string with the spaces added to the specified end so  Returns a copy of the original string with the pad character added to the
1696  that it achieves the desired length.  specified end so that it achieves the desired length.
1697    
1698  =back  =back
1699    
# Line 1137  Line 1725 
1725          return $retVal;          return $retVal;
1726  }  }
1727    
1728    =head3 EOF
1729    
1730    This is a constant that is lexically greater than any useful string.
1731    
1732    =cut
1733    
1734    sub EOF {
1735        return "\xFF\xFF\xFF\xFF\xFF";
1736    }
1737    
1738    =head3 TICK
1739    
1740    C<< my @results = TICK($commandString); >>
1741    
1742    Perform a back-tick operation on a command. If this is a Windows environment, any leading
1743    dot-slash (C<./> will be removed. So, for example, if you were doing
1744    
1745        `./protein.cgi`
1746    
1747    from inside a CGI script, it would work fine in Unix, but would issue an error message
1748    in Windows complaining that C<'.'> is not a valid command. If instead you code
1749    
1750        TICK("./protein.cgi")
1751    
1752    it will work correctly in both environments.
1753    
1754    =over 4
1755    
1756    =item commandString
1757    
1758    The command string to pass to the system.
1759    
1760    =item RETURN
1761    
1762    Returns the standard output from the specified command, as a list.
1763    
1764    =back
1765    
1766    =cut
1767    #: Return Type @;
1768    sub TICK {
1769        # Get the parameters.
1770        my ($commandString) = @_;
1771        # Chop off the dot-slash if this is Windows.
1772        if ($FIG_Config::win_mode) {
1773            $commandString =~ s!^\./!!;
1774        }
1775        # Activate the command and return the result.
1776        return `$commandString`;
1777    }
1778    
1779    =head3 ScriptSetup
1780    
1781    C<< my ($query, $varHash) = ScriptSetup(); >>
1782    
1783    Perform standard tracing and debugging setup for scripts. The value returned is
1784    the CGI object followed by a pre-built variable hash.
1785    
1786    The C<Trace> query parameter is used to determine whether or not tracing is active and
1787    which trace modules (other than C<Tracer> and C<FIG>) should be turned on. Specifying
1788    the C<CGI> trace module will trace parameter and environment information. Parameters are
1789    traced at level 3 and environment variables at level 4. At the end of the script, the
1790    client should call L</ScriptFinish> to output the web page.
1791    
1792    =cut
1793    
1794    sub ScriptSetup {
1795        # Get the CGI query object.
1796        my $query = CGI->new();
1797        # Check for tracing. Set it up if the user asked for it.
1798        if ($query->param('Trace')) {
1799            # Set up tracing to be queued for display at the bottom of the web page.
1800            TSetup($query->param('Trace') . " FIG Tracer", "QUEUE");
1801            # Trace the parameter and environment data.
1802            if (T(CGI => 3)) {
1803                # Here we want to trace the parameter data.
1804                my @names = $query->param;
1805                for my $parmName (sort @names) {
1806                    # Note we skip "Trace", which is for our use only.
1807                    if ($parmName ne 'Trace') {
1808                        my @values = $query->param($parmName);
1809                        Trace("CGI: $parmName = " . join(", ", @values));
1810                    }
1811                }
1812            }
1813            if (T(CGI => 4)) {
1814                # Here we want the environment data too.
1815                for my $envName (sort keys %ENV) {
1816                    Trace("ENV: $envName = $ENV{$envName}");
1817                }
1818            }
1819        } else {
1820            # Here tracing is to be turned off. All we allow is errors traced into the
1821            # error log.
1822            TSetup("0", "WARN");
1823        }
1824        # Create the variable hash.
1825        my $varHash = { DebugData => '' };
1826        # If we're in DEBUG mode, set up the debug mode data for forms.
1827        if (Tracer::DebugMode) {
1828            $varHash->{DebugData} = GetFile("Html/DebugFragment.html");
1829        }
1830        # Return the query object and variable hash.
1831        return ($query, $varHash);
1832    }
1833    
1834    =head3 ScriptFinish
1835    
1836    C<< ScriptFinish($webData, $varHash); >>
1837    
1838    Output a web page at the end of a script. Either the string to be output or the
1839    name of a template file can be specified. If the second parameter is omitted,
1840    it is assumed we have a string to be output; otherwise, it is assumed we have the
1841    name of a template file. The template should have the variable C<DebugData>
1842    specified in any form that invokes a standard script. If debugging mode is turned
1843    on, a form field will be put in that allows the user to enter tracing data.
1844    Trace messages will be placed immediately before the terminal C<BODY> tag in
1845    the output, formatted as a list.
1846    
1847    A typical standard script would loook like the following.
1848    
1849        BEGIN {
1850            # Print the HTML header.
1851            print "CONTENT-TYPE: text/html\n\n";
1852        }
1853        use Tracer;
1854        use CGI;
1855        use FIG;
1856        # ... more uses ...
1857    
1858        my ($query, $varHash) = ScriptSetup();
1859        eval {
1860            # ... get data from $query, put it in $varHash ...
1861        };
1862        if ($@) {
1863            Trace("Script Error: $@") if T(0);
1864        }
1865        ScriptFinish("Html/MyTemplate.html", $varHash);
1866    
1867    The idea here is that even if the script fails, you'll see trace messages and
1868    useful output.
1869    
1870    =over 4
1871    
1872    =item webData
1873    
1874    A string containing either the full web page to be written to the output or the
1875    name of a template file from which the page is to be constructed. If the name
1876    of a template file is specified, then the second parameter must be present;
1877    otherwise, it must be absent.
1878    
1879    =item varHash (optional)
1880    
1881    If specified, then a reference to a hash mapping variable names for a template
1882    to their values. The template file will be read into memory, and variable markers
1883    will be replaced by data in this hash reference.
1884    
1885    =back
1886    
1887    =cut
1888    
1889    sub ScriptFinish {
1890        # Get the parameters.
1891        my ($webData, $varHash) = @_;
1892        # Check for a template file situation.
1893        my $outputString;
1894        if (defined $varHash) {
1895            # Here we have a template file. We need to apply the variables to the template.
1896            $outputString = PageBuilder::Build("<$webData", $varHash, "Html");
1897        } else {
1898            # Here the user gave us a raw string.
1899            $outputString = $webData;
1900        }
1901        # Check for trace messages.
1902        if ($Destination eq "QUEUE") {
1903            # We have trace messages, so we want to put them at the end of the body. This
1904            # is either at the end of the whole string or at the beginning of the BODY
1905            # end-tag.
1906            my $pos = length $outputString;
1907            if ($outputString =~ m#</body>#gi) {
1908                $pos = (pos $outputString) - 7;
1909            }
1910            substr $outputString, $pos, 0, QTrace('Html');
1911        }
1912        # Write the output string.
1913        print $outputString;
1914    }
1915    
1916    =head3 Insure
1917    
1918    C<< Insure($dirName); >>
1919    
1920    Insure a directory is present.
1921    
1922    =over 4
1923    
1924    =item dirName
1925    
1926    Name of the directory to check. If it does not exist, it will be created.
1927    
1928    =back
1929    
1930    =cut
1931    
1932    sub Insure {
1933        my ($dirName) = @_;
1934        if (! -d $dirName) {
1935            Trace("Creating $dirName directory.") if T(2);
1936            eval { mkpath $dirName; };
1937            if ($@) {
1938                Confess("Error creating $dirName: $@");
1939            }
1940        }
1941    }
1942    
1943    =head3 ChDir
1944    
1945    C<< ChDir($dirName); >>
1946    
1947    Change to the specified directory.
1948    
1949    =over 4
1950    
1951    =item dirName
1952    
1953    Name of the directory to which we want to change.
1954    
1955    =back
1956    
1957    =cut
1958    
1959    sub ChDir {
1960        my ($dirName) = @_;
1961        if (! -d $dirName) {
1962            Confess("Cannot change to directory $dirName: no such directory.");
1963        } else {
1964            Trace("Changing to directory $dirName.") if T(4);
1965            my $okFlag = chdir $dirName;
1966            if (! $okFlag) {
1967                Confess("Error switching to directory $dirName.");
1968            }
1969        }
1970    }
1971    
1972  1;  1;

Legend:
Removed from v.1.9  
changed lines
  Added in v.1.43

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3