lib/Treex/Core/Log.pm

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