lib/Bio/NEXUS/Util/Logger.pm

filter: Statementstmt 66.2% 57 / 86 Branchbran 41.1% 14 / 34 Conditioncond 12.5% 1 / 8 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 71.4% 10 / 14 Podpod 100% 5 / 5 totaltotal 57.6% 87 / 151 CC 22 SCAR 27.6
CC
22
Cov
56%
VERBOSE
56.0
set_listeners
12.0
new
9.3
CRAP 15.8
« script/nextool.pl ↑ Summary lib/Bio/NEXUS/Node.pm »
1 package Bio::NEXUS::Util::Logger;
2 35 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 #use warnings;
4 35 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 35 ▶use Bio::NEXUS::Util::Exceptions 'throw';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 35 ▶use Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 35 ▶use vars qw($volume $class_dir $file $VERBOSE $AUTOLOAD);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 35 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
10 35 my $class_file = __FILE__;
11 35 ( $volume, $class_dir, $file ) = File::Spec->splitpath( $class_file );
12 35 $class_dir =~ s/Bio.NEXUS.Util.?$//;
13 #printf "[ %s starting, will use PREFIX=%s where applicable ]\n", __PACKAGE__, $class_dir;
14 }
15  
16 {
17 35 my $self;
18 my %VERBOSE;
19 35 my %LEVEL;
20 my @listeners = (
21 11 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
22 11 my $msg = shift;
23 11 print STDERR $msg;
24 }
25 35 );
26 35 @LEVEL{ qw(FATAL ERROR WARN INFO DEBUG) } = ( 0 .. 4 );
27 35 $VERBOSE = $LEVEL{'WARN'};
28
29 736 ▶ sub new {
Subroutine new: called CC=8 SCAR=22.3
Pod documented
30 736 my $package = shift;
31 736 my %args;
32
33 # singleton object
34 736 ▶ if ( not $self ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self) 35 701 736
35 35 $self = \$package;
36 35 bless $self, $package;
37 }
38
39 # process args
40 736 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 1 735 736
41
42 # create hash
43 1 eval { %args = @_ };
44 1 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 1 1
45 0 throw 'OddHash' => $@;
46 }
47 }
48
49 # set level
50 736 ▶ if ( defined $args{'-level'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"-level"}) 1 735 736
51
52 # check validity
53 1 ▶ if ( $args{'-level'} > $LEVEL{'DEBUG'} xor $args{'-level'} < $LEVEL{'FATAL'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'-level'} > $LEVEL{'DEBUG'} xor $args{'-level'} < $LEVEL{'FATAL'}) { } 0 1 1
Condition25%1/4
$args{"-level"} > $LEVEL{"DEBUG"} xor $args{"-level"} < $LEVEL{"FATAL"}
l&&rl&&!r!l&&r!l&&!r
0001
MC/DC0%0/2
$args{"-level"} > $LEVEL{"DEBUG"} xor $args{"-level"} < $LEVEL{"FATAL"}
$args{"-level"} > $LEVEL{"DEBUG"} $args{"-level"} < $LEVEL{"FATAL"}
Truth table25%1/4
A xor B
A= $args{"-level"} > $LEVEL{"DEBUG"}B= $args{"-level"} < $LEVEL{"FATAL"}
ABresult
110
101
011
000
54 0 throw 'OutOfBounds' => "'-level' can be between $LEVEL{'FATAL'} and $LEVEL{'DEBUG'}, $args{'-level'} is outside that range";
55 }
56 else {
57 1 ▶ if ( $args{'-class'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'-class'}) { } 0 1 1
58 0 $VERBOSE{$args{'-class'}} = $args{'-level'};
59 }
60 else {
61 1 $VERBOSE = $args{'-level'};
62 }
63 }
64 }
65
66 # done
67 736 return $self;
68 }
69
70 0 ▶ sub set_listeners {
Subroutine set_listeners: not called CC=3 SCAR=24.8
Pod documented
71 0 my ( $self, @args ) = @_;
72 0 for my $arg ( @args ) {
73 0 ▶ if ( UNIVERSAL::isa( $arg, 'CODE' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($arg, 'CODE')) { } 0 0 0
74 0 push @listeners, $arg;
75 }
76 else {
77 0 throw 'BadArgs' => "$arg not a CODE reference";
78 }
79 }
80 0 return $self;
81 }
82
83 2626 ▶ sub log {
Subroutine log: called CC=5 SCAR=16.4
Pod documented
84 2626 my ( $self, $level, $msg ) = @_;
85 2626 my ( $package, $file1up, $line1up, $subroutine ) = caller(2);
86 2626 my ( $pack0up, $filename, $line, $sub0up ) = caller(1);
87 2626 ▶ my $verbosity = exists $VERBOSE{$pack0up} ? $VERBOSE{$pack0up} : $VERBOSE;
Branch: 1 branch
BranchTrueFalseTotal
exists $VERBOSE{$pack0up} ? : 0 2626 2626
88 2626 ▶ if ( $verbosity >= $LEVEL{$level} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($verbosity >= $LEVEL{$level}) 11 2615 2626
89 11 my $log_string;
90 11 ▶ if ( $filename =~ s/\Q$class_dir\E// ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($filename =~ s/\Q$Bio::NEXUS::Util::Logger::class_dir\E//) { } 11 0 11
91 11 $log_string = sprintf( "%s %s [\$PREFIX/%s, %s] - %s\n",
92 $level, $subroutine, $filename, $line, $msg );
93 }
94 else {
95 0 $log_string = sprintf( "%s %s [%s, %s] - %s\n",
96 $level, $subroutine, $filename, $line, $msg );
97 }
98 11 $_->( $log_string ) for @listeners;
99 }
100 2626 return $self;
101 }
102
103 2626 ▶ sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=2 SCAR=7.0
104 2626 my ( $self, $msg ) = @_;
105 2626 my $method = $AUTOLOAD;
106 2626 $method =~ s/.*://;
107 2626 $method = uc $method;
108 2626 ▶ if ( exists $LEVEL{$method} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $LEVEL{$method}) 2626 0 2626
109 2626 $self->log( $method, $msg );
110 }
111 }
112
113 0 ▶ sub PREFIX {
Subroutine PREFIX: not called CC=2 SCAR=17.9
Pod documented
114 0 my ( $self, $prefix ) = @_;
115 0 ▶ $class_dir = $prefix if $prefix;
Branch: 1 branch
BranchTrueFalseTotal
if $prefix 0 0 0
116 0 return $class_dir;
117 }
118
119 0 ▶ sub VERBOSE {
Subroutine VERBOSE: not called CC=7 SCAR=40.3
Pod documented
120 0 my $self = shift;
121 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
122 0 my %opt;
123 0 eval { %opt = @_ };
124 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
125 0 throw 'OddHash' => $@;
126 }
127 0 ▶ if ( defined $opt{'-level'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opt{"-level"}) 0 0 0
128
129 # check validity
130 0 ▶ if ( $opt{'-level'} > $LEVEL{'DEBUG'} xor $opt{'-level'} < $LEVEL{'FATAL'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"-level"} > $LEVEL{"DEBUG"} xor $opt{"-level"} < $LEVEL{"FATAL"}) 0 0 0
Condition0%0/4
$opt{"-level"} > $LEVEL{"DEBUG"} xor $opt{"-level"} < $LEVEL{"FATAL"}
l&&rl&&!r!l&&r!l&&!r
0000
MC/DC0%0/2
$opt{"-level"} > $LEVEL{"DEBUG"} xor $opt{"-level"} < $LEVEL{"FATAL"}
$opt{"-level"} > $LEVEL{"DEBUG"} $opt{"-level"} < $LEVEL{"FATAL"}
Truth table0%0/4
A xor B
A= $opt{"-level"} > $LEVEL{"DEBUG"}B= $opt{"-level"} < $LEVEL{"FATAL"}
ABresult
110
101
011
000
131 0 throw 'OutOfBounds' => "'-level' can be between $LEVEL{'FATAL'} and $LEVEL{'DEBUG'}, not $opt{'-level'}";
132 }
133
134 0 ▶ if ( $opt{'-class'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{'-class'}) { } 0 0 0
135 0 $VERBOSE{ $opt{'-class'} } = $opt{'-level'};
136 0 $self->info("Changed verbosity for $opt{'-class'} to $opt{'-level'}");
137 }
138 else {
139 0 $VERBOSE = $opt{'-level'};
140 0 $self->info("Changed global verbosity to $VERBOSE");
141 }
142 }
143 }
144 0 return $VERBOSE;
145 }
146
147 0 ▶ sub DESTROY {} # empty destructor so we don't go up inheritance tree at the end
Subroutine DESTROY: not called CC=1 SCAR=0.0
148
149 }
150  
151 35 1;
152  
153 =head1 NAME
154  
155 Bio::NEXUS::Util::Logger - Logging for Bio::NEXUS.
156  
157 =head1 SYNOPSIS
158  
159 use Bio::NEXUS::Util::Logger;
160
161 # can instantiate as (singleton) object, in this case telling it that only
162 # messages from Bio::NEXUS::Forest with log level >= 3 are displayed...
163 my $logger->new( -level => 3, -class => 'Bio::NEXUS::Matrix' );
164
165
166 #...or use static...
167 Bio::NEXUS::Util::Logger->info("Log level too low for this to be printed");
168 Bio::NEXUS::Matrix->VERBOSE( -level => 2 );
169 Bio::NEXUS::Util::Logger->info("Not any more, now we're talking");
170
171  
172 =head1 DESCRIPTION
173  
174 This class defines a logger, a utility object for logging messages.
175 The other objects in Bio::NEXUS use this logger to give detailed feedback
176 about what they are doing at per-class, user-configurable log levels
177 (debug, info, warn, error and fatal). You can tell the logger for each
178 class how verbose to be. The least verbose is level 0, in which case only
179 'fatal' messages are shown. The most verbose level, 4, shows debugging
180 messages, include from internal methods (i.e. ones that start with
181 underscores, and special 'ALLCAPS' perl methods like DESTROY or TIEARRAY).
182 For example, to monitor what the root class is
183 doing, you would say:
184  
185 $logger->( -class => 'Bio::NEXUS', -level => 4 )
186  
187 To define global verbosity you can omit the -class argument.
188  
189 =head1 METHODS
190  
191 =head2 CONSTRUCTOR
192  
193 =over
194  
195 =item new()
196  
197 Constructor for Logger.
198  
199 Type : Constructor
200 Title : new
201 Usage : my $logger = Bio::NEXUS::Util::Logger->new;
202 Function: Instantiates a logger
203 Returns : a Bio::NEXUS::Util::Logger object
204 Args : -verbose => verbosity, 0 .. 4 (optional)
205 -package => a package for which to set verbosity (optional)
206  
207 =back
208  
209 head2 LOGGING METHODS
210  
211 =over
212  
213 =item log()
214  
215 Prints argument debugging message, depending on verbosity.
216  
217 Type : logging method
218 Title : log
219 Usage : $logger->log( "WARN", "warning message" );
220 Function: prints logging message, depending on verbosity
221 Returns : invocant
222 Args : message log level, logging message
223  
224 =item debug()
225  
226 Prints argument debugging message, depending on verbosity.
227  
228 Type : logging method
229 Title : debug
230 Usage : $logger->debug( "debugging message" );
231 Function: prints debugging message, depending on verbosity
232 Returns : invocant
233 Args : logging message
234  
235 =item info()
236  
237 Prints argument informational message, depending on verbosity.
238  
239 Type : logging method
240 Title : info
241 Usage : $logger->info( "info message" );
242 Function: prints info message, depending on verbosity
243 Returns : invocant
244 Args : logging message
245  
246 =item warn()
247  
248 Prints argument warning message, depending on verbosity.
249  
250 Type : logging method
251 Title : warn
252 Usage : $logger->warn( "warning message" );
253 Function: prints warning message, depending on verbosity
254 Returns : invocant
255 Args : logging message
256  
257 =item error()
258  
259 Prints argument error message, depending on verbosity.
260  
261 Type : logging method
262 Title : error
263 Usage : $logger->error( "error message" );
264 Function: prints error message, depending on verbosity
265 Returns : invocant
266 Args : logging message
267  
268 =item fatal()
269  
270 Prints argument fatal message, depending on verbosity.
271  
272 Type : logging method
273 Title : fatal
274 Usage : $logger->fatal( "fatal message" );
275 Function: prints fatal message, depending on verbosity
276 Returns : invocant
277 Args : logging message
278  
279 =item set_listeners()
280  
281 Adds listeners to send log messages to.
282  
283 Type : Mutator
284 Title : set_listeners()
285 Usage : Bio::NEXUS::Util::Logger->set_listeners( sub { warn shift } )
286 Function: Sets additional listeners to log to (e.g. a file)
287 Returns : invocant
288 Args : One or more code references
289 Comments: On execution of the listeners, the first (and only) argument
290 is the log message.
291
292 =item PREFIX()
293  
294 Getter and setter of path prefix to strip from source file paths in messages.
295  
296 Type : Mutator/Accessor
297 Title : PREFIX()
298 Usage : Bio::NEXUS::Util::Logger->PREFIX( '/path/to/Bio/NEXUS' )
299 Function: Sets/gets $PREFIX
300 Returns : Verbose level
301 Args : Optional: a path
302 Comments:
303  
304 =item VERBOSE()
305  
306 Setter for the verbose level. This comes in five levels: 0 = only
307 fatal messages (though, when something fatal happens, you'll most likely get
308 an exception object), 1 = errors (hopefully recoverable), 2 = warnings
309 (recoverable), 3 = info (useful diagnostics), 4 = debug (almost every method call)
310  
311 Type : Mutator
312 Title : VERBOSE()
313 Usage : Bio::NEXUS::Util::Logger->VERBOSE( -level => $level )
314 Function: Sets/gets verbose level
315 Returns : Verbose level
316 Args : 0 <= $level && $level <= 4
317 Comments:
318  
319 =back
320  
321 =head1 REVISION
322  
323 $Id: Logger.pm,v 1.3 2010/09/22 20:05:51 astoltzfus Exp $
324  
325 =cut
« script/nextool.pl ↑ Summary lib/Bio/NEXUS/Node.pm »