[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.60, Thu Jul 20 03:41:00 2006 UTC revision 1.67, Fri Sep 29 15:00:17 2006 UTC
# Line 32  Line 32 
32      use File::Path;      use File::Path;
33      use File::stat;      use File::stat;
34      use LWP::UserAgent;      use LWP::UserAgent;
35        use Time::HiRes 'gettimeofday';
36        use URI::Escape;
37    
38  =head1 Tracing and Debugging Helpers  =head1 Tracing and Debugging Helpers
39    
# Line 1455  Line 1457 
1457  =item lines  =item lines
1458    
1459  Reference to a list of text lines. The lines will be written to the file in order, with trailing  Reference to a list of text lines. The lines will be written to the file in order, with trailing
1460  new-line characters.  new-line characters. Alternatively, may be a string, in which case the string will be written without
1461    modification.
1462    
1463  =back  =back
1464    
# Line 1466  Line 1469 
1469      my ($fileName, $lines) = @_;      my ($fileName, $lines) = @_;
1470      # Open the output file.      # Open the output file.
1471      my $handle = Open(undef, ">$fileName");      my $handle = Open(undef, ">$fileName");
1472      # Write the lines.      if (ref $lines ne 'ARRAY') {
1473            # Here we have a scalar, so we write it raw.
1474            print $handle $lines;
1475        } else {
1476            # Write the lines one at a time.
1477      for my $line (@{$lines}) {      for my $line (@{$lines}) {
1478          print $handle "$line\n";          print $handle "$line\n";
1479      }      }
1480        }
1481      # Close the output file.      # Close the output file.
1482      close $handle;      close $handle;
1483  }  }
# Line 1734  Line 1742 
1742          $retVal = 1;          $retVal = 1;
1743      } else {      } else {
1744          # Here debug mode is off, so we generate an error page.          # Here debug mode is off, so we generate an error page.
1745          my $pageString = PageBuilder::Build("<Html/ErrorPage.html", {}, "Html");          my $pageString = PageBuilder::Build("<<Html/ErrorPage.html", {}, "Html");
1746          print $pageString;          print $pageString;
1747      }      }
1748      # Return the determination indicator.      # Return the determination indicator.
# Line 1905  Line 1913 
1913      my $query = CGI->new();      my $query = CGI->new();
1914      # Check for tracing. Set it up if the user asked for it.      # Check for tracing. Set it up if the user asked for it.
1915      if ($query->param('Trace')) {      if ($query->param('Trace')) {
1916          # Set up tracing to be queued for display at the bottom of the web page.          # Set up tracing.
1917          TSetup($query->param('Trace') . " FIG Tracer", "QUEUE");          my $ttype = ($query->param('TF') ? ">$FIG_Config::temp/Trace$$.log" : "QUEUE");
1918            TSetup($query->param('Trace') . " FIG Tracer", $ttype);
1919          # Trace the parameter and environment data.          # Trace the parameter and environment data.
1920            TraceParms($query);
1921        } else {
1922            # Here tracing is to be turned off. All we allow is errors traced into the
1923            # error log.
1924            TSetup("0", "WARN");
1925        }
1926        # Create the variable hash.
1927        my $varHash = { DebugData => '' };
1928        # Return the query object and variable hash.
1929        return ($query, $varHash);
1930    }
1931    
1932    =head3 TraceParms
1933    
1934    C<< Tracer::TraceParms($query); >>
1935    
1936    Trace the CGI parameters at trace level CGI => 3 and the environment variables
1937    at level CGI => 4.
1938    
1939    =over 4
1940    
1941    =item query
1942    
1943    CGI query object containing the parameters to trace.
1944    
1945    =back
1946    
1947    =cut
1948    
1949    sub TraceParms {
1950        # Get the parameters.
1951        my ($query) = @_;
1952          if (T(CGI => 3)) {          if (T(CGI => 3)) {
1953              # Here we want to trace the parameter data.              # Here we want to trace the parameter data.
1954              my @names = $query->param;              my @names = $query->param;
# Line 1918  Line 1959 
1959                      Trace("CGI: $parmName = " . join(", ", @values));                      Trace("CGI: $parmName = " . join(", ", @values));
1960                  }                  }
1961              }              }
1962            # Now output a GET-style URL for this query.
1963            my $getURL = $query->url(-relative => 1, -query => 1);
1964            # Strip out the Trace parameters.
1965            $getURL =~ s/Trace=\d[^;&]+[;&]//;
1966            $getURL =~ s/TF=\d[;&]//;
1967            # Output the URL.
1968            Trace("URL: ../FIG/$getURL");
1969            # Display the request method.
1970            my $method = $query->request_method();
1971            Trace("Method: $method");
1972          }          }
1973          if (T(CGI => 4)) {          if (T(CGI => 4)) {
1974              # Here we want the environment data too.              # Here we want the environment data too.
# Line 1925  Line 1976 
1976                  Trace("ENV: $envName = $ENV{$envName}");                  Trace("ENV: $envName = $ENV{$envName}");
1977              }              }
1978          }          }
     } else {  
         # Here tracing is to be turned off. All we allow is errors traced into the  
         # error log.  
         TSetup("0", "WARN");  
     }  
     # Create the variable hash.  
     my $varHash = { DebugData => '' };  
     # If we're in DEBUG mode, set up the debug mode data for forms.  
     if (Tracer::DebugMode) {  
         $varHash->{DebugData} = GetFile("Html/DebugFragment.html");  
     }  
     # Return the query object and variable hash.  
     return ($query, $varHash);  
