blib/lib/Log/Any/Adapter/Screen.pm

filter: Statementstmt 40.9% 36 / 88 Branchbran 0.0% 0 / 36 Conditioncond 0.0% 0 / 26 MC/DCmcdc 0.0% 0 / 16 Subroutinesub 46.1% 6 / 13 Podpod 0.0% 0 / 3 totaltotal 23.0% 42 / 182 CC 29 SCAR 47.9
CC
29
Cov
24%
init
272.0
_min_level
56.0
__ANON__
30.0
CRAP 120.8
↑ Summary
1 package Log::Any::Adapter::Screen;
2  
3 1 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶use Log::Any;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Log::Any::Adapter::Util qw(make_method);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use parent qw(Log::Any::Adapter::Base);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
12 1 our $DATE = '2023-11-21'; # DATE
13 1 our $DIST = 'Log-Any-Adapter-Screen'; # DIST
14 1 our $VERSION = '0.141'; # VERSION
15  
16 1 my $CODE_RESET = "\e[0m"; # PRECOMPUTED FROM: do { require Term::ANSIColor; Term::ANSIColor::color('reset') }
17 1 my $DEFAULT_COLORS = {alert=>"\e[31m",critical=>"\e[31m",debug=>"",emergency=>"\e[31m",error=>"\e[35m",info=>"\e[32m",notice=>"\e[32m",trace=>"\e[33m",warning=>"\e[1;34m"}; # PRECOMPUTED FROM: do { require Term::ANSIColor; my $tmp = {trace=>'yellow', debug=>'', info=>'green',notice=>'green',warning=>'bold blue',error=>'magenta',critical=>'red',alert=>'red',emergency=>'red'}; for (keys %$tmp) { if ($tmp->{$_}) { $tmp->{$_} = Term::ANSIColor::color($tmp->{$_}) } }; $tmp }
18  
19 1 my $Time0;
20  
21 1 my @logging_methods = Log::Any->logging_methods;
22 1 our %logging_levels;
23 1 for my $i (0..@logging_methods-1) {
24 9 $logging_levels{$logging_methods[$i]} = $i;
25 }
26 # some common typos
27 1 $logging_levels{warn} = $logging_levels{warning};
28  
29 0 ▶sub _min_level {
Subroutine _min_level: not called CC=7 SCAR=40.3
30 0 my $self = shift;
31  
32 return $ENV{LOG_LEVEL}
33 0 ▶ if $ENV{LOG_LEVEL} && defined $logging_levels{$ENV{LOG_LEVEL}};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"LOG_LEVEL"} and defined $Log::Any::Adapter::Screen::logging_levels{$ENV{"LOG_LEVEL"}} 0 0 0
Condition0%0/3
$ENV{"LOG_LEVEL"} and defined $Log::Any::Adapter::Screen::logging_levels{$ENV{"LOG_LEVEL"}}
!ll&&!rl&&r
000
MC/DC0%0/2
$ENV{"LOG_LEVEL"} and defined $Log::Any::Adapter::Screen::logging_levels{$ENV{"LOG_LEVEL"}}
$ENV{"LOG_LEVEL"} defined $Log::Any::Adapter::Screen::logging_levels{$ENV{"LOG_LEVEL"}}
Truth table0%0/3
A and B
A= $ENV{"LOG_LEVEL"}B= defined $Log::Any::Adapter::Screen::logging_levels{$ENV{"LOG_LEVEL"}}
ABresult
0X0
100
111
34 0 ▶ return 'trace' if $ENV{TRACE};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"TRACE"} 0 0 0
35 0 ▶ return 'debug' if $ENV{DEBUG};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"DEBUG"} 0 0 0
36 0 ▶ return 'info' if $ENV{VERBOSE};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"VERBOSE"} 0 0 0
37 0 ▶ return 'error' if $ENV{QUIET};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"QUIET"} 0 0 0
38 0 $self->{default_level};
39 }
40  
41 0 ▶sub init {
Subroutine init: not called CC=16 SCAR=56.1
Pod undocumented
42 0 my ($self) = @_;
43 0 ▶ $self->{default_level} //= 'warning';
Condition0%0/2
$self->{"default_level"} //= "warning"
l!l
00
MC/DC0%0/1
$self->{"default_level"} //= "warning"
$self->{"default_level"}
Truth table0%0/2
A
A= $self->{"default_level"}
Aresult
11
00
44 0 ▶ $self->{stderr} //= 1;
Condition0%0/2
$self->{"stderr"} //= 1
l!l
00
MC/DC0%0/1
$self->{"stderr"} //= 1
$self->{"stderr"}
Truth table0%0/2
A
A= $self->{"stderr"}
Aresult
11
00
45 0 ▶ $self->{use_color} //= do {
Condition0%0/3
$self->{"use_color"} //= do { if (exists $ENV{"NO_COLOR"}) { 0; } elsif (defined $ENV{"COLOR"}) { $ENV{"COLOR"}; } else { -t STDOUT; } }
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"use_color"} //= do { if (exists $ENV{"NO_COLOR"}) { 0; } elsif (defined $ENV{"COLOR"}) { $ENV{"COLOR"}; } else { -t STDOUT; } }
$self->{"use_color"} do { if (exists $ENV{"NO_COLOR"}) { 0; } elsif (defined $ENV{"COLOR"}) { $ENV{"COLOR"}; } else { -t STDOUT; } }
Truth table0%0/3
A //= B
A= $self->{"use_color"}B= do { if (exists $ENV{"NO_COLOR"}) { 0; } elsif (defined $ENV{"COLOR"}) { $ENV{"COLOR"}; } else { -t STDOUT; } }
ABresult
1X1
011
000
46 0 ▶ if (exists $ENV{NO_COLOR}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $ENV{'NO_COLOR'}) { } 0 0 0
elsif (defined $ENV{'COLOR'}) { } 0 0 0
47 0 0;
48 } elsif (defined $ENV{COLOR}) {
49 0 $ENV{COLOR};
50 } else {
51 0 (-t STDOUT); ## no critic: InputOutput::ProhibitInteractiveTest
52 }
53 };
54 0 ▶ if ($self->{colors}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'colors'}) { } 0 0 0
55 0 require Term::ANSIColor;
56 # convert color names to escape sequence
57 0 my $orig = $self->{colors};
58 $self->{colors} = {
59 0 ▶ map {($_,($orig->{$_} ? Term::ANSIColor::color($orig->{$_}) : ''))}
Branch: 1 branch
BranchTrueFalseTotal
$orig->{$_} ? : 0 0 0
60 keys %$orig
61 };
62 } else {
63 0 $self->{colors} = $DEFAULT_COLORS;
64 }
65 0 ▶ $self->{min_level} = $self->{log_level} if(exists $self->{log_level} && ! exists $self->{min_level});
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"log_level"} and not exists $self->{"min_level"} 0 0 0
Condition0%0/3
exists $self->{"log_level"} and not exists $self->{"min_level"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $self->{"log_level"} and not exists $self->{"min_level"}
exists $self->{"log_level"} not exists $self->{"min_level"}
Truth table0%0/3
A and not(B)
A= exists $self->{"log_level"}B= not exists $self->{"min_level"}
ABresult
0X0
100
111
66 0 delete $self->{log_level};
67 0 ▶ $self->{min_level} //= $self->_min_level;
Condition0%0/3
$self->{"min_level"} //= $self->_min_level
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"min_level"} //= $self->_min_level
$self->{"min_level"} $self->_min_level
Truth table0%0/3
A //= B
A= $self->{"min_level"}B= $self->_min_level
ABresult
1X1
011
000
68 0 ▶ if (!$self->{formatter}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"formatter"}) 0 0 0
69 0 ▶ if (($ENV{LOG_PREFIX} // '') eq 'elapsed') {
Branch: 1 branch
BranchTrueFalseTotal
if (($ENV{"LOG_PREFIX"} // "") eq "elapsed") 0 0 0
Condition0%0/2
$ENV{"LOG_PREFIX"} // ""
l!l
00
MC/DC0%0/1
$ENV{"LOG_PREFIX"} // ""
$ENV{"LOG_PREFIX"}
Truth table0%0/2
A
A= $ENV{"LOG_PREFIX"}
Aresult
11
00
70 0 require Time::HiRes;
71 0 ▶ $Time0 //= Time::HiRes::time();
Condition0%0/3
$Time0 //= &Time::HiRes::time()
l!l&&r!l&&!r
000
MC/DC0%0/2
$Time0 //= &Time::HiRes::time()
$Time0 &Time::HiRes::time()
Truth table0%0/3
A //= B
A= $Time0B= &Time::HiRes::time()
ABresult
1X1
011
000
72 }
73 0 ▶ $self->{formatter} = sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
74 0 my ($self, $msg) = @_;
75 0 ▶ my $env = $ENV{LOG_PREFIX} // '';
Condition0%0/2
$ENV{"LOG_PREFIX"} // ""
l!l
00
MC/DC0%0/1
$ENV{"LOG_PREFIX"} // ""
$ENV{"LOG_PREFIX"}
Truth table0%0/2
A
A= $ENV{"LOG_PREFIX"}
Aresult
11
00
76 0 ▶ if ($env eq 'elapsed') {
Branch: 1 branch
BranchTrueFalseTotal
if ($env eq "elapsed") 0 0 0
77 0 my $time = Time::HiRes::time();
78 0 $msg = sprintf("[%9.3fms] %s", ($time - $Time0)*1000, $msg);
79 }
80 0 $msg;
81 0 };
82 }
83 0 ▶ $self->{_fh} = $self->{stderr} ? \*STDERR : \*STDOUT;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'stderr'} ? : 0 0 0
84 }
85  
86 0 ▶sub hook_before_log {
Subroutine hook_before_log: not called CC=1 SCAR=6.9
Pod undocumented
87 0 return;
88 #my ($self, $msg) = @_;
89 }
90  
91 0 ▶sub hook_after_log {
Subroutine hook_after_log: not called CC=2 SCAR=17.9
Pod undocumented
92 0 my ($self, $msg) = @_;
93 0 ▶ print { $self->{_fh} } "\n" unless $msg =~ /\n\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $msg =~ /\n\z/ 0 0 0
94 }
95  
96 1 for my $method (Log::Any->logging_methods()) {
97 make_method(
98 $method,
99 0 ▶ sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
100 0 my ($self, $msg) = @_;
101  
102 return if $logging_levels{$method} <
103 0 ▶ $logging_levels{$self->{min_level}};
Branch: 1 branch
BranchTrueFalseTotal
if $Log::Any::Adapter::Screen::logging_levels{$method} < $Log::Any::Adapter::Screen::logging_levels{$self->{"min_level"}} 0 0 0
104  
105 0 $self->hook_before_log($msg);
106  
107 0 ▶ if ($self->{formatter}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"formatter"}) 0 0 0
108 0 $msg = $self->{formatter}->($self, $msg);
109 }
110  
111 0 ▶ if ($self->{use_color} && $self->{colors}{$method}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"use_color"} and $self->{"colors"}{$method}) 0 0 0
Condition0%0/3
$self->{"use_color"} and $self->{"colors"}{$method}
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"use_color"} and $self->{"colors"}{$method}
$self->{"use_color"} $self->{"colors"}{$method}
Truth table0%0/3
A and B
A= $self->{"use_color"}B= $self->{"colors"}{$method}
ABresult
0X0
100
111
112 0 $msg = $self->{colors}{$method} . $msg . $CODE_RESET;
113 }
114  
115 0 print { $self->{_fh} } $msg;
116  
117 0 $self->hook_after_log($msg);
118 }
119 9 );
120 }
121  
122 1 for my $method (Log::Any->detection_methods()) {
123 9 my $level = $method; $level =~ s/^is_//;
124 make_method(
125 $method,
126 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
127 0 my $self = shift;
128 0 $logging_levels{$level} >= $logging_levels{$self->{min_level}};
129 }
130 9 );
131 }
132  
133 1 1;
134 # ABSTRACT: (ADOPTME) Send logs to screen, with colors and some other features
135  
136 __END__
↑ Summary