| 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
Condition0%0/3
$ENV{"LOG_LEVEL"} and defined $Log::Any::Adapter::Screen::logging_levels{$ENV{"LOG_LEVEL"}}
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"}}
| |||||||||||||||||||||||||||||
| 34 | 0 | ▶ | return 'trace' if $ENV{TRACE}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 35 | 0 | ▶ | return 'debug' if $ENV{DEBUG}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 36 | 0 | ▶ | return 'info' if $ENV{VERBOSE}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 37 | 0 | ▶ | return 'error' if $ENV{QUIET}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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"
MC/DC0%0/1
$self->{"default_level"} //= "warning"
$self->{"default_level"}
Truth table0%0/2
A
A= $self->{"default_level"}
| |||||||||||||||||||||||||||||
| 44 | 0 | ▶ | $self->{stderr} //= 1; | ||||||||||||||||||||||||||
|
Condition0%0/2
$self->{"stderr"} //= 1
MC/DC0%0/1
$self->{"stderr"} //= 1
$self->{"stderr"}
Truth table0%0/2
A
A= $self->{"stderr"}
| |||||||||||||||||||||||||||||
| 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;
}
}
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;
}
}
| |||||||||||||||||||||||||||||
| 46 | 0 | ▶ | if (exists $ENV{NO_COLOR}) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
Condition0%0/3
exists $self->{"log_level"} and not exists $self->{"min_level"}
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"}
| |||||||||||||||||||||||||||||
| 66 | 0 | delete $self->{log_level}; | |||||||||||||||||||||||||||
| 67 | 0 | ▶ | $self->{min_level} //= $self->_min_level; | ||||||||||||||||||||||||||
|
Condition0%0/3
$self->{"min_level"} //= $self->_min_level
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
| |||||||||||||||||||||||||||||
| 68 | 0 | ▶ | if (!$self->{formatter}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | 0 | ▶ | if (($ENV{LOG_PREFIX} // '') eq 'elapsed') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$ENV{"LOG_PREFIX"} // ""
MC/DC0%0/1
$ENV{"LOG_PREFIX"} // ""
$ENV{"LOG_PREFIX"}
Truth table0%0/2
A
A= $ENV{"LOG_PREFIX"}
| |||||||||||||||||||||||||||||
| 70 | 0 | require Time::HiRes; | |||||||||||||||||||||||||||
| 71 | 0 | ▶ | $Time0 //= Time::HiRes::time(); | ||||||||||||||||||||||||||
|
Condition0%0/3
$Time0 //= &Time::HiRes::time()
MC/DC0%0/2
$Time0 //= &Time::HiRes::time()
$Time0
&Time::HiRes::time()
Truth table0%0/3
A //= B
A= $Time0B= &Time::HiRes::time()
| |||||||||||||||||||||||||||||
| 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"} // ""
MC/DC0%0/1
$ENV{"LOG_PREFIX"} // ""
$ENV{"LOG_PREFIX"}
Truth table0%0/2
A
A= $ENV{"LOG_PREFIX"}
| |||||||||||||||||||||||||||||
| 76 | 0 | ▶ | if ($env eq 'elapsed') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 0 | $self->hook_before_log($msg); | |||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 0 | ▶ | if ($self->{formatter}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | 0 | $msg = $self->{formatter}->($self, $msg); | |||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 0 | ▶ | if ($self->{use_color} && $self->{colors}{$method}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$self->{"use_color"} and $self->{"colors"}{$method}
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}
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||