File Coverage

blib/lib/Log/Any/Adapter/Screen.pm
Criterion Covered Total %
statement 17 69 24.6
branch 0 36 0.0
condition 0 26 0.0
subroutine 6 13 46.1
pod 0 3 0.0
total 23 147 15.6


line stmt bran cond sub pod time code
1             package Log::Any::Adapter::Screen;
2              
3 1     1   306742 use 5.010001;
  1         3  
4 1     1   4 use strict;
  1         2  
  1         21  
5 1     1   8 use warnings;
  1         2  
  1         78  
6              
7 1     1   550 use Log::Any;
  1         8641  
  1         5  
8 1     1   46 use Log::Any::Adapter::Util qw(make_method);
  1         2  
  1         38  
9 1     1   4 use parent qw(Log::Any::Adapter::Base);
  1         1  
  1         24  
10              
11             our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
12             our $DATE = '2023-11-21'; # DATE
13             our $DIST = 'Log-Any-Adapter-Screen'; # DIST
14             our $VERSION = '0.141'; # VERSION
15              
16             my $CODE_RESET = "\e[0m"; # PRECOMPUTED FROM: do { require Term::ANSIColor; Term::ANSIColor::color('reset') }
17             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             my $Time0;
20              
21             my @logging_methods = Log::Any->logging_methods;
22             our %logging_levels;
23             for my $i (0..@logging_methods-1) {
24             $logging_levels{$logging_methods[$i]} = $i;
25             }
26             # some common typos
27             $logging_levels{warn} = $logging_levels{warning};
28              
29             sub _min_level {
30 0     0     my $self = shift;
31              
32             return $ENV{LOG_LEVEL}
33 0 0 0       if $ENV{LOG_LEVEL} && defined $logging_levels{$ENV{LOG_LEVEL}};
34 0 0         return 'trace' if $ENV{TRACE};
35 0 0         return 'debug' if $ENV{DEBUG};
36 0 0         return 'info' if $ENV{VERBOSE};
37 0 0         return 'error' if $ENV{QUIET};
38 0           $self->{default_level};
39             }
40              
41             sub init {
42 0     0 0   my ($self) = @_;
43 0   0       $self->{default_level} //= 'warning';
44 0   0       $self->{stderr} //= 1;
45 0   0       $self->{use_color} //= do {
46 0 0         if (exists $ENV{NO_COLOR}) {
    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 0         if ($self->{colors}) {
55 0           require Term::ANSIColor;
56             # convert color names to escape sequence
57 0           my $orig = $self->{colors};
58             $self->{colors} = {
59 0 0         map {($_,($orig->{$_} ? Term::ANSIColor::color($orig->{$_}) : ''))}
  0            
60             keys %$orig
61             };
62             } else {
63 0           $self->{colors} = $DEFAULT_COLORS;
64             }
65 0 0 0       $self->{min_level} = $self->{log_level} if(exists $self->{log_level} && ! exists $self->{min_level});
66 0           delete $self->{log_level};
67 0   0       $self->{min_level} //= $self->_min_level;
68 0 0         if (!$self->{formatter}) {
69 0 0 0       if (($ENV{LOG_PREFIX} // '') eq 'elapsed') {
70 0           require Time::HiRes;
71 0   0       $Time0 //= Time::HiRes::time();
72             }
73             $self->{formatter} = sub {
74 0     0     my ($self, $msg) = @_;
75 0   0       my $env = $ENV{LOG_PREFIX} // '';
76 0 0         if ($env eq 'elapsed') {
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 0         $self->{_fh} = $self->{stderr} ? \*STDERR : \*STDOUT;
84             }
85              
86             sub hook_before_log {
87 0     0 0   return;
88             #my ($self, $msg) = @_;
89             }
90              
91             sub hook_after_log {
92 0     0 0   my ($self, $msg) = @_;
93 0 0         print { $self->{_fh} } "\n" unless $msg =~ /\n\z/;
  0            
94             }
95              
96             for my $method (Log::Any->logging_methods()) {
97             make_method(
98             $method,
99             sub {
100 0     0     my ($self, $msg) = @_;
101              
102             return if $logging_levels{$method} <
103 0 0         $logging_levels{$self->{min_level}};
104              
105 0           $self->hook_before_log($msg);
106              
107 0 0         if ($self->{formatter}) {
108 0           $msg = $self->{formatter}->($self, $msg);
109             }
110              
111 0 0 0       if ($self->{use_color} && $self->{colors}{$method}) {
112 0           $msg = $self->{colors}{$method} . $msg . $CODE_RESET;
113             }
114              
115 0           print { $self->{_fh} } $msg;
  0            
116              
117 0           $self->hook_after_log($msg);
118             }
119             );
120             }
121              
122             for my $method (Log::Any->detection_methods()) {
123             my $level = $method; $level =~ s/^is_//;
124             make_method(
125             $method,
126             sub {
127 0     0     my $self = shift;
128 0           $logging_levels{$level} >= $logging_levels{$self->{min_level}};
129             }
130             );
131             }
132              
133             1;
134             # ABSTRACT: (ADOPTME) Send logs to screen, with colors and some other features
135              
136             __END__