ger.pm

filter: Statementstmt 93.3% 84 / 90 Branchbran 75.0% 39 / 52 Conditioncond 100% 5 / 5 MC/DCmcdc 100% 3 / 3 Subroutinesub 77.7% 7 / 9 Podpod 0.0% 0 / 3 totaltotal 85.1% 138 / 162 CC 32 SCAR 23.5
CC
32
Cov
87%
install_routines
15.1
add_target
14.1
_set_default_null_routines
4.0
CRAP 10.5
« ger/Format/Default.pm ↑ Summary ger/Plugin/MultilevelLog.pm »
1 ## no critic: TestingAndDebugging::RequireUseStrict
2 package Log::ger;
3  
4 11 our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
5 11 our $DATE = '2023-12-29'; # DATE
6 11 our $DIST = 'Log-ger'; # DIST
7 11 our $VERSION = '0.042'; # VERSION
8  
9 #IFUNBUILT
10 # use strict 'subs', 'vars';
11 # use warnings;
12 #END IFUNBUILT
13  
14 11 our $re_addr = qr/\(0x([0-9a-f]+)/o;
15  
16 11 our %Levels = (
17 fatal => 10,
18 error => 20,
19 warn => 30,
20 info => 40,
21 debug => 50,
22 trace => 60,
23 );
24  
25 11 our %Level_Aliases = (
26 off => 0,
27 warning => 30,
28 );
29  
30 11 our $Current_Level = 30;
31  
32 11 our $Caller_Depth_Offset = 0;
33  
34 # a flag that can be used by null output to skip using formatter
35 11 our $_outputter_is_null;
36  
37 11 our $_dumper;
38  
39 11 our %Global_Hooks;
40  
41 # in Log/ger/Heavy.pm
42 # our %Default_Hooks = (
43  
44 11 our %Package_Targets; # key = package name, value = \%per_target_conf
45 11 our %Per_Package_Hooks; # key = package name, value = { phase => hooks, ... }
46  
47 11 our %Hash_Targets; # key = hash address, value = [$hashref, \%per_target_conf]
48 11 our %Per_Hash_Hooks; # key = hash address, value = { phase => hooks, ... }
49  
50 11 our %Object_Targets; # key = object address, value = [$obj, \%per_target_conf]
51 11 our %Per_Object_Hooks; # key = object address, value = { phase => hooks, ... }
52  
53 2 ▶my $sub0 = sub {0};
Subroutine __ANON__: called CC=1 SCAR=0.0
54 0 ▶my $sub1 = sub {1};
Subroutine __ANON__: not called CC=1 SCAR=1.2
55 11 my $default_null_routines;
56  
57 53 ▶sub install_routines {
Subroutine install_routines: called CC=15 SCAR=27.1
Pod undocumented
58 53 my ($target, $target_arg, $routines, $name_routines) = @_;
59  
60 53 ▶ if ($name_routines && !defined &subname) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name_routines and not defined &subname) 2 51 53
Condition100%3/3
$name_routines and not defined &subname
!ll&&!rl&&r
2492
MC/DC100%2/2
$name_routines and not defined &subname
$name_routines not defined &subname
Truth table100%3/3
A and not(B)
A= $name_routinesB= not defined &subname
ABresult
0X0
100
111
61 2 ▶ if (eval { require Sub::Name; 1 }) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { require Sub::Name; 1 } }) { } 2 0 2
62 2 *subname = \&Sub::Name::subname;
63 } else {
64 0 ▶ *subname = sub {};
Subroutine __ANON__: not called CC=1 SCAR=6.9
65 }
66 }
67  
68 53 ▶ if ($target eq 'package') {
Branch: 3 branches
BranchTrueFalseTotal
if ($target eq 'package') { } 22 31 53
elsif ($target eq 'object') { } 26 5 31
elsif ($target eq 'hash') { } 5 0 5
69 #IFUNBUILT
70 # no warnings 'redefine';
71 #END IFUNBUILT
72 22 for my $r (@$routines) {
73 278 my ($code, $name, $lnum, $type) = @$r;
74 278 ▶ next unless $type =~ /_sub\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $type =~ /_sub\z/ 12 266 278
75 #print "D:installing $name to package $target_arg\n";
76 266 *{"$target_arg\::$name"} = $code;
77 266 ▶ subname("$target_arg\::$name", $code) if $name_routines;
Branch: 1 branch
BranchTrueFalseTotal
if $name_routines 254 12 266
78 }
79 } elsif ($target eq 'object') {
80 #IFUNBUILT
81 # no warnings 'redefine';
82 #END IFUNBUILT
83 26 my $pkg = ref $target_arg;
84 26 for my $r (@$routines) {
85 324 my ($code, $name, $lnum, $type) = @$r;
86 324 ▶ next unless $type =~ /_method\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $type =~ /_method\z/ 12 312 324
87 312 *{"$pkg\::$name"} = $code;
88 312 ▶ subname("$pkg\::$name", $code) if $name_routines;
Branch: 1 branch
BranchTrueFalseTotal
if $name_routines 300 12 312
89 }
90 } elsif ($target eq 'hash') {
91 5 for my $r (@$routines) {
92 60 my ($code, $name, $lnum, $type) = @$r;
93 60 ▶ next unless $type =~ /_sub\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $type =~ /_sub\z/ 30 30 60
94 30 $target_arg->{$name} = $code;
95 }
96 }
97 }
98  
99 5 ▶sub add_target {
Subroutine add_target: called CC=11 SCAR=26.4
Pod undocumented
100 5 my ($target_type, $target_name, $per_target_conf, $replace) = @_;
101 5 ▶ $replace = 1 unless defined $replace;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $replace 4 1 5
102  
103 5 ▶ if ($target_type eq 'package') {
Branch: 3 branches
BranchTrueFalseTotal
if ($target_type eq 'package') { } 3 2 5
elsif ($target_type eq 'object') { } 1 1 2
elsif ($target_type eq 'hash') { } 1 0 1
104 3 ▶ unless ($replace) { return if $Package_Targets{$target_name} }
Branch: 2 branches
BranchTrueFalseTotal
unless ($replace) 1 2 3
if $Package_Targets{$target_name} 0 1 1
105 3 $Package_Targets{$target_name} = $per_target_conf;
106 } elsif ($target_type eq 'object') {
107 1 my ($addr) = "$target_name" =~ $re_addr;
108 0 ▶ unless ($replace) { return if $Object_Targets{$addr} }
Branch: 2 branches
BranchTrueFalseTotal
unless ($replace) 0 1 1
if $Object_Targets{$addr} 0 0 0
109 1 $Object_Targets{$addr} = [$target_name, $per_target_conf];
110 } elsif ($target_type eq 'hash') {
111 1 my ($addr) = "$target_name" =~ $re_addr;
112 0 ▶ unless ($replace) { return if $Hash_Targets{$addr} }
Branch: 2 branches
BranchTrueFalseTotal
unless ($replace) 0 1 1
if $Hash_Targets{$addr} 0 0 0
113 1 $Hash_Targets{$addr} = [$target_name, $per_target_conf];
114 }
115 }
116  
117 2 ▶sub _set_default_null_routines {
Subroutine _set_default_null_routines: called CC=4 SCAR=13.9
118 $default_null_routines ||= [
119 2 ▶ (map {(
Condition100%2/2
$default_null_routines ||= [map({[$sub0, "log_$_", $Levels{$_}, "logger_sub"], [$Levels{$_} > $Current_Level ? $sub0 : $sub1, "log_is_$_", $Levels{$_}, "level_checker_sub"], [$sub0, $_, $Levels{$_}, "logger_method"], [$Levels{$_} > $Current_Level ? $sub0 : $sub1, "is_$_", $Levels{$_}, "level_checker_method"];} keys %Levels)]
l!l
11
MC/DC100%1/1
$default_null_routines ||= [map({[$sub0, "log_$_", $Levels{$_}, "logger_sub"], [$Levels{$_} > $Current_Level ? $sub0 : $sub1, "log_is_$_", $Levels{$_}, "level_checker_sub"], [$sub0, $_, $Levels{$_}, "logger_method"], [$Levels{$_} > $Current_Level ? $sub0 : $sub1, "is_$_", $Levels{$_}, "level_checker_method"];} keys %Levels)]
$default_null_routines
Truth table100%2/2
A
A= $default_null_routines
Aresult
11
00
120 [$sub0, "log_$_", $Levels{$_}, 'logger_sub'],
121 [$Levels{$_} > $Current_Level ? $sub0 : $sub1, "log_is_$_", $Levels{$_}, 'level_checker_sub'],
122 [$sub0, $_, $Levels{$_}, 'logger_method'],
123 6 ▶ [$Levels{$_} > $Current_Level ? $sub0 : $sub1, "is_$_", $Levels{$_}, 'level_checker_method'],
Branch: 2 branches
BranchTrueFalseTotal
$Levels{$_} > $Current_Level ? : 3 3 6
$Levels{$_} > $Current_Level ? : 3 3 6
124 )} keys %Levels),
125 ];
126 }
127  
128 1 ▶sub get_logger {
Subroutine get_logger: called CC=3 SCAR=11.3
Pod undocumented
129 1 my ($package, %per_target_conf) = @_;
130  
131 1 my $caller = caller(0);
132 $per_target_conf{category} = $caller
133 1 ▶ if !defined($per_target_conf{category});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $per_target_conf{"category"} 1 0 1
134 1 my $obj = []; $obj =~ $re_addr;
135 1 my $pkg = "Log::ger::Obj$1"; bless $obj, $pkg;
136 1 add_target(object => $obj, \%per_target_conf);
137 1 ▶ if (keys %Global_Hooks) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %Global_Hooks) { } 0 1 1
138 0 require Log::ger::Heavy;
139 0 init_target(object => $obj, \%per_target_conf);
140 } else {
141 # if we haven't added any hooks etc, skip init_target() process and use
142 # this preconstructed routines as shortcut, to save startup overhead
143 1 _set_default_null_routines();
144 1 install_routines(object => $obj, $default_null_routines, 0);
145 }
146 1 $obj; # XXX add DESTROY to remove from list of targets
147 }
148  
149 2 ▶sub _import_to {
Subroutine _import_to: called CC=3 SCAR=11.0
150 2 my ($package, $target_pkg, %per_target_conf) = @_;
151  
152 $per_target_conf{category} = $target_pkg
153 2 ▶ if !defined($per_target_conf{category});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $per_target_conf{"category"} 2 0 2
154 2 add_target(package => $target_pkg, \%per_target_conf);
155 2 ▶ if (keys %Global_Hooks) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %Global_Hooks) { } 1 1 2
156 1 require Log::ger::Heavy;
157 1 init_target(package => $target_pkg, \%per_target_conf);
158 } else {
159 # if we haven't added any hooks etc, skip init_target() process and use
160 # this preconstructed routines as shortcut, to save startup overhead
161 1 _set_default_null_routines();
162 1 install_routines(package => $target_pkg, $default_null_routines, 0);
163 }
164 }
165  
166 2 ▶sub import {
Subroutine import: called CC=1 SCAR=0.0
167 2 my ($package, %per_target_conf) = @_;
168  
169 2 my $caller = caller(0);
170 2 $package->_import_to($caller, %per_target_conf);
171 }
172  
173 11 1;
174 # ABSTRACT: A lightweight, flexible logging framework
175  
176 __END__
« ger/Format/Default.pm ↑ Summary ger/Plugin/MultilevelLog.pm »