| 1 | package Log::ger::Output::Screen; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 3 | our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY | |||||||||||||||||||||||||||
| 7 | 3 | our $DATE = '2022-01-16'; # DATE | |||||||||||||||||||||||||||
| 8 | 3 | our $DIST = 'Log-ger-Output-Screen'; # DIST | |||||||||||||||||||||||||||
| 9 | 3 | our $VERSION = '0.019'; # VERSION | |||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 3 | ▶ | if ($^O eq 'MSWin32') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 12 | 0 | eval { require Win32::Console::ANSI }; | |||||||||||||||||||||||||||
| 13 | } | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 3 | ▶ | use Log::ger::Util; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 3 | our %colors_16 = ( | |||||||||||||||||||||||||||
| 18 | 10 => "\e[31m" , # fatal , red | ||||||||||||||||||||||||||||
| 19 | 20 => "\e[35m" , # error , magenta | ||||||||||||||||||||||||||||
| 20 | 30 => "\e[1;34m", # warning, light blue | ||||||||||||||||||||||||||||
| 21 | 40 => "\e[32m" , # info , green | ||||||||||||||||||||||||||||
| 22 | 50 => "", # debug , no color | ||||||||||||||||||||||||||||
| 23 | 60 => "\e[33m" , # trace , orange | ||||||||||||||||||||||||||||
| 24 | ); | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 3 | our %colors_256 = ( | |||||||||||||||||||||||||||
| 27 | # pastel'ish, all brighter and avoid pure red/blue which is hard to see on | ||||||||||||||||||||||||||||
| 28 | # laptop screen in a bright environment | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 10 => "\e[1;38;5;160m", # fatal , bright red3 | ||||||||||||||||||||||||||||
| 31 | 20 => "\e[1;38;5;163m", # error , bright magenta3 | ||||||||||||||||||||||||||||
| 32 | 30 => "\e[1;38;5;69m" , # warning, bright cornflowerblue | ||||||||||||||||||||||||||||
| 33 | 40 => "\e[1;38;5;113m", # info , bright darkolivegreen3 | ||||||||||||||||||||||||||||
| 34 | 50 => "" , # debug , no color | ||||||||||||||||||||||||||||
| 35 | 60 => "\e[1;38;5;130m", # trace , bright darkorange3 | ||||||||||||||||||||||||||||
| 36 | ); | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 3 | our %level_map; | |||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 4 | ▶ | sub meta { +{ | ||||||||||||||||||||||||||
|
Subroutine
meta: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 41 | 4 | v => 2, | |||||||||||||||||||||||||||
| 42 | } } | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 1 | ▶ | sub _pick_color { | ||||||||||||||||||||||||||
|
Subroutine
_pick_color: called CC=7 SCAR=33.5
| |||||||||||||||||||||||||||||
| 45 | 1 | my ($level, $color_depth) = @_; | |||||||||||||||||||||||||||
| 46 | 1 | ▶ | my $colors = $color_depth >= 256 ? \%colors_256 : \%colors_16; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 47 | 1 | ▶ | if (defined(my $c = $colors->{$level})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 48 | 1 | return $c; | |||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | 0 | ▶ | if (defined(my $clevel = $level_map{$level})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 51 | 0 | return $colors->{$clevel}; | |||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | # find the nearest | ||||||||||||||||||||||||||||
| 55 | 0 | my ($dist, $clevel); | |||||||||||||||||||||||||||
| 56 | 0 | for my $k (keys %$colors) { | |||||||||||||||||||||||||||
| 57 | 0 | my $d = abs($k - $level); | |||||||||||||||||||||||||||
| 58 | 0 | ▶ | if (!defined($dist) || $dist > $d) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not defined $dist or $dist > $d
MC/DC0%0/2
not defined $dist or $dist > $d
not defined $dist
$dist > $d
Truth table0%0/3
not(A) or B
A= not defined $distB= $dist > $d
| |||||||||||||||||||||||||||||
| 59 | 0 | $dist = $d; | |||||||||||||||||||||||||||
| 60 | 0 | $clevel = $k; | |||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | 0 | $level_map{$level} = $clevel; | |||||||||||||||||||||||||||
| 64 | 0 | return $colors->{$clevel}; | |||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 7 | ▶ | sub hook_before_log { | ||||||||||||||||||||||||||
|
Subroutine
hook_before_log: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 68 | 7 | my ($ctx, $msg) = @_; | |||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 7 | ▶ | sub hook_after_log { | ||||||||||||||||||||||||||
|
Subroutine
hook_after_log: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 72 | 7 | my ($ctx, $msg) = @_; | |||||||||||||||||||||||||||
| 73 | 7 | ▶ | print { $ctx->{_fh} } "\n" unless $msg =~ /\R\z/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 4 | ▶ | sub get_hooks { | ||||||||||||||||||||||||||
|
Subroutine
get_hooks: called CC=15 SCAR=35.4
Pod
undocumented
| |||||||||||||||||||||||||||||
| 77 | 4 | my %plugin_conf = @_; | |||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 4 | my $stderr = $plugin_conf{stderr}; | |||||||||||||||||||||||||||
| 80 | 4 | ▶ | $stderr = 1 unless defined $stderr; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | 4 | ▶ | my $handle = $stderr ? \*STDERR : \*STDOUT; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | 4 | my $use_color = do { | |||||||||||||||||||||||||||
| 83 | 4 | ▶ | if (defined $plugin_conf{use_color}) { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 84 | 1 | $plugin_conf{use_color}; | |||||||||||||||||||||||||||
| 85 | } elsif (exists $ENV{NO_COLOR}) { | ||||||||||||||||||||||||||||
| 86 | 0 | 0; | |||||||||||||||||||||||||||
| 87 | } elsif (defined $ENV{COLOR}) { | ||||||||||||||||||||||||||||
| 88 | 0 | $ENV{COLOR}; | |||||||||||||||||||||||||||
| 89 | } else { | ||||||||||||||||||||||||||||
| 90 | 3 | ▶ | $stderr ? (-t STDERR) : (-t STDOUT); ## no critic: InputOutput::ProhibitInteractiveTest | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | }; | ||||||||||||||||||||||||||||
| 93 | 4 | my $color_depth = do { | |||||||||||||||||||||||||||
| 94 | 4 | ▶ | if (defined $plugin_conf{color_depth}) { | ||||||||||||||||||||||||||
|
Branch: 5 branches
| |||||||||||||||||||||||||||||
| 95 | 1 | $plugin_conf{color_depth}; | |||||||||||||||||||||||||||
| 96 | } elsif (defined $ENV{COLOR_DEPTH}) { | ||||||||||||||||||||||||||||
| 97 | 0 | $ENV{COLOR_DEPTH}; | |||||||||||||||||||||||||||
| 98 | } elsif (!$use_color) { | ||||||||||||||||||||||||||||
| 99 | 3 | 0; | |||||||||||||||||||||||||||
| 100 | ▶ | } elsif (defined $ENV{COLORTERM} && $ENV{COLORTERM} eq 'truecolor') { | |||||||||||||||||||||||||||
|
Condition0%0/3
defined $ENV{"COLORTERM"} and $ENV{"COLORTERM"} eq "truecolor"
MC/DC0%0/2
defined $ENV{"COLORTERM"} and $ENV{"COLORTERM"} eq "truecolor"
defined $ENV{"COLORTERM"}
$ENV{"COLORTERM"} eq "truecolor"
Truth table0%0/3
A and B
A= defined $ENV{"COLORTERM"}B= $ENV{"COLORTERM"} eq "truecolor"
| |||||||||||||||||||||||||||||
| 101 | 0 | 16777216; | |||||||||||||||||||||||||||
| 102 | ▶ | } elsif (defined $ENV{TERM} && $ENV{TERM} =~ /256color/) { | |||||||||||||||||||||||||||
|
Condition0%0/3
defined $ENV{"TERM"} and $ENV{"TERM"} =~ /256color/
MC/DC0%0/2
defined $ENV{"TERM"} and $ENV{"TERM"} =~ /256color/
defined $ENV{"TERM"}
$ENV{"TERM"} =~ /256color/
Truth table0%0/3
A and B
A= defined $ENV{"TERM"}B= $ENV{"TERM"} =~ /256color/
| |||||||||||||||||||||||||||||
| 103 | 0 | 256; | |||||||||||||||||||||||||||
| 104 | } else { | ||||||||||||||||||||||||||||
| 105 | 0 | 16; | |||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | }; | ||||||||||||||||||||||||||||
| 108 | 4 | my $formatter = $plugin_conf{formatter}; | |||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 4 | ▶ | if ($plugin_conf{colorize_tags}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 111 | 0 | require Color::ANSI::Util; | |||||||||||||||||||||||||||
| 112 | 0 | require Color::RGB::Util; | |||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | return { | ||||||||||||||||||||||||||||
| 116 | create_outputter => [ | ||||||||||||||||||||||||||||
| 117 | __PACKAGE__, # key | ||||||||||||||||||||||||||||
| 118 | 50, # priority | ||||||||||||||||||||||||||||
| 119 | 16 | ▶ | sub { # hook | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.9
| |||||||||||||||||||||||||||||
| 120 | 16 | my %hook_args = @_; # see Log::ger::Manual::Internals/"Arguments passed to hook" | |||||||||||||||||||||||||||
| 121 | 7 | ▶ | my $outputter = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=10 SCAR=30.7
| |||||||||||||||||||||||||||||
| 122 | 7 | my ($per_target_conf, $msg, $per_msg_conf) = @_; | |||||||||||||||||||||||||||
| 123 | 7 | ▶ | my $level; $level = $per_msg_conf->{level} if $per_msg_conf; $level = $hook_args{level} unless defined $level; | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 124 | 7 | ▶ | if ($formatter) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 125 | 0 | $msg = $formatter->($msg); | |||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | 7 | hook_before_log({ _fh=>$handle }, $msg); | |||||||||||||||||||||||||||
| 128 | 7 | ▶ | if ($use_color) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 129 | 1 | my $line_color = _pick_color($level, $color_depth); | |||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | 1 | ▶ | if ($plugin_conf{colorize_tags}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 132 | 0 | my $prog_prefix; | |||||||||||||||||||||||||||
| 133 | 0 | ▶ | if ($msg =~ s/\A([\w-]+:?\s*)//) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 134 | 0 | $prog_prefix = $1; | |||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | 0 | my (@tags, @seps); | |||||||||||||||||||||||||||
| 137 | 0 | ▶ | while ($msg =~ s/\A\[([^\]]+)\](\s*)//g) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 138 | 0 | push @tags, $1; | |||||||||||||||||||||||||||
| 139 | 0 | push @seps, $2; | |||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | #use DD; dd {msg=>$msg, tags=>\@tags, prog_prefix=>$prog_prefix}; | ||||||||||||||||||||||||||||
| 142 | 0 | ▶ | print $handle $line_color, $prog_prefix, "\e[0m" if defined $prog_prefix; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 143 | 0 | for my $i (0 .. $#tags) { | |||||||||||||||||||||||||||
| 144 | # XXX force ansifg() to use the same color depth as us | ||||||||||||||||||||||||||||
| 145 | 0 | print $handle ( | |||||||||||||||||||||||||||
| 146 | $line_color, "[", "\e[0m", | ||||||||||||||||||||||||||||
| 147 | Color::ANSI::Util::ansifg( Color::RGB::Util::assign_rgb_light_color($tags[$i]) ), $tags[$i], "\e[0m", | ||||||||||||||||||||||||||||
| 148 | $line_color, "]", $seps[$i], "\e[0m", | ||||||||||||||||||||||||||||
| 149 | ); | ||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | 1 | print $handle $line_color, $msg, "\e[0m"; | |||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | } else { | ||||||||||||||||||||||||||||
| 156 | 6 | print $handle $msg; | |||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||
| 158 | 7 | hook_after_log({ _fh=>$handle }, $msg); | |||||||||||||||||||||||||||
| 159 | 16 | }; | |||||||||||||||||||||||||||
| 160 | 16 | [$outputter]; | |||||||||||||||||||||||||||
| 161 | 4 | }], | |||||||||||||||||||||||||||
| 162 | }; | ||||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | 3 | 1; | |||||||||||||||||||||||||||
| 166 | # ABSTRACT: Output log to screen | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | __END__ | ||||||||||||||||||||||||||||