3 See the NOTICE file distributed with
this work
for additional information
4 regarding copyright ownership.
6 Licensed under the Apache License, Version 2.0 (the
"License");
7 you may not use
this file except in compliance with the License.
8 You may obtain a copy of the License at
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an
"AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 See the License
for the specific language governing permissions and
16 limitations under the License.
23 Please email comments or questions to the
public Ensembl
24 developers list at <http:
26 Questions may also be sent to the Ensembl help desk at
38 qw(
throw warning deprecate verbose
try catch);
40 or to get all methods just
44 eval {
throw(
"this is an exception with a stack trace") };
46 print
"Caught exception:\n$@";
49 # Or you can us the try/catch confortable syntax instead to deal with
50 # throw or die. Don't forget the ";" after the catch block. With
51 # this syntax, the original $@ is in $_ in the catch subroutine.
54 throw(
"this is an exception with a stack trace");
56 catch { print
"Caught exception:\n$_" };
61 warning(
'this is a silent warning');
63 #show deprecated and warning messages but not info
66 warning(
'this is a warning');
71 info(
'this is an informational message');
73 sub my_sub { deprecate(
'use other_sub() instead') }
76 info(
'This is a high priority info message.', 1000 );
80 This is derived from the Bio::Root module in BioPerl. Some formatting
81 has been changed and the deprecate
function has been added. Most
82 notably the
object methods are now
static class methods that can be
83 called without inheriting from
Bio::Root. This is
84 especially useful for throwing exceptions with stack traces outside of a
87 The originaly implementations of these methods were by Steve Chervitz
88 and refactored by Ewan Birney.
90 It is recommended that these functions be used instead of inheriting
91 unnecessarily from the Bio::Root object. The
92 functions exported by this package provide a set of useful error
108 use vars qw(@ISA @EXPORT);
111 @EXPORT = qw(
throw warning stack_trace_dump
112 stack_trace verbose deprecate
info try catch);
114 my $VERBOSITY = 3000;
115 my $DEFAULT_INFO = 4000;
116 my $DEFAULT_DEPRECATE = 3000;
117 my $DEFAULT_WARNING = 2000;
118 my $DEFAULT_EXCEPTION = 1000;
123 Arg [1] :
string $msg
124 Arg [2] : (optional)
int $level
125 override the
default level of exception throwing
127 throw(
'We have a problem');
128 Description: Throws an exception which
if not caught by an eval will
129 provide a stack trace to STDERR and die. If the verbosity level
130 is lower than the level of the
throw, then no error message is
131 displayed but the program will still die (unless the exception
134 Exceptions : thrown every time
135 Caller : generally on error
142 # For backwards compatibility with Bio::EnsEMBL::Root::throw: Allow
143 # to be called as an object method as well as class method. Root
144 # function now deprecated so call will have the string instead.
146 $string = shift
if ( ref($string) ); # Skip
object if one provided.
147 $string = shift
if ( $string eq
"Bio::EnsEMBL::Utils::Exception" );
150 $level = $DEFAULT_EXCEPTION
if ( !defined($level) );
152 if ( $VERBOSITY < $level ) {
153 die(
"\n"); # still die, but silently
160 "-------------------- EXCEPTION --------------------\n" .
163 "Date (localtime) = %s\n" .
164 "Ensembl API version = %s\n" .
165 "---------------------------------------------------\n",
175 Arg [1] :
string warning(message);
176 Arg [2] : (optional)
int level
177 Override the
default level of
this warning changning the level
178 of verbosity at which it is displayed.
181 Description: If the verbosity level is higher or equal to the level of
this
182 warning then a warning message is printed to STDERR. If the
183 verbosity lower then nothing is done. Under the
default
184 levels of warning and verbosity warnings will be displayed.
186 Exceptions : warning every time
194 # See throw() for this:
195 $string = shift
if ( ref($string) ); # Skip
object if one provided.
196 $string = shift
if ( $string eq
"Bio::EnsEMBL::Utils::Exception" );
199 $level = $DEFAULT_WARNING
if ( !defined($level) );
201 return if ( $VERBOSITY < $level );
204 my $line = $caller[2] ||
'';
206 # Use only two sub-dirs for brevity when reporting the file name.
211 while ( @path && $i < 2 ) {
213 $file = pop(@path) .
"/$file";
222 $caller_line = $caller[2];
223 $caller_file = pop(@path);
224 while ( @path && $i < 2 ) {
226 $caller_file = pop(@path) .
"/$caller_file";
232 "-------------------- WARNING ----------------------\n" .
234 "FILE: %s LINE: %d\n",
235 $string, $file, $line );
237 if ( defined($caller_file) ) {
238 $out .= sprintf(
"CALLED BY: %s LINE: %d\n", $caller_file,
242 "Date (localtime) = %s\n" .
243 "Ensembl API version = %s\n" .
244 "---------------------------------------------------\n",
255 Arg [1] :
string $string
256 The message to be displayed
257 Arg [2] : (optional)
int $level
258 Override the
default level of
this message so it is displayed at
259 a different level of verbosity than it normally would be.
261 Description: This prints an
info message to STDERR
if verbosity is higher
262 than the level of the message. By
default info messages are not
272 $string = shift
if($string eq
"Bio::EnsEMBL::Utils::Exception");
275 $level = $DEFAULT_INFO
if(!defined($level));
277 return if($VERBOSITY < $level);
279 print STDERR
"INFO: $string\n";
286 Arg [1] : (optional)
int
288 #turn warnings and everything more important on (e.g. exception)
291 info(
"This won't be displayed");
294 #turn exception messages on
296 warning(
"This won't do anything");
297 throw(
"Die with a message");
301 warning(
"This won't do anything");
302 throw(
"Die silently without a message");
304 #turn on all messages
306 info(
"All messages are now displayed");
309 print
"Verbosity is pretty high";
312 Description: Gets/Sets verbosity level which defines which messages are
313 to be displayed. An integer value may be passed or one of the
331 my $verbosity = shift;
332 $verbosity = shift
if($verbosity eq
"Bio::EnsEMBL::Utils::Exception");
333 if($verbosity =~ /\d+/) { #check
if verbosity is an integer
334 $VERBOSITY = $verbosity;
336 $verbosity = uc($verbosity);
337 if($verbosity eq
'OFF' || $verbosity eq
'NOTHING' ||
338 $verbosity eq
'NONE') {
340 } elsif($verbosity eq
'EXCEPTION' || $verbosity eq
'THROW') {
341 $VERBOSITY = $DEFAULT_EXCEPTION;
342 } elsif($verbosity eq
'WARNING' || $verbosity eq
'WARN') {
343 $VERBOSITY = $DEFAULT_WARNING;
344 } elsif($verbosity eq
'DEPRECATE' || $verbosity eq
'DEPRECATED') {
345 $VERBOSITY = $DEFAULT_DEPRECATE;
346 } elsif($verbosity eq
'INFO') {
347 $VERBOSITY = $DEFAULT_INFO;
348 } elsif($verbosity eq
'ON' || $verbosity eq
'ALL') {
351 $VERBOSITY = $DEFAULT_WARNING;
352 warning(
"Unknown level of verbosity: $verbosity");
362 =head2 stack_trace_dump
364 Arg [1] : (optional)
int $levels
365 The number of levels to ignore from the top of the stack when
366 creating the dump. This is useful when
this is called internally
367 from a warning or
throw function when the immediate caller and
368 stack_trace_dump
function calls are themselves uninteresting.
371 Description: Returns a stack trace formatted as a
string
374 Caller : general,
throw, warning
378 sub stack_trace_dump{
381 my $levels = 2; #
default is 2 levels so stack_trace_dump call is not present
382 $levels = shift
if(@_);
383 $levels = shift
if($levels eq
"Bio::EnsEMBL::Utils::Exception");
384 $levels = 1
if($levels < 1);
392 my ($module,$function,$file,$position);
395 foreach my $stack ( @stack) {
396 ($module,$file,$position,$function) = @{$stack};
397 $out .=
"STACK $function $file:$position\n";
409 Description: Gives an array to a reference of arrays with stack trace
info
410 each coming from the caller(stack_number) call
411 Returntype : array of listrefs of strings
421 while ( my @call = caller($i++)) {
423 # major annoyance that caller puts caller context as
424 # function name. Hence some monkeying around...
425 $prev->[3] = $call[3];
429 $prev->[3] =
'toplevel';
437 Arg [1] :
string $mesg
438 A message describing why a method is deprecated
441 deprecate(
'Please use new_sub() instead');
443 Description: Prints a warning to STDERR that the method which called
444 deprecate() is deprecated. Also prints the line number and
445 file from which the deprecated method was called. Deprecated
446 warnings only appear once for each location the method was
447 called from. No message is displayed if the level of verbosity
448 is lower than the level of the warning.
450 Exceptions : warning every time
451 Caller : deprecated methods
459 $mesg = shift
if($mesg eq
"Bio::EnsEMBL::Utils::Exception"); #skip
object if one provided
463 $level = $DEFAULT_DEPRECATE
if(!defined($level));
465 return if($VERBOSITY < $level);
467 my @caller = caller(1);
468 my $subname = $caller[3] ;
469 my $line = $caller[2];
471 #use only 2 subdirs for brevity when reporting the filename
473 my @path = $caller[1];
476 while(@path && $i < 2) {
481 #keep track of who called this method so that the warning is only displayed
482 #once per deprecated call
483 return if $DEPRECATED{
"$line:$file:$subname"};
485 if ( $VERBOSITY > -1 ) {
487 "\n------------------ DEPRECATED ---------------------\n"
488 .
"Deprecated method call in file $file line $line.\n"
489 .
"Method $subname is deprecated.\n"
491 .
"Ensembl API version = "
493 .
"---------------------------------------------------\n";
496 $DEPRECATED{
"$line:$file:$subname"} = 1;
501 Arg [1] : anonymous subroutine
502 the block to be tried
503 Arg [2] :
return value of the
catch function
506 try { block1 }
catch { block2 };
507 { block1 } is the 1st argument
508 catch { block2 } is the 2nd argument
511 throw(
"this is an exception with a stack trace");
513 print
"Caught exception:\n$_";
515 In block2, $_ is assigned the value of the first
516 throw or die statement executed in block 1.
518 Description: Replaces the classical syntax
521 by a more confortable one.
522 In the
try/
catch syntax, the original $@ is in $_ in the
catch subroutine.
523 This
try/
catch implementation is a copy and paste from
524 "Programming Perl" 3rd Edition, July 2000, by L.Wall, T. Christiansen
525 & J. Orwant. p227, and is only possible because of subroutine prototypes.
526 Returntype : depend on what is implemented the
try or
catch block
534 my ($try, $catch) = @_;