[Bio] / FigKernelPackages / Tracer.pm Repository:
ViewVC logotype

Annotation of /FigKernelPackages/Tracer.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.132 - (view) (download) (as text)

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