Treex/Core/Log.pm

filter: Statementstmt 73.6% 106 / 144 Branchbran 36.8% 14 / 38 Conditioncond 31.2% 5 / 16 MC/DCmcdc 10.0% 1 / 10 Subroutinesub 87.5% 21 / 24 Podpod 81.8% 9 / 11 totaltotal 64.2% 156 / 243 CC 27 SCAR 19.9
CC
27
Cov
63%
del_hook
20.0
log_info
13.4
progress
12.0
CRAP 7.3
« Treex/Core/Document.pm ↑ Summary Treex/Core/Node/Aligned.pm »
1 package Treex::Core::Log;
2 93 $Treex::Core::Log::VERSION = '2.20210102';
3 93 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 93 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 93 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 93 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 93 ▶use English '-no_match_vars';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 93 ▶use Carp qw(cluck);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 93 ▶use IO::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 93 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 93 ▶use Time::HiRes qw(time);
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 93 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 93 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
18 93 our @EXPORT = qw(log_fatal log_warn log_info log_debug log_memory running_time); ## no critic (ProhibitAutomaticExportation)
19  
20 93 $Carp::CarpLevel = 1;
21  
22 93 ▶binmode STDOUT, ":encoding(utf-8)";
Subroutine BEGIN: called CC=1 SCAR=0.0
23 93 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 93 *STDERR->autoflush();
32 }
33  
34  
35 93 my @ERROR_LEVEL_NAMES = qw(ALL DEBUG INFO WARN FATAL);
36 93 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 93 ▶use Moose::Util::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
48 93 enum 'ErrorLevel' => [keys %ERROR_LEVEL_VALUE];
49  
50 # how many characters of a string-eval are to be shown in the output
51 93 $Carp::MaxEvalLen = 100;
52  
53 93 my $unfinished_line;
54  
55 # By default report only messages with INFO or higher level
56 93 my $current_error_level_value = $ERROR_LEVEL_VALUE{'INFO'};
57  
58 # Time when treex was executed.
59 93 our $init_time = time ();
60  
61 # returns time elapsed from $init_time.
62 sub running_time
63 46 ▶{
Subroutine running_time: called CC=1 SCAR=0.0
Pod undocumented
64 46 return sprintf('%10.3f', time() - $init_time);
65 }
66  
67 # allows to suppress messages with lower than given importance
68 7 ▶sub log_set_error_level {
Subroutine log_set_error_level: called CC=2 SCAR=7.4
Pod documented
69 7 my $new_error_level = uc(shift);
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 0 ▶sub get_error_level {
Subroutine get_error_level: not called CC=1 SCAR=6.9
Pod documented
78 0 return $ERROR_LEVEL_NAMES[$current_error_level_value];
79 }
80  
81 # fatal error messages can't be suppressed
82 11 ▶sub log_fatal {
Subroutine log_fatal: called CC=5 SCAR=22.0
Pod documented
83 11 my $message = shift;
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 3 ▶sub log_warn {
Subroutine log_warn: called CC=4 SCAR=14.7
Pod documented
106 3 my ( $message, $carp ) = @_;
107 3 ▶ if ( $current_error_level_value <= $ERROR_LEVEL_VALUE{'WARN'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($current_error_level_value <= $ERROR_LEVEL_VALUE{"WARN"}) 1 2 3
108 1 my $line = "";
109 1 ▶ if ($unfinished_line) {
Branch: 1 branch
BranchTrueFalseTotal
if ($unfinished_line) 0 1 1
110 0 $line = "\n";
111 0 $unfinished_line = 0;
112 }
113 1 $line .= "TREEX-WARN:" . running_time() . ":\t$message\n";
114  
115 1 ▶ if ($carp) {
Branch: 1 branch
BranchTrueFalseTotal
if ($carp) { } 0 1 1
116 0 Carp::carp $line;
117 }
118 else {
119 1 print STDERR $line;
120 }
121 }
122 3 run_hooks('WARN');
123 3 return;
124 }
125  
126 25 ▶sub log_debug {
Subroutine log_debug: called CC=4 SCAR=23.1
Pod documented
127 25 my ( $message, $no_print_stack ) = @_;
128 25 ▶ if ( $current_error_level_value <= $ERROR_LEVEL_VALUE{'DEBUG'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($current_error_level_value <= $ERROR_LEVEL_VALUE{"DEBUG"}) 0 25 25
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 25 run_hooks('DEBUG');
144 25 return;
145 }
146  
147 46 ▶sub log_info {
Subroutine log_info: called CC=9 SCAR=25.9
Pod documented
148 46 my ( $message, $arg_ref ) = @_;
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 0 ▶sub progress { # progress se pres ntred neposila, protoze by se stejne neflushoval
Subroutine progress: not called CC=3 SCAR=24.8
Pod documented
178 0 ▶ return if $current_error_level_value > $ERROR_LEVEL_VALUE{'INFO'};
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 93 my %hooks; # subroutines can be associated with reported events
191  
192 2 ▶sub add_hook {
Subroutine add_hook: called CC=2 SCAR=6.9
Pod documented
193 2 my ( $level, $subroutine ) = @_;
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 0 ▶sub del_hook {
Subroutine del_hook: not called CC=4 SCAR=30.0
Pod undocumented
200 0 my ( $level, $pos ) = @_;
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 85 ▶sub run_hooks {
Subroutine run_hooks: called CC=2 SCAR=6.9
Pod documented
211 85 my ($level) = @_;
212 85 foreach my $subroutine ( @{ $hooks{$level} } ) {
213 3 &$subroutine;
214 }
215 85 return;
216 }
217  
218 93 1;
219  
220 __END__
« Treex/Core/Document.pm ↑ Summary Treex/Core/Node/Aligned.pm »