ger/Util.pm

filter: Statementstmt 56.4% 101 / 179 Branchbran 39.0% 32 / 82 Conditioncond 25.0% 4 / 16 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 55.1% 16 / 29 Podpod 100% 18 / 18 totaltotal 51.5% 171 / 332 CC 58 SCAR 36.2
CC
58
Cov
49%
_action_on_hooks
104.5
set_plugin
46.8
reinit_target
37.1
CRAP 37.4
« ger/Heavy.pm ↑ Summary ger/Format/Default.pm »
1 package Log::ger::Util;
2  
3 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 10 our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
7 10 our $DATE = '2023-12-29'; # DATE
8 10 our $DIST = 'Log-ger'; # DIST
9 10 our $VERSION = '0.042'; # VERSION
10  
11 10 require Log::ger;
12 10 require Log::ger::Heavy;
13  
14 0 ▶sub _dump {
Subroutine _dump: not called CC=3 SCAR=20.3
15 0 ▶ unless ($Log::ger::_dumper) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($Log::ger::_dumper) 0 0 0
16 0 eval {
17 10 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
18 0 require Data::Dmp;
19 0 $Data::Dmp::OPT_REMOVE_PRAGMAS = 1;
20 0 1;
21 };
22 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 0 0
23 10 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
24 0 require Data::Dumper;
25 0 ▶ $Log::ger::_dumper = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
26 0 local $Data::Dumper::Terse = 1;
27 0 local $Data::Dumper::Indent = 0;
28 0 local $Data::Dumper::Useqq = 1;
29 0 local $Data::Dumper::Deparse = 1;
30 0 local $Data::Dumper::Quotekeys = 0;
31 0 local $Data::Dumper::Sortkeys = 1;
32 0 local $Data::Dumper::Trailingcomma = 1;
33 0 local $Data::Dumper::Useqq = 1; # to show "\034", possible bug in Data::Dumper
34 0 Data::Dumper::Dumper($_[0]);
35 0 };
36 } else {
37 0 ▶ $Log::ger::_dumper = sub { Data::Dmp::dmp($_[0]) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
38 }
39 }
40 0 $Log::ger::_dumper->($_[0]);
41 }
42  
43 14 ▶sub numeric_level {
Subroutine numeric_level: called CC=4 SCAR=17.1
Pod documented
44 14 my $level = shift;
45 14 ▶ return $level if $level =~ /\A\d+\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $level =~ /\A\d+\z/ 13 1 14
46 return $Log::ger::Levels{$level}
47 1 ▶ if defined $Log::ger::Levels{$level};
Branch: 1 branch
BranchTrueFalseTotal
if defined $Log::ger::Levels{$level} 1 0 1
48 return $Log::ger::Level_Aliases{$level}
49 0 ▶ if defined $Log::ger::Level_Aliases{$level};
Branch: 1 branch
BranchTrueFalseTotal
if defined $Log::ger::Level_Aliases{$level} 0 0 0
50 0 die "Unknown level '$level'";
51 }
52  
53 3 ▶sub string_level {
Subroutine string_level: called CC=5 SCAR=16.1
Pod documented
54 3 my $level = shift;
55 3 ▶ return $level if defined $Log::ger::Levels{$level};
Branch: 1 branch
BranchTrueFalseTotal
if defined $Log::ger::Levels{$level} 1 2 3
56 $level = $Log::ger::Level_Aliases{$level}
57 2 ▶ if defined $Log::ger::Level_Aliases{$level};
Branch: 1 branch
BranchTrueFalseTotal
if defined $Log::ger::Level_Aliases{$level} 1 1 2
58 2 for (keys %Log::ger::Levels) {
59 10 my $v = $Log::ger::Levels{$_};
60 10 ▶ return $_ if $v == $level;
Branch: 1 branch
BranchTrueFalseTotal
if $v == $level 2 8 10
61 }
62 0 die "Unknown level '$level'";
63 }
64  
65 10 ▶sub set_level {
Subroutine set_level: called CC=1 SCAR=0.0
Pod documented
66 10 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
67 10 $Log::ger::Current_Level = numeric_level(shift);
68 10 reinit_all_targets();
69 }
70  
71 12 ▶sub _action_on_hooks {
Subroutine _action_on_hooks: called CC=16 SCAR=46.5
72 10 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
73  
74 12 my ($action, $target_type, $target_name, $phase) = splice @_, 0, 4;
75  
76 12 ▶ my $hooks = $Log::ger::Global_Hooks{$phase} or die "Unknown phase '$phase'";
Branch: 1 branch
BranchTrueFalseTotal
my $hooks = $Log::ger::Global_Hooks{$phase} or die "Unknown phase '${phase}'" 12 0 12
77 12 ▶ if ($target_type eq 'package') {
Branch: 3 branches
BranchTrueFalseTotal
if ($target_type eq 'package') { } 3 9 12
elsif ($target_type eq 'object') { } 0 9 9
elsif ($target_type eq 'hash') { } 0 9 9
78 3 ▶ $hooks = ($Log::ger::Per_Package_Hooks{$target_name}{$phase} ||= []);
Condition50%1/2
$Log::ger::Per_Package_Hooks{$target_name}{$phase} ||= []
l!l
03
MC/DC0%0/1
$Log::ger::Per_Package_Hooks{$target_name}{$phase} ||= []
$Log::ger::Per_Package_Hooks{$target_name}{$phase}
Truth table50%1/2
A
A= $Log::ger::Per_Package_Hooks{$target_name}{$phase}
Aresult
11
00
79 } elsif ($target_type eq 'object') {
80 0 my ($addr) = $target_name =~ $Log::ger::re_addr;
81 0 ▶ $hooks = ($Log::ger::Per_Object_Hooks{$addr}{$phase} ||= []);
Condition0%0/2
$Log::ger::Per_Object_Hooks{$addr}{$phase} ||= []
l!l
00
MC/DC0%0/1
$Log::ger::Per_Object_Hooks{$addr}{$phase} ||= []
$Log::ger::Per_Object_Hooks{$addr}{$phase}
Truth table0%0/2
A
A= $Log::ger::Per_Object_Hooks{$addr}{$phase}
Aresult
11
00
82 } elsif ($target_type eq 'hash') {
83 0 my ($addr) = $target_name =~ $Log::ger::re_addr;
84 0 ▶ $hooks = ($Log::ger::Per_Hash_Hooks{$addr}{$phase} ||= []);
Condition0%0/2
$Log::ger::Per_Hash_Hooks{$addr}{$phase} ||= []
l!l
00
MC/DC0%0/1
$Log::ger::Per_Hash_Hooks{$addr}{$phase} ||= []
$Log::ger::Per_Hash_Hooks{$addr}{$phase}
Truth table0%0/2
A
A= $Log::ger::Per_Hash_Hooks{$addr}{$phase}
Aresult
11
00
85 }
86  
87 12 ▶ if ($action eq 'add') {
Branch: 6 branches
BranchTrueFalseTotal
if ($action eq 'add') { } 12 0 12
elsif ($action eq 'remove') { } 0 0 0
elsif ($action eq 'reset') { } 0 0 0
elsif ($action eq 'empty') { } 0 0 0
elsif ($action eq 'save') { } 0 0 0
elsif ($action eq 'restore') { } 0 0 0
88 12 my $hook = shift;
89 # XXX remove duplicate key
90 # my $key = $hook->[0];
91 12 unshift @$hooks, $hook;
92 } elsif ($action eq 'remove') {
93 0 my $code = shift;
94 0 for my $i (reverse 0..$#{$hooks}) {
95 0 ▶ splice @$hooks, $i, 1 if $code->($hooks->[$i]);
Branch: 1 branch
BranchTrueFalseTotal
if &$code($hooks->[$i]) 0 0 0
96 }
97 } elsif ($action eq 'reset') {
98 0 my $saved = [@$hooks];
99 splice @$hooks, 0, scalar(@$hooks),
100 0 @{ $Log::ger::Default_Hooks{$phase} };
101 0 return $saved;
102 } elsif ($action eq 'empty') {
103 0 my $saved = [@$hooks];
104 0 splice @$hooks, 0;
105 0 return $saved;
106 } elsif ($action eq 'save') {
107 0 return [@$hooks];
108 } elsif ($action eq 'restore') {
109 0 my $saved = shift;
110 0 splice @$hooks, 0, scalar(@$hooks), @$saved;
111 0 return $saved;
112 }
113 }
114  
115 9 ▶sub add_hook {
Subroutine add_hook: called CC=1 SCAR=0.0
Pod documented
116 9 my ($phase, $hook) = @_;
117 9 _action_on_hooks('add', '', undef, $phase, $hook);
118 }
119  
120 3 ▶sub add_per_target_hook {
Subroutine add_per_target_hook: called CC=1 SCAR=0.0
Pod documented
121 3 my ($target_type, $target_name, $phase, $hook) = @_;
122 3 _action_on_hooks('add', $target_type, $target_name, $phase, $hook);
123 }
124  
125 0 ▶sub remove_hook {
Subroutine remove_hook: not called CC=1 SCAR=6.9
Pod documented
126 0 my ($phase, $code) = @_;
127 0 _action_on_hooks('remove', '', undef, $phase, $code);
128 }
129  
130 0 ▶sub remove_per_target_hook {
Subroutine remove_per_target_hook: not called CC=1 SCAR=6.9
Pod documented
131 0 my ($target_type, $target_name, $phase, $code) = @_;
132 0 _action_on_hooks('remove', $target_type, $target_name, $phase, $code);
133 }
134  
135 0 ▶sub reset_hooks {
Subroutine reset_hooks: not called CC=1 SCAR=6.9
Pod documented
136 0 my ($phase) = @_;
137 0 _action_on_hooks('reset', '', undef, $phase);
138 }
139  
140 0 ▶sub reset_per_target_hooks {
Subroutine reset_per_target_hooks: not called CC=1 SCAR=6.9
Pod documented
141 0 my ($target_type, $target_name, $phase) = @_;
142 0 _action_on_hooks('reset', $target_type, $target_name, $phase);
143 }
144  
145 0 ▶sub empty_hooks {
Subroutine empty_hooks: not called CC=1 SCAR=6.9
Pod documented
146 0 my ($phase) = @_;
147 0 _action_on_hooks('empty', '', undef, $phase);
148 }
149  
150 0 ▶sub empty_per_target_hooks {
Subroutine empty_per_target_hooks: not called CC=1 SCAR=6.9
Pod documented
151 0 my ($target_type, $target_name, $phase) = @_;
152 0 _action_on_hooks('empty', $target_type, $target_name, $phase);
153 }
154  
155 0 ▶sub save_hooks {
Subroutine save_hooks: not called CC=1 SCAR=6.9
Pod documented
156 0 my ($phase) = @_;
157 0 _action_on_hooks('save', '', undef, $phase);
158 }
159  
160 0 ▶sub save_per_target_hooks {
Subroutine save_per_target_hooks: not called CC=1 SCAR=6.9
Pod documented
161 0 my ($target_type, $target_name, $phase) = @_;
162 0 _action_on_hooks('save', $target_type, $target_name, $phase);
163 }
164  
165 0 ▶sub restore_hooks {
Subroutine restore_hooks: not called CC=1 SCAR=6.9
Pod documented
166 0 my ($phase, $saved) = @_;
167 0 _action_on_hooks('restore', '', undef, $phase, $saved);
168 }
169  
170 0 ▶sub restore_per_target_hooks {
Subroutine restore_per_target_hooks: not called CC=1 SCAR=6.9
Pod documented
171 0 my ($target_type, $target_name, $phase, $saved) = @_;
172 0 _action_on_hooks('restore', $target_type, $target_name, $phase, $saved);
173 }
174  
175 1 ▶sub reinit_target {
Subroutine reinit_target: called CC=8 SCAR=36.1
Pod documented
176 1 my ($target_type, $target_name) = @_;
177  
178 # adds target if not already exists
179 1 Log::ger::add_target($target_type, $target_name, {}, 0);
180  
181 1 ▶ if ($target_type eq 'package') {
Branch: 3 branches
BranchTrueFalseTotal
if ($target_type eq 'package') { } 1 0 1
elsif ($target_type eq 'object') { } 0 0 0
elsif ($target_type eq 'hash') { } 0 0 0
182 1 my $per_target_conf = $Log::ger::Package_Targets{$target_name};
183 1 Log::ger::init_target(package => $target_name, $per_target_conf);
184 } elsif ($target_type eq 'object') {
185 0 ▶ my ($obj_addr) = $target_name =~ $Log::ger::re_addr
Branch: 1 branch
BranchTrueFalseTotal
my($obj_addr) = $target_name =~ /$Log::ger::re_addr/ or die "Invalid object '${target_name}': not a reference" 0 0 0
186 or die "Invalid object '$target_name': not a reference";
187 0 ▶ my $v = $Log::ger::Object_Targets{$obj_addr}
Branch: 1 branch
BranchTrueFalseTotal
my $v = $Log::ger::Object_Targets{$obj_addr} or die "Unknown object target '${target_name}'" 0 0 0
188 or die "Unknown object target '$target_name'";
189 0 Log::ger::init_target(object => $v->[0], $v->[1]);
190 } elsif ($target_type eq 'hash') {
191 0 ▶ my ($hash_addr) = $target_name =~ $Log::ger::re_addr
Branch: 1 branch
BranchTrueFalseTotal
my($hash_addr) = $target_name =~ /$Log::ger::re_addr/ or die "Invalid hashref '${target_name}': not a reference" 0 0 0
192 or die "Invalid hashref '$target_name': not a reference";
193 0 ▶ my $v = $Log::ger::Hash_Targets{$hash_addr}
Branch: 1 branch
BranchTrueFalseTotal
my $v = $Log::ger::Hash_Targets{$hash_addr} or die "Unknown hash target '${target_name}'" 0 0 0
194 or die "Unknown hash target '$target_name'";
195 0 Log::ger::init_target(hash => $v->[0], $v->[1]);
196 } else {
197 0 die "Unknown target type '$target_type'";
198 }
199 }
200  
201 19 ▶sub reinit_all_targets {
Subroutine reinit_all_targets: called CC=4 SCAR=13.9
Pod documented
202 19 for my $pkg (keys %Log::ger::Package_Targets) {
203 #print "D:reinit package $pkg\n";
204 Log::ger::init_target(
205 18 package => $pkg, $Log::ger::Package_Targets{$pkg});
206 }
207 19 for my $k (keys %Log::ger::Object_Targets) {
208 18 my ($obj, $per_target_conf) = @{ $Log::ger::Object_Targets{$k} };
209 18 Log::ger::init_target(object => $obj, $per_target_conf);
210 }
211 19 for my $k (keys %Log::ger::Hash_Targets) {
212 4 my ($hash, $per_target_conf) = @{ $Log::ger::Hash_Targets{$k} };
213 4 Log::ger::init_target(hash => $hash, $per_target_conf);
214 }
215 }
216  
217 10 ▶sub set_plugin {
Subroutine set_plugin: called CC=24 SCAR=38.5
Pod documented
218 10 my %args = @_;
219  
220 10 my $hooks;
221 10 ▶ if ($args{hooks}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'hooks'}) { } 0 10 10
222 0 $hooks = $args{hooks};
223 } else {
224 10 ▶ no strict 'refs'; ## no critic: TestingAndDebugging::ProhibitNoStrict
Subroutine BEGIN: called CC=1 SCAR=0.0
225 10 ▶ my $prefix = $args{prefix} || 'Log::ger::Plugin::';
Condition50%1/2
$args{'prefix'} || 'Log::ger::Plugin::'
l!l
100
MC/DC0%0/1
$args{'prefix'} || 'Log::ger::Plugin::'
$args{'prefix'}
Truth table50%1/2
A
A= $args{'prefix'}
Aresult
11
00
226 10 my $mod = $args{name};
227 10 ▶ $mod = $prefix . $mod unless index($mod, $prefix) == 0;
Branch: 1 branch
BranchTrueFalseTotal
unless index($mod, $prefix) == 0 10 0 10
228 10 (my $mod_pm = "$mod.pm") =~ s!::!/!g;
229 10 require $mod_pm;
230 10 ▶ my $meta = $mod->can("meta") ? $mod->meta : {v=>1};
Branch: 1 branch
BranchTrueFalseTotal
$mod->can('meta') ? : 10 0 10
231 10 ▶ my $v = $meta->{v} || 1;
Condition50%1/2
$meta->{'v'} || 1
l!l
100
MC/DC0%0/1
$meta->{'v'} || 1
$meta->{'v'}
Truth table50%1/2
A
A= $meta->{'v'}
Aresult
11
00
232  
233 # history of v bumping:
234 #
235 # - v increased from 1 to 2 in Log::ger v0.037 to force all plugins that
236 # were not compatible with Log::ger 0.032 (removed
237 # create_logml_routine phase) to be upgraded.
238  
239 10 ▶ unless ($v == 2) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($v == 2) 0 10 10
240 0 ▶ die "Plugin '$mod' (version ".(${"$mod\::VERSION"} || "dev").")".
Condition0%0/2
${'Log::ger::Util::VERSION';} || 'dev'
l!l
00
MC/DC0%0/1
${'Log::ger::Util::VERSION';} || 'dev'
${'Log::ger::Util::VERSION';}
Truth table0%0/2
A
A= ${'Log::ger::Util::VERSION';}
Aresult
11
00
241 " follows meta version $v but Log::ger (version ".
242 0 ▶ (${__PACKAGE__."::VERSION"} || "dev").
Condition0%0/2
${"${mod}::VERSION";} || 'dev'
l!l
00
MC/DC0%0/1
${"${mod}::VERSION";} || 'dev'
${"${mod}::VERSION";}
Truth table0%0/2
A
A= ${"${mod}::VERSION";}
Aresult
11
00
243 ") (>0.032) requires meta version 2, ".
244 "please upgrade the plugin first";
245 }
246 10 ▶ $hooks = &{"$mod\::get_hooks"}(%{ $args{conf} || {} });
Condition50%1/2
$args{'conf'} || {}
l!l
100
MC/DC0%0/1
$args{'conf'} || {}
$args{'conf'}
Truth table50%1/2
A
A= $args{'conf'}
Aresult
11
00
247 }
248  
249 {
250 10 ▶ last unless $args{replace_package_regex};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"replace_package_regex"} 1 9 10
251 9 my $all_hooks;
252 9 ▶ if (!$args{target}) {
Branch: 4 branches
BranchTrueFalseTotal
if (not $args{'target'}) { } 9 0 9
elsif ($args{'target'} eq 'package') { } 0 0 0
elsif ($args{'target'} eq 'object') { } 0 0 0
elsif ($args{'target'} eq 'hash') { } 0 0 0
253 9 $all_hooks = \%Log::ger::Global_Hooks;
254 } elsif ($args{target} eq 'package') {
255 0 $all_hooks = $Log::ger::Per_Package_Hooks{ $args{target_arg} };
256 } elsif ($args{target} eq 'object') {
257 0 my ($addr) = $args{target_arg} =~ $Log::ger::re_addr;
258 0 $all_hooks = $Log::ger::Per_Object_Hooks{$addr};
259 } elsif ($args{target} eq 'hash') {
260 0 my ($addr) = $args{target_arg} =~ $Log::ger::re_addr;
261 0 $all_hooks = $Log::ger::Per_Hash_Hooks{$addr};
262 }
263 9 ▶ last unless $all_hooks;
Branch: 1 branch
BranchTrueFalseTotal
unless $all_hooks 0 9 9
264 9 for my $phase (keys %$all_hooks) {
265 90 my $hooks = $all_hooks->{$phase};
266 90 for my $i (reverse 0..$#{$hooks}) {
267 splice @$hooks, $i, 1
268 43 ▶ if $hooks->[$i][0] =~ $args{replace_package_regex};
Branch: 1 branch
BranchTrueFalseTotal
if $hooks->[$i][0] =~ /$args{"replace_package_regex"}/ 7 36 43
269 }
270 }
271 }
272  
273 10 for my $phase (keys %$hooks) {
274 12 my $hook = $hooks->{$phase};
275 12 ▶ if (defined $args{target}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{'target'}) { } 3 9 12
276 add_per_target_hook(
277 3 $args{target}, $args{target_arg}, $phase, $hook);
278 } else {
279 9 add_hook($phase, $hook);
280 }
281 }
282  
283 10 my $reinit = $args{reinit};
284 10 ▶ $reinit = 1 unless defined $reinit;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $reinit 10 0 10
285 10 ▶ if ($reinit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($reinit) 10 0 10
286 10 ▶ if (defined $args{target}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{'target'}) { } 1 9 10
287 1 reinit_target($args{target}, $args{target_arg});
288 } else {
289 9 reinit_all_targets();
290 }
291 }
292 }
293  
294 10 1;
295 # ABSTRACT: Utility routines for Log::ger
296  
297 __END__
« ger/Heavy.pm ↑ Summary ger/Format/Default.pm »