[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.5, Fri Feb 25 18:38:49 2005 UTC revision 1.6, Mon Mar 7 02:01:51 2005 UTC
# Line 2  Line 2 
2    
3          require Exporter;          require Exporter;
4          @ISA = ('Exporter');          @ISA = ('Exporter');
5          @EXPORT = qw(Trace T TSetup QTrace Confess Cluck Min Max);          @EXPORT = qw(Trace T TSetup QTrace Confess Cluck Min Max Assert);
6          @EXPORT_OK = qw(GetFile GetOptions Merge MergeOptions ParseCommand ParseRecord UnEscape);          @EXPORT_OK = qw(GetFile GetOptions Merge MergeOptions ParseCommand ParseRecord UnEscape);
7          use strict;          use strict;
8          use Carp qw(longmess croak);          use Carp qw(longmess croak);
# Line 91  Line 91 
91  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
92  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
93  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. In addition to sending
94  the trace messages to a file, you can specify a special destination. C<HTML> will  the trace messages to a file, you can specify a special destination. C<HTML> will cause
95  cause tracing to the standard output with each line formatted as an HTML paragraph. C<TEXT>  tracing to the standard output with each line formatted as an HTML paragraph. C<TEXT>
96  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
97  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
98  messages to be stored in a queue for later retrieval by the L</QTrace> method. C<WARN> will cause  messages to be stored in a queue for later retrieval by the L</QTrace> method. C<WARN> will
99  trace messages to be emitted as warnings using the B<warn> directive.  C<NONE> will cause  cause trace messages to be emitted as warnings using the B<warn> directive.  C<NONE> will
100  tracing to be suppressed.  cause tracing to be suppressed.
101    
102  =back  =back
103    
# Line 127  Line 127 
127          }          }
128  }  }
129    
130    =head3 SetLevel
131    
132    C<< Tracer::SetLevel($newLevel); >>
133    
134    Modify the trace level. A higher trace level will cause more messages to appear.
135    
136    =over 4
137    
138    =item newLevel
139    
140    Proposed new trace level.
141    
142    =back
143    
144    =cut
145    
146    sub SetLevel {
147        $TraceLevel = $_[0];
148    }
149    
150  =head3 Now  =head3 Now
151    
152  C<< my $string = Tracer::Now(); >>  C<< my $string = Tracer::Now(); >>
# Line 655  Line 675 
675    
676  =head3 GetFile  =head3 GetFile
677    
678  C<< my $fileContents = Tracer::GetFile($fileName); >>  C<< my @fileContents = Tracer::GetFile($fileName); >>
679    
680  Return the entire contents of a file.  Return the entire contents of a file.
681    
# Line 667  Line 687 
687    
688  =item RETURN  =item RETURN
689    
690  Returns the entire file as a single string. If an error occurs, will return  In a list context, returns the entire file as a list with the line terminators removed.
691  an empty string.  In a scalar context, returns the entire file as a string.
692    
693  =back  =back
694    
# Line 678  Line 698 
698          # Get the parameters.          # Get the parameters.
699          my ($fileName) = @_;          my ($fileName) = @_;
700          # Declare the return variable.          # Declare the return variable.
701          my $retVal = "";          my @retVal = ();
702          # Open the file for input.          # Open the file for input.
703          my $ok = open INPUTFILE, "<$fileName";          my $ok = open INPUTFILE, "<$fileName";
704          if (!$ok) {          if (!$ok) {
705                  # If we had an error, trace it. We will automatically return a null string.                  # If we had an error, trace it. We will automatically return a null value.
706                  Trace(0, "Could not open \"$fileName\" for input.");                  Trace("Could not open \"$fileName\" for input.") if T(0);
707          } else {          } else {
708                  # Read the whole file into the return variable.                  # Read the whole file into the return variable, stripping off an terminator
709                  while (<INPUTFILE>) {          # characters.
710                          $retVal .= $_;          my $lineCount = 0;
711                    while (my $line = <INPUTFILE>) {
712                $lineCount++;
713                $line =~ s/(\r|\n)+$//g;
714                            push @retVal, $line;
715                  }                  }
716                  # Close it.                  # Close it.
717                  close INPUTFILE;                  close INPUTFILE;
718            my $actualLines = @retVal;
719            Trace("$lineCount lines read from $fileName. $actualLines processed.") if T(0);
720            }
721            # Return the file's contents in the desired format.
722        if (wantarray) {
723                return @retVal;
724        } else {
725            return join "\n", @retVal;
726          }          }
         # Return the file's contents.  
         return $retVal;  
727  }  }
728    
729  =head3 QTrace  =head3 QTrace
# Line 742  Line 772 
772    
773  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. The stack
774  trace will only appear if the trace level for this package is 1 or more. When used with  trace will only appear if the trace level for this package is 1 or more. When used with
775  the OR operator, this method can function as a debugging assert. So, for example  the OR operator and the L</Assert> method, B<Confess> can function as a debugging assert.
776    So, for example
777    
778  C<< ($recNum >= 0) || Confess("Invalid record number $recNum."); >>  C<< Assert($recNum >= 0) || Confess("Invalid record number $recNum."); >>
779    
780  Will abort the program with a stack trace if the value of C<$recNum> is negative.  Will abort the program with a stack trace if the value of C<$recNum> is negative.
781    
# Line 767  Line 798 
798          croak(">>> $message");          croak(">>> $message");
799  }  }
800    
801    =head3 Assert
802    
803    C<< Assert($condition1, $condition2, ... $conditionN); >>
804    
805    Return TRUE if all the conditions are true. This method can be used in conjunction with
806    the OR operator and the L</Confess> method, B<Assert> can function as a debugging assert.
807    So, for example
808    
809    C<< Assert($recNum >= 0) || Confess("Invalid record number $recNum."); >>
810    
811    Will abort the program with a stack trace if the value of C<$recNum> is negative.
812    
813    =cut
814    sub Assert {
815        my $retVal = 1;
816        LOOP: for my $condition (@_) {
817            if (! $condition) {
818                $retVal = 0;
819                last LOOP;
820            }
821        }
822        return $retVal;
823    }
824    
825  =head3 Cluck  =head3 Cluck
826    
827  C<< Cluck($message); >>  C<< Cluck($message); >>

Legend:
Removed from v.1.5  
changed lines
  Added in v.1.6

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3