Treex/Core/Log.pm

filter: Statementstmt 67.3% 97 / 144 Branchbran 26.3% 10 / 38 Conditioncond 31.2% 5 / 16 MC/DCmcdc 10.0% 1 / 10 Subroutinesub 83.3% 20 / 24 Podpod 81.8% 9 / 11 totaltotal 58.4% 142 / 243 CC 27 SCAR 21.5
CC
27
Cov
57%
log_debug
20.0
del_hook
20.0
log_info
13.4
CRAP 8.6
« Treex/Core/Config.pm ↑ Summary Treex/Core/Files.pm »
1 package Treex::Core::Log;
2 94 $Treex::Core::Log::VERSION = '2.20151210';
3 94 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 94 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 94 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 94 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 94 ▶use English '-no_match_vars';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 94 ▶use Carp qw(cluck);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 94 ▶use IO::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 94 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 94 ▶use Time::HiRes qw(time);
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 94 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 94 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
18 94 our @EXPORT = qw(log_fatal log_warn log_info log_debug log_memory running_time); ## no critic (ProhibitAutomaticExportation)
19  
20 94 $Carp::CarpLevel = 1;
21  
22 94 ▶binmode STDOUT, ":encoding(utf-8)";
Subroutine BEGIN: called CC=1 SCAR=0.0
23 94 binmode STDERR, ":encoding(utf-8)";
24  
25 # Autoflush after every Perl statement should enforce that INFO and FATALs are ordered correctly.
26 {
27  
28 #my $oldfh = select(STDERR);
29 #$| = 1;
30 #select($oldfh);
31 94 *STDERR->autoflush();
32 }
33  
34  
35 94 my @ERROR_LEVEL_NAMES = qw(ALL DEBUG INFO WARN FATAL);
36 94 Readonly my %ERROR_LEVEL_VALUE => map {$ERROR_LEVEL_NAMES[$_] => $_} (0 .. $#ERROR_LEVEL_NAMES);
37  
38 #Readonly my %ERROR_LEVEL_VALUE => (
39 # 'ALL' => 0,
40 # 'DEBUG' => 1,
41 # 'INFO' => 2,
42 # 'WARN' => 3,
43 # 'FATAL' => 4,
44 #);
45  
46  
47 94 ▶use Moose::Util::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
48 94 enum 'ErrorLevel' => [keys %ERROR_LEVEL_VALUE];
49  
50 # how many characters of a string-eval are to be shown in the output
51 94 $Carp::MaxEvalLen = 100;
52  
53 94 my $unfinished_line;
54  
55 # By default report only messages with INFO or higher level
56 94 my $current_error_level_value = $ERROR_LEVEL_VALUE{'INFO'};
57  
58 # Time when treex was executed.
59 94 our $init_time = time ();
60  
61 # returns time elapsed from $init_time.
62 sub running_time
63 {
64 45 ▶ return sprintf('%10.3f', time() - $init_time);
Subroutine running_time: called CC=1 SCAR=0.0
Pod undocumented
65 }
66  
67 # allows to suppress messages with lower than given importance
68 sub log_set_error_level {
69 7 ▶ my $new_error_level = uc(shift);
Subroutine log_set_error_level: called CC=2 SCAR=7.4
Pod documented
70 7 ▶ if ( not defined $ERROR_LEVEL_VALUE{$new_error_level} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $ERROR_LEVEL_VALUE{$new_error_level}) 0 7 7
71 0 log_fatal("Unacceptable errorlevel: $new_error_level");
72 }
73 7 $current_error_level_value = $ERROR_LEVEL_VALUE{$new_error_level};
74 7 return;
75 }
76  
77 sub get_error_level {
78 0 ▶ return $ERROR_LEVEL_NAMES[$current_error_level_value];
Subroutine get_error_level: not called CC=1 SCAR=6.9
Pod documented
79 }
80  
81 # fatal error messages can't be suppressed
82 sub log_fatal {
83 11 ▶ my $message = shift;
Subroutine log_fatal: called CC=5 SCAR=22.0
Pod documented
84 11 ▶ if ($unfinished_line) {
Branch: 1 branch
BranchTrueFalseTotal
if ($unfinished_line) 0 11 11
85 0 print STDERR "\n";
86 0 $unfinished_line = 0;
87 }
88 11 my $line = "TREEX-FATAL:" . running_time() . ":\t$message\n\n";
89 11 ▶ if ( $current_error_level_value <= $ERROR_LEVEL_VALUE{'DEBUG'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($current_error_level_value <= $ERROR_LEVEL_VALUE{"DEBUG"}) 0 11 11
90 0 ▶ if ($OS_ERROR) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Treex::Core::Log::OS_ERROR) 0 0 0
91 0 $line .= "PERL ERROR MESSAGE: $OS_ERROR\n";
92 }
93 0 ▶ if ($EVAL_ERROR) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Treex::Core::Log::EVAL_ERROR) 0 0 0
94 0 $line .= "PERL EVAL ERROR MESSAGE: $EVAL_ERROR\n";
95 }
96 }
97 11 $line .= "PERL STACK:";
98 11 cluck $line;
99 11 run_hooks('FATAL');
100 11 die "\n";
101 }
102  
103 # TODO: redesign API - $carp, $no_print_stack
104  
105 sub log_warn {
106 2 ▶ my ( $message, $carp ) = @_;
Subroutine log_warn: called CC=4 SCAR=23.1
Pod documented
107 2 ▶ if ( $current_error_level_value <= $ERROR_LEVEL_VALUE{'WARN'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($current_error_level_value <= $ERROR_LEVEL_VALUE{"WARN"}) 0 2 2
108 0 my $line = "";
109 0 ▶ if ($unfinished_line) {
Branch: 1 branch
BranchTrueFalseTotal
if ($unfinished_line) 0 0 0
110 0 $line = "\n";
111 0 $unfinished_line = 0;
112 }
113 0 $line .= "TREEX-WARN:" . running_time() . ":\t$message\n";
114  
115 0 ▶ if ($carp) {
Branch: 1 branch
BranchTrueFalseTotal
if ($carp) { } 0 0 0
116 0 Carp::carp $line;
117 }
118 else {
119 0 print STDERR $line;
120 }
121 }
122 2 run_hooks('WARN');
123 2 return;
124 }
125  
126 sub log_debug {
127 0 ▶ my ( $message, $no_print_stack ) = @_;
Subroutine log_debug: not called CC=4 SCAR=30.0
Pod documented
128 0 ▶ if ( $current_error_level_value <= $ERROR_LEVEL_VALUE{'DEBUG'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($current_error_level_value <= $ERROR_LEVEL_VALUE{"DEBUG"}) 0 0 0
129 0 my $line = "";
130 0 ▶ if ($unfinished_line) {
Branch: 1 branch
BranchTrueFalseTotal
if ($unfinished_line) 0 0 0
131 0 $line = "\n";
132 0 $unfinished_line = 0;
133 }
134 0 $line .= "TREEX-DEBUG:" . running_time() . ":\t$message\n";
135  
136 0 ▶ if ($no_print_stack) {
Branch: 1 branch
BranchTrueFalseTotal
if ($no_print_stack) { } 0 0 0
137 0 print STDERR $line;
138 }
139 else {
140 0 Carp::cluck $line;
141 }
142 }
143 0 run_hooks('DEBUG');
144 0 return;
145 }
146  
147 sub log_info {
148 46 ▶ my ( $message, $arg_ref ) = @_;
Subroutine log_info: called CC=9 SCAR=25.9
Pod documented
149 46 ▶ if ( $current_error_level_value <= $ERROR_LEVEL_VALUE{'INFO'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($current_error_level_value <= $ERROR_LEVEL_VALUE{"INFO"}) 34 12 46
150 34 ▶ my $same_line = defined $arg_ref && $arg_ref->{same_line};
Condition33%1/3
defined $arg_ref && $arg_ref->{'same_line'}
!ll&&!rl&&r
3400
MC/DC0%0/2
defined $arg_ref && $arg_ref->{'same_line'}
defined $arg_ref $arg_ref->{'same_line'}
Truth table33%1/3
A && B
A= defined $arg_refB= $arg_ref->{'same_line'}
ABresult
0X0
100
111
151 34 my $line = "";
152 34 ▶ if ( $unfinished_line && !$same_line ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($unfinished_line and not $same_line) 0 34 34
Condition33%1/3
$unfinished_line and not $same_line
!ll&&!rl&&r
3400
MC/DC0%0/2
$unfinished_line and not $same_line
$unfinished_line not $same_line
Truth table33%1/3
A and not(B)
A= $unfinished_lineB= not $same_line
ABresult
0X0
100
111
153 0 $line = "\n";
154 0 $unfinished_line = 0;
155 }
156 34 ▶ if ( !$same_line || !$unfinished_line ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($same_line and $unfinished_line) 34 0 34
Condition33%1/3
$same_line and $unfinished_line
!ll&&!rl&&r
3400
MC/DC0%0/2
$same_line and $unfinished_line
$same_line $unfinished_line
Truth table33%1/3
A and B
A= $same_lineB= $unfinished_line
ABresult
0X0
100
111
157 34 $line .= "TREEX-INFO:" . running_time() . ":\t";
158 }
159 34 $line .= $message;
160  
161 34 ▶ if ($same_line) {
Branch: 1 branch
BranchTrueFalseTotal
if ($same_line) { } 0 34 34
162 0 $unfinished_line = 1;
163 }
164 else {
165 34 $line .= "\n";
166 }
167  
168 34 print STDERR $line;
169 34 ▶ if ($same_line) {
Branch: 1 branch
BranchTrueFalseTotal
if ($same_line) 0 34 34
170 0 STDERR->flush;
171 }
172 }
173 46 run_hooks('INFO');
174 46 return;
175 }
176  
177 sub progress { # progress se pres ntred neposila, protoze by se stejne neflushoval
178 0 ▶ return if $current_error_level_value > $ERROR_LEVEL_VALUE{'INFO'};
Subroutine progress: not called CC=3 SCAR=24.8
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if $current_error_level_value > $ERROR_LEVEL_VALUE{"INFO"} 0 0 0
179 0 ▶ if ( not $unfinished_line ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($unfinished_line) 0 0 0
180 0 print STDERR "TREEX-PROGRESS:" . running_time() . ":\t";
181 }
182 0 print STDERR "*";
183 0 STDERR->flush;
184 0 $unfinished_line = 1;
185 0 return;
186 }
187  
188 # ---------- HOOKS -----------------
189  
190 94 my %hooks; # subroutines can be associated with reported events
191  
192 sub add_hook {
193 2 ▶ my ( $level, $subroutine ) = @_;
Subroutine add_hook: called CC=2 SCAR=6.9
Pod documented
194 2 ▶ $hooks{$level} ||= [];
Condition100%2/2
$hooks{$level} ||= []
l!l
11
MC/DC100%1/1
$hooks{$level} ||= []
$hooks{$level}
Truth table100%2/2
A
A= $hooks{$level}
Aresult
11
00
195 2 push @{ $hooks{$level} }, $subroutine;
196 2 return scalar(@{$hooks{$level}}) - 1;
197 }
198  
199 sub del_hook {
200 0 ▶ my ( $level, $pos ) = @_;
Subroutine del_hook: not called CC=4 SCAR=30.0
Pod undocumented
201 0 ▶ $hooks{$level} ||= [];
Condition0%0/2
$hooks{$level} ||= []
l!l
00
MC/DC0%0/1
$hooks{$level} ||= []
$hooks{$level}
Truth table0%0/2
A
A= $hooks{$level}
Aresult
11
00
202 0 ▶ if ( $pos < 0 || $pos >= scalar(@{$hooks{$level}}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos < 0 or $pos >= scalar @{$hooks{$level};}) 0 0 0
Condition0%0/3
$pos < 0 or $pos >= scalar @{$hooks{$level};}
l!l&&r!l&&!r
000
MC/DC0%0/2
$pos < 0 or $pos >= scalar @{$hooks{$level};}
$pos < 0 $pos >= scalar @{$hooks{$level};}
Truth table0%0/3
A or B
A= $pos < 0B= $pos >= scalar @{$hooks{$level};}
ABresult
1X1
011
000
203 0 return;
204 }
205 0 splice(@{$hooks{$level}}, $pos, 1);
206  
207 0 return;
208 }
209  
210 sub run_hooks {
211 59 ▶ my ($level) = @_;
Subroutine run_hooks: called CC=2 SCAR=6.9
Pod documented
212 59 foreach my $subroutine ( @{ $hooks{$level} } ) {
213 3 &$subroutine;
214 }
215 59 return;
216 }
217  
218 94 1;
219  
220 __END__
221  
222  
223 =encoding utf-8
224  
225 =head1 NAME
226  
227 Treex::Core::Log - logger tailored for the needs of Treex
228  
229 =head1 VERSION
230  
231 version 2.20151210
232  
233 =head1 SYNOPSIS
234  
235 use Treex::Core::Log;
236  
237 Treex::Core::Log::log_set_error_level('DEBUG');
238  
239 sub epilog {
240 print STDERR "I'm going to cease!";
241 }
242 Treex::Core::Log::add_hook('FATAL',&epilog());
243  
244 sub test_value {
245 my $value = shift;
246 log_fatal "Negative values are unacceptable" if $ARGV < 0;
247 log_warn "Zero value is suspicious" if $ARGV == 0;
248 log_debug "test: value=$value";
249 }
250  
251  
252  
253 =head1 DESCRIPTION
254  
255 C<Treex::Core::Log> is a logger developed with the Treex system.
256 It uses more or less standard leveled set of reporting functions,
257 printing the messages at C<STDERR>.
258  
259  
260 Note that this module might be completely substituted
261 by more elaborate solutions such as L<Log::Log4perl> in the
262 whole Treex in the future
263  
264  
265 =head2 Error levels
266  
267  
268 Specifying error level can be used for suppressing
269 reports with lower severity. This module supports four
270 ordered levels of report severity (plus a special value
271 comprising them all).
272  
273 =over 4
274  
275 =item FATAL
276  
277 =item WARN
278  
279 =item INFO - the default value
280  
281 =item DEBUG
282  
283 =item ALL
284  
285 =back
286  
287 The current error level can be accessed by the following functions:
288  
289 =over 4
290  
291 =item log_set_error_level($error_level)
292  
293 =item get_error_level()
294  
295 =back
296  
297  
298  
299 =head2 Basic reporting functions
300  
301 All the following functions are exported by default.
302  
303 =over 4
304  
305 =item log_fatal($message)
306  
307 print the message, print the Perl stack too, and exit
308  
309 =item log_warn($message)
310  
311 =item log_info($message)
312  
313 =item log_debug($message)
314  
315 =back
316  
317  
318  
319 =head2 Other reporting functions
320  
321 =over 4
322  
323 =item log_memory
324  
325 print the consumed memory
326  
327 =item progress
328  
329 print another asterisk in a 'progress bar' composed of asterisks
330  
331 =back
332  
333  
334  
335  
336 =head2 Hooks
337  
338 Another functions can be called prior to reporting events, by
339 hooking a function on a certain error level event.
340  
341 =over 4
342  
343 =item add_hook($level, &hook_subroutine)
344  
345 add the subroutine to the list of subroutines called prior
346 to reporting events with the given level
347  
348 =item run_hooks($level)
349  
350 run all subroutines for the given error level
351  
352 =back
353  
354  
355  
356 =head1 AUTHOR
357  
358 Zdeněk Žabokrtský <zabokrtsky@ufal.mff.cuni.cz>
359  
360 =head1 COPYRIGHT AND LICENSE
361  
362 Copyright © 2011 by Institute of Formal and Applied Linguistics, Charles University in Prague
363  
364 This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
« Treex/Core/Config.pm ↑ Summary Treex/Core/Files.pm »