Util.pm

filter: Statementstmt 77.6% 73 / 94 Branchbran 55.7% 29 / 52 Conditioncond 15.6% 8 / 51 MC/DCmcdc 11.5% 3 / 26 Subroutinesub 68.4% 13 / 19 Podpod 86.6% 13 / 15 totaltotal 54.0% 139 / 257 CC 46 SCAR 30.7
CC
46
Cov
56%
must_show_location
42.0
parse_locale
40.6
must_show_stack
30.0
CRAP 21.6
↑ Summary Minimal.pm »
1 # This code is part of Perl distribution Log-Report-Optional version 1.08.
2 # The POD got stripped from this file by OODoc version 3.04.
3 # For contributors see file ChangeLog.
4  
5 # This software is copyright (c) 2013-2025 by Mark Overmeer.
6  
7 # This is free software; you can redistribute it and/or modify it under
8 # the same terms as the Perl 5 programming language system itself.
9 # SPDX-License-Identifier: Artistic-1.0-Perl OR GPL-1.0-or-later
10  
11 #oodist: *** DO NOT USE THIS VERSION FOR PRODUCTION ***
12 #oodist: This file contains OODoc-style documentation which will get stripped
13 #oodist: during its release in the distribution. You can use this file for
14 #oodist: testing, however the code of this development version may be broken!
15 #oorestyle: old style disclaimer to be removed.
16 #oorestyle: not using Log::Report yet.
17  
18 # This code is part of distribution Log-Report-Optional. Meta-POD processed
19 # with OODoc into POD and HTML manual-pages. See README.md
20 # Copyright Mark Overmeer. Licensed under the same terms as Perl itself.
21  
22 package Log::Report::Util;{
23 5 our $VERSION = '1.08';
24 }
25  
26 5 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 5 ▶use String::Print qw/printi/;
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 5 our @EXPORT = qw/
34 @reasons is_reason is_fatal use_errno mode_number expand_reasons
35 mode_accepts must_show_location must_show_stack escape_chars
36 unescape_chars to_html parse_locale pkg2domain
37 /;
38 # [0.994 parse_locale deprecated, but kept hidden]
39  
40 5 our @EXPORT_OK = qw/%reason_code/;
41  
42 #use Log::Report 'log-report';
43 5 ▶sub N__w($) { split ' ', $_[0] }
Subroutine N__w: called CC=1 SCAR=0.0
Pod undocumented
44  
45 # ordered!
46 5 our @reasons = N__w('TRACE ASSERT INFO NOTICE WARNING MISTAKE ERROR FAULT ALERT FAILURE PANIC');
47 5 our %reason_code; { my $i=1; %reason_code = map +($_ => $i++), @reasons }
48  
49 5 my %reason_set = (
50 ALL => \@reasons,
51 FATAL => [ qw/ERROR FAULT FAILURE PANIC/ ],
52 NONE => [ ],
53 PROGRAM => [ qw/TRACE ASSERT INFO NOTICE WARNING PANIC/ ],
54 SYSTEM => [ qw/FAULT ALERT FAILURE/ ],
55 USER => [ qw/MISTAKE ERROR/ ],
56 );
57  
58 5 my %is_fatal = map +($_ => 1), @{$reason_set{FATAL}};
59 5 my %use_errno = map +($_ => 1), qw/FAULT ALERT FAILURE/;
60  
61 5 my %modes = (NORMAL => 0, VERBOSE => 1, ASSERT => 2, DEBUG => 3, 0 => 0, 1 => 1, 2 => 2, 3 => 3);
62 5 my @mode_accepts = ('NOTICE-', 'INFO-', 'ASSERT-', 'ALL');
63  
64 # horrible mutual dependency with Log::Report(::Minimal)
65 sub error__x($%)
66 0 ▶{ if(Log::Report::Minimal->can('error')) # loaded the ::Mimimal version
Subroutine error__x: not called CC=2 SCAR=17.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
if ('Log::Report::Minimal'->can('error')) { } 0 0 0
67 0 { Log::Report::Minimal::error(Log::Report::Minimal::__x(@_)) }
68 0 else { Log::Report::error(Log::Report::__x(@_)) }
69 }
70  
71 #--------------------
72  
73  
74  
75 sub expand_reasons($)
76 17 ▶{ my $reasons = shift or return ();
Subroutine expand_reasons: called CC=15 SCAR=28.4
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
my $reasons = shift() or return () 15 2 17
77 15 ▶ $reasons = [ split m/\,/, $reasons ] if ref $reasons ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $reasons ne "ARRAY" 13 2 15
78  
79 15 my %r;
80 15 foreach my $r (@$reasons)
81 18 ▶ { if($r =~ m/^([a-z]*)\-([a-z]*)/i )
Branch: 3 branches
BranchTrueFalseTotal
if ($r =~ /^([a-z]*)\-([a-z]*)/i) { } 8 10 18
elsif ($Log::Report::Util::reason_code{$r}) { } 4 6 10
elsif (my $s = $reason_set{$r}) { } 6 0 6
82 8 ▶ { my $begin = $reason_code{$1 || 'TRACE'};
Condition100%2/2
$1 || 'TRACE'
l!l
71
MC/DC100%1/1
$1 || 'TRACE'
$1
Truth table100%2/2
A
A= $1
Aresult
11
00
83 8 ▶ my $end = $reason_code{$2 || 'PANIC'};
Condition100%2/2
$2 || 'PANIC'
l!l
35
MC/DC100%1/1
$2 || 'PANIC'
$2
Truth table100%2/2
A
A= $2
Aresult
11
00
84 8 ▶ $begin && $end
Branch: 2 branches
BranchTrueFalseTotal
$begin and $end or &error__x("unknown reason {which} in '{reasons}'", "which", $begin ? $2 : $1, "reasons", $reasons) 8 0 8
$begin ? : 0 0 0
Condition33%1/3
$begin and $end
!ll&&!rl&&r
008
MC/DC0%0/2
$begin and $end
$begin $end
Truth table33%1/3
A and B
A= $beginB= $end
ABresult
0X0
100
111
85 or error__x "unknown reason {which} in '{reasons}'", which => ($begin ? $2 : $1), reasons => $reasons;
86  
87 8 ▶ error__x"reason '{begin}' more serious than '{end}' in '{reasons}", begin => $1, end => $2, reasons => $reasons
Branch: 1 branch
BranchTrueFalseTotal
if $begin >= $end 0 8 8
88 if $begin >= $end;
89  
90 8 $r{$_}++ for $begin..$end;
91 }
92 4 elsif($reason_code{$r}) { $r{$reason_code{$r}}++ }
93 6 elsif(my $s = $reason_set{$r}) { $r{$reason_code{$_}}++ for @$s }
94 else
95 0 { error__x"unknown reason {which} in '{reasons}'", which => $r, reasons => $reasons;
96 }
97 }
98 15 (undef, @reasons)[sort {$a <=> $b} keys %r];
99 }
100  
101  
102 2 ▶sub is_reason($) { $reason_code{$_[0]} }
Subroutine is_reason: called CC=1 SCAR=0.0
Pod documented
103 2 ▶sub is_fatal($) { $is_fatal{$_[0]} }
Subroutine is_fatal: called CC=1 SCAR=0.0
Pod documented
104 2 ▶sub use_errno($) { $use_errno{$_[0]} }
Subroutine use_errno: called CC=1 SCAR=0.0
Pod documented
105  
106 #--------------------
107  
108 0 ▶sub mode_number($) { $modes{$_[0]} }
Subroutine mode_number: not called CC=1 SCAR=6.9
Pod documented
109  
110  
111 4 ▶sub mode_accepts($) { $mode_accepts[$modes{$_[0]}] }
Subroutine mode_accepts: called CC=1 SCAR=0.0
Pod documented
112  
113  
114 sub must_show_location($$)
115 0 ▶{ my ($mode, $reason) = @_;
Subroutine must_show_location: not called CC=6 SCAR=37.4
Pod documented
116 $reason eq 'ASSERT'
117 || $reason eq 'PANIC'
118 || ($mode==2 && $reason_code{$reason} >= $reason_code{WARNING})
119 0 ▶ || ($mode==3 && $reason_code{$reason} >= $reason_code{MISTAKE});
Branch: 1 branch
BranchTrueFalseTotal
$reason eq "ASSERT" or $reason eq "PANIC" or $mode == 2 and $Log::Report::Util::reason_code{$reason} >= $Log::Report::Util::reason_code{"WARNING"} or $mode == 3 and $Log::Report::Util::reason_code{$reason} >= $Log::Report::Util::reason_code{"MISTAKE"} 0 0 0
Condition0%0/15
$reason eq 'ASSERT' || $reason eq 'PANIC' || $mode == 2 && $Log::Report::Util::reason_code{$reason} >= $Log::Report::Util::reason_code{'WARNING'} || $mode == 3 && $Log::Report::Util::reason_code{$reason} >= $Log::Report::Util::reason_code{'MISTAKE'}
l!l&&r!l&&!r
000
$reason eq "ASSERT" or $reason eq "PANIC" or $mode == 2 and $Log::Report::Util::reason_code{$reason} >= $Log::Report::Util::reason_code{"WARNING"}
l!l&&r!l&&!r
000
$reason eq "ASSERT" or $reason eq "PANIC"
l!l&&r!l&&!r
000
$mode == 2 and $Log::Report::Util::reason_code{$reason} >= $Log::Report::Util::reason_code{"WARNING"}
!ll&&!rl&&r
000
$mode == 3 and $Log::Report::Util::reason_code{$reason} >= $Log::Report::Util::reason_code{"MISTAKE"}
!ll&&!rl&&r
000
MC/DC0%0/6
$reason eq 'ASSERT' || $reason eq 'PANIC' || $mode == 2 && $Log::Report::Util::reason_code{$reason} >= $Log::Report::Util::reason_code{'WARNING'} || $mode == 3 && $Log::Report::Util::reason_code{$reason} >= $Log::Report::Util::reason_code{'MISTAKE'}
$reason eq "ASSERT" $reason eq "PANIC" $mode == 2 $Log::Report::Util::reason_code{$reason} >= $Log::Report::Util::reason_code{"WARNING"} $mode == 3 $Log::Report::Util::reason_code{$reason} >= $Log::Report::Util::reason_code{"MISTAKE"}
Truth table0%0/9
A or B or C and D || E and F
A= $reason eq "ASSERT"B= $reason eq "PANIC"C= $mode == 2D= $Log::Report::Util::reason_code{$reason} >= $Log::Report::Util::reason_code{"WARNING"}E= $mode == 3F= $Log::Report::Util::reason_code{$reason} >= $Log::Report::Util::reason_code{"MISTAKE"}
ABCDEFresult
1XXXXX1
01XXXX1
0011XX1
000X111
0010111
000X0X0
000X100
00100X0
0010100
120 }
121  
122  
123 sub must_show_stack($$)
124 0 ▶{ my ($mode, $reason) = @_;
Subroutine must_show_stack: not called CC=5 SCAR=34.0
Pod documented
125 $reason eq 'PANIC'
126 || ($mode==2 && $reason_code{$reason} >= $reason_code{ALERT})
127 0 ▶ || ($mode==3 && $reason_code{$reason} >= $reason_code{ERROR});
Branch: 1 branch
BranchTrueFalseTotal
$reason eq "PANIC" or $mode == 2 and $Log::Report::Util::reason_code{$reason} >= $Log::Report::Util::reason_code{"ALERT"} or $mode == 3 and $Log::Report::Util::reason_code{$reason} >= $Log::Report::Util::reason_code{"ERROR"} 0 0 0
Condition0%0/12
$reason eq 'PANIC' || $mode == 2 && $Log::Report::Util::reason_code{$reason} >= $Log::Report::Util::reason_code{'ALERT'} || $mode == 3 && $Log::Report::Util::reason_code{$reason} >= $Log::Report::Util::reason_code{'ERROR'}
l!l&&r!l&&!r
000
$reason eq "PANIC" or $mode == 2 and $Log::Report::Util::reason_code{$reason} >= $Log::Report::Util::reason_code{"ALERT"}
l!l&&r!l&&!r
000
$mode == 2 and $Log::Report::Util::reason_code{$reason} >= $Log::Report::Util::reason_code{"ALERT"}
!ll&&!rl&&r
000
$mode == 3 and $Log::Report::Util::reason_code{$reason} >= $Log::Report::Util::reason_code{"ERROR"}
!ll&&!rl&&r
000
MC/DC0%0/5
$reason eq 'PANIC' || $mode == 2 && $Log::Report::Util::reason_code{$reason} >= $Log::Report::Util::reason_code{'ALERT'} || $mode == 3 && $Log::Report::Util::reason_code{$reason} >= $Log::Report::Util::reason_code{'ERROR'}
$reason eq "PANIC" $mode == 2 $Log::Report::Util::reason_code{$reason} >= $Log::Report::Util::reason_code{"ALERT"} $mode == 3 $Log::Report::Util::reason_code{$reason} >= $Log::Report::Util::reason_code{"ERROR"}
Truth table0%0/8
A or B and C || D and E
A= $reason eq "PANIC"B= $mode == 2C= $Log::Report::Util::reason_code{$reason} >= $Log::Report::Util::reason_code{"ALERT"}D= $mode == 3E= $Log::Report::Util::reason_code{$reason} >= $Log::Report::Util::reason_code{"ERROR"}
ABCDEresult
1XXXX1
011XX1
00X111
010111
00X0X0
00X100
0100X0
010100
128 }
129  
130 #--------------------
131  
132 5 my %unescape = (
133 '\a' => "\a", '\b' => "\b", '\f' => "\f", '\n' => "\n",
134 '\r' => "\r", '\t' => "\t", '\"' => '"', '\\\\' => '\\',
135 '\e' => "\x1b", '\v' => "\x0b",
136 );
137 5 my %escape = reverse %unescape;
138  
139 sub escape_chars($)
140 0 ▶{ my $str = shift;
Subroutine escape_chars: not called CC=2 SCAR=17.9
Pod documented
141 0 ▶ $str =~ s/([\x00-\x1F\x7F"\\])/$escape{$1} || '?'/ge;
Condition0%0/2
$escape{$1} || '?'
l!l
00
MC/DC0%0/1
$escape{$1} || '?'
$escape{$1}
Truth table0%0/2
A
A= $escape{$1}
Aresult
11
00
142 0 $str;
143 }
144  
145 sub unescape_chars($)
146 0 ▶{ my $str = shift;
Subroutine unescape_chars: not called CC=2 SCAR=17.9
Pod documented
147 0 ▶ $str =~ s/(\\.)/$unescape{$1} || $1/ge;
Condition0%0/3
$unescape{$1} || $1
l!l&&r!l&&!r
000
MC/DC0%0/2
$unescape{$1} || $1
$unescape{$1} $1
Truth table0%0/3
A || B
A= $unescape{$1}B= $1
ABresult
1X1
011
000
148 0 $str;
149 }
150  
151  
152 5 my %tohtml = qw/ > gt < lt " quot & amp /;
153  
154 sub to_html($)
155 1 ▶{ my $s = shift;
Subroutine to_html: called CC=1 SCAR=0.0
Pod documented
156 1 $s =~ s/([<>"&])/\&${tohtml{$1}};/g;
157 1 $s;
158 }
159  
160  
161 sub parse_locale($)
162 11 ▶{ my $locale = shift;
Subroutine parse_locale: called CC=16 SCAR=37.0
Pod documented
163 11 ▶ defined $locale && length $locale
Branch: 1 branch
BranchTrueFalseTotal
defined $locale and length $locale or return 10 1 11
Condition66%2/3
defined $locale and length $locale
!ll&&!rl&&r
0110
MC/DC50%1/2
defined $locale and length $locale
defined $locale length $locale
Truth table66%2/3
A and B
A= defined $localeB= length $locale
ABresult
0X0
100
111
164 or return;
165  
166 10 ▶ if($locale !~
Branch: 1 branch
BranchTrueFalseTotal
unless ($locale =~ /^ ([a-z_]+) (?: \. ([\w-]+) )? # codeset (?: \@ (\S+) )? # modifier $/xi) 0 10 10
167 m/^ ([a-z_]+)
168 (?: \. ([\w-]+) )? # codeset
169 (?: \@ (\S+) )? # modifier
170 $/ix)
171 { # Windows Finnish_Finland.1252?
172 0 $locale =~ s/.*\.//;
173 0 ▶ return wantarray ? ($locale) : { language => $locale };
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
174 }
175  
176 10 my ($lang, $codeset, $modifier) = ($1, $2, $3);
177  
178 10 my @subtags = split /[_-]/, $lang;
179 10 my $primary = lc shift @subtags;
180  
181 10 ▶ my $language
Branch: 5 branches
BranchTrueFalseTotal
$primary eq 'c' ? : 1 9 10
$primary eq 'posix' ? : 1 8 9
$primary =~ /^[a-z]{2,3}$/ ? : 8 0 8
$primary eq 'i' && @subtags ? : 0 0 0
$primary eq 'x' && @subtags ? : 0 0 0
Condition0%0/6
$primary eq "i" and @subtags
!ll&&!rl&&r
000
$primary eq "x" and @subtags
!ll&&!rl&&r
000
MC/DC0%0/2
$primary eq "i" and @subtags
$primary eq "i" @subtags
MC/DC0%0/2
$primary eq "x" and @subtags
$primary eq "x" @subtags
Truth table0%0/3
A and B
A= $primary eq "i"B= @subtags
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= $primary eq "x"B= @subtags
ABresult
0X0
100
111
182 = $primary eq 'c' ? 'C'
183 : $primary eq 'posix' ? 'POSIX'
184 : $primary =~ m/^[a-z]{2,3}$/ ? $primary # ISO639-1 and -2
185 : $primary eq 'i' && @subtags ? lc(shift @subtags) # IANA
186 : $primary eq 'x' && @subtags ? lc(shift @subtags) # Private
187 : error__x"unknown locale language in locale `{locale}'", locale => $locale;
188  
189 10 my $script;
190 10 ▶ $script = ucfirst lc shift @subtags
Branch: 1 branch
BranchTrueFalseTotal
if @subtags > 1 and length $subtags[0] > 3 0 10 10
Condition33%1/3
@subtags > 1 and length $subtags[0] > 3
!ll&&!rl&&r
1000
MC/DC0%0/2
@subtags > 1 and length $subtags[0] > 3
@subtags > 1 length $subtags[0] > 3
Truth table33%1/3
A and B
A= @subtags > 1B= length $subtags[0] > 3
ABresult
0X0
100
111
191 if @subtags > 1 && length $subtags[0] > 3;
192  
193 10 ▶ my $territory = @subtags ? uc(shift @subtags) : undef;
Branch: 1 branch
BranchTrueFalseTotal
@subtags ? : 4 6 10
194  
195 10 ▶ return ($language, $territory, $codeset, $modifier)
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 10 0 10
196 if wantarray;
197  
198 +{
199 0 language => $language,
200 script => $script,
201 territory => $territory,
202 codeset => $codeset,
203 modifier => $modifier,
204 variant => join('-', @subtags),
205 };
206 }
207  
208  
209 5 my %pkg2domain;
210 sub pkg2domain($;$$$)
211 8 ▶{ my $pkg = shift;
Subroutine pkg2domain: called CC=5 SCAR=16.4
Pod documented
212 8 my $d = $pkg2domain{$pkg};
213 8 ▶ @_ or return $d ? $d->[0] : 'default';
Branch: 2 branches
BranchTrueFalseTotal
@_ or return $d ? $d->[0] : "default" 5 3 8
$d ? : 3 0 3
214  
215 5 my ($domain, $fn, $line) = @_;
216 5 ▶ if($d)
Branch: 1 branch
BranchTrueFalseTotal
if ($d) 1 4 5
217 { # registration already exists
218 1 ▶ return $domain if $d->[0] eq $domain;
Branch: 1 branch
BranchTrueFalseTotal
if $d->[0] eq $domain 1 0 1
219 0 printi "conflict: package {pkg} in translation domain {domain1} in {file1} line {line1}, but in {domain2} in {file2} line {line2}",
220 pkg => $pkg, domain1 => $domain, file1 => $fn, line1 => $line,
221 domain2 => $d->[0], file2 => $d->[1], line2 => $d->[2];
222 }
223  
224 # new registration
225 4 $pkg2domain{$pkg} = [$domain, $fn, $line];
226 4 $domain;
227 }
228  
229 5 1;
↑ Summary Minimal.pm »