[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.34, Fri Jan 6 05:34:21 2006 UTC revision 1.35, Wed Jan 11 19:26:45 2006 UTC
# Line 19  Line 19 
19    
20      require Exporter;      require Exporter;
21      @ISA = ('Exporter');      @ISA = ('Exporter');
22      @EXPORT = qw(Trace T TSetup QTrace Confess Cluck Min Max Assert Open OpenDir TICK StandardSetup);      @EXPORT = qw(Trace T TSetup QTrace Confess Cluck Min Max Assert Open OpenDir TICK StandardSetup ScriptSetup ScriptFinish);
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);
# Line 257  Line 257 
257  Standard tracing is output to the standard output and echoed to the file  Standard tracing is output to the standard output and echoed to the file
258  C<trace.log> in the FIG temporary directory.  C<trace.log> in the FIG temporary directory.
259    
260  The default trace level is 3. This dumps out all SQL commands if SQL tracing  The default trace level is 2. To get all messages, specify a trace level of 4.
261  is turned on and tends to produce one flurry of messages per genome. To get all  For a genome-by-genome update, use 3.
 messages, specify a trace level of 4. For generally quiet output, use 2.  
262    
263  The I<options> parameter is a reference to a hash containing the command-line  The I<options> parameter is a reference to a hash containing the command-line
264  options and their default values. Command-line options may be in the form of switches  options and their default values. Command-line options may be in the form of switches
# Line 273  Line 272 
272  the following code.  the following code.
273    
274      my ($options, @parameters) = Tracer::StandardSetup(["DocUtils"],      my ($options, @parameters) = Tracer::StandardSetup(["DocUtils"],
275                                                        { trace => 3, sql => 0,                                                        { safe => 0, noAlias => 0,
                                                         safe => 0, noAlias => 0,  
276                                                          start => ' ', tblFiles => 0},                                                          start => ' ', tblFiles => 0},
277                                                      @ARGV);                                                      @ARGV);
278    
# Line 341  Line 339 
339      # Get the parameters.      # Get the parameters.
340      my ($categories, $options, @argv) = @_;      my ($categories, $options, @argv) = @_;
341      # Add the tracing options.      # Add the tracing options.
342      $options->{trace} = 3;      $options->{trace} = 2;
343      $options->{sql} = 0;      $options->{sql} = 0;
344      # Parse the command line.      # Parse the command line.
345      my ($retOptions, @retParameters) = ParseCommand($options, @argv);      my ($retOptions, @retParameters) = ParseCommand($options, @argv);
# Line 1211  Line 1209 
1209    
1210  C<< my @fileContents = Tracer::GetFile($fileName); >>  C<< my @fileContents = Tracer::GetFile($fileName); >>
1211    
1212  Return the entire contents of a file.      or
1213    
1214    C<< my $fileContents = Tracer::GetFile($fileName); >>
1215    
1216    Return the entire contents of a file. In list context, line-ends are removed and
1217    each line is a list element. In scalar context, line-ends are replaced by C<\n>.
1218    
1219  =over 4  =over 4
1220    
# Line 1670  Line 1673 
1673      return `$commandString`;      return `$commandString`;
1674  }  }
1675    
1676    =head3 ScriptSetup
1677    
1678    C<< my ($query, $varHash) = ScriptSetup(); >>
1679    
1680    Perform standard tracing and debugging setup for scripts. The value returned is
1681    the CGI object followed by a pre-built variable hash.
1682    
1683    The C<Trace> query parameter is used to determine whether or not tracing is active and
1684    which trace modules (other than C<Tracer> and C<FIG>) should be turned on. Specifying
1685    the C<CGI> trace module will trace parameter and environment information. Parameters are
1686    traced at level 3 and environment variables at level 4. At the end of the script, the
1687    client should call L</ScriptFinish> to output the web page.
1688    
1689    =cut
1690    
1691    sub ScriptSetup {
1692        # Get the CGI query object.
1693        my $query = CGI->new();
1694        # Check for tracing. Set it up if the user asked for it.
1695        if ($query->param('Trace')) {
1696            # Set up tracing to be queued for display at the bottom of the web page.
1697            TSetup($query->param('Trace') . " FIG Tracer", "QUEUE");
1698            # Trace the parameter and environment data.
1699            if (T(CGI => 3)) {
1700                # Here we want to trace the parameter data.
1701                my @names = $query->param;
1702                for my $parmName (sort @names) {
1703                    # Note we skip "Trace", which is for our use only.
1704                    if ($parmName ne 'Trace') {
1705                        my @values = $query->param($parmName);
1706                        Trace("CGI: $parmName = " . join(", ", @values));
1707                    }
1708                }
1709            }
1710            if (T(CGI => 4)) {
1711                # Here we want the environment data too.
1712                for my $envName (sort keys %ENV) {
1713                    Trace("ENV: $envName = $ENV{$envName}");
1714                }
1715            }
1716        } else {
1717            # Here tracing is to be turned off. All we allow is errors traced into the
1718            # error log.
1719            TSetup("0", "WARN");
1720        }
1721        # Create the variable hash.
1722        my $varHash = { DebugData => '' };
1723        # If we're in DEBUG mode, set up the debug mode data for forms.
1724        if (Tracer::DebugMode) {
1725            $varHash->{DebugData} = GetFile("Html/DebugFragment.html");
1726        }
1727        # Return the query object and variable hash.
1728        return ($query, $varHash);
1729    }
1730    
1731    =head3 ScriptFinish
1732    
1733    C<< ScriptFinish($webData, $varHash); >>
1734    
1735    Output a web page at the end of a script. Either the string to be output or the
1736    name of a template file can be specified. If the second parameter is omitted,
1737    it is assumed we have a string to be output; otherwise, it is assumed we have the
1738    name of a template file. The template should have the variable C<DebugData>
1739    specified in any form that invokes a standard script. If debugging mode is turned
1740    on, a form field will be put in that allows the user to enter tracing data.
1741    Trace messages will be placed immediately before the terminal C<BODY> tag in
1742    the output, formatted as a list.
1743    
1744    A typical standard script would loook like the following.
1745    
1746        BEGIN {
1747            # Print the HTML header.
1748            print "CONTENT-TYPE: text/html\n\n";
1749        }
1750        use Tracer;
1751        use CGI;
1752        use FIG;
1753        # ... more uses ...
1754    
1755        my ($query, $varHash) = ScriptSetup();
1756        eval {
1757            # ... get data from $query, put it in $varHash ...
1758        };
1759        if ($@) {
1760            Trace("Script Error: $@") if T(0);
1761        }
1762        ScriptFinish("Html/MyTemplate.html", $varHash);
1763    
1764    The idea here is that even if the script fails, you'll see trace messages and
1765    useful output.
1766    
1767    =over 4
1768    
1769    =item webData
1770    
1771    A string containing either the full web page to be written to the output or the
1772    name of a template file from which the page is to be constructed. If the name
1773    of a template file is specified, then the second parameter must be present;
1774    otherwise, it must be absent.
1775    
1776    =item varHash (optional)
1777    
1778    If specified, then a reference to a hash mapping variable names for a template
1779    to their values. The template file will be read into memory, and variable markers
1780    will be replaced by data in this hash reference.
1781    
1782    =cut
1783    
1784    sub ScriptFinish {
1785        # Get the parameters.
1786        my ($webData, $varHash) = @_;
1787        # Check for a template file situation.
1788        my $outputString;
1789        if (defined $varHash) {
1790            # Here we have a template file. We need to apply the variables to the template.
1791            $outputString = PageBuilder::Build("<$webData", $varHash, "Html");
1792        } else {
1793            # Here the user gave us a raw string.
1794            $outputString = $webData;
1795        }
1796        # Check for trace messages.
1797        if ($Destination eq "QUEUE") {
1798            # We have trace messages, so we want to put them at the end of the body. This
1799            # is either at the end of the whole string or at the beginning of the BODY
1800            # end-tag.
1801            my $pos = length $outputString;
1802            if ($outputString =~ m#</body>#gi) {
1803                $pos = (pos $outputString) - 7;
1804            }
1805            substr $outputString, $pos, 0, QTrace('Html');
1806        }
1807        # Write the output string.
1808        print $outputString;
1809    }
1810    
1811  1;  1;

Legend:
Removed from v.1.34  
changed lines
  Added in v.1.35

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3