1979  }  }
1980    
1981  =head3 ScriptFinish  =head3 ScriptFinish
# Line 2001  Line 2039 
2039      # Check for a template file situation.      # Check for a template file situation.
2040      my $outputString;      my $outputString;
2041      if (defined $varHash) {      if (defined $varHash) {
2042          # Here we have a template file. We need to apply the variables to the template.          # Here we have a template file. We need to determine the template type.
2043          $outputString = PageBuilder::Build("<$webData", $varHash, "Html");          my $template;
2044            if ($FIG_Config::template_url && $webData =~ /\.php$/) {
2045                $template = "$FIG_Config::template_url/$webData";
2046            } else {
2047                $template = "<<$webData";
2048            }
2049            $outputString = PageBuilder::Build($template, $varHash, "Html");
2050      } else {      } else {
2051          # Here the user gave us a raw string.          # Here the user gave us a raw string.
2052          $outputString = $webData;          $outputString = $webData;
# Line 2324  Line 2368 
2368      }      }
2369  }  }
2370    
2371    =head3 CompareLists
2372    
2373    C<< my ($inserted, $deleted) = Tracer::CompareLists(\@newList, \@oldList, $keyIndex); >>
2374    
2375    Compare two lists of tuples, and return a hash analyzing the differences. The lists
2376    are presumed to be sorted alphabetically by the value in the $keyIndex column.
2377    The return value contains a list of items that are only in the new list
2378    (inserted) and only in the old list (deleted).
2379    
2380    =over 4
2381    
2382    =item newList
2383    
2384    Reference to a list of new tuples.
2385    
2386    =item oldList
2387    
2388    Reference to a list of old tuples.
2389    
2390    =item keyIndex (optional)
2391    
2392    Index into each tuple of its key field. The default is 0.
2393    
2394    =item RETURN
2395    
2396    Returns a 2-tuple consisting of a reference to the list of items that are only in the new
2397    list (inserted) followed by a reference to the list of items that are only in the old
2398    list (deleted).
2399    
2400    =back
2401    
2402    =cut
2403    
2404    sub CompareLists {
2405        # Get the parameters.
2406        my ($newList, $oldList, $keyIndex) = @_;
2407        if (! defined $keyIndex) {
2408            $keyIndex = 0;
2409        }
2410        # Declare the return variables.
2411        my ($inserted, $deleted) = ([], []);
2412        # Loop through the two lists simultaneously.
2413        my ($newI, $oldI) = (0, 0);
2414        my ($newN, $oldN) = (scalar @{$newList}, scalar @{$oldList});
2415        while ($newI < $newN || $oldI < $oldN) {
2416            # Get the current object in each list. Note that if one
2417            # of the lists is past the end, we'll get undef.
2418            my $newItem = $newList->[$newI];
2419            my $oldItem = $oldList->[$oldI];
2420            if (! defined($newItem) || defined($oldItem) && $newItem->[$keyIndex] gt $oldItem->[$keyIndex]) {
2421                # The old item is not in the new list, so mark it deleted.
2422                push @{$deleted}, $oldItem;
2423                $oldI++;
2424            } elsif (! defined($oldItem) || $oldItem->[$keyIndex] gt $newItem->[$keyIndex]) {
2425                # The new item is not in the old list, so mark it inserted.
2426                push @{$inserted}, $newItem;
2427                $newI++;
2428            } else {
2429                # The item is in both lists, so push forward.
2430                $oldI++;
2431                $newI++;
2432            }
2433        }
2434        # Return the result.
2435        return ($inserted, $deleted);
2436    }
2437    
2438    =head3 GetLine
2439    
2440    C<< my @data = Tracer::GetLine($handle); >>
2441    
2442    Read a line of data from a tab-delimited file.
2443    
2444    =over 4
2445    
2446    =item handle
2447    
2448    Open file handle from which to read.
2449    
2450    =item RETURN
2451    
2452    Returns a list of the fields in the record read. The fields are presumed to be
2453    tab-delimited. If we are at the end of the file, then an empty list will be
2454    returned. If an empty line is read, a single list item consisting of a null
2455    string will be returned.
2456    
2457    =back
2458    
2459    =cut
2460    
2461    sub GetLine {
2462        # Get the parameters.
2463        my ($handle) = @_;
2464        # Declare the return variable.
2465        my @retVal = ();
2466        # Read from the file.
2467        my $line = <$handle>;
2468        # Only proceed if we found something.
2469        if (defined $line) {
2470            # Remove the new-line.
2471            chomp $line;
2472            # If the line is empty, return a single empty string; otherwise, parse
2473            # it into fields.
2474            if ($line eq "") {
2475                push @retVal, "";
2476            } else {
2477                push @retVal, split /\t/,$line;
2478            }
2479        }
2480        # Return the result.
2481        return @retVal;
2482    }
2483    
2484    =head3 PutLine
2485    
2486    C<< Tracer::PutLine($handle, \@fields); >>
2487    
2488    Write a line of data to a tab-delimited file. The specified field values will be
2489    output in tab-separated form, with a trailing new-line.
2490    
2491    =over 4
2492    
2493    =item handle
2494    
2495    Output file handle.
2496    
2497    =item fields
2498    
2499    List of field values.
2500    
2501    =back
2502    
2503    =cut
2504    
2505    sub PutLine {
2506        # Get the parameters.
2507        my ($handle, $fields) = @_;
2508        # Write the data.
2509        print $handle join("\t", @{$fields}) . "\n";
2510    }
2511    
2512    =head3 GenerateURL
2513    
2514    C<< my $queryUrl = Tracer::GenerateURL($page, %parameters); >>
2515    
2516    Generate a GET-style URL for the specified page with the specified parameter
2517    names and values. The values will be URL-escaped automatically. So, for
2518    example
2519    
2520        Tracer::GenerateURL("form.cgi", type => 1, string => "\"high pass\" or highway")
2521    
2522    would return
2523    
2524        form.cgi?type=1&string=%22high%20pass%22%20or%20highway
2525    
2526    =over 4
2527    
2528    =item page
2529    
2530    Page URL.
2531    
2532    =item parameters
2533    
2534    Hash mapping parameter names to parameter values.
2535    
2536    =item RETURN
2537    
2538    Returns a GET-style URL that goes to the specified page and passes in the
2539    specified parameters and values.
2540    
2541    =back
2542    
2543    =cut
2544    
2545    sub GenerateURL {
2546        # Get the parameters.
2547        my ($page, %parameters) = @_;
2548        # Prime the return variable with the page URL.
2549        my $retVal = $page;
2550        # Loop through the parameters, creating parameter elements in a list.
2551        my @parmList = map { "$_=" . uri_escape($parameters{$_}) } keys %parameters;
2552        # If the list is nonempty, tack it on.
2553        if (@parmList) {
2554            $retVal .= "?" . join("&", @parmList);
2555        }
2556        # Return the result.
2557        return $retVal;
2558    }
2559    
2560  1;  1;

Legend:
Removed from v.1.60  
changed lines
  Added in v.1.67

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3