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

Annotation of /FigKernelPackages/Tracer.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.96 - (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.12 require Exporter;
22 :     @ISA = ('Exporter');
23 : parrello 1.72 @EXPORT = qw(Trace T TSetup QTrace Confess Cluck Min Max Assert Open OpenDir TICK StandardSetup EmergencyKey ETracing ScriptSetup ScriptFinish Insure ChDir Emergency);
24 : parrello 1.12 @EXPORT_OK = qw(GetFile GetOptions Merge MergeOptions ParseCommand ParseRecord UnEscape Escape);
25 :     use strict;
26 : gdpusch 1.96 use Carp qw(longmess croak carp);
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 : olson 1.1
40 :     =head1 Tracing and Debugging Helpers
41 :    
42 : parrello 1.72 =head2 Tracing
43 : olson 1.1
44 :     This package provides simple tracing for debugging and reporting purposes. To use it simply call the
45 : parrello 1.72 L</TSetup> or L</ETracing> method to set the options and call L</Trace> to write out trace messages.
46 :     L</TSetup> and L</ETracing> both establish a I<trace level> and a list of I<categories>. Similarly,
47 :     each trace message has a I<trace level> and I<category> associated with it. Only messages whose trace
48 :     level is less than or equal to the setup trace level and whose category is activated will
49 : parrello 1.2 be written. Thus, a higher trace level on a message indicates that the message
50 : parrello 1.72 is less likely to be seen, while a higher trace level passed to B<TSetup> means more trace messages will
51 :     appear.
52 : olson 1.1
53 : parrello 1.72 =head3 Putting Trace Messages in Your Code
54 :    
55 :     To generate a trace message, use the following syntax.
56 :    
57 :     Trace($message) if T(errors => 4);
58 : olson 1.1
59 : parrello 1.2 This statement will produce a trace message if the trace level is 4 or more and the C<errors>
60 : parrello 1.72 category is active. There is a special category C<main> that is always active, so
61 : olson 1.1
62 : parrello 1.72 Trace($message) if T(main => 4);
63 : olson 1.1
64 :     will trace if the trace level is 4 or more.
65 :    
66 :     If the category name is the same as the package name, all you need is the number. So, if the
67 :     following call is made in the B<Sprout> package, it will appear if the C<Sprout> category is
68 :     active and the trace level is 2 or more.
69 :    
70 : parrello 1.72 Trace($message) if T(2);
71 :    
72 :     In scripts, where no package name is available, the category defaults to C<main>.
73 :    
74 :     =head3 Custom Tracing
75 :    
76 :     Many programs have customized tracing configured using the L</TSetup> method. This is no longer
77 :     the preferred method, but a knowledge of how custom tracing works can make the more modern
78 :     L</Emergency Tracing> easier to understand.
79 : olson 1.1
80 : parrello 1.72 To set up custom tracing, you call the L</TSetup> method. The method takes as input a trace level,
81 :     a list of category names, and a destination. The trace level and list of category names are
82 : olson 1.1 specified as a space-delimited string. Thus
83 :    
84 : parrello 1.72 TSetup('3 errors Sprout ERDB', 'TEXT');
85 : olson 1.1
86 : parrello 1.7 sets the trace level to 3, activates the C<errors>, C<Sprout>, and C<ERDB> categories, and
87 : parrello 1.72 specifies that messages should be sent to the standard output.
88 : parrello 1.12
89 :     To turn on tracing for ALL categories, use an asterisk. The call below sets every category to
90 :     level 3 and writes the output to the standard error output. This sort of thing might be
91 :     useful in a CGI environment.
92 :    
93 : parrello 1.72 TSetup('3 *', 'WARN');
94 : olson 1.1
95 : parrello 1.72 In addition standard error and file output for trace messages, you can specify that the trace messages
96 : olson 1.1 be queued. The messages can then be retrieved by calling the L</QTrace> method. This approach
97 :     is useful if you are building a web page. Instead of having the trace messages interspersed with
98 :     the page output, they can be gathered together and displayed at the end of the page. This makes
99 :     it easier to debug page formatting problems.
100 :    
101 : parrello 1.72 Finally, you can specify that all trace messages be emitted to a file, or the standard output and
102 :     a file at the same time. To trace to a file, specify the filename with an output character in front
103 :     of it.
104 :    
105 :     TSetup('4 SQL', ">$fileName");
106 :    
107 :     To trace to the standard output and a file at the same time, put a C<+> in front of the angle
108 :     bracket.
109 :    
110 :     TSetup('3 *', "+>$fileName");
111 : parrello 1.4
112 : olson 1.1 The flexibility of tracing makes it superior to simple use of directives like C<die> and C<warn>.
113 :     Tracer calls can be left in the code with minimal overhead and then turned on only when needed.
114 :     Thus, debugging information is available and easily retrieved even when the application is
115 :     being used out in the field.
116 :    
117 : parrello 1.72 =head3 Trace Levels
118 :    
119 : parrello 1.10 There is no hard and fast rule on how to use trace levels. The following is therefore only
120 :     a suggestion.
121 :    
122 :     =over 4
123 :    
124 : parrello 1.32 =item Error 0
125 : parrello 1.10
126 :     Message indicates an error that may lead to incorrect results or that has stopped the
127 :     application entirely.
128 :    
129 : parrello 1.32 =item Warning 1
130 : parrello 1.10
131 :     Message indicates something that is unexpected but that probably did not interfere
132 :     with program execution.
133 :    
134 : parrello 1.32 =item Notice 2
135 : parrello 1.10
136 :     Message indicates the beginning or end of a major task.
137 :    
138 : parrello 1.32 =item Information 3
139 : parrello 1.10
140 :     Message indicates a subtask. In the FIG system, a subtask generally relates to a single
141 :     genome. This would be a big loop that is not expected to execute more than 500 times or so.
142 :    
143 : parrello 1.32 =item Detail 4
144 : parrello 1.10
145 :     Message indicates a low-level loop iteration.
146 :    
147 :     =back
148 :    
149 : parrello 1.69 The format of trace messages is important because some utilities analyze trace files.
150 : parrello 1.72 There are three fields-- the time stamp, the category name, and the text.
151 :     The time stamp is between square brackets and the category name between angle brackets.
152 :     After the category name there is a colon (C<:>) followed by the message text.
153 :     If the square brackets or angle brackets are missing, then the trace management
154 :     utilities assume that they are encountering a set of pre-formatted lines.
155 :    
156 :     Note, however, that this formatting is done automatically by the tracing functions. You
157 :     only need to know about it if you want to parse a trace file.
158 :    
159 :     =head3 Emergency Tracing
160 :    
161 :     Sometimes, you need a way for tracing to happen automatically without putting parameters
162 :     in a form or on the command line. Emergency tracing does this. You invoke emergency tracing
163 :     from the debug form, which is accessed from I<MySeedInstance>C</FIG/Html/SetPassword.html>.
164 :     Emergency tracing requires you specify a tracing key. For command-line tools, the key is
165 :     taken from the C<TRACING> environment variable. For web services, the key is taken from
166 :     a cookie. Either way, the key tells the tracing facility who you are, so that you control
167 :     the tracing in your environment without stepping on other users.
168 :    
169 :     The key can be anything you want. If you don't have a key, the C<SetPassword> page will
170 :     generate one for you.
171 :    
172 :     You can activate and de-activate emergency tracing from the debugging control panel, as
173 :     well as display the trace file itself.
174 :    
175 :     To enable emergency tracing in your code, call
176 :    
177 :     ETracing($cgi)
178 :    
179 :     from a web script and
180 :    
181 :     ETracing()
182 :    
183 :     from a command-line script.
184 :    
185 :     The web script will look for the tracing key in the cookies, and the command-line
186 :     script will look for it in the C<TRACING> environment variable. If you are
187 :     using the L</StandardScript> or L</StandardSetup> methods, emergency tracing
188 :     will be configured automatically.
189 :    
190 : parrello 1.84 NOTE: to configure emergency tracing from the command line instead of the Debugging
191 :     Control Panel (see below), use the C<trace.pl> script.
192 :    
193 : parrello 1.72 =head3 Debugging Control Panel
194 :    
195 :     The debugging control panel provides several tools to assist in development of
196 :     SEED and Sprout software. You access the debugging control panel from the URL
197 :     C</FIG/Html/SetPassword.html> in whichever seed instance you're using. (So,
198 :     for example, the panel access point for the development NMPDR system is
199 :     C<http://web-1.nmpdr.org/next/FIG/Html/SetPassword.html>. Contact Bruce to
200 :     find out what the password is. From this page, you can also specify a tracing
201 :     key. If you don't specify a key, one will be generated for you.
202 :    
203 :     =head4 Emergency Tracing Form
204 :    
205 :     At the bottom of the debugging control panel is a form that allows you to
206 :     specify a trace level and tracing categories. Special and common categories
207 :     are listed with check boxes. You can hold your mouse over a check box to see
208 :     what its category does. In general, however, a category name is the same as
209 :     the name of the package in which the trace message occurs.
210 :    
211 :     Additional categories can be entered in an input box, delimited by spaces or commas.
212 :    
213 :     The B<Activate> button turns on Emergency tracing at the level you specify with the
214 :     specified categories active. The B<Terminate> button turns tracing off. The
215 :     B<Show File> button displays the current contents of the trace file. The tracing
216 :     form at the bottom of the control panel is designed for emergency tracing, so it
217 :     will only affect programs that call L</ETracing>, L</StandardScript>,
218 :     or L</StandardSetup>.
219 :    
220 :     =head4 Script Form
221 :    
222 :     The top form of the debugging control panel allows you to enter a tiny script and
223 :     have the output generated in a formatted table. Certain object variables are
224 :     predefined in the script, including a FIG object (C<$fig>), a CGI object (C<$cgi>),
225 :     and-- if Sprout is active-- Sprout (C<$sprout>) and SFXlate (C<$sfx>) objects.
226 :    
227 :     The last line of the script must be a scalar, but it can be a reference to a hash,
228 :     a list, a list of lists, and various other combinations. If you select the appropriate
229 :     data type in the dropdown box, the output will be formatted accordingly. The form
230 :     also has controls for specifying tracing. These controls override any emergency
231 :     tracing in effect.
232 :    
233 :     =head4 Database Query Forms
234 :    
235 :     The forms between the script form and the emergency tracing form allow you to
236 :     make queries against the database. The FIG query form allows simple queries against
237 :     a single FIG table. The Sprout query form uses the B<GetAll> method to do a
238 :     multi-table query against the Sprout database. B<GetAll> is located in the B<ERDB>
239 :     package, and it takes five parameters.
240 :    
241 :     GetAll(\@objectNames, $filterClause, \@parameters, \@fields, $count);
242 :    
243 :     Each of the five parameters corresponds to a text box on the query form:
244 :    
245 :     =over 4
246 :    
247 :     =item Objects
248 :    
249 :     Comma-separated list containing the names of the entity and relationship objects to be retrieved.
250 :    
251 :     =item Filter
252 :    
253 :     WHERE/ORDER BY clause (without the WHERE) to be used to filter and sort the query. The WHERE clause can
254 :     be parameterized with parameter markers (C<?>). Each field used must be specified in the standard form
255 :     B<I<objectName>(I<fieldName>)> or B<$I<number>(I<fieldName>)> where I<fieldName> is the name of a
256 :     field, I<objectName> is the name of the entity or relationship object containing the field, and
257 :     I<number> is the 1-based position of the object in the object list. Any parameters
258 :     specified in the filter clause should be specified in the B<Params> field.
259 :     The fields in a filter clause can come from primary entity relations,
260 :     relationship relations, or secondary entity relations; however, all of the
261 :     entities and relationships involved must be included in the list of object names.
262 :    
263 :     =item Params
264 :    
265 :     List of the parameters to be substituted in for the parameters marks in the filter clause. This
266 :     is a comma-separated list without any quoting or escaping.
267 :    
268 :     =item fields
269 :    
270 :     Comma-separated list of the fields to be returned in each element of the list returned. Fields
271 :     are specified in the same manner as in the filter clause.
272 :    
273 :     =item count
274 :    
275 :     Maximum number of records to return. If omitted or 0, all available records will be returned.
276 :    
277 :     =back
278 :    
279 :     B<GetAll> automatically joins together the entities and relationships listed in the object
280 :     names. This simplifies the coding of the filter clause, but it means that some queries are
281 :     not possible, since they cannot be expressed in a linear sequence of joins. This is a limitation
282 :     that has yet to be addressed.
283 : parrello 1.69
284 : olson 1.1 =cut
285 : parrello 1.2
286 : olson 1.1 # Declare the configuration variables.
287 :    
288 : parrello 1.94 my $Destination = "WARN"; # Description of where to send the trace output.
289 : parrello 1.12 my $TeeFlag = 0; # TRUE if output is going to a file and to the
290 :     # standard output
291 : parrello 1.3 my %Categories = ( main => 1 );
292 : parrello 1.12 # hash of active category names
293 :     my $TraceLevel = 0; # trace level; a higher trace level produces more
294 :     # messages
295 :     my @Queue = (); # queued list of trace messages.
296 : parrello 1.7 my $LastCategory = "main"; # name of the last category interrogated
297 : parrello 1.11 my $SetupCount = 0; # number of times TSetup called
298 : parrello 1.12 my $AllTrace = 0; # TRUE if we are tracing all categories.
299 : olson 1.1
300 : parrello 1.93 =head2 Tracing Methods
301 :    
302 :     =head3 Setups
303 :    
304 :     my $count = Tracer::Setups();
305 :    
306 :     Return the number of times L</TSetup> has been called.
307 :    
308 :     This method allows for the creation of conditional tracing setups where, for example, we
309 :     may want to set up tracing if nobody else has done it before us.
310 :    
311 :     =cut
312 :    
313 :     sub Setups {
314 :     return $SetupCount;
315 :     }
316 : olson 1.1
317 :     =head3 TSetup
318 :    
319 : parrello 1.92 TSetup($categoryList, $target);
320 : olson 1.1
321 :     This method is used to specify the trace options. The options are stored as package data
322 :     and interrogated by the L</Trace> and L</T> methods.
323 :    
324 :     =over 4
325 :    
326 :     =item categoryList
327 :    
328 :     A string specifying the trace level and the categories to be traced, separated by spaces.
329 :     The trace level must come first.
330 :    
331 :     =item target
332 :    
333 :     The destination for the trace output. To send the trace output to a file, specify the file
334 :     name preceded by a ">" symbol. If a double symbol is used (">>"), then the data is appended
335 : parrello 1.10 to the file. Otherwise the file is cleared before tracing begins. Precede the first ">"
336 :     symbol with a C<+> to echo output to a file AND to the standard output. In addition to
337 :     sending the trace messages to a file, you can specify a special destination. C<HTML> will
338 :     cause tracing to the standard output with each line formatted as an HTML paragraph. C<TEXT>
339 : parrello 1.5 will cause tracing to the standard output as ordinary text. C<ERROR> will cause trace
340 : parrello 1.9 messages to be sent to the standard error output as ordinary text. C<QUEUE> will cause trace
341 : parrello 1.6 messages to be stored in a queue for later retrieval by the L</QTrace> method. C<WARN> will
342 : parrello 1.9 cause trace messages to be emitted as warnings using the B<warn> directive. C<NONE> will
343 : parrello 1.6 cause tracing to be suppressed.
344 : olson 1.1
345 :     =back
346 :    
347 :     =cut
348 :    
349 :     sub TSetup {
350 : parrello 1.12 # Get the parameters.
351 :     my ($categoryList, $target) = @_;
352 :     # Parse the category list.
353 :     my @categoryData = split /\s+/, $categoryList;
354 :     # Extract the trace level.
355 :     $TraceLevel = shift @categoryData;
356 :     # Presume category-based tracing until we learn otherwise.
357 :     $AllTrace = 0;
358 :     # Build the category hash. Note that if we find a "*", we turn on non-category
359 : parrello 1.33 # tracing. We must also clear away any pre-existing data.
360 : parrello 1.34 %Categories = ( main => 1 );
361 : parrello 1.12 for my $category (@categoryData) {
362 :     if ($category eq '*') {
363 :     $AllTrace = 1;
364 :     } else {
365 : parrello 1.13 $Categories{lc $category} = 1;
366 : parrello 1.12 }
367 :     }
368 :     # Now we need to process the destination information. The most important special
369 :     # cases are the single ">", which requires we clear the file first, and the
370 :     # "+" prefix which indicates a double echo.
371 :     if ($target =~ m/^\+?>>?/) {
372 :     if ($target =~ m/^\+/) {
373 :     $TeeFlag = 1;
374 :     $target = substr($target, 1);
375 :     }
376 :     if ($target =~ m/^>[^>]/) {
377 :     open TRACEFILE, $target;
378 : parrello 1.69 print TRACEFILE "[" . Now() . "] <Tracer>: Tracing initialized.\n";
379 : parrello 1.12 close TRACEFILE;
380 :     $Destination = ">$target";
381 :     } else {
382 :     $Destination = $target;
383 :     }
384 :     } else {
385 :     $Destination = uc($target);
386 :     }
387 :     # Increment the setup counter.
388 :     $SetupCount++;
389 : parrello 1.11 }
390 :    
391 : parrello 1.93 =head3 SetLevel
392 : parrello 1.31
393 : parrello 1.93 Tracer::SetLevel($newLevel);
394 : parrello 1.31
395 : parrello 1.93 Modify the trace level. A higher trace level will cause more messages to appear.
396 : parrello 1.31
397 : parrello 1.93 =over 4
398 : parrello 1.31
399 : parrello 1.93 =item newLevel
400 : parrello 1.31
401 : parrello 1.93 Proposed new trace level.
402 : parrello 1.31
403 : parrello 1.93 =back
404 : parrello 1.31
405 : parrello 1.93 =cut
406 : parrello 1.31
407 : parrello 1.93 sub SetLevel {
408 :     $TraceLevel = $_[0];
409 :     }
410 : parrello 1.31
411 : parrello 1.93 =head3 ParseTraceDate
412 : parrello 1.31
413 : parrello 1.93 my $time = Tracer::ParseTraceDate($dateString);
414 : parrello 1.31
415 : parrello 1.93 Convert a date from the trace file into a PERL timestamp.
416 : parrello 1.31
417 : parrello 1.93 =over 4
418 : parrello 1.31
419 : parrello 1.93 =item dateString
420 : parrello 1.31
421 : parrello 1.93 The date string from the trace file. The format of the string is determined by the
422 :     L</Now> method.
423 : parrello 1.31
424 : parrello 1.93 =item RETURN
425 : parrello 1.31
426 : parrello 1.93 Returns a PERL time, that is, a number of seconds since the epoch, or C<undef> if
427 :     the time string is invalid.
428 : parrello 1.31
429 : parrello 1.93 =back
430 : parrello 1.38
431 : parrello 1.93 =cut
432 : parrello 1.38
433 : parrello 1.93 sub ParseTraceDate {
434 :     # Get the parameters.
435 :     my ($dateString) = @_;
436 :     # Declare the return variable.
437 :     my $retVal;
438 :     # Parse the date.
439 :     if ($dateString =~ m#(\d+)/(\d+)/(\d+)\s+(\d+):(\d+):(\d+)#) {
440 :     # Create a time object. Note we need to convert the day, month,
441 :     # and year to a different base. Years count from 1900, and
442 :     # the internal month value is relocated to January = 0.
443 :     $retVal = timelocal($6, $5, $4, $2, $1 - 1, $3 - 1900);
444 :     }
445 :     # Return the result.
446 :     return $retVal;
447 :     }
448 : parrello 1.31
449 : parrello 1.93 =head3 LogErrors
450 : parrello 1.42
451 : parrello 1.93 Tracer::LogErrors($fileName);
452 : parrello 1.31
453 : parrello 1.93 Route the standard error output to a log file.
454 : parrello 1.31
455 : parrello 1.93 =over 4
456 : parrello 1.31
457 : parrello 1.93 =item fileName
458 : parrello 1.31
459 : parrello 1.93 Name of the file to receive the error output.
460 : parrello 1.31
461 : parrello 1.93 =back
462 : parrello 1.31
463 : parrello 1.93 =cut
464 : parrello 1.84
465 : parrello 1.93 sub LogErrors {
466 :     # Get the file name.
467 :     my ($fileName) = @_;
468 :     # Open the file as the standard error output.
469 :     open STDERR, '>', $fileName;
470 :     }
471 : parrello 1.84
472 : parrello 1.93 =head3 Trace
473 : parrello 1.31
474 : parrello 1.93 Trace($message);
475 : parrello 1.31
476 : parrello 1.93 Write a trace message to the target location specified in L</TSetup>. If there has not been
477 :     any prior call to B<TSetup>.
478 : parrello 1.31
479 : parrello 1.93 =over 4
480 : parrello 1.42
481 : parrello 1.93 =item message
482 : parrello 1.42
483 : parrello 1.93 Message to write.
484 : parrello 1.42
485 : parrello 1.93 =back
486 : parrello 1.72
487 : parrello 1.93 =cut
488 : parrello 1.36
489 : parrello 1.93 sub Trace {
490 :     # Get the parameters.
491 :     my ($message) = @_;
492 :     # Get the timestamp.
493 :     my $timeStamp = Now();
494 :     # Format the message. Note we strip off any line terminators at the end.
495 :     my $prefix = "[$timeStamp] <$LastCategory>: ";
496 :     my $formatted = $prefix . Strip($message);
497 :     # Process according to the destination.
498 :     if ($Destination eq "TEXT") {
499 :     # Write the message to the standard output.
500 :     print "$formatted\n";
501 :     } elsif ($Destination eq "ERROR") {
502 :     # Write the message to the error output.
503 :     print STDERR "$formatted\n";
504 :     } elsif ($Destination eq "QUEUE") {
505 :     # Push the message into the queue.
506 :     push @Queue, "$formatted";
507 :     } elsif ($Destination eq "HTML") {
508 :     # Convert the message to HTML and write it to the standard output.
509 :     my $escapedMessage = CGI::escapeHTML($message);
510 :     print "<p>$timeStamp $LastCategory: $escapedMessage</p>\n";
511 :     } elsif ($Destination eq "WARN") {
512 :     # Emit the message as a warning.
513 : gdpusch 1.95 carp $message;
514 : parrello 1.93 } elsif ($Destination =~ m/^>>/) {
515 :     # Write the trace message to an output file.
516 :     (open TRACING, $Destination) || die "Tracing open for \"$Destination\" failed: $!";
517 :     print TRACING "$formatted\n";
518 :     close TRACING;
519 :     # If the Tee flag is on, echo it to the standard output.
520 :     if ($TeeFlag) {
521 :     print "$formatted\n";
522 :     }
523 :     }
524 :     }
525 : parrello 1.36
526 : parrello 1.93 =head3 T
527 : parrello 1.36
528 : parrello 1.93 my $switch = T($category, $traceLevel);
529 : parrello 1.36
530 : parrello 1.93 or
531 : parrello 1.44
532 : parrello 1.93 my $switch = T($traceLevel);
533 : parrello 1.44
534 : parrello 1.93 Return TRUE if the trace level is at or above a specified value and the specified category
535 :     is active, else FALSE. If no category is specified, the caller's package name is used.
536 : parrello 1.44
537 : parrello 1.93 =over 4
538 : parrello 1.44
539 : parrello 1.93 =item category
540 : parrello 1.44
541 : parrello 1.93 Category to which the message belongs. If not specified, the caller's package name is
542 :     used.
543 : parrello 1.31
544 : parrello 1.93 =item traceLevel
545 : parrello 1.31
546 : parrello 1.93 Relevant tracing level.
547 : parrello 1.31
548 : parrello 1.93 =item RETURN
549 : parrello 1.31
550 : parrello 1.93 TRUE if a message at the specified trace level would appear in the trace, else FALSE.
551 : parrello 1.31
552 : parrello 1.93 =back
553 : parrello 1.36
554 : parrello 1.93 =cut
555 :    
556 :     sub T {
557 :     # Declare the return variable.
558 :     my $retVal = 0;
559 :     # Only proceed if tracing is turned on.
560 :     if ($Destination ne "NONE") {
561 :     # Get the parameters.
562 :     my ($category, $traceLevel) = @_;
563 :     if (!defined $traceLevel) {
564 :     # Here we have no category, so we need to get the calling package.
565 :     # The calling package is normally the first parameter. If it is
566 :     # omitted, the first parameter will be the tracelevel. So, the
567 :     # first thing we do is shift the so-called category into the
568 :     # $traceLevel variable where it belongs.
569 :     $traceLevel = $category;
570 :     my ($package, $fileName, $line) = caller;
571 :     # If there is no calling package, we default to "main".
572 :     if (!$package) {
573 :     $category = "main";
574 :     } else {
575 :     my @cats = split /::/, $package;
576 :     $category = $cats[$#cats];
577 :     }
578 :     }
579 :     # Save the category name.
580 :     $LastCategory = $category;
581 :     # Convert it to lower case before we hash it.
582 :     $category = lc $category;
583 :     # Use the category and tracelevel to compute the result.
584 :     if (ref $traceLevel) {
585 :     Confess("Bad trace level.");
586 :     } elsif (ref $TraceLevel) {
587 :     Confess("Bad trace config.");
588 :     }
589 :     $retVal = ($traceLevel <= $TraceLevel && ($AllTrace || exists $Categories{$category}));
590 :     }
591 :     # Return the computed result.
592 :     return $retVal;
593 :     }
594 :    
595 :     =head3 QTrace
596 :    
597 :     my $data = QTrace($format);
598 :    
599 :     Return the queued trace data in the specified format.
600 :    
601 :     =over 4
602 :    
603 :     =item format
604 :    
605 :     C<html> to format the data as an HTML list, C<text> to format it as straight text.
606 :    
607 :     =back
608 :    
609 :     =cut
610 :    
611 :     sub QTrace {
612 :     # Get the parameter.
613 :     my ($format) = @_;
614 :     # Create the return variable.
615 :     my $retVal = "";
616 :     # Only proceed if there is an actual queue.
617 :     if (@Queue) {
618 :     # Process according to the format.
619 :     if ($format =~ m/^HTML$/i) {
620 :     # Convert the queue into an HTML list.
621 :     $retVal = "<ul>\n";
622 :     for my $line (@Queue) {
623 :     my $escapedLine = CGI::escapeHTML($line);
624 :     $retVal .= "<li>$escapedLine</li>\n";
625 :     }
626 :     $retVal .= "</ul>\n";
627 :     } elsif ($format =~ m/^TEXT$/i) {
628 :     # Convert the queue into a list of text lines.
629 :     $retVal = join("\n", @Queue) . "\n";
630 :     }
631 :     # Clear the queue.
632 :     @Queue = ();
633 :     }
634 :     # Return the formatted list.
635 :     return $retVal;
636 :     }
637 :    
638 :     =head3 Confess
639 :    
640 :     Confess($message);
641 :    
642 :     Trace the call stack and abort the program with the specified message. When used with
643 :     the OR operator and the L</Assert> method, B<Confess> can function as a debugging assert.
644 :     So, for example
645 :    
646 :     Assert($recNum >= 0) || Confess("Invalid record number $recNum.");
647 :    
648 :     Will abort the program with a stack trace if the value of C<$recNum> is negative.
649 :    
650 :     =over 4
651 :    
652 :     =item message
653 :    
654 :     Message to include in the trace.
655 :    
656 :     =back
657 :    
658 :     =cut
659 :    
660 :     sub Confess {
661 :     # Get the parameters.
662 :     my ($message) = @_;
663 :     if (! defined($FIG_Config::no_tool_hdr)) {
664 :     # Here we have a tool header. Display its length so that the user can adjust the line numbers.
665 :     my $toolHeaderFile = "$FIG_Config::fig_disk/dist/releases/current/$FIG_Config::arch/tool_hdr";
666 :     # Only proceed if the tool header file is actually present.
667 :     if (-f $toolHeaderFile) {
668 :     my @lines = GetFile($toolHeaderFile);
669 :     Trace("Tool header has " . scalar(@lines) . " lines.");
670 :     }
671 :     }
672 :     # Trace the call stack.
673 :     Cluck($message);
674 :     # Abort the program.
675 :     croak(">>> $message");
676 :     }
677 :    
678 :     =head3 Assert
679 :    
680 :     Assert($condition1, $condition2, ... $conditionN);
681 :    
682 :     Return TRUE if all the conditions are true. This method can be used in conjunction with
683 :     the OR operator and the L</Confess> method as a debugging assert.
684 :     So, for example
685 :    
686 :     Assert($recNum >= 0) || Confess("Invalid record number $recNum.");
687 :    
688 :     Will abort the program with a stack trace if the value of C<$recNum> is negative.
689 :    
690 :     =cut
691 :     sub Assert {
692 :     my $retVal = 1;
693 :     LOOP: for my $condition (@_) {
694 :     if (! $condition) {
695 :     $retVal = 0;
696 :     last LOOP;
697 :     }
698 :     }
699 :     return $retVal;
700 :     }
701 :    
702 :     =head3 Cluck
703 :    
704 :     Cluck($message);
705 :    
706 :     Trace the call stack. Note that for best results, you should qualify the call with a
707 :     trace condition. For example,
708 :    
709 :     Cluck("Starting record parse.") if T(3);
710 :    
711 :     will only trace the stack if the trace level for the package is 3 or more.
712 :    
713 :     =over 4
714 :    
715 :     =item message
716 :    
717 :     Message to include in the trace.
718 :    
719 :     =back
720 :    
721 :     =cut
722 :    
723 :     sub Cluck {
724 :     # Get the parameters.
725 :     my ($message) = @_;
726 :     # Trace what's happening.
727 :     Trace("Stack trace for event: $message");
728 :     my $confession = longmess($message);
729 :     # Convert the confession to a series of trace messages. Note we skip any
730 :     # messages relating to calls into Tracer.
731 :     for my $line (split /\s*\n/, $confession) {
732 :     Trace($line) if ($line !~ /Tracer\.pm/);
733 :     }
734 :     }
735 :    
736 :     =head3 ScriptSetup
737 :    
738 :     my ($cgi, $varHash) = ScriptSetup($noTrace);
739 :    
740 :     Perform standard tracing and debugging setup for scripts. The value returned is
741 :     the CGI object followed by a pre-built variable hash. At the end of the script,
742 :     the client should call L</ScriptFinish> to output the web page.
743 :    
744 :     This method calls L</ETracing> to configure tracing, which allows the tracing
745 :     to be configured via the emergency tracing form on the debugging control panel.
746 :     Tracing will then be turned on automatically for all programs that use the L</ETracing>
747 :     method, which includes every program that uses this method or L</StandardSetup>.
748 : parrello 1.36
749 : parrello 1.93 =over 4
750 : parrello 1.31
751 : parrello 1.93 =item noTrace (optional)
752 : parrello 1.31
753 : parrello 1.93 If specified, tracing will be suppressed. This is useful if the script wants to set up
754 :     tracing manually.
755 : parrello 1.31
756 :     =item RETURN
757 :    
758 : parrello 1.93 Returns a two-element list consisting of a CGI query object and a variable hash for
759 :     the output page.
760 : parrello 1.31
761 :     =back
762 :    
763 :     =cut
764 :    
765 : parrello 1.93 sub ScriptSetup {
766 : parrello 1.31 # Get the parameters.
767 : parrello 1.93 my ($noTrace) = @_;
768 :     # Get the CGI query object.
769 :     my $cgi = CGI->new();
770 :     # Set up tracing if it's not suppressed.
771 :     ETracing($cgi) unless $noTrace;
772 :     # Create the variable hash.
773 :     my $varHash = { results => '' };
774 :     # Return the query object and variable hash.
775 :     return ($cgi, $varHash);
776 :     }
777 :    
778 :     =head3 ETracing
779 :    
780 :     ETracing($parameter);
781 :    
782 :     Set up emergency tracing. Emergency tracing is tracing that is turned
783 :     on automatically for any program that calls this method. The emergency
784 :     tracing parameters are stored in a a file identified by a tracing key.
785 :     If this method is called with a CGI object, then the tracing key is
786 :     taken from a cookie. If it is called with no parameters, then the tracing
787 :     key is taken from an environment variable. If it is called with a string,
788 :     the tracing key is that string.
789 :    
790 :     =over 4
791 :    
792 :     =item parameter
793 :    
794 :     A parameter from which the tracing key is computed. If it is a scalar,
795 :     that scalar is used as the tracing key. If it is a CGI object, the
796 :     tracing key is taken from the C<IP> cookie. If it is omitted, the
797 :     tracing key is taken from the C<TRACING> environment variable. If it
798 :     is a CGI object and emergency tracing is not on, the C<Trace> and
799 :     C<TF> parameters will be used to determine the type of tracing.
800 :    
801 :     =back
802 :    
803 :     =cut
804 :    
805 :     sub ETracing {
806 :     # Get the parameter.
807 :     my ($parameter) = @_;
808 :     # Check for CGI mode.
809 :     my $cgi = (ref $parameter eq 'CGI' ? $parameter : undef);
810 :     # Default to no tracing except errors.
811 :     my ($tracing, $dest) = ("0", "WARN");
812 :     # Check for emergency tracing.
813 :     my $tkey = EmergencyKey($parameter);
814 :     my $emergencyFile = EmergencyFileName($tkey);
815 :     if (-e $emergencyFile) {
816 :     # We have the file. Read in the data.
817 :     my @tracing = GetFile($emergencyFile);
818 :     # Pull off the time limit.
819 :     my $expire = shift @tracing;
820 :     # Convert it to seconds.
821 :     $expire *= 3600;
822 :     # Check the file data.
823 :     my $stat = stat($emergencyFile);
824 :     my ($now) = gettimeofday;
825 :     if ($now - $stat->mtime > $expire) {
826 :     # Delete the expired file.
827 :     unlink $emergencyFile;
828 : parrello 1.44 } else {
829 : parrello 1.93 # Emergency tracing is on. Pull off the destination and
830 :     # the trace level;
831 :     $dest = shift @tracing;
832 :     my $level = shift @tracing;
833 :     # Convert the destination to a real tracing destination.
834 :     # temp directory.
835 :     $dest = EmergencyTracingDest($tkey, $dest);
836 :     # Insure Tracer is specified.
837 :     my %moduleHash = map { $_ => 1 } @tracing;
838 :     $moduleHash{Tracer} = 1;
839 :     # Set the trace parameter.
840 :     $tracing = join(" ", $level, sort keys %moduleHash);
841 : parrello 1.44 }
842 : parrello 1.93 } elsif (defined $cgi) {
843 :     # There's no emergency tracing, but we have a CGI object, so check
844 :     # for tracing from the form parameters.
845 :     if ($cgi->param('Trace')) {
846 :     # Here the user has requested tracing via a form.
847 :     $dest = ($cgi->param('TF') ? ">$FIG_Config::temp/Trace$$.log" : "QUEUE");
848 :     $tracing = $cgi->param('Trace') . " Tracer";
849 : parrello 1.36 }
850 :     }
851 : parrello 1.93 # Setup the tracing we've determined from all the stuff above.
852 :     TSetup($tracing, $dest);
853 : parrello 1.94 # Check to see if we're a web script.
854 : parrello 1.93 if (defined $cgi) {
855 : parrello 1.94 # Yes we are. Trace the form and environment data.
856 : parrello 1.93 TraceParms($cgi);
857 : parrello 1.94 # Check for RAW mode. In raw mode, we print a fake header so that we see everything
858 :     # emitted by the script in its raw form.
859 :     if (T(Raw => 3)) {
860 :     print CGI::header(-type => 'text/plain', -tracing => 'Raw');
861 :     }
862 : parrello 1.83 }
863 : parrello 1.31 }
864 :    
865 : parrello 1.93 =head3 EmergencyFileName
866 :    
867 :     my $fileName = Tracer::EmergencyFileName($tkey);
868 :    
869 :     Return the emergency tracing file name. This is the file that specifies
870 :     the tracing information.
871 :    
872 :     =over 4
873 :    
874 :     =item tkey
875 :    
876 :     Tracing key for the current program.
877 : parrello 1.11
878 : parrello 1.93 =item RETURN
879 : parrello 1.11
880 : parrello 1.93 Returns the name of the file to contain the emergency tracing information.
881 : parrello 1.11
882 : parrello 1.93 =back
883 : parrello 1.11
884 :     =cut
885 :    
886 : parrello 1.93 sub EmergencyFileName {
887 :     # Get the parameters.
888 :     my ($tkey) = @_;
889 :     # Compute the emergency tracing file name.
890 :     return "$FIG_Config::temp/Emergency$tkey.txt";
891 : olson 1.1 }
892 :    
893 : parrello 1.93 =head3 EmergencyFileTarget
894 : parrello 1.10
895 : parrello 1.93 my $fileName = Tracer::EmergencyFileTarget($tkey);
896 : parrello 1.10
897 : parrello 1.93 Return the emergency tracing target file name. This is the file that receives
898 :     the tracing output for file-based tracing.
899 : parrello 1.10
900 : parrello 1.93 =over 4
901 : parrello 1.10
902 : parrello 1.93 =item tkey
903 : parrello 1.10
904 : parrello 1.93 Tracing key for the current program.
905 : parrello 1.10
906 : parrello 1.93 =item RETURN
907 : parrello 1.10
908 : parrello 1.93 Returns the name of the file to contain the trace output.
909 : parrello 1.10
910 : parrello 1.93 =back
911 : parrello 1.10
912 : parrello 1.93 =cut
913 : parrello 1.10
914 : parrello 1.93 sub EmergencyFileTarget {
915 :     # Get the parameters.
916 :     my ($tkey) = @_;
917 :     # Compute the emergency tracing file name.
918 :     return "$FIG_Config::temp/trace$tkey.log";
919 :     }
920 : parrello 1.10
921 : parrello 1.93 =head3 EmergencyTracingDest
922 : parrello 1.10
923 : parrello 1.93 my $dest = Tracer::EmergencyTracingDest($tkey, $myDest);
924 : parrello 1.10
925 : parrello 1.93 This method converts an emergency tracing destination to a real
926 :     tracing destination. The main difference is that if the
927 :     destination is C<FILE> or C<APPEND>, we convert it to file
928 :     output. If the destination is C<DUAL>, we convert it to file
929 :     and standard output.
930 : parrello 1.10
931 :     =over 4
932 :    
933 : parrello 1.93 =item tkey
934 : parrello 1.10
935 : parrello 1.93 Tracing key for this environment.
936 : parrello 1.10
937 : parrello 1.93 =item myDest
938 : parrello 1.10
939 : parrello 1.93 Destination from the emergency tracing file.
940 : parrello 1.10
941 :     =item RETURN
942 :    
943 : parrello 1.93 Returns a destination that can be passed into L</TSetup>.
944 : parrello 1.10
945 :     =back
946 :    
947 :     =cut
948 :    
949 : parrello 1.93 sub EmergencyTracingDest {
950 : parrello 1.12 # Get the parameters.
951 : parrello 1.93 my ($tkey, $myDest) = @_;
952 :     # Declare the return variable.
953 :     my $retVal = $myDest;
954 :     # Process according to the destination value.
955 :     if ($myDest eq 'FILE') {
956 :     $retVal = ">" . EmergencyFileTarget($tkey);
957 :     } elsif ($myDest eq 'APPEND') {
958 :     $retVal = ">>" . EmergencyFileTarget($tkey);
959 :     } elsif ($myDest eq 'DUAL') {
960 :     $retVal = "+>" . EmergencyFileTarget($tkey);
961 : parrello 1.12 }
962 : parrello 1.93 # Return the result.
963 :     return $retVal;
964 : parrello 1.10 }
965 :    
966 : parrello 1.93 =head3 Emergency
967 :    
968 :     Emergency($key, $hours, $dest, $level, @modules);
969 :    
970 :     Turn on emergency tracing. This method is normally invoked over the web from
971 :     a debugging console, but it can also be called by the C<trace.pl> script.
972 :     The caller specifies the duration of the emergency in hours, the desired tracing
973 :     destination, the trace level, and a list of the trace modules to activate.
974 :     For the length of the duration, when a program in an environment with the
975 :     specified tracing key active invokes a Sprout CGI script, tracing will be
976 :     turned on automatically. See L</TSetup> for more about tracing setup and
977 :     L</ETracing> for more about emergency tracing.
978 :    
979 :     =over 4
980 : parrello 1.11
981 : parrello 1.93 =item tkey
982 : parrello 1.11
983 : parrello 1.93 The tracing key. This is used to identify the control file and the trace file.
984 : parrello 1.11
985 : parrello 1.93 =item hours
986 : parrello 1.11
987 : parrello 1.93 Number of hours to keep emergency tracing alive.
988 : parrello 1.11
989 : parrello 1.93 =item dest
990 : parrello 1.11
991 : parrello 1.93 Tracing destination. If no path information is specified for a file
992 :     destination, it is put in the FIG temporary directory.
993 : parrello 1.11
994 : parrello 1.93 =item level
995 : parrello 1.11
996 : parrello 1.93 Tracing level. A higher level means more trace messages.
997 : parrello 1.11
998 : parrello 1.93 =item modules
999 : parrello 1.11
1000 : parrello 1.93 A list of the tracing modules to activate.
1001 : parrello 1.11
1002 :     =back
1003 :    
1004 :     =cut
1005 : parrello 1.93
1006 :     sub Emergency {
1007 : parrello 1.11 # Get the parameters.
1008 : parrello 1.93 my ($tkey, $hours, $dest, $level, @modules) = @_;
1009 :     # Create the emergency file.
1010 :     my $specFile = EmergencyFileName($tkey);
1011 :     my $outHandle = Open(undef, ">$specFile");
1012 :     print $outHandle join("\n", $hours, $dest, $level, @modules, "");
1013 : parrello 1.11 }
1014 :    
1015 : parrello 1.93 =head3 EmergencyKey
1016 :    
1017 :     my $tkey = EmergencyKey($parameter);
1018 :    
1019 :     Return the Key to be used for emergency tracing. This could be an IP address,
1020 :     a session ID, or a user name, depending on the environment.
1021 : parrello 1.11
1022 : parrello 1.93 =over 4
1023 : parrello 1.11
1024 : parrello 1.93 =item parameter
1025 : parrello 1.11
1026 : parrello 1.93 Parameter defining the method for finding the tracing key. If it is a scalar,
1027 :     then it is presumed to be the tracing key itself. If it is a CGI object, then
1028 :     the tracing key is taken from the C<IP> cookie. Otherwise, the tracing key is
1029 :     taken from the C<TRACING> environment variable.
1030 : parrello 1.29
1031 : parrello 1.93 =item RETURN
1032 : parrello 1.11
1033 : parrello 1.93 Returns the key to be used for labels in emergency tracing.
1034 : parrello 1.11
1035 : parrello 1.93 =back
1036 : parrello 1.11
1037 : parrello 1.93 =cut
1038 : parrello 1.29
1039 : parrello 1.93 sub EmergencyKey {
1040 :     # Get the parameters.
1041 :     my ($parameter) = @_;
1042 :     # Declare the return variable.
1043 :     my $retVal;
1044 :     # Determine the parameter type.
1045 :     if (! defined $parameter) {
1046 :     # Here we're supposed to check the environment.
1047 :     $retVal = $ENV{TRACING};
1048 :     } else {
1049 :     my $ptype = ref $parameter;
1050 :     if ($ptype eq 'CGI') {
1051 :     # Here we were invoked from a web page. Look for a cookie.
1052 :     $retVal = $parameter->cookie('IP');
1053 :     } elsif (! $ptype) {
1054 :     # Here the key was passed in.
1055 :     $retVal = $parameter;
1056 :     }
1057 :     }
1058 :     # If no luck finding a key, use the PID.
1059 :     if (! defined $retVal) {
1060 :     $retVal = $$;
1061 :     }
1062 :     # Return the result.
1063 :     return $retVal;
1064 :     }
1065 : parrello 1.11
1066 :    
1067 : parrello 1.93 =head3 TraceParms
1068 : parrello 1.11
1069 : parrello 1.93 Tracer::TraceParms($cgi);
1070 : parrello 1.11
1071 : parrello 1.93 Trace the CGI parameters at trace level CGI => 3 and the environment variables
1072 : parrello 1.94 at level CGI => 4. A self-referencing URL is traced at level CGI => 2.
1073 : parrello 1.11
1074 : parrello 1.93 =over 4
1075 : parrello 1.11
1076 : parrello 1.93 =item cgi
1077 : parrello 1.31
1078 : parrello 1.93 CGI query object containing the parameters to trace.
1079 : parrello 1.31
1080 : parrello 1.11 =back
1081 :    
1082 :     =cut
1083 : parrello 1.93
1084 :     sub TraceParms {
1085 : parrello 1.11 # Get the parameters.
1086 : parrello 1.93 my ($cgi) = @_;
1087 : parrello 1.94 if (T(CGI => 2)) {
1088 :     # Here we trace the GET-style URL for the script.
1089 :     Trace("URL: " . $cgi->url(-relative => 1, -query => 1));
1090 :     }
1091 : parrello 1.93 if (T(CGI => 3)) {
1092 :     # Here we want to trace the parameter data.
1093 :     my @names = $cgi->param;
1094 :     for my $parmName (sort @names) {
1095 :     # Note we skip the Trace parameters, which are for our use only.
1096 :     if ($parmName ne 'Trace' && $parmName ne 'TF') {
1097 :     my @values = $cgi->param($parmName);
1098 :     Trace("CGI: $parmName = " . join(", ", @values));
1099 :     }
1100 :     }
1101 :     # Display the request method.
1102 :     my $method = $cgi->request_method();
1103 :     Trace("Method: $method");
1104 :     }
1105 :     if (T(CGI => 4)) {
1106 :     # Here we want the environment data too.
1107 :     for my $envName (sort keys %ENV) {
1108 :     Trace("ENV: $envName = $ENV{$envName}");
1109 : parrello 1.12 }
1110 :     }
1111 : parrello 1.11 }
1112 :    
1113 : parrello 1.94 =head3 TraceImages
1114 :    
1115 :     Tracer::TraceImages($htmlString);
1116 :    
1117 :     Trace information about all of an html document's images. The tracing
1118 :     will be for type "IMG" at level 3. The image's source string
1119 :     will be displayed. This is generally either the URL of the image or
1120 :     raw data for the image itself. If the source is too long, only the first 300
1121 :     characters will be shown at trace level 3. The entire source will be shown,
1122 :     however, at trace level 4. This method is not very smart, and might catch
1123 :     Javascript code, but it is still useful when debugging the arcane
1124 :     behavior of images in multiple browser environments.
1125 :    
1126 :     =over 4
1127 :    
1128 :     =item htmlString
1129 :    
1130 :     HTML text for an outgoing web page.
1131 :    
1132 :     =back
1133 :    
1134 :     =cut
1135 :    
1136 :     sub TraceImages {
1137 :     # Only proceed if we're at the proper trace level.
1138 :     if (T(IMG => 3)) {
1139 :     # For performance reasons we're manipulating $_[0] instead of retrieving the string
1140 :     # into a variable called "$htmlString". This is because we expect html strings to be
1141 :     # long, and don't want to copy them any more than we have to.
1142 :     Trace(length($_[0]) . " characters in web page.");
1143 :     # Loop through the HTML, culling image tags.
1144 :     while ($_[0] =~ /<img\s+[^>]+?src="([^"]+)"/sgi) {
1145 :     # Extract the source string and determine whether or not it's too long.
1146 :     my $srcString = $1;
1147 :     my $pos = pos($_[0]) - length($srcString);
1148 :     my $excess = length($srcString) - 300;
1149 :     # We'll put the display string in here.
1150 :     my $srcDisplay = $srcString;
1151 :     # If it's a data string, split it at the comma.
1152 :     $srcDisplay =~ s/^(data[^,]+,)/$1\n/;
1153 :     # If there's no excess or we're at trace level 4, we're done. At level 3 with
1154 :     # a long string, however, we only show the first 300 characters.
1155 :     if ($excess > 0 && ! T(IMG => 4)) {
1156 :     $srcDisplay = substr($srcDisplay,0,300) . "\nplus $excess characters.";
1157 :     }
1158 :     # Output the trace message.
1159 :     Trace("Image tag at position $pos:\n$srcDisplay");
1160 :     }
1161 :     }
1162 :     }
1163 :    
1164 :    
1165 : parrello 1.93 =head3 ScriptFinish
1166 :    
1167 :     ScriptFinish($webData, $varHash);
1168 :    
1169 :     Output a web page at the end of a script. Either the string to be output or the
1170 :     name of a template file can be specified. If the second parameter is omitted,
1171 :     it is assumed we have a string to be output; otherwise, it is assumed we have the
1172 :     name of a template file. The template should have the variable C<DebugData>
1173 :     specified in any form that invokes a standard script. If debugging mode is turned
1174 :     on, a form field will be put in that allows the user to enter tracing data.
1175 :     Trace messages will be placed immediately before the terminal C<BODY> tag in
1176 :     the output, formatted as a list.
1177 :    
1178 :     A typical standard script would loook like the following.
1179 :    
1180 :     BEGIN {
1181 :     # Print the HTML header.
1182 :     print "CONTENT-TYPE: text/html\n\n";
1183 :     }
1184 :     use Tracer;
1185 :     use CGI;
1186 :     use FIG;
1187 :     # ... more uses ...
1188 : parrello 1.6
1189 : parrello 1.93 my ($cgi, $varHash) = ScriptSetup();
1190 :     eval {
1191 :     # ... get data from $cgi, put it in $varHash ...
1192 :     };
1193 :     if ($@) {
1194 :     Trace("Script Error: $@") if T(0);
1195 :     }
1196 :     ScriptFinish("Html/MyTemplate.html", $varHash);
1197 : parrello 1.6
1198 : parrello 1.93 The idea here is that even if the script fails, you'll see trace messages and
1199 :     useful output.
1200 : parrello 1.6
1201 :     =over 4
1202 :    
1203 : parrello 1.93 =item webData
1204 :    
1205 :     A string containing either the full web page to be written to the output or the
1206 :     name of a template file from which the page is to be constructed. If the name
1207 :     of a template file is specified, then the second parameter must be present;
1208 :     otherwise, it must be absent.
1209 :    
1210 :     =item varHash (optional)
1211 : parrello 1.6
1212 : parrello 1.93 If specified, then a reference to a hash mapping variable names for a template
1213 :     to their values. The template file will be read into memory, and variable markers
1214 :     will be replaced by data in this hash reference.
1215 : parrello 1.6
1216 :     =back
1217 :    
1218 :     =cut
1219 :    
1220 : parrello 1.93 sub ScriptFinish {
1221 :     # Get the parameters.
1222 :     my ($webData, $varHash) = @_;
1223 :     # Check for a template file situation.
1224 :     my $outputString;
1225 :     if (defined $varHash) {
1226 :     # Here we have a template file. We need to determine the template type.
1227 :     my $template;
1228 :     if ($FIG_Config::template_url && $webData =~ /\.php$/) {
1229 :     $template = "$FIG_Config::template_url/$webData";
1230 :     } else {
1231 :     $template = "<<$webData";
1232 :     }
1233 :     $outputString = PageBuilder::Build($template, $varHash, "Html");
1234 :     } else {
1235 :     # Here the user gave us a raw string.
1236 :     $outputString = $webData;
1237 :     }
1238 :     # Check for trace messages.
1239 :     if ($Destination ne "NONE" && $TraceLevel > 0) {
1240 :     # We have trace messages, so we want to put them at the end of the body. This
1241 :     # is either at the end of the whole string or at the beginning of the BODY
1242 :     # end-tag.
1243 :     my $pos = length $outputString;
1244 :     if ($outputString =~ m#</body>#gi) {
1245 :     $pos = (pos $outputString) - 7;
1246 :     }
1247 :     # If the trace messages were queued, we unroll them. Otherwise, we display the
1248 :     # destination.
1249 :     my $traceHtml;
1250 :     if ($Destination eq "QUEUE") {
1251 :     $traceHtml = QTrace('Html');
1252 :     } elsif ($Destination =~ /^>>(.+)$/) {
1253 :     # Here the tracing output it to a file. We code it as a hyperlink so the user
1254 :     # can copy the file name into the clipboard easily.
1255 :     my $actualDest = $1;
1256 :     $traceHtml = "<p>Tracing output to $actualDest.</p>\n";
1257 :     } else {
1258 :     # Here we have one of the special destinations.
1259 :     $traceHtml = "<P>Tracing output type is $Destination.</p>\n";
1260 :     }
1261 :     substr $outputString, $pos, 0, $traceHtml;
1262 :     }
1263 :     # Write the output string.
1264 :     print $outputString;
1265 : parrello 1.6 }
1266 :    
1267 : parrello 1.93 =head2 Command-Line Utility Methods
1268 : olson 1.1
1269 : parrello 1.93 =head3 SendSMS
1270 : olson 1.1
1271 : parrello 1.93 my $msgID = Tracer::SendSMS($phoneNumber, $msg);
1272 : olson 1.1
1273 : parrello 1.93 Send a text message to a phone number using Clickatell. The FIG_Config file must contain the
1274 :     user name, password, and API ID for the relevant account in the hash reference variable
1275 :     I<$FIG_Config::phone>, using the keys C<user>, C<password>, and C<api_id>. For
1276 :     example, if the user name is C<BruceTheHumanPet>, the password is C<silly>, and the API ID
1277 :     is C<2561022>, then the FIG_Config file must contain
1278 : olson 1.1
1279 : parrello 1.93 $phone = { user => 'BruceTheHumanPet',
1280 :     password => 'silly',
1281 :     api_id => '2561022' };
1282 : olson 1.1
1283 : parrello 1.93 The original purpose of this method was to insure Bruce would be notified immediately when the
1284 :     Sprout Load terminates. Care should be taken if you do not wish Bruce to be notified immediately
1285 :     when you call this method.
1286 : olson 1.1
1287 : parrello 1.93 The message ID will be returned if successful, and C<undef> if an error occurs.
1288 : parrello 1.74
1289 : parrello 1.93 =over 4
1290 : parrello 1.74
1291 : parrello 1.93 =item phoneNumber
1292 : parrello 1.74
1293 : parrello 1.93 Phone number to receive the message, in international format. A United States phone number
1294 :     would be prefixed by "1". A British phone number would be prefixed by "44".
1295 : parrello 1.74
1296 : parrello 1.93 =item msg
1297 : parrello 1.74
1298 : parrello 1.93 Message to send to the specified phone.
1299 : parrello 1.74
1300 :     =item RETURN
1301 :    
1302 : parrello 1.93 Returns the message ID if successful, and C<undef> if the message could not be sent.
1303 : parrello 1.74
1304 : parrello 1.78 =back
1305 :    
1306 : parrello 1.74 =cut
1307 :    
1308 : parrello 1.93 sub SendSMS {
1309 : parrello 1.74 # Get the parameters.
1310 : parrello 1.93 my ($phoneNumber, $msg) = @_;
1311 :     # Declare the return variable. If we do not change it, C<undef> will be returned.
1312 : parrello 1.74 my $retVal;
1313 : parrello 1.93 # Only proceed if we have phone support.
1314 :     if (! defined $FIG_Config::phone) {
1315 :     Trace("Phone support not present in FIG_Config.") if T(1);
1316 :     } else {
1317 :     # Get the phone data.
1318 :     my $parms = $FIG_Config::phone;
1319 :     # Get the Clickatell URL.
1320 :     my $url = "http://api.clickatell.com/http/";
1321 :     # Create the user agent.
1322 :     my $ua = LWP::UserAgent->new;
1323 :     # Request a Clickatell session.
1324 :     my $resp = $ua->post("$url/sendmsg", { user => $parms->{user},
1325 :     password => $parms->{password},
1326 :     api_id => $parms->{api_id},
1327 :     to => $phoneNumber,
1328 :     text => $msg});
1329 :     # Check for an error.
1330 :     if (! $resp->is_success) {
1331 :     Trace("Alert failed.") if T(1);
1332 :     } else {
1333 :     # Get the message ID.
1334 :     my $rstring = $resp->content;
1335 :     if ($rstring =~ /^ID:\s+(.*)$/) {
1336 :     $retVal = $1;
1337 :     } else {
1338 :     Trace("Phone attempt failed with $rstring") if T(1);
1339 :     }
1340 :     }
1341 : parrello 1.74 }
1342 :     # Return the result.
1343 :     return $retVal;
1344 :     }
1345 :    
1346 : parrello 1.93 =head3 StandardSetup
1347 : olson 1.1
1348 : parrello 1.93 my ($options, @parameters) = StandardSetup(\@categories, \%options, $parmHelp, @ARGV);
1349 : olson 1.1
1350 : parrello 1.93 This method performs standard command-line parsing and tracing setup. The return
1351 :     values are a hash of the command-line options and a list of the positional
1352 :     parameters. Tracing is automatically set up and the command-line options are
1353 :     validated.
1354 : olson 1.1
1355 : parrello 1.93 This is a complex method that does a lot of grunt work. The parameters can
1356 :     be more easily understood, however, once they are examined individually.
1357 : olson 1.1
1358 : parrello 1.93 The I<categories> parameter is the most obtuse. It is a reference to a list of
1359 :     special-purpose tracing categories. Most tracing categories are PERL package
1360 :     names. So, for example, if you wanted to turn on tracing inside the B<Sprout>,
1361 :     B<ERDB>, and B<SproutLoad> packages, you would specify the categories
1362 : olson 1.1
1363 : parrello 1.93 ["Sprout", "SproutLoad", "ERDB"]
1364 : olson 1.1
1365 : parrello 1.93 This would cause trace messages in the specified three packages to appear in
1366 :     the output. There are two special tracing categories that are automatically
1367 :     handled by this method. In other words, if you used L</TSetup> you would need
1368 :     to include these categories manually, but if you use this method they are turned
1369 :     on automatically.
1370 : parrello 1.5
1371 :     =over 4
1372 :    
1373 : parrello 1.93 =item SQL
1374 : parrello 1.5
1375 : parrello 1.93 Traces SQL commands and activity.
1376 : parrello 1.5
1377 : parrello 1.93 =item Tracer
1378 : parrello 1.5
1379 : parrello 1.93 Traces error messages and call stacks.
1380 : parrello 1.5
1381 :     =back
1382 :    
1383 : parrello 1.93 C<SQL> is only turned on if the C<-sql> option is specified in the command line.
1384 :     The trace level is specified using the C<-trace> command-line option. For example,
1385 :     the following command line for C<TransactFeatures> turns on SQL tracing and runs
1386 :     all tracing at level 3.
1387 : parrello 1.5
1388 : parrello 1.93 TransactFeatures -trace=3 -sql register ../xacts IDs.tbl
1389 : parrello 1.5
1390 : parrello 1.93 Standard tracing is output to the standard output and echoed to the file
1391 :     C<trace>I<$$>C<.log> in the FIG temporary directory, where I<$$> is the
1392 :     process ID. You can also specify the C<user> parameter to put a user ID
1393 :     instead of a process ID in the trace file name. So, for example
1394 : olson 1.1
1395 : parrello 1.93 The default trace level is 2. To get all messages, specify a trace level of 4.
1396 :     For a genome-by-genome update, use 3.
1397 : olson 1.1
1398 : parrello 1.93 TransactFeatures -trace=3 -sql -user=Bruce register ../xacts IDs.tbl
1399 : olson 1.1
1400 : parrello 1.93 would send the trace output to C<traceBruce.log> in the temporary directory.
1401 : olson 1.1
1402 : parrello 1.93 The I<options> parameter is a reference to a hash containing the command-line
1403 :     options, their default values, and an explanation of what they mean. Command-line
1404 :     options may be in the form of switches or keywords. In the case of a switch, the
1405 :     option value is 1 if it is specified and 0 if it is not specified. In the case
1406 :     of a keyword, the value is separated from the option name by an equal sign. You
1407 :     can see this last in the command-line example above.
1408 : olson 1.1
1409 : parrello 1.93 You can specify a different default trace level by setting C<$options->{trace}>
1410 :     prior to calling this method.
1411 : olson 1.1
1412 : parrello 1.93 An example at this point would help. Consider, for example, the command-line utility
1413 :     C<TransactFeatures>. It accepts a list of positional parameters plus the options
1414 :     C<safe>, C<noAlias>, C<start>, and C<tblFiles>. To start up this command, we execute
1415 :     the following code.
1416 : olson 1.1
1417 : parrello 1.93 my ($options, @parameters) = Tracer::StandardSetup(["DocUtils"],
1418 :     { safe => [0, "use database transactions"],
1419 :     noAlias => [0, "do not expect aliases in CHANGE transactions"],
1420 :     start => [' ', "start with this genome"],
1421 :     tblFiles => [0, "output TBL files containing the corrected IDs"] },
1422 :     "<command> <transactionDirectory> <IDfile>",
1423 :     @ARGV);
1424 : olson 1.1
1425 :    
1426 : parrello 1.93 The call to C<ParseCommand> specifies the default values for the options and
1427 :     stores the actual options in a hash that is returned as C<$options>. The
1428 :     positional parameters are returned in C<@parameters>.
1429 : olson 1.1
1430 : parrello 1.93 The following is a sample command line for C<TransactFeatures>.
1431 : olson 1.1
1432 : parrello 1.93 TransactFeatures -trace=2 -noAlias register ../xacts IDs.tbl
1433 : olson 1.1
1434 : parrello 1.93 Single and double hyphens are equivalent. So, you could also code the
1435 :     above command as
1436 : olson 1.1
1437 : parrello 1.93 TransactFeatures --trace=2 --noAlias register ../xacts IDs.tbl
1438 : olson 1.1
1439 : parrello 1.93 In this case, C<register>, C<../xacts>, and C<IDs.tbl> are the positional
1440 :     parameters, and would find themselves in I<@parameters> after executing the
1441 :     above code fragment. The tracing would be set to level 2, and the categories
1442 :     would be C<Tracer>, and <DocUtils>. C<Tracer> is standard,
1443 :     and C<DocUtils> was included because it came in within the first parameter
1444 :     to this method. The I<$options> hash would be
1445 : olson 1.1
1446 : parrello 1.93 { trace => 2, sql => 0, safe => 0,
1447 :     noAlias => 1, start => ' ', tblFiles => 0 }
1448 : olson 1.1
1449 : parrello 1.93 Use of C<StandardSetup> in this way provides a simple way of performing
1450 :     standard tracing setup and command-line parsing. Note that the caller is
1451 :     not even aware of the command-line switches C<-trace> and C<-sql>, which
1452 :     are used by this method to control the tracing. If additional tracing features
1453 :     need to be added in the future, they can be processed by this method without
1454 :     upsetting the command-line utilities.
1455 : olson 1.1
1456 : parrello 1.93 If the C<background> option is specified on the command line, then the
1457 :     standard and error outputs will be directed to files in the temporary
1458 :     directory, using the same suffix as the trace file. So, if the command
1459 :     line specified
1460 : olson 1.1
1461 : parrello 1.93 -user=Bruce -background
1462 : olson 1.1
1463 : parrello 1.93 then the trace output would go to C<traceBruce.log>, the standard output to
1464 :     C<outBruce.log>, and the error output to C<errBruce.log>. This is designed to
1465 :     simplify starting a command in the background.
1466 : olson 1.1
1467 : parrello 1.93 The user name is also used as the tracing key for L</Emergency Tracing>.
1468 :     Specifying a value of C<E> for the trace level causes emergency tracing to
1469 :     be used instead of custom tracing. If the user name is not specified,
1470 :     the tracing key is taken from the C<Tracing> environment variable. If there
1471 :     is no value for that variable, the tracing key will be computed from the PID.
1472 : olson 1.1
1473 : parrello 1.93 Finally, if the special option C<-help> is specified, the option
1474 :     names will be traced at level 0 and the program will exit without processing.
1475 :     This provides a limited help capability. For example, if the user enters
1476 : olson 1.1
1477 : parrello 1.93 TransactFeatures -help
1478 : olson 1.1
1479 : parrello 1.93 he would see the following output.
1480 : olson 1.1
1481 : parrello 1.93 TransactFeatures [options] <command> <transactionDirectory> <IDfile>
1482 :     -trace tracing level (default E)
1483 :     -sql trace SQL commands
1484 :     -safe use database transactions
1485 :     -noAlias do not expect aliases in CHANGE transactions
1486 :     -start start with this genome
1487 :     -tblFiles output TBL files containing the corrected IDs
1488 : olson 1.1
1489 : parrello 1.93 The caller has the option of modifying the tracing scheme by placing a value
1490 :     for C<trace> in the incoming options hash. The default value can be overridden,
1491 :     or the tracing to the standard output can be turned off by suffixing a minus
1492 :     sign to the trace level. So, for example,
1493 : olson 1.1
1494 : parrello 1.93 { trace => [0, "tracing level (default 0)"],
1495 :     ...
1496 : olson 1.1
1497 : parrello 1.93 would set the default trace level to 0 instead of E, while
1498 : olson 1.1
1499 : parrello 1.93 { trace => ["2-", "tracing level (default 2)"],
1500 :     ...
1501 : olson 1.1
1502 : parrello 1.93 would set the default to 2, but trace only to the log file, not to the
1503 :     standard output.
1504 : olson 1.1
1505 : parrello 1.93 The parameters to this method are as follows.
1506 : olson 1.1
1507 : parrello 1.93 =over 4
1508 : olson 1.1
1509 : parrello 1.93 =item categories
1510 : parrello 1.2
1511 : parrello 1.93 Reference to a list of tracing category names. These should be names of
1512 :     packages whose internal workings will need to be debugged to get the
1513 :     command working.
1514 : olson 1.1
1515 : parrello 1.93 =item options
1516 : olson 1.1
1517 : parrello 1.93 Reference to a hash containing the legal options for the current command mapped
1518 :     to their default values and descriptions. The user can override the defaults
1519 :     by specifying the options as command-line switches prefixed by a hyphen.
1520 :     Tracing-related options may be added to this hash. If the C<-h> option is
1521 :     specified on the command line, the option descriptions will be used to
1522 :     explain the options. To turn off tracing to the standard output, add a
1523 :     minus sign to the value for C<trace> (see above).
1524 : olson 1.1
1525 : parrello 1.93 =item parmHelp
1526 : olson 1.1
1527 : parrello 1.93 A string that vaguely describes the positional parameters. This is used
1528 :     if the user specifies the C<-h> option.
1529 : olson 1.1
1530 : parrello 1.93 =item argv
1531 : olson 1.1
1532 : parrello 1.93 List of command line parameters, including the option switches, which must
1533 :     precede the positional parameters and be prefixed by a hyphen.
1534 : olson 1.1
1535 :     =item RETURN
1536 :    
1537 : parrello 1.93 Returns a list. The first element of the list is the reference to a hash that
1538 :     maps the command-line option switches to their values. These will either be the
1539 :     default values or overrides specified on the command line. The remaining
1540 :     elements of the list are the position parameters, in order.
1541 : olson 1.1
1542 :     =back
1543 :    
1544 :     =cut
1545 :    
1546 : parrello 1.93 sub StandardSetup {
1547 :     # Get the parameters.
1548 :     my ($categories, $options, $parmHelp, @argv) = @_;
1549 :     # Get the default tracing key.
1550 :     my $tkey = EmergencyKey();
1551 :     # Add the tracing options.
1552 :     if (! exists $options->{trace}) {
1553 :     $options->{trace} = ['2', "tracing level (E for emergency tracing)"];
1554 :     }
1555 :     $options->{sql} = [0, "turn on SQL tracing"];
1556 :     $options->{help} = [0, "display command-line options"];
1557 :     $options->{user} = [$tkey, "tracing key"];
1558 :     $options->{background} = [0, "spool standard and error output"];
1559 :     # Create a parsing hash from the options hash. The parsing hash
1560 :     # contains the default values rather than the default value
1561 :     # and the description. While we're at it, we'll memorize the
1562 :     # length of the longest option name.
1563 :     my $longestName = 0;
1564 :     my %parseOptions = ();
1565 :     for my $key (keys %{$options}) {
1566 :     if (length $key > $longestName) {
1567 :     $longestName = length $key;
1568 :     }
1569 :     $parseOptions{$key} = $options->{$key}->[0];
1570 :     }
1571 :     # Parse the command line.
1572 :     my ($retOptions, @retParameters) = ParseCommand(\%parseOptions, @argv);
1573 :     # Get the logfile suffix.
1574 :     my $suffix = $retOptions->{user};
1575 :     # Check for background mode.
1576 :     if ($retOptions->{background}) {
1577 :     my $outFileName = "$FIG_Config::temp/out$suffix.log";
1578 :     my $errFileName = "$FIG_Config::temp/err$suffix.log";
1579 :     open STDOUT, ">$outFileName";
1580 :     open STDERR, ">$errFileName";
1581 :     # Check for phone support. If we have phone support and a phone number,
1582 :     # we want to turn it on.
1583 :     if ($ENV{PHONE} && defined($FIG_Config::phone)) {
1584 :     $retOptions->{phone} = $ENV{PHONE};
1585 :     }
1586 :     }
1587 :     # Now we want to set up tracing. First, we need to know if the user
1588 :     # wants emergency tracing.
1589 :     if ($retOptions->{trace} eq 'E') {
1590 :     ETracing($retOptions->{user});
1591 :     } else {
1592 :     # Here the tracing is controlled from the command line.
1593 :     my @cats = @{$categories};
1594 :     if ($retOptions->{sql}) {
1595 :     push @cats, "SQL";
1596 :     }
1597 :     # Add the default categories.
1598 :     push @cats, "Tracer";
1599 :     # Next, we create the category string by joining the categories.
1600 :     my $cats = join(" ", @cats);
1601 :     # Check to determine whether or not the caller wants to turn off tracing
1602 :     # to the standard output.
1603 :     my $traceLevel = $retOptions->{trace};
1604 :     my $textOKFlag = 1;
1605 :     if ($traceLevel =~ /^(.)-/) {
1606 :     $traceLevel = $1;
1607 :     $textOKFlag = 0;
1608 :     }
1609 :     # Now we set up the trace mode.
1610 :     my $traceMode;
1611 :     # Verify that we can open a file in the FIG temporary directory.
1612 :     my $traceFileName = "$FIG_Config::temp/trace$suffix.log";
1613 :     if (open TESTTRACE, ">$traceFileName") {
1614 :     # Here we can trace to a file.
1615 :     $traceMode = ">$traceFileName";
1616 :     if ($textOKFlag) {
1617 :     # Echo to standard output if the text-OK flag is set.
1618 :     $traceMode = "+$traceMode";
1619 :     }
1620 :     # Close the test file.
1621 :     close TESTTRACE;
1622 :     } else {
1623 :     # Here we can't trace to a file. We trace to the standard output if it's
1624 :     # okay, and the error log otherwise.
1625 :     if ($textOKFlag) {
1626 :     $traceMode = "TEXT";
1627 : parrello 1.12 } else {
1628 : parrello 1.93 $traceMode = "WARN";
1629 : parrello 1.12 }
1630 :     }
1631 : parrello 1.93 # Now set up the tracing.
1632 :     TSetup("$traceLevel $cats", $traceMode);
1633 :     }
1634 :     # Check for the "help" option. If it is specified, dump the command-line
1635 :     # options and exit the program.
1636 :     if ($retOptions->{help}) {
1637 :     $0 =~ m#[/\\](\w+)(\.pl)?$#i;
1638 :     print "$1 [options] $parmHelp\n";
1639 :     for my $key (sort keys %{$options}) {
1640 :     my $name = Pad($key, $longestName, 0, ' ');
1641 :     my $desc = $options->{$key}->[1];
1642 :     if ($options->{$key}->[0]) {
1643 :     $desc .= " (default " . $options->{$key}->[0] . ")";
1644 :     }
1645 :     print " $name $desc\n";
1646 : parrello 1.36 }
1647 : parrello 1.93 exit(0);
1648 :     }
1649 :     # Trace the options, if applicable.
1650 :     if (T(3)) {
1651 :     my @parms = grep { $retOptions->{$_} } keys %{$retOptions};
1652 :     Trace("Selected options: " . join(", ", sort @parms) . ".");
1653 : parrello 1.3 }
1654 : parrello 1.93 # Return the parsed parameters.
1655 :     return ($retOptions, @retParameters);
1656 : olson 1.1 }
1657 :    
1658 : parrello 1.93 =head3 ReadOptions
1659 : olson 1.1
1660 : parrello 1.93 my %options = Tracer::ReadOptions($fileName);
1661 : olson 1.1
1662 : parrello 1.93 Read a set of options from a file. Each option is encoded in a line of text that has the
1663 :     format
1664 : olson 1.1
1665 : parrello 1.93 I<optionName>C<=>I<optionValue>C<; >I<comment>
1666 : olson 1.1
1667 : parrello 1.93 The option name must consist entirely of letters, digits, and the punctuation characters
1668 :     C<.> and C<_>, and is case sensitive. Blank lines and lines in which the first nonblank
1669 :     character is a semi-colon will be ignored. The return hash will map each option name to
1670 :     the corresponding option value.
1671 : olson 1.1
1672 :     =over 4
1673 :    
1674 : parrello 1.93 =item fileName
1675 : olson 1.1
1676 : parrello 1.93 Name of the file containing the option data.
1677 : olson 1.1
1678 :     =item RETURN
1679 :    
1680 : parrello 1.93 Returns a hash mapping the option names specified in the file to their corresponding option
1681 :     value.
1682 : olson 1.1
1683 :     =back
1684 :    
1685 :     =cut
1686 :    
1687 : parrello 1.93 sub ReadOptions {
1688 : parrello 1.12 # Get the parameters.
1689 : parrello 1.93 my ($fileName) = @_;
1690 :     # Open the file.
1691 :     (open CONFIGFILE, "<$fileName") || Confess("Could not open option file $fileName.");
1692 :     # Count the number of records read.
1693 :     my ($records, $comments) = 0;
1694 :     # Create the return hash.
1695 :     my %retVal = ();
1696 :     # Loop through the file, accumulating key-value pairs.
1697 :     while (my $line = <CONFIGFILE>) {
1698 :     # Denote we've read a line.
1699 :     $records++;
1700 :     # Determine the line type.
1701 :     if ($line =~ /^\s*[\n\r]/) {
1702 :     # A blank line is a comment.
1703 :     $comments++;
1704 :     } elsif ($line =~ /^\s*([A-Za-z0-9_\.]+)=([^;]*);/) {
1705 :     # Here we have an option assignment.
1706 :     retVal{$1} = $2;
1707 :     } elsif ($line =~ /^\s*;/) {
1708 :     # Here we have a text comment.
1709 :     $comments++;
1710 : parrello 1.12 } else {
1711 : parrello 1.93 # Here we have an invalid line.
1712 :     Trace("Invalid option statement in record $records.") if T(0);
1713 : parrello 1.12 }
1714 :     }
1715 : parrello 1.93 # Return the hash created.
1716 :     return %retVal;
1717 : olson 1.1 }
1718 :    
1719 : parrello 1.93 =head3 GetOptions
1720 : parrello 1.9
1721 : parrello 1.93 Tracer::GetOptions(\%defaults, \%options);
1722 : parrello 1.9
1723 : parrello 1.93 Merge a specified set of options into a table of defaults. This method takes two hash references
1724 :     as input and uses the data from the second to update the first. If the second does not exist,
1725 :     there will be no effect. An error will be thrown if one of the entries in the second hash does not
1726 :     exist in the first.
1727 : parrello 1.9
1728 : parrello 1.93 Consider the following example.
1729 : parrello 1.9
1730 : parrello 1.93 my $optionTable = GetOptions({ dbType => 'mySQL', trace => 0 }, $options);
1731 : parrello 1.9
1732 : parrello 1.93 In this example, the variable B<$options> is expected to contain at most two options-- B<dbType> and
1733 :     B<trace>. The default database type is C<mySQL> and the default trace level is C<0>. If the value of
1734 :     B<$options> is C<< {dbType => 'Oracle'} >>, then the database type will be changed to C<Oracle> and
1735 :     the trace level will remain at 0. If B<$options> is undefined, then the database type and trace level
1736 :     will remain C<mySQL> and C<0>. If, on the other hand, B<$options> is defined as
1737 : parrello 1.9
1738 : parrello 1.93 {databaseType => 'Oracle'}
1739 : parrello 1.9
1740 : parrello 1.93 an error will occur because the B<databaseType> option does not exist.
1741 : parrello 1.9
1742 : parrello 1.93 =over 4
1743 : parrello 1.9
1744 : parrello 1.93 =item defaults
1745 : parrello 1.9
1746 : parrello 1.93 Table of default option values.
1747 : parrello 1.9
1748 : parrello 1.93 =item options
1749 : olson 1.1
1750 : parrello 1.93 Table of overrides, if any.
1751 : olson 1.1
1752 :     =item RETURN
1753 :    
1754 : parrello 1.93 Returns a reference to the default table passed in as the first parameter.
1755 : olson 1.1
1756 :     =back
1757 :    
1758 :     =cut
1759 :    
1760 : parrello 1.93 sub GetOptions {
1761 :     # Get the parameters.
1762 :     my ($defaults, $options) = @_;
1763 :     # Check for overrides.
1764 :     if ($options) {
1765 :     # Loop through the overrides.
1766 :     while (my ($option, $setting) = each %{$options}) {
1767 :     # Insure this override exists.
1768 :     if (!exists $defaults->{$option}) {
1769 :     croak "Unrecognized option $option encountered.";
1770 : parrello 1.12 } else {
1771 : parrello 1.93 # Apply the override.
1772 :     $defaults->{$option} = $setting;
1773 : parrello 1.12 }
1774 :     }
1775 :     }
1776 : parrello 1.93 # Return the merged table.
1777 :     return $defaults;
1778 : olson 1.1 }
1779 :    
1780 : parrello 1.93 =head3 MergeOptions
1781 : olson 1.1
1782 : parrello 1.93 Tracer::MergeOptions(\%table, \%defaults);
1783 : olson 1.1
1784 : parrello 1.93 Merge default values into a hash table. This method looks at the key-value pairs in the
1785 :     second (default) hash, and if a matching key is not found in the first hash, the default
1786 :     pair is copied in. The process is similar to L</GetOptions>, but there is no error-
1787 :     checking and no return value.
1788 : olson 1.1
1789 :     =over 4
1790 :    
1791 : parrello 1.93 =item table
1792 : olson 1.1
1793 : parrello 1.93 Hash table to be updated with the default values.
1794 : olson 1.1
1795 : parrello 1.93 =item defaults
1796 : olson 1.1
1797 : parrello 1.93 Default values to be merged into the first hash table if they are not already present.
1798 : olson 1.1
1799 :     =back
1800 :    
1801 :     =cut
1802 :    
1803 : parrello 1.93 sub MergeOptions {
1804 :     # Get the parameters.
1805 :     my ($table, $defaults) = @_;
1806 :     # Loop through the defaults.
1807 :     while (my ($key, $value) = each %{$defaults}) {
1808 :     if (!exists $table->{$key}) {
1809 :     $table->{$key} = $value;
1810 :     }
1811 : parrello 1.12 }
1812 : olson 1.1 }
1813 :    
1814 : parrello 1.93 =head3 ParseCommand
1815 : olson 1.1
1816 : parrello 1.93 my ($options, @arguments) = Tracer::ParseCommand(\%optionTable, @inputList);
1817 : olson 1.1
1818 : parrello 1.93 Parse a command line consisting of a list of parameters. The initial parameters may be option
1819 :     specifiers of the form C<->I<option> or C<->I<option>C<=>I<value>. The options are stripped
1820 :     off and merged into a table of default options. The remainder of the command line is
1821 :     returned as a list of positional arguments. For example, consider the following invocation.
1822 : olson 1.1
1823 : parrello 1.93 my ($options, @arguments) = ParseCommand({ errors => 0, logFile => 'trace.log'}, @words);
1824 : olson 1.1
1825 : parrello 1.93 In this case, the list @words will be treated as a command line and there are two options available,
1826 :     B<errors> and B<logFile>. If @words has the following format
1827 : olson 1.1
1828 : parrello 1.93 -logFile=error.log apple orange rutabaga
1829 : olson 1.1
1830 : parrello 1.93 then at the end of the invocation, C<$options> will be
1831 : olson 1.1
1832 : parrello 1.93 { errors => 0, logFile => 'error.log' }
1833 : olson 1.1
1834 : parrello 1.93 and C<@arguments> will contain
1835 : olson 1.1
1836 : parrello 1.93 apple orange rutabaga
1837 : olson 1.1
1838 : parrello 1.93 The parser allows for some escape sequences. See L</UnEscape> for a description. There is no
1839 :     support for quote characters. Options can be specified with single or double hyphens.
1840 : parrello 1.54
1841 :     =over 4
1842 :    
1843 : parrello 1.93 =item optionTable
1844 : parrello 1.54
1845 : parrello 1.93 Table of default options.
1846 : parrello 1.54
1847 : parrello 1.93 =item inputList
1848 : parrello 1.54
1849 : parrello 1.93 List of words on the command line.
1850 : parrello 1.54
1851 :     =item RETURN
1852 :    
1853 : parrello 1.93 Returns a reference to the option table and a list of the positional arguments.
1854 : parrello 1.54
1855 :     =back
1856 :    
1857 :     =cut
1858 :    
1859 : parrello 1.93 sub ParseCommand {
1860 : parrello 1.54 # Get the parameters.
1861 : parrello 1.93 my ($optionTable, @inputList) = @_;
1862 :     # Process any options in the input list.
1863 :     my %overrides = ();
1864 :     while ((@inputList > 0) && ($inputList[0] =~ /^--?/)) {
1865 :     # Get the current option.
1866 :     my $arg = shift @inputList;
1867 :     # Pull out the option name.
1868 :     $arg =~ /^--?([^=]*)/g;
1869 :     my $name = $1;
1870 :     # Check for an option value.
1871 :     if ($arg =~ /\G=(.*)$/g) {
1872 :     # Here we have a value for the option.
1873 :     $overrides{$name} = UnEscape($1);
1874 :     } else {
1875 :     # Here there is no value, so we use 1.
1876 :     $overrides{$name} = 1;
1877 :     }
1878 :     }
1879 :     # Merge the options into the defaults.
1880 :     GetOptions($optionTable, \%overrides);
1881 :     # Translate the remaining parameters.
1882 :     my @retVal = ();
1883 :     for my $inputParm (@inputList) {
1884 :     push @retVal, UnEscape($inputParm);
1885 : parrello 1.54 }
1886 : parrello 1.93 # Return the results.
1887 :     return ($optionTable, @retVal);
1888 : parrello 1.54 }
1889 :    
1890 : parrello 1.93
1891 :     =head2 File Utility Methods
1892 :    
1893 : olson 1.1 =head3 GetFile
1894 :    
1895 : parrello 1.92 my @fileContents = Tracer::GetFile($fileName);
1896 : olson 1.1
1897 : parrello 1.35 or
1898 :    
1899 : parrello 1.92 my $fileContents = Tracer::GetFile($fileName);
1900 : parrello 1.35
1901 :     Return the entire contents of a file. In list context, line-ends are removed and
1902 :     each line is a list element. In scalar context, line-ends are replaced by C<\n>.
1903 : olson 1.1
1904 :     =over 4
1905 :    
1906 :     =item fileName
1907 :    
1908 :     Name of the file to read.
1909 :    
1910 :     =item RETURN
1911 :    
1912 : parrello 1.6 In a list context, returns the entire file as a list with the line terminators removed.
1913 : parrello 1.39 In a scalar context, returns the entire file as a string. If an error occurs opening
1914 :     the file, an empty list will be returned.
1915 : olson 1.1
1916 :     =back
1917 :    
1918 :     =cut
1919 :    
1920 :     sub GetFile {
1921 : parrello 1.12 # Get the parameters.
1922 :     my ($fileName) = @_;
1923 :     # Declare the return variable.
1924 :     my @retVal = ();
1925 :     # Open the file for input.
1926 : parrello 1.60 my $handle = Open(undef, "<$fileName");
1927 :     # Read the whole file into the return variable, stripping off any terminator
1928 :     # characters.
1929 :     my $lineCount = 0;
1930 :     while (my $line = <$handle>) {
1931 :     $lineCount++;
1932 :     $line = Strip($line);
1933 :     push @retVal, $line;
1934 :     }
1935 :     # Close it.
1936 :     close $handle;
1937 :     my $actualLines = @retVal;
1938 : parrello 1.77 Trace("$actualLines lines read from file $fileName.") if T(File => 2);
1939 : parrello 1.12 # Return the file's contents in the desired format.
1940 : parrello 1.9 if (wantarray) {
1941 : parrello 1.12 return @retVal;
1942 : parrello 1.6 } else {
1943 :     return join "\n", @retVal;
1944 :     }
1945 : olson 1.1 }
1946 :    
1947 : parrello 1.60 =head3 PutFile
1948 :    
1949 : parrello 1.92 Tracer::PutFile($fileName, \@lines);
1950 : parrello 1.60
1951 :     Write out a file from a list of lines of text.
1952 :    
1953 :     =over 4
1954 :    
1955 :     =item fileName
1956 :    
1957 :     Name of the output file.
1958 :    
1959 :     =item lines
1960 :    
1961 :     Reference to a list of text lines. The lines will be written to the file in order, with trailing
1962 : parrello 1.66 new-line characters. Alternatively, may be a string, in which case the string will be written without
1963 :     modification.
1964 : parrello 1.60
1965 :     =back
1966 :    
1967 :     =cut
1968 :    
1969 :     sub PutFile {
1970 :     # Get the parameters.
1971 :     my ($fileName, $lines) = @_;
1972 :     # Open the output file.
1973 :     my $handle = Open(undef, ">$fileName");
1974 : parrello 1.77 # Count the lines written.
1975 : parrello 1.66 if (ref $lines ne 'ARRAY') {
1976 :     # Here we have a scalar, so we write it raw.
1977 :     print $handle $lines;
1978 : parrello 1.77 Trace("Scalar put to file $fileName.") if T(File => 3);
1979 : parrello 1.66 } else {
1980 :     # Write the lines one at a time.
1981 : parrello 1.77 my $count = 0;
1982 : parrello 1.66 for my $line (@{$lines}) {
1983 :     print $handle "$line\n";
1984 : parrello 1.77 $count++;
1985 : parrello 1.66 }
1986 : parrello 1.77 Trace("$count lines put to file $fileName.") if T(File => 3);
1987 : parrello 1.60 }
1988 :     # Close the output file.
1989 :     close $handle;
1990 :     }
1991 :    
1992 : parrello 1.93 =head3 ParseRecord
1993 : olson 1.1
1994 : parrello 1.93 my @fields = Tracer::ParseRecord($line);
1995 : olson 1.1
1996 : parrello 1.93 Parse a tab-delimited data line. The data line is split into field values. Embedded tab
1997 :     and new-line characters in the data line must be represented as C<\t> and C<\n>, respectively.
1998 :     These will automatically be converted.
1999 : olson 1.1
2000 :     =over 4
2001 :    
2002 : parrello 1.93 =item line
2003 :    
2004 :     Line of data containing the tab-delimited fields.
2005 :    
2006 :     =item RETURN
2007 : olson 1.1
2008 : parrello 1.93 Returns a list of the fields found in the data line.
2009 : olson 1.1
2010 :     =back
2011 :    
2012 :     =cut
2013 :    
2014 : parrello 1.93 sub ParseRecord {
2015 : parrello 1.12 # Get the parameter.
2016 : parrello 1.93 my ($line) = @_;
2017 :     # Remove the trailing new-line, if any.
2018 :     chomp $line;
2019 :     # Split the line read into pieces using the tab character.
2020 :     my @retVal = split /\t/, $line;
2021 :     # Trim and fix the escapes in each piece.
2022 :     for my $value (@retVal) {
2023 :     # Trim leading whitespace.
2024 :     $value =~ s/^\s+//;
2025 :     # Trim trailing whitespace.
2026 :     $value =~ s/\s+$//;
2027 :     # Delete the carriage returns.
2028 :     $value =~ s/\r//g;
2029 :     # Convert the escapes into their real values.
2030 :     $value =~ s/\\t/"\t"/ge;
2031 :     $value =~ s/\\n/"\n"/ge;
2032 :     }
2033 :     # Return the result.
2034 :     return @retVal;
2035 :     }
2036 :    
2037 :     =head3 Merge
2038 :    
2039 :     my @mergedList = Tracer::Merge(@inputList);
2040 :    
2041 :     Sort a list of strings and remove duplicates.
2042 :    
2043 :     =over 4
2044 :    
2045 :     =item inputList
2046 :    
2047 :     List of scalars to sort and merge.
2048 :    
2049 :     =item RETURN
2050 :    
2051 :     Returns a list containing the same elements sorted in ascending order with duplicates
2052 :     removed.
2053 :    
2054 :     =back
2055 :    
2056 :     =cut
2057 :    
2058 :     sub Merge {
2059 :     # Get the input list in sort order.
2060 :     my @inputList = sort @_;
2061 :     # Only proceed if the list has at least two elements.
2062 :     if (@inputList > 1) {
2063 :     # Now we want to move through the list splicing out duplicates.
2064 :     my $i = 0;
2065 :     while ($i < @inputList) {
2066 :     # Get the current entry.
2067 :     my $thisEntry = $inputList[$i];
2068 :     # Find out how many elements duplicate the current entry.
2069 :     my $j = $i + 1;
2070 :     my $dup1 = $i + 1;
2071 :     while ($j < @inputList && $inputList[$j] eq $thisEntry) { $j++; };
2072 :     # If the number is nonzero, splice out the duplicates found.
2073 :     if ($j > $dup1) {
2074 :     splice @inputList, $dup1, $j - $dup1;
2075 : parrello 1.14 }
2076 : parrello 1.93 # Now the element at position $dup1 is different from the element before it
2077 :     # at position $i. We push $i forward one position and start again.
2078 :     $i++;
2079 : parrello 1.14 }
2080 : parrello 1.12 }
2081 : parrello 1.93 # Return the merged list.
2082 :     return @inputList;
2083 : olson 1.1 }
2084 :    
2085 : parrello 1.93 =head3 Open
2086 : olson 1.1
2087 : parrello 1.93 my $handle = Open($fileHandle, $fileSpec, $message);
2088 : olson 1.1
2089 : parrello 1.93 Open a file.
2090 : olson 1.1
2091 : parrello 1.93 The I<$fileSpec> is essentially the second argument of the PERL C<open>
2092 :     function. The mode is specified using Unix-like shell information. So, for
2093 :     example,
2094 : olson 1.1
2095 : parrello 1.93 Open(\*LOGFILE, '>>/usr/spool/news/twitlog', "Could not open twit log.");
2096 : olson 1.1
2097 : parrello 1.93 would open for output appended to the specified file, and
2098 : olson 1.1
2099 : parrello 1.93 Open(\*DATASTREAM, "| sort -u >$outputFile", "Could not open $outputFile.");
2100 : olson 1.1
2101 : parrello 1.93 would open a pipe that sorts the records written and removes duplicates. Note
2102 :     the use of file handle syntax in the Open call. To use anonymous file handles,
2103 :     code as follows.
2104 : olson 1.1
2105 : parrello 1.93 my $logFile = Open(undef, '>>/usr/spool/news/twitlog', "Could not open twit log.");
2106 : olson 1.1
2107 : parrello 1.93 The I<$message> parameter is used if the open fails. If it is set to C<0>, then
2108 :     the open returns TRUE if successful and FALSE if an error occurred. Otherwise, a
2109 :     failed open will throw an exception and the third parameter will be used to construct
2110 :     an error message. If the parameter is omitted, a standard message is constructed
2111 :     using the file spec.
2112 : olson 1.1
2113 : parrello 1.93 Could not open "/usr/spool/news/twitlog"
2114 : olson 1.1
2115 : parrello 1.93 Note that the mode characters are automatically cleaned from the file name.
2116 :     The actual error message from the file system will be captured and appended to the
2117 :     message in any case.
2118 : parrello 1.6
2119 : parrello 1.93 Could not open "/usr/spool/news/twitlog": file not found.
2120 : parrello 1.6
2121 : parrello 1.93 In some versions of PERL the only error message we get is a number, which
2122 :     corresponds to the C++ C<errno> value.
2123 : parrello 1.6
2124 : parrello 1.93 Could not open "/usr/spool/news/twitlog": 6.
2125 : parrello 1.6
2126 : parrello 1.93 =over 4
2127 : parrello 1.6
2128 : parrello 1.93 =item fileHandle
2129 : parrello 1.6
2130 : parrello 1.93 File handle. If this parameter is C<undef>, a file handle will be generated
2131 :     and returned as the value of this method.
2132 : olson 1.1
2133 : parrello 1.93 =item fileSpec
2134 : olson 1.1
2135 : parrello 1.93 File name and mode, as per the PERL C<open> function.
2136 : olson 1.1
2137 : parrello 1.93 =item message (optional)
2138 : olson 1.1
2139 : parrello 1.93 Error message to use if the open fails. If omitted, a standard error message
2140 :     will be generated. In either case, the error information from the file system
2141 :     is appended to the message. To specify a conditional open that does not throw
2142 :     an error if it fails, use C<0>.
2143 : olson 1.1
2144 : parrello 1.93 =item RETURN
2145 : olson 1.1
2146 : parrello 1.93 Returns the name of the file handle assigned to the file, or C<undef> if the
2147 :     open failed.
2148 : olson 1.1
2149 :     =back
2150 :    
2151 :     =cut
2152 :    
2153 : parrello 1.93 sub Open {
2154 : parrello 1.12 # Get the parameters.
2155 : parrello 1.93 my ($fileHandle, $fileSpec, $message) = @_;
2156 :     # Attempt to open the file.
2157 :     my $rv = open $fileHandle, $fileSpec;
2158 :     # If the open failed, generate an error message.
2159 :     if (! $rv) {
2160 :     # Save the system error message.
2161 :     my $sysMessage = $!;
2162 :     # See if we need a default message.
2163 :     if (!$message) {
2164 :     # Clean any obvious mode characters and leading spaces from the
2165 :     # filename.
2166 :     my ($fileName) = FindNamePart($fileSpec);
2167 :     $message = "Could not open \"$fileName\"";
2168 :     }
2169 :     # Terminate with an error using the supplied message and the
2170 :     # error message from the file system.
2171 :     Confess("$message: $!");
2172 : parrello 1.12 }
2173 : parrello 1.93 # Return the file handle.
2174 :     return $fileHandle;
2175 : olson 1.1 }
2176 :    
2177 : parrello 1.93 =head3 FindNamePart
2178 :    
2179 :     my ($fileName, $start, $len) = Tracer::FindNamePart($fileSpec);
2180 :    
2181 :     Extract the portion of a file specification that contains the file name.
2182 :    
2183 :     A file specification is the string passed to an C<open> call. It specifies the file
2184 :     mode and name. In a truly complex situation, it can specify a pipe sequence. This
2185 :     method assumes that the file name is whatever follows the first angle bracket
2186 :     sequence. So, for example, in the following strings the file name is
2187 :     C</usr/fig/myfile.txt>.
2188 : parrello 1.5
2189 : parrello 1.93 >>/usr/fig/myfile.txt
2190 :     </usr/fig/myfile.txt
2191 :     | sort -u > /usr/fig/myfile.txt
2192 : parrello 1.5
2193 : parrello 1.93 If the method cannot find a file name using its normal methods, it will return the
2194 :     whole incoming string.
2195 : parrello 1.5
2196 :     =over 4
2197 :    
2198 : parrello 1.93 =item fileSpec
2199 : parrello 1.5
2200 : parrello 1.93 File specification string from which the file name is to be extracted.
2201 : parrello 1.5
2202 :     =item RETURN
2203 :    
2204 : parrello 1.93 Returns a three-element list. The first element contains the file name portion of
2205 :     the specified string, or the whole string if a file name cannot be found via normal
2206 :     methods. The second element contains the start position of the file name portion and
2207 :     the third element contains the length.
2208 : parrello 1.5
2209 :     =back
2210 :    
2211 :     =cut
2212 : parrello 1.93 #: Return Type $;
2213 :     sub FindNamePart {
2214 :     # Get the parameters.
2215 :     my ($fileSpec) = @_;
2216 :     # Default to the whole input string.
2217 :     my ($retVal, $pos, $len) = ($fileSpec, 0, length $fileSpec);
2218 :     # Parse out the file name if we can.
2219 :     if ($fileSpec =~ m/(<|>>?)(.+?)(\s*)$/) {
2220 :     $retVal = $2;
2221 :     $len = length $retVal;
2222 :     $pos = (length $fileSpec) - (length $3) - $len;
2223 : parrello 1.12 }
2224 : parrello 1.93 # Return the result.
2225 :     return ($retVal, $pos, $len);
2226 : parrello 1.5 }
2227 :    
2228 : parrello 1.93 =head3 OpenDir
2229 : parrello 1.5
2230 : parrello 1.93 my @files = OpenDir($dirName, $filtered, $flag);
2231 : parrello 1.5
2232 : parrello 1.93 Open a directory and return all the file names. This function essentially performs
2233 :     the functions of an C<opendir> and C<readdir>. If the I<$filtered> parameter is
2234 :     set to TRUE, all filenames beginning with a period (C<.>), dollar sign (C<$>),
2235 :     or pound sign (C<#>) and all filenames ending with a tilde C<~>) will be
2236 :     filtered out of the return list. If the directory does not open and I<$flag> is not
2237 :     set, an exception is thrown. So, for example,
2238 : parrello 1.5
2239 : parrello 1.93 my @files = OpenDir("/Volumes/fig/contigs", 1);
2240 : parrello 1.5
2241 : parrello 1.93 is effectively the same as
2242 : parrello 1.5
2243 : parrello 1.93 opendir(TMP, "/Volumes/fig/contigs") || Confess("Could not open /Volumes/fig/contigs.");
2244 :     my @files = grep { $_ !~ /^[\.\$\#]/ && $_ !~ /~$/ } readdir(TMP);
2245 : parrello 1.5
2246 : parrello 1.93 Similarly, the following code
2247 : parrello 1.5
2248 : parrello 1.93 my @files = grep { $_ =~ /^\d/ } OpenDir("/Volumes/fig/orgs", 0, 1);
2249 : parrello 1.5
2250 : parrello 1.93 Returns the names of all files in C</Volumes/fig/orgs> that begin with digits and
2251 :     automatically returns an empty list if the directory fails to open.
2252 : parrello 1.5
2253 : parrello 1.93 =over 4
2254 : parrello 1.5
2255 : parrello 1.93 =item dirName
2256 : parrello 1.5
2257 : parrello 1.93 Name of the directory to open.
2258 : parrello 1.5
2259 : parrello 1.93 =item filtered
2260 : parrello 1.5
2261 : parrello 1.93 TRUE if files whose names begin with a period (C<.>) should be automatically removed
2262 :     from the list, else FALSE.
2263 : parrello 1.5
2264 : parrello 1.93 =item flag
2265 : parrello 1.5
2266 : parrello 1.93 TRUE if a failure to open is okay, else FALSE
2267 : parrello 1.5
2268 : parrello 1.93 =back
2269 : parrello 1.5
2270 : parrello 1.93 =cut
2271 :     #: Return Type @;
2272 :     sub OpenDir {
2273 :     # Get the parameters.
2274 :     my ($dirName, $filtered, $flag) = @_;
2275 :     # Declare the return variable.
2276 :     my @retVal = ();
2277 :     # Open the directory.
2278 :     if (opendir(my $dirHandle, $dirName)) {
2279 :     # The directory opened successfully. Get the appropriate list according to the
2280 :     # strictures of the filter parameter.
2281 :     if ($filtered) {
2282 :     @retVal = grep { $_ !~ /^[\.\$\#]/ && $_ !~ /~$/ } readdir $dirHandle;
2283 :     } else {
2284 :     @retVal = readdir $dirHandle;
2285 :     }
2286 :     } elsif (! $flag) {
2287 :     # Here the directory would not open and it's considered an error.
2288 :     Confess("Could not open directory $dirName.");
2289 :     }
2290 :     # Return the result.
2291 :     return @retVal;
2292 :     }
2293 : parrello 1.5
2294 :    
2295 : parrello 1.93 =head3 Insure
2296 : parrello 1.5
2297 : parrello 1.93 Insure($dirName, $chmod);
2298 : parrello 1.5
2299 : parrello 1.93 Insure a directory is present.
2300 : parrello 1.5
2301 : parrello 1.93 =over 4
2302 : parrello 1.5
2303 : parrello 1.93 =item dirName
2304 : olson 1.1
2305 : parrello 1.93 Name of the directory to check. If it does not exist, it will be created.
2306 : parrello 1.7
2307 : parrello 1.93 =item chmod (optional)
2308 : parrello 1.7
2309 : parrello 1.93 Security privileges to be given to the directory if it is created.
2310 : parrello 1.7
2311 : parrello 1.93 =back
2312 : parrello 1.7
2313 :     =cut
2314 :    
2315 : parrello 1.93 sub Insure {
2316 :     my ($dirName, $chmod) = @_;
2317 :     if (! -d $dirName) {
2318 :     Trace("Creating $dirName directory.") if T(2);
2319 :     eval {
2320 :     mkpath $dirName;
2321 :     # If we have permissions specified, set them here.
2322 :     if (defined($chmod)) {
2323 :     chmod $chmod, $dirName;
2324 :     }
2325 :     };
2326 :     if ($@) {
2327 :     Confess("Error creating $dirName: $@");
2328 :     }
2329 : parrello 1.12 }
2330 : parrello 1.9 }
2331 :    
2332 : parrello 1.93 =head3 ChDir
2333 : parrello 1.9
2334 : parrello 1.93 ChDir($dirName);
2335 : parrello 1.9
2336 : parrello 1.93 Change to the specified directory.
2337 : parrello 1.9
2338 :     =over 4
2339 :    
2340 : parrello 1.93 =item dirName
2341 : parrello 1.9
2342 : parrello 1.93 Name of the directory to which we want to change.
2343 : parrello 1.9
2344 :     =back
2345 :    
2346 :     =cut
2347 :    
2348 : parrello 1.93 sub ChDir {
2349 :     my ($dirName) = @_;
2350 :     if (! -d $dirName) {
2351 :     Confess("Cannot change to directory $dirName: no such directory.");
2352 :     } else {
2353 :     Trace("Changing to directory $dirName.") if T(File => 4);
2354 :     my $okFlag = chdir $dirName;
2355 :     if (! $okFlag) {
2356 :     Confess("Error switching to directory $dirName.");
2357 :     }
2358 :     }
2359 : parrello 1.9 }
2360 :    
2361 : parrello 1.93 =head3 SetPermissions
2362 :    
2363 :     Tracer::SetPermissions($dirName, $group, $mask, %otherMasks);
2364 : parrello 1.9
2365 : parrello 1.93 Set the permissions for a directory and all the files and folders inside it.
2366 :     In addition, the group ownership will be changed to the specified value.
2367 : parrello 1.9
2368 : parrello 1.93 This method is more vulnerable than most to permission and compatability
2369 :     problems, so it does internal error recovery.
2370 : parrello 1.9
2371 :     =over 4
2372 :    
2373 : parrello 1.93 =item dirName
2374 :    
2375 :     Name of the directory to process.
2376 :    
2377 :     =item group
2378 : parrello 1.9
2379 : parrello 1.93 Name of the group to be assigned.
2380 : parrello 1.9
2381 : parrello 1.93 =item mask
2382 : parrello 1.9
2383 : parrello 1.93 Permission mask. Bits that are C<1> in this mask will be ORed into the
2384 :     permission bits of any file or directory that does not already have them
2385 :     set to 1.
2386 : parrello 1.9
2387 : parrello 1.93 =item otherMasks
2388 : parrello 1.9
2389 : parrello 1.93 Map of search patterns to permission masks. If a directory name matches
2390 :     one of the patterns, that directory and all its members and subdirectories
2391 :     will be assigned the new pattern. For example, the following would
2392 :     assign 01664 to most files, but would use 01777 for directories named C<tmp>.
2393 : parrello 1.9
2394 : parrello 1.93 Tracer::SetPermissions($dirName, 'fig', 01664, '^tmp$' => 01777);
2395 : parrello 1.9
2396 : parrello 1.93 The list is ordered, so the following would use 0777 for C<tmp1> and
2397 :     0666 for C<tmp>, C<tmp2>, or C<tmp3>.
2398 : parrello 1.22
2399 : parrello 1.93 Tracer::SetPermissions($dirName, 'fig', 01664, '^tmp1' => 0777,
2400 :     '^tmp' => 0666);
2401 : parrello 1.9
2402 : parrello 1.93 Note that the pattern matches are all case-insensitive, and only directory
2403 :     names are matched, not file names.
2404 : parrello 1.9
2405 :     =back
2406 :    
2407 :     =cut
2408 :    
2409 : parrello 1.93 sub SetPermissions {
2410 : parrello 1.12 # Get the parameters.
2411 : parrello 1.93 my ($dirName, $group, $mask, @otherMasks) = @_;
2412 :     # Set up for error recovery.
2413 :     eval {
2414 :     # Switch to the specified directory.
2415 :     ChDir($dirName);
2416 :     # Get the group ID.
2417 :     my $gid = getgrnam($group);
2418 :     # Get the mask for tracing.
2419 :     my $traceMask = sprintf("%04o", $mask) . "($mask)";
2420 :     Trace("Fixing permissions for directory $dirName using group $group($gid) and mask $traceMask.") if T(File => 2);
2421 :     my $fixCount = 0;
2422 :     my $lookCount = 0;
2423 :     # @dirs will be a stack of directories to be processed.
2424 :     my @dirs = (getcwd());
2425 :     while (scalar(@dirs) > 0) {
2426 :     # Get the current directory.
2427 :     my $dir = pop @dirs;
2428 :     # Check for a match to one of the specified directory names. To do
2429 :     # that, we need to pull the individual part of the name off of the
2430 :     # whole path.
2431 :     my $simpleName = $dir;
2432 :     if ($dir =~ m!/([^/]+)$!) {
2433 :     $simpleName = $1;
2434 :     }
2435 :     Trace("Simple directory name for $dir is $simpleName.") if T(File => 4);
2436 :     # Search for a match.
2437 :     my $match = 0;
2438 :     my $i;
2439 :     for ($i = 0; $i < $#otherMasks && ! $match; $i += 2) {
2440 :     my $pattern = $otherMasks[$i];
2441 :     if ($simpleName =~ /$pattern/i) {
2442 :     $match = 1;
2443 :     }
2444 :     }
2445 :     # Check for a match. Note we use $i-1 because the loop added 2
2446 :     # before terminating due to the match.
2447 :     if ($match && $otherMasks[$i-1] != $mask) {
2448 :     # This directory matches one of the incoming patterns, and it's
2449 :     # a different mask, so we process it recursively with that mask.
2450 :     SetPermissions($dir, $group, $otherMasks[$i-1], @otherMasks);
2451 :     } else {
2452 :     # Here we can process normally. Get all of the non-hidden members.
2453 :     my @submems = OpenDir($dir, 1);
2454 :     for my $submem (@submems) {
2455 :     # Get the full name.
2456 :     my $thisMem = "$dir/$submem";
2457 :     Trace("Checking member $thisMem.") if T(4);
2458 :     $lookCount++;
2459 :     if ($lookCount % 1000 == 0) {
2460 :     Trace("$lookCount members examined. Current is $thisMem. Mask is $traceMask") if T(File => 3);
2461 :     }
2462 :     # Fix the group.
2463 :     chown -1, $gid, $thisMem;
2464 :     # Insure this member is not a symlink.
2465 :     if (! -l $thisMem) {
2466 :     # Get its info.
2467 :     my $fileInfo = stat $thisMem;
2468 :     # Only proceed if we got the info. Otherwise, it's a hard link
2469 :     # and we want to skip it anyway.
2470 :     if ($fileInfo) {
2471 :     my $fileMode = $fileInfo->mode;
2472 :     if (($fileMode & $mask) != $mask) {
2473 :     # Fix this member.
2474 :     $fileMode |= $mask;
2475 :     chmod $fileMode, $thisMem;
2476 :     $fixCount++;
2477 :     }
2478 :     # If it's a subdirectory, stack it.
2479 :     if (-d $thisMem) {
2480 :     push @dirs, $thisMem;
2481 :     }
2482 :     }
2483 :     }
2484 :     }
2485 :     }
2486 : parrello 1.12 }
2487 : parrello 1.93 Trace("$lookCount files and directories processed, $fixCount fixed.") if T(File => 2);
2488 :     };
2489 :     # Check for an error.
2490 :     if ($@) {
2491 :     Confess("SetPermissions error: $@");
2492 : parrello 1.12 }
2493 : parrello 1.7 }
2494 :    
2495 : parrello 1.93 =head3 GetLine
2496 : parrello 1.29
2497 : parrello 1.93 my @data = Tracer::GetLine($handle);
2498 : parrello 1.15
2499 : parrello 1.93 Read a line of data from a tab-delimited file.
2500 : parrello 1.15
2501 :     =over 4
2502 :    
2503 : parrello 1.93 =item handle
2504 : parrello 1.15
2505 : parrello 1.93 Open file handle from which to read.
2506 : parrello 1.15
2507 :     =item RETURN
2508 :    
2509 : parrello 1.93 Returns a list of the fields in the record read. The fields are presumed to be
2510 :     tab-delimited. If we are at the end of the file, then an empty list will be
2511 :     returned. If an empty line is read, a single list item consisting of a null
2512 :     string will be returned.
2513 : parrello 1.15
2514 :     =back
2515 :    
2516 :     =cut
2517 : parrello 1.93
2518 :     sub GetLine {
2519 : parrello 1.15 # Get the parameters.
2520 : parrello 1.93 my ($handle) = @_;
2521 :     # Declare the return variable.
2522 :     my @retVal = ();
2523 :     Trace("File position is " . tell($handle) . ". EOF flag is " . eof($handle) . ".") if T(File => 4);
2524 :     # Read from the file.
2525 :     my $line = <$handle>;
2526 :     # Only proceed if we found something.
2527 :     if (defined $line) {
2528 :     # Remove the new-line. We are a bit over-cautious here because the file may be coming in via an
2529 :     # upload control and have a nonstandard EOL combination.
2530 :     $line =~ s/(\r|\n)+$//;
2531 :     # Here we do some fancy tracing to help in debugging complicated EOL marks.
2532 :     if (T(File => 4)) {
2533 :     my $escapedLine = $line;
2534 :     $escapedLine =~ s/\n/\\n/g;
2535 :     $escapedLine =~ s/\r/\\r/g;
2536 :     $escapedLine =~ s/\t/\\t/g;
2537 :     Trace("Line read: -->$escapedLine<--");
2538 :     }
2539 :     # If the line is empty, return a single empty string; otherwise, parse
2540 :     # it into fields.
2541 :     if ($line eq "") {
2542 :     push @retVal, "";
2543 :     } else {
2544 :     push @retVal, split /\t/,$line;
2545 :     }
2546 :     } else {
2547 :     # Trace the reason the read failed.
2548 :     Trace("End of file: $!") if T(File => 3);
2549 : parrello 1.15 }
2550 : parrello 1.93 # Return the result.
2551 :     return @retVal;
2552 : parrello 1.15 }
2553 :    
2554 : parrello 1.93 =head3 PutLine
2555 :    
2556 :     Tracer::PutLine($handle, \@fields, $eol);
2557 : parrello 1.35
2558 : parrello 1.93 Write a line of data to a tab-delimited file. The specified field values will be
2559 :     output in tab-separated form, with a trailing new-line.
2560 : parrello 1.35
2561 : parrello 1.93 =over 4
2562 : parrello 1.35
2563 : parrello 1.93 =item handle
2564 : parrello 1.69
2565 : parrello 1.93 Output file handle.
2566 : parrello 1.69
2567 : parrello 1.93 =item fields
2568 : parrello 1.69
2569 : parrello 1.93 List of field values.
2570 : parrello 1.69
2571 : parrello 1.93 =item eol (optional)
2572 : parrello 1.69
2573 : parrello 1.93 End-of-line character (default is "\n").
2574 : parrello 1.69
2575 :     =back
2576 : parrello 1.35
2577 :     =cut
2578 :    
2579 : parrello 1.93 sub PutLine {
2580 : parrello 1.69 # Get the parameters.
2581 : parrello 1.93 my ($handle, $fields, $eol) = @_;
2582 :     # Write the data.
2583 :     print $handle join("\t", @{$fields}) . ($eol || "\n");
2584 : parrello 1.69 }
2585 :    
2586 :    
2587 :    
2588 : parrello 1.93 =head2 Other Useful Methods
2589 :    
2590 :     =head3 ParseParm
2591 :    
2592 :     my $listValue = Tracer::ParseParm($string);
2593 :    
2594 :     Convert a parameter into a list reference. If the parameter is undefined,
2595 :     an undefined value will be returned. Otherwise, it will be parsed as a
2596 :     comma-separated list of values.
2597 : parrello 1.69
2598 :     =over 4
2599 :    
2600 : parrello 1.93 =item string
2601 :    
2602 :     Incoming string.
2603 :    
2604 :     =item RETURN
2605 : parrello 1.69
2606 : parrello 1.93 Returns a reference to a list of values, or C<undef> if the incoming value
2607 :     was undefined.
2608 : parrello 1.69
2609 :     =back
2610 :    
2611 :     =cut
2612 :    
2613 : parrello 1.93 sub ParseParm {
2614 :     # Get the parameters.
2615 :     my ($string) = @_;
2616 :     # Declare the return variable.
2617 :     my $retVal;
2618 :     # Check for data.
2619 :     if (defined $string) {
2620 :     # We have some, so split it into a list.
2621 :     $retVal = [ split /\s*,\s*/, $string];
2622 : parrello 1.72 }
2623 : parrello 1.93 # Return the result.
2624 :     return $retVal;
2625 : parrello 1.69 }
2626 :    
2627 :    
2628 :    
2629 :    
2630 : parrello 1.93 =head3 Now
2631 : parrello 1.69
2632 : parrello 1.93 my $string = Tracer::Now();
2633 : parrello 1.69
2634 : parrello 1.93 Return a displayable time stamp containing the local time.
2635 : parrello 1.69
2636 : parrello 1.93 =cut
2637 : parrello 1.69
2638 : parrello 1.93 sub Now {
2639 :     my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
2640 :     my $retVal = _p2($mon+1) . "/" . _p2($mday) . "/" . ($year + 1900) . " " .
2641 :     _p2($hour) . ":" . _p2($min) . ":" . _p2($sec);
2642 :     return $retVal;
2643 :     }
2644 : parrello 1.69
2645 : parrello 1.93 # Pad a number to 2 digits.
2646 :     sub _p2 {
2647 :     my ($value) = @_;
2648 :     $value = "0$value" if ($value < 10);
2649 :     return $value;
2650 : parrello 1.69 }
2651 :    
2652 : parrello 1.93 =head3 Escape
2653 : parrello 1.69
2654 : parrello 1.93 my $codedString = Tracer::Escape($realString);
2655 : parrello 1.69
2656 : parrello 1.93 Escape a string for use in a command. Tabs will be replaced by C<\t>, new-lines
2657 :     replaced by C<\n>, carriage returns will be deleted, and backslashes will be doubled. The
2658 :     result is to reverse the effect of L</UnEscape>.
2659 : parrello 1.69
2660 :     =over 4
2661 :    
2662 : parrello 1.93 =item realString
2663 : parrello 1.69
2664 : parrello 1.93 String to escape.
2665 : parrello 1.69
2666 :     =item RETURN
2667 :    
2668 : parrello 1.93 Escaped equivalent of the real string.
2669 :    
2670 :     =back
2671 :    
2672 :     =cut
2673 :    
2674 :     sub Escape {
2675 :     # Get the parameter.
2676 :     my ($realString) = @_;
2677 :     # Initialize the return variable.
2678 :     my $retVal = "";
2679 :     # Loop through the parameter string, looking for sequences to escape.
2680 :     while (length $realString > 0) {
2681 :     # Look for the first sequence to escape.
2682 :     if ($realString =~ /^(.*?)([\n\t\r\\])/) {
2683 :     # Here we found it. The text preceding the sequence is in $1. The sequence
2684 :     # itself is in $2. First, move the clear text to the return variable.
2685 :     $retVal .= $1;
2686 :     # Strip the processed section off the real string.
2687 :     $realString = substr $realString, (length $2) + (length $1);
2688 :     # Get the matched character.
2689 :     my $char = $2;
2690 :     # If we have a CR, we are done.
2691 :     if ($char ne "\r") {
2692 :     # It's not a CR, so encode the escape sequence.
2693 :     $char =~ tr/\t\n/tn/;
2694 :     $retVal .= "\\" . $char;
2695 :     }
2696 :     } else {
2697 :     # Here there are no more escape sequences. The rest of the string is
2698 :     # transferred unmodified.
2699 :     $retVal .= $realString;
2700 :     $realString = "";
2701 :     }
2702 :     }
2703 :     # Return the result.
2704 :     return $retVal;
2705 : parrello 1.69 }
2706 :    
2707 : parrello 1.93 =head3 UnEscape
2708 : parrello 1.69
2709 : parrello 1.93 my $realString = Tracer::UnEscape($codedString);
2710 : parrello 1.69
2711 : parrello 1.93 Replace escape sequences with their actual equivalents. C<\t> will be replaced by
2712 :     a tab, C<\n> by a new-line character, and C<\\> by a backslash. C<\r> codes will
2713 :     be deleted.
2714 : parrello 1.69
2715 :     =over 4
2716 :    
2717 : parrello 1.93 =item codedString
2718 : parrello 1.69
2719 : parrello 1.93 String to un-escape.
2720 : parrello 1.69
2721 :     =item RETURN
2722 :    
2723 : parrello 1.93 Returns a copy of the original string with the escape sequences converted to their actual
2724 :     values.
2725 : parrello 1.69
2726 :     =back
2727 :    
2728 :     =cut
2729 :    
2730 : parrello 1.93 sub UnEscape {
2731 :     # Get the parameter.
2732 :     my ($codedString) = @_;
2733 :     # Initialize the return variable.
2734 :     my $retVal = "";
2735 :     # Only proceed if the incoming string is nonempty.
2736 :     if (defined $codedString) {
2737 :     # Loop through the parameter string, looking for escape sequences. We can't do
2738 :     # translating because it causes problems with the escaped slash. ("\\t" becomes
2739 :     # "\<tab>" no matter what we do.)
2740 :     while (length $codedString > 0) {
2741 :     # Look for the first escape sequence.
2742 :     if ($codedString =~ /^(.*?)\\(\\|n|t|r)/) {
2743 :     # Here we found it. The text preceding the sequence is in $1. The sequence
2744 :     # itself is in $2. First, move the clear text to the return variable.
2745 :     $retVal .= $1;
2746 :     $codedString = substr $codedString, (2 + length $1);
2747 :     # Get the escape value.
2748 :     my $char = $2;
2749 :     # If we have a "\r", we are done.
2750 :     if ($char ne 'r') {
2751 :     # Here it's not an 'r', so we convert it.
2752 :     $char =~ tr/\\tn/\\\t\n/;
2753 :     $retVal .= $char;
2754 :     }
2755 :     } else {
2756 :     # Here there are no more escape sequences. The rest of the string is
2757 :     # transferred unmodified.
2758 :     $retVal .= $codedString;
2759 :     $codedString = "";
2760 :     }
2761 :     }
2762 : parrello 1.35 }
2763 : parrello 1.69 # Return the result.
2764 :     return $retVal;
2765 :     }
2766 :    
2767 : parrello 1.93 =head3 Percent
2768 : parrello 1.69
2769 : parrello 1.93 my $percent = Tracer::Percent($number, $base);
2770 : parrello 1.69
2771 : parrello 1.93 Returns the percent of the base represented by the given number. If the base
2772 :     is zero, returns zero.
2773 : parrello 1.69
2774 :     =over 4
2775 :    
2776 : parrello 1.93 =item number
2777 : parrello 1.69
2778 : parrello 1.93 Percent numerator.
2779 : parrello 1.69
2780 : parrello 1.93 =item base
2781 : parrello 1.69
2782 : parrello 1.93 Percent base.
2783 : parrello 1.69
2784 : parrello 1.93 =item RETURN
2785 : parrello 1.69
2786 : parrello 1.93 Returns the percentage of the base represented by the numerator.
2787 : parrello 1.69
2788 :     =back
2789 :    
2790 :     =cut
2791 :    
2792 : parrello 1.93 sub Percent {
2793 : parrello 1.69 # Get the parameters.
2794 : parrello 1.93 my ($number, $base) = @_;
2795 :     # Declare the return variable.
2796 :     my $retVal = 0;
2797 :     # Compute the percent.
2798 :     if ($base != 0) {
2799 :     $retVal = $number * 100 / $base;
2800 :     }
2801 :     # Return the result.
2802 :     return $retVal;
2803 : parrello 1.69 }
2804 :    
2805 : parrello 1.93 =head3 Min
2806 : parrello 1.69
2807 : parrello 1.93 my $min = Min($value1, $value2, ... $valueN);
2808 : parrello 1.69
2809 : parrello 1.93 Return the minimum argument. The arguments are treated as numbers.
2810 : parrello 1.69
2811 :     =over 4
2812 :    
2813 : parrello 1.93 =item $value1, $value2, ... $valueN
2814 : parrello 1.69
2815 : parrello 1.93 List of numbers to compare.
2816 : parrello 1.69
2817 :     =item RETURN
2818 :    
2819 : parrello 1.93 Returns the lowest number in the list.
2820 : parrello 1.69
2821 :     =back
2822 :    
2823 :     =cut
2824 :    
2825 : parrello 1.93 sub Min {
2826 :     # Get the parameters. Note that we prime the return value with the first parameter.
2827 :     my ($retVal, @values) = @_;
2828 :     # Loop through the remaining parameters, looking for the lowest.
2829 :     for my $value (@values) {
2830 :     if ($value < $retVal) {
2831 :     $retVal = $value;
2832 : parrello 1.72 }
2833 :     }
2834 : parrello 1.93 # Return the minimum found.
2835 : parrello 1.70 return $retVal;
2836 : parrello 1.35 }
2837 :    
2838 : parrello 1.93 =head3 Max
2839 :    
2840 :     my $max = Max($value1, $value2, ... $valueN);
2841 : parrello 1.69
2842 : parrello 1.93 Return the maximum argument. The arguments are treated as numbers.
2843 : parrello 1.65
2844 : parrello 1.93 =over 4
2845 : parrello 1.65
2846 : parrello 1.93 =item $value1, $value2, ... $valueN
2847 : parrello 1.65
2848 : parrello 1.93 List of numbers to compare.
2849 : parrello 1.65
2850 : parrello 1.93 =item RETURN
2851 : parrello 1.65
2852 : parrello 1.93 Returns the highest number in the list.
2853 : parrello 1.65
2854 :     =back
2855 :    
2856 :     =cut
2857 :    
2858 : parrello 1.93 sub Max {
2859 :     # Get the parameters. Note that we prime the return value with the first parameter.
2860 :     my ($retVal, @values) = @_;
2861 :     # Loop through the remaining parameters, looking for the highest.
2862 :     for my $value (@values) {
2863 :     if ($value > $retVal) {
2864 :     $retVal = $value;
2865 : parrello 1.65 }
2866 :     }
2867 : parrello 1.93 # Return the maximum found.
2868 :     return $retVal;
2869 : parrello 1.65 }
2870 :    
2871 : parrello 1.93 =head3 DebugMode
2872 : parrello 1.35
2873 : parrello 1.93 if (Tracer::DebugMode) { ...code... }
2874 : parrello 1.35
2875 : parrello 1.93 Return TRUE if debug mode has been turned on, else abort.
2876 : parrello 1.35
2877 : parrello 1.93 Certain CGI scripts are too dangerous to exist in the production
2878 :     environment. This method provides a simple way to prevent them
2879 :     from working unless they are explicitly turned on by creating a password
2880 :     cookie via the B<SetPassword> script. If debugging mode
2881 :     is not turned on, an error will occur.
2882 : parrello 1.37
2883 : parrello 1.35 =cut
2884 :    
2885 : parrello 1.93 sub DebugMode {
2886 :     # Declare the return variable.
2887 :     my $retVal = 0;
2888 :     # Check the debug configuration.
2889 :     my $password = CGI::cookie("DebugMode");
2890 :     my $encrypted = Digest::MD5::md5_hex($password);
2891 :     if ($encrypted eq "252dec43280e0c0d6a75ffcec486e61d") {
2892 :     $retVal = 1;
2893 : parrello 1.35 } else {
2894 : parrello 1.93 # Here debug mode is off, so we generate an error.
2895 :     Confess("Cannot use this facility without logging in.");
2896 : parrello 1.35 }
2897 : parrello 1.93 # Return the determination indicator.
2898 :     return $retVal;
2899 : parrello 1.35 }
2900 :    
2901 : parrello 1.93 =head3 Strip
2902 : parrello 1.37
2903 : parrello 1.93 my $string = Tracer::Strip($line);
2904 : parrello 1.37
2905 : parrello 1.93 Strip all line terminators off a string. This is necessary when dealing with files
2906 :     that may have been transferred back and forth several times among different
2907 :     operating environments.
2908 : parrello 1.37
2909 :     =over 4
2910 :    
2911 : parrello 1.93 =item line
2912 : parrello 1.37
2913 : parrello 1.93 Line of text to be stripped.
2914 : parrello 1.37
2915 : parrello 1.93 =item RETURN
2916 : parrello 1.91
2917 : parrello 1.93 The same line of text with all the line-ending characters chopped from the end.
2918 : parrello 1.91
2919 : parrello 1.37 =back
2920 :    
2921 :     =cut
2922 :    
2923 : parrello 1.93 sub Strip {
2924 :     # Get a copy of the parameter string.
2925 :     my ($string) = @_;
2926 :     my $retVal = (defined $string ? $string : "");
2927 :     # Strip the line terminator characters.
2928 :     $retVal =~ s/(\r|\n)+$//g;
2929 :     # Return the result.
2930 :     return $retVal;
2931 : parrello 1.43 }
2932 :    
2933 : parrello 1.93 =head3 Pad
2934 : parrello 1.43
2935 : parrello 1.93 my $paddedString = Tracer::Pad($string, $len, $left, $padChar);
2936 : parrello 1.43
2937 : parrello 1.93 Pad a string to a specified length. The pad character will be a
2938 :     space, and the padding will be on the right side unless specified
2939 :     in the third parameter.
2940 : parrello 1.43
2941 :     =over 4
2942 :    
2943 : parrello 1.93 =item string
2944 :    
2945 :     String to be padded.
2946 :    
2947 :     =item len
2948 :    
2949 :     Desired length of the padded string.
2950 :    
2951 :     =item left (optional)
2952 :    
2953 :     TRUE if the string is to be left-padded; otherwise it will be padded on the right.
2954 :    
2955 :     =item padChar (optional)
2956 :    
2957 :     Character to use for padding. The default is a space.
2958 :    
2959 :     =item RETURN
2960 : parrello 1.43
2961 : parrello 1.93 Returns a copy of the original string with the pad character added to the
2962 :     specified end so that it achieves the desired length.
2963 : parrello 1.43
2964 :     =back
2965 :    
2966 :     =cut
2967 :    
2968 : parrello 1.93 sub Pad {
2969 :     # Get the parameters.
2970 :     my ($string, $len, $left, $padChar) = @_;
2971 :     # Compute the padding character.
2972 :     if (! defined $padChar) {
2973 :     $padChar = " ";
2974 :     }
2975 :     # Compute the number of spaces needed.
2976 :     my $needed = $len - length $string;
2977 :     # Copy the string into the return variable.
2978 :     my $retVal = $string;
2979 :     # Only proceed if padding is needed.
2980 :     if ($needed > 0) {
2981 :     # Create the pad string.
2982 :     my $pad = $padChar x $needed;
2983 :     # Affix it to the return value.
2984 :     if ($left) {
2985 :     $retVal = $pad . $retVal;
2986 :     } else {
2987 :     $retVal .= $pad;
2988 : parrello 1.43 }
2989 : parrello 1.37 }
2990 : parrello 1.93 # Return the result.
2991 :     return $retVal;
2992 : parrello 1.37 }
2993 :    
2994 : parrello 1.93 =head3 EOF
2995 :    
2996 :     This is a constant that is lexically greater than any useful string.
2997 :    
2998 :     =cut
2999 :    
3000 :     sub EOF {
3001 :     return "\xFF\xFF\xFF\xFF\xFF";
3002 :     }
3003 : parrello 1.59
3004 : parrello 1.93 =head3 TICK
3005 : parrello 1.59
3006 : parrello 1.93 my @results = TICK($commandString);
3007 : parrello 1.59
3008 : parrello 1.93 Perform a back-tick operation on a command. If this is a Windows environment, any leading
3009 :     dot-slash (C<./> will be removed. So, for example, if you were doing
3010 : parrello 1.59
3011 : parrello 1.93 `./protein.cgi`
3012 : parrello 1.59
3013 : parrello 1.93 from inside a CGI script, it would work fine in Unix, but would issue an error message
3014 :     in Windows complaining that C<'.'> is not a valid command. If instead you code
3015 : parrello 1.59
3016 : parrello 1.93 TICK("./protein.cgi")
3017 : parrello 1.59
3018 : parrello 1.93 it will work correctly in both environments.
3019 : parrello 1.59
3020 : parrello 1.93 =over 4
3021 : parrello 1.59
3022 : parrello 1.93 =item commandString
3023 : parrello 1.59
3024 : parrello 1.93 The command string to pass to the system.
3025 : parrello 1.59
3026 :     =item RETURN
3027 :    
3028 : parrello 1.93 Returns the standard output from the specified command, as a list.
3029 : parrello 1.59
3030 :     =back
3031 :    
3032 :     =cut
3033 : parrello 1.93 #: Return Type @;
3034 :     sub TICK {
3035 : parrello 1.59 # Get the parameters.
3036 : parrello 1.93 my ($commandString) = @_;
3037 :     # Chop off the dot-slash if this is Windows.
3038 :     if ($FIG_Config::win_mode) {
3039 :     $commandString =~ s!^\./!!;
3040 : parrello 1.59 }
3041 : parrello 1.93 # Activate the command and return the result.
3042 :     return `$commandString`;
3043 : parrello 1.59 }
3044 :    
3045 : parrello 1.93
3046 : parrello 1.55 =head3 CommaFormat
3047 :    
3048 : parrello 1.92 my $formatted = Tracer::CommaFormat($number);
3049 : parrello 1.55
3050 :     Insert commas into a number.
3051 :    
3052 :     =over 4
3053 :    
3054 :     =item number
3055 :    
3056 :     A sequence of digits.
3057 :    
3058 :     =item RETURN
3059 :    
3060 :     Returns the same digits with commas strategically inserted.
3061 :    
3062 :     =back
3063 :    
3064 :     =cut
3065 :    
3066 :     sub CommaFormat {
3067 :     # Get the parameters.
3068 :     my ($number) = @_;
3069 :     # Pad the length up to a multiple of three.
3070 :     my $padded = "$number";
3071 :     $padded = " " . $padded while length($padded) % 3 != 0;
3072 :     # This is a fancy PERL trick. The parentheses in the SPLIT pattern
3073 :     # cause the delimiters to be included in the output stream. The
3074 :     # GREP removes the empty strings in between the delimiters.
3075 :     my $retVal = join(",", grep { $_ ne '' } split(/(...)/, $padded));
3076 :     # Clean out the spaces.
3077 :     $retVal =~ s/ //g;
3078 :     # Return the result.
3079 :     return $retVal;
3080 :     }
3081 : parrello 1.46
3082 :    
3083 : parrello 1.62 =head3 CompareLists
3084 :    
3085 : parrello 1.92 my ($inserted, $deleted) = Tracer::CompareLists(\@newList, \@oldList, $keyIndex);
3086 : parrello 1.62
3087 :     Compare two lists of tuples, and return a hash analyzing the differences. The lists
3088 :     are presumed to be sorted alphabetically by the value in the $keyIndex column.
3089 :     The return value contains a list of items that are only in the new list
3090 :     (inserted) and only in the old list (deleted).
3091 :    
3092 :     =over 4
3093 :    
3094 :     =item newList
3095 :    
3096 :     Reference to a list of new tuples.
3097 :    
3098 :     =item oldList
3099 :    
3100 :     Reference to a list of old tuples.
3101 :    
3102 :     =item keyIndex (optional)
3103 :    
3104 :     Index into each tuple of its key field. The default is 0.
3105 :    
3106 :     =item RETURN
3107 :    
3108 :     Returns a 2-tuple consisting of a reference to the list of items that are only in the new
3109 :     list (inserted) followed by a reference to the list of items that are only in the old
3110 :     list (deleted).
3111 :    
3112 :     =back
3113 :    
3114 :     =cut
3115 :    
3116 :     sub CompareLists {
3117 :     # Get the parameters.
3118 :     my ($newList, $oldList, $keyIndex) = @_;
3119 :     if (! defined $keyIndex) {
3120 :     $keyIndex = 0;
3121 :     }
3122 :     # Declare the return variables.
3123 :     my ($inserted, $deleted) = ([], []);
3124 :     # Loop through the two lists simultaneously.
3125 :     my ($newI, $oldI) = (0, 0);
3126 :     my ($newN, $oldN) = (scalar @{$newList}, scalar @{$oldList});
3127 :     while ($newI < $newN || $oldI < $oldN) {
3128 :     # Get the current object in each list. Note that if one
3129 :     # of the lists is past the end, we'll get undef.
3130 :     my $newItem = $newList->[$newI];
3131 :     my $oldItem = $oldList->[$oldI];
3132 : parrello 1.63 if (! defined($newItem) || defined($oldItem) && $newItem->[$keyIndex] gt $oldItem->[$keyIndex]) {
3133 : parrello 1.62 # The old item is not in the new list, so mark it deleted.
3134 :     push @{$deleted}, $oldItem;
3135 :     $oldI++;
3136 :     } elsif (! defined($oldItem) || $oldItem->[$keyIndex] gt $newItem->[$keyIndex]) {
3137 :     # The new item is not in the old list, so mark it inserted.
3138 :     push @{$inserted}, $newItem;
3139 :     $newI++;
3140 :     } else {
3141 :     # The item is in both lists, so push forward.
3142 :     $oldI++;
3143 :     $newI++;
3144 :     }
3145 :     }
3146 :     # Return the result.
3147 :     return ($inserted, $deleted);
3148 :     }
3149 :    
3150 : parrello 1.65 =head3 GenerateURL
3151 :    
3152 : parrello 1.92 my $queryUrl = Tracer::GenerateURL($page, %parameters);
3153 : parrello 1.65
3154 :     Generate a GET-style URL for the specified page with the specified parameter
3155 :     names and values. The values will be URL-escaped automatically. So, for
3156 :     example
3157 :    
3158 :     Tracer::GenerateURL("form.cgi", type => 1, string => "\"high pass\" or highway")
3159 :    
3160 :     would return
3161 :    
3162 : parrello 1.79 form.cgi?type=1;string=%22high%20pass%22%20or%20highway
3163 : parrello 1.65
3164 :     =over 4
3165 :    
3166 :     =item page
3167 :    
3168 :     Page URL.
3169 :    
3170 :     =item parameters
3171 :    
3172 :     Hash mapping parameter names to parameter values.
3173 :    
3174 :     =item RETURN
3175 :    
3176 :     Returns a GET-style URL that goes to the specified page and passes in the
3177 :     specified parameters and values.
3178 :    
3179 :     =back
3180 :    
3181 :     =cut
3182 :    
3183 :     sub GenerateURL {
3184 :     # Get the parameters.
3185 :     my ($page, %parameters) = @_;
3186 :     # Prime the return variable with the page URL.
3187 :     my $retVal = $page;
3188 :     # Loop through the parameters, creating parameter elements in a list.
3189 :     my @parmList = map { "$_=" . uri_escape($parameters{$_}) } keys %parameters;
3190 :     # If the list is nonempty, tack it on.
3191 :     if (@parmList) {
3192 : parrello 1.79 $retVal .= "?" . join(";", @parmList);
3193 : parrello 1.65 }
3194 :     # Return the result.
3195 :     return $retVal;
3196 :     }
3197 :    
3198 : parrello 1.78 =head3 ApplyURL
3199 :    
3200 : parrello 1.92 Tracer::ApplyURL($table, $target, $url);
3201 : parrello 1.78
3202 :     Run through a two-dimensional table (or more accurately, a list of lists), converting the
3203 :     I<$target> column to HTML text having a hyperlink to a URL in the I<$url> column. The
3204 :     URL column will be deleted by this process and the target column will be HTML-escaped.
3205 :    
3206 :     This provides a simple way to process the results of a database query into something
3207 :     displayable by combining a URL with text.
3208 :    
3209 :     =over 4
3210 :    
3211 :     =item table
3212 :    
3213 :     Reference to a list of lists. The elements in the containing list will be updated by
3214 :     this method.
3215 :    
3216 :     =item target
3217 :    
3218 :     The index of the column to be converted into HTML.
3219 :    
3220 :     =item url
3221 :    
3222 :     The index of the column containing the URL. Note that the URL must have a recognizable
3223 :     C<http:> at the beginning.
3224 :    
3225 :     =back
3226 :    
3227 :     =cut
3228 :    
3229 :     sub ApplyURL {
3230 :     # Get the parameters.
3231 :     my ($table, $target, $url) = @_;
3232 :     # Loop through the table.
3233 :     for my $row (@{$table}) {
3234 :     # Apply the URL to the target cell.
3235 :     $row->[$target] = CombineURL($row->[$target], $row->[$url]);
3236 :     # Delete the URL from the row.
3237 :     delete $row->[$url];
3238 :     }
3239 :     }
3240 :    
3241 :     =head3 CombineURL
3242 :    
3243 : parrello 1.92 my $combinedHtml = Tracer::CombineURL($text, $url);
3244 : parrello 1.78
3245 :     This method will convert the specified text into HTML hyperlinked to the specified
3246 :     URL. The hyperlinking will only take place if the URL looks legitimate: that is, it
3247 :     is defined and begins with an C<http:> header.
3248 :    
3249 :     =over 4
3250 :    
3251 :     =item text
3252 :    
3253 :     Text to return. This will be HTML-escaped automatically.
3254 :    
3255 :     =item url
3256 :    
3257 :     A URL to be hyperlinked to the text. If it does not look like a URL, then the text
3258 :     will be returned without any hyperlinking.
3259 :    
3260 :     =item RETURN
3261 :    
3262 :     Returns the original text, HTML-escaped, with the URL hyperlinked to it. If the URL
3263 :     doesn't look right, the HTML-escaped text will be returned without any further
3264 :     modification.
3265 :    
3266 :     =back
3267 :    
3268 :     =cut
3269 :    
3270 :     sub CombineURL {
3271 :     # Get the parameters.
3272 :     my ($text, $url) = @_;
3273 :     # Declare the return variable.
3274 :     my $retVal = CGI::escapeHTML($text);
3275 :     # Verify the URL.
3276 :     if (defined($url) && $url =~ m!http://!i) {
3277 :     # It's good, so we apply it to the text.
3278 :     $retVal = "<a href=\"$url\">$retVal</a>";
3279 :     }
3280 :     # Return the result.
3281 :     return $retVal;
3282 :     }
3283 :    
3284 : parrello 1.93 =head3 Cmp
3285 :    
3286 :     my $cmp = Tracer::Cmp($a, $b);
3287 :    
3288 :     This method performs a universal sort comparison. Each value coming in is
3289 :     separated into a leading text part and a trailing number part. The text
3290 :     part is string compared, and if both parts are equal, then the number
3291 :     parts are compared numerically. A stream of just numbers or a stream of
3292 :     just strings will sort correctly, and a mixed stream will sort with the
3293 :     numbers first. Strings with a label and a number will sort in the
3294 :     expected manner instead of lexically.
3295 :    
3296 :     =over 4
3297 :    
3298 :     =item a
3299 :    
3300 :     First item to compare.
3301 :    
3302 :     =item b
3303 :    
3304 :     Second item to compare.
3305 :    
3306 :     =item RETURN
3307 :    
3308 :     Returns a negative number if the first item should sort first (is less), a positive
3309 :     number if the first item should sort second (is greater), and a zero if the items are
3310 :     equal.
3311 :    
3312 :     =back
3313 :    
3314 :     =cut
3315 :    
3316 :     sub Cmp {
3317 :     # Get the parameters.
3318 :     my ($a, $b) = @_;
3319 :     # Declare the return value.
3320 :     my $retVal;
3321 :     # Check for nulls.
3322 :     if (! defined($a)) {
3323 :     $retVal = (! defined($b) ? 0 : -1);
3324 :     } elsif (! defined($b)) {
3325 :     $retVal = 1;
3326 :     } else {
3327 :     # Here we have two real values. Parse the two strings.
3328 :     $a =~ /^(\D*)(\d*)$/;
3329 :     my $aParsed = [$1, $2];
3330 :     $b =~ /^(\D*)(\d*)$/;
3331 :     my $bParsed = [$1, $2];
3332 :     # Compare the string parts.
3333 :     $retVal = $aParsed->[0] cmp $bParsed->[0];
3334 :     if (! $retVal) {
3335 :     $retVal = $aParsed->[1] <=> $bParsed->[1];
3336 :     }
3337 :     }
3338 :     # Return the result.
3339 :     return $retVal;
3340 :     }
3341 :    
3342 : parrello 1.87
3343 : olson 1.88 1;

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3