File Coverage

blib/lib/Log/Dispatch/Screen/Gentoo.pm
Criterion Covered Total %
statement 24 25 96.0
branch 2 4 50.0
condition 1 3 33.3
subroutine 7 7 100.0
pod 0 1 0.0
total 34 40 85.0


line stmt bran cond sub pod time code
1             package Log::Dispatch::Screen::Gentoo;
2             # ABSTRACT: Gentoo-colored screen logging output
3             $Log::Dispatch::Screen::Gentoo::VERSION = '0.003';
4 2     2   586888 use strict;
  2         12  
  2         63  
5 2     2   13 use warnings;
  2         4  
  2         56  
6 2     2   11 use parent 'Log::Dispatch::Screen';
  2         7  
  2         15  
7 2     2   152782 use Encode ();
  2         5  
  2         43  
8 2     2   10 use Module::Runtime qw< require_module >;
  2         6  
  2         16  
9 2     2   1156 use Term::GentooFunctions ':all';
  2         9098  
  2         1109  
10              
11             ## no critic qw(Modules::RequireExplicitInclusion)
12             my $encode
13             = eval { require Unicode::UTF8; 1; }
14             ? sub { Unicode::UTF8::encode_utf8( $_[0] ) }
15             : sub { Encode::encode_utf8( $_[0] ) };
16              
17             # einfo
18             # ewarn
19             # eerror
20             # equiet
21             # ebegin
22             # eend
23              
24             our %FUNCTION_MAP = (
25             '' => sub { einfo( $_[0] ) },
26             'debug' => sub { einfo( $_[0] ) },
27             'info' => sub { einfo( $_[0] ) },
28             'notice' => sub { einfo( $_[0] ) },
29             'warning' => sub { ewarn( $_[0] ) },
30             'error' => sub { eerror( $_[0] ) },
31             'critical' => sub { eerror( $_[0] ) },
32             'alert' => sub { eerror( $_[0] ) },
33             'emergency' => sub { eerror( $_[0] ) },
34             );
35              
36             sub log_message {
37 11     11 0 24377 my ( $self, %p ) = @_;
38              
39 11         26 my $level = $p{'level'};
40              
41             my $message
42             = $self->{'utf8'}
43             ? $encode->( $p{'message'} )
44 11 50       46 : $p{'message'};
45              
46 11   33     38 my $print_func = $FUNCTION_MAP{$level} //= $FUNCTION_MAP{''};
47              
48 11 50       26 if ( $self->{'stderr'} ) {
49             # Should be STDERR
50 11         44 $print_func->($message);
51             } else {
52             # Should be STDOUT
53 0           $print_func->($message);
54             }
55             }
56              
57             1;
58              
59             __END__
60              
61             =pod
62              
63             =encoding UTF-8
64              
65             =head1 NAME
66              
67             Log::Dispatch::Screen::Gentoo - Gentoo-colored screen logging output
68              
69             =head1 VERSION
70              
71             version 0.003
72              
73             =head1 SYNOPSIS
74              
75             use Log::Dispatch;
76              
77             my $log = Log::Dispatch->new(
78             'outputs' => [
79             [
80             'Screen::Gentoo',
81             'min_level' => 'debug',
82             'stderr' => 1,
83             'newline' => 1,
84             ],
85             ],
86             );
87              
88             $log->info('Information');
89             $log->warning('Uh oh!');
90             $log->critical('No oh!');
91              
92             =head1 DESCRIPTION
93              
94             This implements a colorful output that uses L<Term::GentooFunctions> to
95             print out the output.
96              
97             It also works with indentation when using C<eindent> and C<eoutdent> from
98             L<Term::GentooFunctions>.
99              
100             If you have L<Unicode::UTF8> available, it will use that to support UTF-8
101             character encodings. (This is much faster than L<Encode>.)
102              
103             One limitation this has is that there are only three colors, which means
104             that you cannot see a difference between levels C<debug>, C<notice>, and
105             C<info> which all have a green color, or between C<error>, C<critical>,
106             C<alert>, and C<emergency> which all have a red color.
107              
108             At least for now.
109              
110             =head1 SEE ALSO
111              
112             =over 4
113              
114             =item * L<Log::Dispatch::Screen::Color>
115              
116             Colors entire lines, not just the beginning. Try it out.
117              
118             =item * L<Unicode::UTF8>
119              
120             =back
121              
122             =head1 AUTHOR
123              
124             Sawyer X <xsawyerx@cpan.org>
125              
126             =head1 COPYRIGHT AND LICENSE
127              
128             This software is Copyright (c) 2020 by Sawyer X.
129              
130             This is free software, licensed under:
131              
132             The MIT (X11) License
133              
134             =cut