Usul/Log.pm

filter: Statementstmt 94.1% 113 / 120 Branchbran 67.3% 35 / 52 Conditioncond 45.8% 11 / 24 MC/DCmcdc 14.2% 2 / 14 Subroutinesub 92.0% 23 / 25 Podpod 100% 3 / 3 totaltotal 78.5% 187 / 238 CC 39 SCAR 18.5
CC
39
Cov
81%
__ANON__
12.0
__ANON__
11.4
log
9.0
CRAP 6.3
« Usul/TraitFor/ConnectInfo.pm ↑ Summary Usul/Functions.pm »
1 package Class::Usul::Log;
2  
3 4 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 4 ▶use Class::Usul::Constants qw( FALSE LOG_LEVELS NUL SPC TRUE );
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use Class::Usul::Functions qw( is_hashref is_member merge_attributes
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 untaint_identifier );
8 4 ▶use Class::Usul::Types qw( Bool DataEncoding HashRef
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 LoadableClass Logger SimpleStr Undef );
10 4 ▶use Encode qw( encode );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use File::Basename qw( dirname );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 ▶use File::DataClass::Types qw( Path );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 4 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
14 4 ▶use Sub::Install qw( install_sub );
Subroutine BEGIN: called CC=1 SCAR=0.0
15 4 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 # Attribute constructors
18 6 ▶my $_build__log = sub {
Subroutine _build__log: called CC=1 SCAR=0.0
19 6 return $_[ 0 ]->_log_class->new( %{ $_[ 0 ]->_log_attributes } );
20 4 };
21  
22 3 ▶my $_build__log_class = sub {
Subroutine __ANON__: called CC=2 SCAR=7.6
Subroutine _build__log_class: called CC=2 SCAR=7.6
23 3 ▶ return $_[ 0 ]->_logfile ? 'Log::Handler' : 'Class::Null';
Branch: 2 branches
BranchTrueFalseTotal
$_[0]->_logfile ? : 3 0 3
$_[0]->_logfile ? : 4 0 4
24 4 };
25  
26 # Private attributes
27 4 has '_debug_flag' => is => 'ro', isa => Bool, default => FALSE,
28 init_arg => 'debug';
29  
30 4 has '_encoding' => is => 'ro', isa => DataEncoding | Undef,
31 init_arg => 'encoding';
32  
33 4 has '_log' => is => 'lazy', isa => Logger,
34 builder => $_build__log, init_arg => 'log';
35  
36 has '_log_attributes' => is => 'lazy', isa => HashRef,
37 2 ▶ builder => sub { {} }, init_arg => 'log_attributes';
Subroutine __ANON__: called CC=1 SCAR=0.0
38  
39 4 has '_log_class' => is => 'lazy', isa => LoadableClass, coerce => TRUE,
40 builder => $_build__log_class, init_arg => 'log_class';
41  
42 4 has '_logfile' => is => 'ro', isa => Path | Undef, coerce => TRUE,
43 init_arg => 'logfile';
44  
45 # Private class attributes
46 4 my $loggers = {};
47  
48 # Construction
49 8 ▶around 'BUILDARGS' => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
50 8 my ($orig, $class, @args) = @_; my $attr = $orig->( $class, @args );
51  
52 8 ▶ my $builder = $attr->{builder} or return $attr;
Branch: 1 branch
BranchTrueFalseTotal
my $builder = $attr->{"builder"} or return $attr 6 2 8
53 6 ▶ my $config = $builder->can( 'config' ) ? $builder->config : {};
Branch: 1 branch
BranchTrueFalseTotal
$builder->can('config') ? : 6 0 6
54 6 my $keys = [ qw( appclass encoding log_attributes log_class logfile ) ];
55  
56 6 merge_attributes $attr, $builder, [ 'debug' ];
57 6 merge_attributes $attr, $config, $keys;
58  
59 6 return $attr;
60 4 };
61  
62 8 ▶sub BUILD {
Subroutine BUILD: called CC=3 SCAR=11.0
Pod documented
63 8 my ($self, $attr) = @_;
64  
65 8 ▶ exists $attr->{appclass} and $loggers->{ $attr->{appclass} } = $self;
Branch: 1 branch
BranchTrueFalseTotal
exists $attr->{"appclass"} and $$loggers{$attr->{"appclass"}} = $self 6 2 8
66 8 ▶ exists $loggers->{default} or $loggers->{default} = $self;
Branch: 1 branch
BranchTrueFalseTotal
exists $loggers->{"default"} or $loggers->{"default"} = $self 4 4 8
67  
68 8 return;
69 }
70  
71 2 ▶sub import {
Subroutine import: called CC=6 SCAR=21.0
72 2 my $class = shift;
73 0 ▶ my $params = { (is_hashref $_[ 0 ]) ? %{+ shift } : () };
Branch: 1 branch
BranchTrueFalseTotal
&is_hashref($_[0]) ? : 0 2 2
74 2 my @wanted = @_;
75 2 my $target = caller;
76  
77 2 for my $wanted (grep { defined $_ } @wanted) {
78 1 ▶ if ($wanted eq 'get_logger') {
Branch: 1 branch
BranchTrueFalseTotal
if ($wanted eq 'get_logger') { } 0 1 1
79 0 ▶ my $subr = $params->{as} // 'get_logger';
Condition0%0/2
$params->{"as"} // "get_logger"
l!l
00
MC/DC0%0/1
$params->{"as"} // "get_logger"
$params->{"as"}
Truth table0%0/2
A
A= $params->{"as"}
Aresult
11
00
80  
81 0 ▶ install_sub { into => $target, as => $subr, code => sub ($) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
82 0 return $loggers->{ $_[ 0 ] };
83 0 } };
84 }
85 else {
86 1 ▶ my $subr = $params->{as} // 'log';
Condition50%1/2
$params->{"as"} // "log"
l!l
01
MC/DC0%0/1
$params->{"as"} // "log"
$params->{"as"}
Truth table50%1/2
A
A= $params->{"as"}
Aresult
11
00
87  
88 7 ▶ install_sub { into => $target, as => $subr, code => sub (;@) {
Subroutine __ANON__: called CC=1 SCAR=0.0
89 7 return $loggers->{ $wanted }->log( @_ );
90 1 } };
91 }
92 }
93  
94 2 return;
95 }
96  
97 7 ▶around '_log_attributes' => sub {
Subroutine __ANON__: called CC=9 SCAR=24.4
98 7 my ($orig, $self) = @_; my $attr = $orig->( $self );
99  
100 7 ▶ $self->_log_class ne 'Log::Handler' and return $attr;
Branch: 1 branch
BranchTrueFalseTotal
$self->_log_class ne "Log::Handler" and return $attr 0 7 7
101  
102 7 ▶ my $fattr = $attr->{file} //= {};
Condition50%1/2
$attr->{"file"} //= {}
l!l
07
MC/DC0%0/1
$attr->{"file"} //= {}
$attr->{"file"}
Truth table50%1/2
A
A= $attr->{"file"}
Aresult
11
00
103 7 ▶ my $logfile = $self->_logfile // $fattr->{filename};
Condition33%1/3
$self->_logfile // $fattr->{"filename"}
l!l&&r!l&&!r
700
MC/DC0%0/2
$self->_logfile // $fattr->{"filename"}
$self->_logfile $fattr->{"filename"}
Truth table33%1/3
A // B
A= $self->_logfileB= $fattr->{"filename"}
ABresult
1X1
011
000
104  
105 7 ▶ ($logfile and -d dirname( "${logfile}" )) or return $attr;
Branch: 1 branch
BranchTrueFalseTotal
$logfile and -d dirname("$logfile") or return $attr 7 0 7
Condition33%1/3
$logfile and -d dirname("$logfile")
!ll&&!rl&&r
007
MC/DC0%0/2
$logfile and -d dirname("$logfile")
$logfile -d dirname("$logfile")
Truth table33%1/3
A and B
A= $logfileB= -d dirname("$logfile")
ABresult
0X0
100
111
106  
107 7 $fattr->{alias} = 'file-out';
108 7 $fattr->{filename} = "${logfile}";
109 $fattr->{maxlevel} = $self->_debug_flag ? 'debug'
110 7 ▶ : untaint_identifier $fattr->{maxlevel} // 'info';
Branch: 1 branch
BranchTrueFalseTotal
$self->_debug_flag ? : 3 4 7
Condition50%1/2
&untaint_identifier($fattr->{"maxlevel"}) // "info"
l!l
04
MC/DC0%0/1
&untaint_identifier($fattr->{"maxlevel"}) // "info"
&untaint_identifier($fattr->{"maxlevel"})
Truth table50%1/2
A
A= &untaint_identifier($fattr->{"maxlevel"})
Aresult
11
00
111 7 ▶ $fattr->{mode } = untaint_identifier $fattr->{mode } // 'append';
Condition50%1/2
&untaint_identifier($fattr->{"mode"}) // "append"
l!l
07
MC/DC0%0/1
&untaint_identifier($fattr->{"mode"}) // "append"
&untaint_identifier($fattr->{"mode"})
Truth table50%1/2
A
A= &untaint_identifier($fattr->{"mode"})
Aresult
11
00
112  
113 7 return $attr;
114 4 };
115  
116 # Private functions
117 4 ▶my $add_methods = sub {
Subroutine __ANON__: called CC=3 SCAR=11.8
118 4 my ($class, @methods) = @_;
119  
120 4 for my $method (@methods) {
121 $class->can( $method ) or
122 20 ▶ install_sub { into => $class, as => $method, code => sub {
Subroutine __ANON__: called CC=9 SCAR=24.8
123 20 ▶ my ($self, $text, $opts) = @_; $text or return FALSE;
Branch: 1 branch
BranchTrueFalseTotal
$text or return 0 20 0 20
124  
125 20 $text = ucfirst "${text}"; chomp $text; $text .= "\n";
126  
127 20 ▶ if (defined $opts) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts) 1 19 20
128 1 ▶ my $lead = ucfirst $opts->{leader} // NUL;
Condition50%1/2
ucfirst $opts->{"leader"} // ""
l!l
10
MC/DC0%0/1
ucfirst $opts->{"leader"} // ""
ucfirst $opts->{"leader"}
Truth table50%1/2
A
A= ucfirst $opts->{"leader"}
Aresult
11
00
129 my $tag = $opts->{tag}
130 1 ▶ // ($opts->{user} ? $opts->{user}->username : NUL);
Branch: 1 branch
BranchTrueFalseTotal
$opts->{'user'} ? : 0 0 0
Condition33%1/3
$opts->{"tag"} // ($opts->{"user"} ? $opts->{"user"}->username : "")
l!l&&r!l&&!r
100
MC/DC0%0/2
$opts->{"tag"} // ($opts->{"user"} ? $opts->{"user"}->username : "")
$opts->{"tag"} ($opts->{"user"} ? $opts->{"user"}->username : "")
Truth table33%1/3
A // B
A= $opts->{"tag"}B= ($opts->{"user"} ? $opts->{"user"}->username : "")
ABresult
1X1
011
000
131  
132 1 ▶ $tag = $tag ? "[${tag}] " : $lead ? SPC : NUL;
Branch: 2 branches
BranchTrueFalseTotal
$tag ? : 1 0 1
$lead ? : 0 0 0
133 1 $text = "${lead}${tag}${text}";
134 }
135  
136 20 ▶ $self->_encoding and $text = encode( $self->_encoding, $text );
Branch: 1 branch
BranchTrueFalseTotal
$self->_encoding and $text = &encode(scalar $self->_encoding, $text) 20 0 20
137 20 $self->_log->$method( $text );
138 20 return TRUE;
139 24 ▶ } };
Branch: 1 branch
BranchTrueFalseTotal
$class->can($method) or install_sub({"into", $class, "as", $method, "code", sub { my($self, $text, $opts) = @_; $text or return 0; $text = ucfirst "$text"; chomp $text; $text .= "\n"; if (defined $opts) { my $lead = ucfirst $opts->{"leader"} // ""; my $tag = $opts->{"tag"} // ($opts->{"user"} ? $opts->{"user"}->username : ""); $tag = $tag ? "[$tag] " : ($lead ? " " : ""); $text = "$lead$tag$text"; } ; $self->_encoding and $text = &encode(scalar $self->_encoding, $text); $self->_log->$method($text); return 1; } }) 0 24 24
140 }
141  
142 4 return;
143 4 };
144  
145 4 $add_methods->( __PACKAGE__, LOG_LEVELS );
146  
147 4 my @arg_names = qw( level message options );
148  
149 4 ▶my $inline_args = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
150 4 my $n = shift; return (map { $arg_names[ $_ ] => $_[ $_ ] } 0 .. $n - 1);
151 4 };
152  
153 # Public methods
154 0 ▶sub filehandle {
Subroutine filehandle: not called CC=2 SCAR=17.9
Pod documented
155 0 ▶ my $self = shift; $self->_log_class ne 'Log::Handler' and return;
Branch: 1 branch
BranchTrueFalseTotal
$self->_log_class ne "Log::Handler" and return 0 0 0
156  
157 0 return $self->_log->output( 'file-out' )->{fh};
158 }
159  
160 7 ▶sub log {
Subroutine log: called CC=9 SCAR=22.0
Pod documented
161 7 ▶ my ($self, @args) = @_; my $n = 0; $n++ while (defined $args[ $n ]);
Branch: 1 branch
BranchTrueFalseTotal
defined $args[$n] and do { ++$n } 13 7 20
162  
163 7 ▶ my $args = ($n == 0 ) ? {}
Branch: 5 branches
BranchTrueFalseTotal
$n == 0 ? : 1 6 7
&is_hashref($args[0]) ? : 1 5 6
$n == 1 ? : 1 4 5
$n == 2 ? : 2 2 4
$n == 3 ? : 1 1 2
164 : (is_hashref $args[ 0 ]) ? $args[ 0 ]
165 : ($n == 1 ) ? { $inline_args->( 2, 'info', @args ) }
166 : ($n == 2 ) ? { $inline_args->( 2, @args ) }
167 : ($n == 3 ) ? { $inline_args->( 3, @args ) }
168 : { @args };
169  
170 7 my $level = $args->{level}; $level and is_member $level, LOG_LEVELS
171 7 ▶ and return $self->$level( $args->{message}, $args->{options} );
Branch: 1 branch
BranchTrueFalseTotal
$level and &is_member($level, &LOG_LEVELS()) and return $self->$level($args->{"message"}, $args->{"options"}) 5 2 7
Condition100%3/3
$level and &is_member($level, &LOG_LEVELS())
!ll&&!rl&&r
115
MC/DC100%2/2
$level and &is_member($level, &LOG_LEVELS())
$level &is_member($level, &LOG_LEVELS())
Truth table100%3/3
A and B
A= $levelB= &is_member($level, &LOG_LEVELS())
ABresult
0X0
100
111
172  
173 2 return FALSE;
174 }
175  
176 4 1;
177  
178 __END__
« Usul/TraitFor/ConnectInfo.pm ↑ Summary Usul/Functions.pm »