blib/lib/Mojo/Log/Role/Color.pm

filter: Statementstmt 92.8% 52 / 56 Branchbran 77.7% 14 / 18 Conditioncond 56.2% 9 / 16 MC/DCmcdc 11.1% 1 / 9 Subroutinesub 80.0% 12 / 15 Podpod n/a totaltotal 77.1% 88 / 114 CC 17 SCAR 13.2
CC
17
Cov
83%
_l
6.7
import
6.0
__ANON__
3.3
CRAP 3.7
↑ Summary
1 package Mojo::Log::Role::Color;
2 4 ▶use Mojo::Base -role;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 4 ▶use Term::ANSIColor ();
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 4 our $VERSION = '0.04';
7  
8 4 our %COLORS = (
9 debug => ['cyan'],
10 error => ['red'],
11 fatal => ['white on_red'],
12 info => ['green'],
13 warn => ['yellow'],
14 );
15  
16 4 ▶has colored => sub { $ENV{MOJO_LOG_COLORS} // -t shift->handle };
Subroutine __ANON__: called CC=2 SCAR=7.1
Condition66%2/3
$ENV{"MOJO_LOG_COLORS"} // -t (shift())->handle
l!l&&r!l&&!r
220
MC/DC0%0/2
$ENV{"MOJO_LOG_COLORS"} // -t (shift())->handle
$ENV{"MOJO_LOG_COLORS"} -t (shift())->handle
Truth table66%2/3
A // B
A= $ENV{"MOJO_LOG_COLORS"}B= -t (shift())->handle
ABresult
1X1
011
000
17  
18 12 ▶around format => sub {
Subroutine __ANON__: called CC=3 SCAR=11.1
19 12 my ($next, $self) = (shift, shift);
20  
21 # set
22 12 ▶ return $next->($self, @_) if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 1 11 12
23  
24 # get
25 11 my $formatter = $next->($self);
26 11 ▶ return $formatter unless $self->colored;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->colored 5 6 11
27  
28 6 ▶ return sub {
Subroutine __ANON__: called CC=3 SCAR=12.0
29 6 my $level = $_[1];
30 6 my $message = $formatter->(@_);
31 6 ▶ my $newline = $message =~ s!(\r?\n)$!! ? $1 : '';
Branch: 1 branch
BranchTrueFalseTotal
$message =~ s/(\r?\n)$//u ? : 6 0 6
32 6 ▶ return Term::ANSIColor::colored($COLORS{$level} || $COLORS{debug}, $message)
Condition33%1/3
$Mojo::Log::Role::Color::COLORS{$level} || $Mojo::Log::Role::Color::COLORS{'debug'}
l!l&&r!l&&!r
600
MC/DC0%0/2
$Mojo::Log::Role::Color::COLORS{$level} || $Mojo::Log::Role::Color::COLORS{'debug'}
$Mojo::Log::Role::Color::COLORS{$level} $Mojo::Log::Role::Color::COLORS{'debug'}
Truth table33%1/3
A || B
A= $Mojo::Log::Role::Color::COLORS{$level}B= $Mojo::Log::Role::Color::COLORS{'debug'}
ABresult
1X1
011
000
33 . $newline;
34 6 };
35 4 };
36  
37 2 ▶sub import {
Subroutine import: called CC=6 SCAR=18.0
38 2 my $class = shift;
39 2 ▶ return unless my @flags = @_;
Branch: 1 branch
BranchTrueFalseTotal
unless my(@flags) = 0 2 2
40  
41 2 my $caller = caller;
42 2 ▶ while (my $flag = shift @flags) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $flag = shift @flags) 2 2 4
43 2 ▶ if ($flag eq '-func') {
Branch: 1 branch
BranchTrueFalseTotal
if ($flag eq "-func") 2 0 2
44 2 require Mojo::Log;
45 2 ▶ my $fqn = shift @flags || 'l';
Condition100%2/2
shift @flags || 'l'
l!l
11
MC/DC100%1/1
shift @flags || 'l'
shift @flags
Truth table100%2/2
A
A= shift @flags
Aresult
11
00
46 2 ▶ $fqn = "${caller}::$fqn" unless $fqn =~ m!::!;
Branch: 1 branch
BranchTrueFalseTotal
unless $fqn =~ /::/u 1 1 2
47 4 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
48 2 *$fqn = \&_l;
49 }
50 }
51 }
52  
53 1 ▶sub _f {
Subroutine _f: called CC=1 SCAR=0.2
54 1 my $format = shift;
55  
56 state $f = {
57 0 ▶ ymd => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
58 0 my ($d, $m, $y) = (localtime $_[0])[3, 4, 5];
59 0 sprintf sprintf '%04d-%02d-%02d', $y + 1900, $m + 1, $d;
60 },
61 1 ▶ hms => sub {
Subroutine __ANON__: called CC=2 SCAR=7.2
62 1 my ($s, $m, $h) = localtime $_[0];
63 1 ▶ sprintf '%02d:%02d:%08.5f', $h, $m, "$s." . ((split /\./, $_[0])[1] // 0);
Condition50%1/2
(split(/\./u, $_[0], 0))[1] // 0
l!l
10
MC/DC0%0/1
(split(/\./u, $_[0], 0))[1] // 0
(split(/\./u, $_[0], 0))[1]
Truth table50%1/2
A
A= (split(/\./u, $_[0], 0))[1]
Aresult
11
00
64 },
65 0 ▶ level => sub { $_[1] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
66 1 ▶ m => sub { join "\n", @{$_[2]}, '' },
Subroutine __ANON__: called CC=1 SCAR=0.0
67 0 ▶ pid => sub {$$},
Subroutine __ANON__: not called CC=1 SCAR=6.9
68 1 };
69  
70 1 my $re = join '|', keys %$f;
71 1 $re = qr{%($re)};
72  
73 1 ▶ return sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
74 1 my ($str, $time, $level) = ($format, shift, shift);
75 1 $str =~ s!$re!{$f->{$1}($time, $level, \@_)}!ge;
76 1 return $str;
77 1 };
78 }
79  
80 4 ▶sub _l {
Subroutine _l: called CC=6 SCAR=19.0
81 4 my ($level, $format, @args) = @_;
82 state $log
83 = Mojo::Log->with_roles('+Color')->new->colored($ENV{MOJO_LOG_COLORS} // 1)
84 4 ▶ ->format(_f($ENV{MOJO_LOG_FORMAT} || '[%hms] %m'));
Condition50%2/4
$ENV{"MOJO_LOG_COLORS"} // 1
l!l
01
$ENV{'MOJO_LOG_FORMAT'} || '[%hms] %m'
l!l
01
MC/DC0%0/1
$ENV{"MOJO_LOG_COLORS"} // 1
$ENV{"MOJO_LOG_COLORS"}
MC/DC0%0/1
$ENV{'MOJO_LOG_FORMAT'} || '[%hms] %m'
$ENV{'MOJO_LOG_FORMAT'}
Truth table50%1/2
A
A= $ENV{"MOJO_LOG_COLORS"}
Aresult
11
00
Truth table50%1/2
A
A= $ENV{'MOJO_LOG_FORMAT'}
Aresult
11
00
85 4 ▶ return $log unless $level;
Branch: 1 branch
BranchTrueFalseTotal
unless $level 3 1 4
86 return $log->$level(@args
87 1 ▶ ? sprintf $format, map { $_ // 'undef' } @args
Branch: 1 branch
BranchTrueFalseTotal
@args ? : 1 0 1
Condition50%1/2
$_ // "undef"
l!l
10
MC/DC0%0/1
$_ // "undef"
$_
Truth table50%1/2
A
A= $_
Aresult
11
00
88 : $format);
89 }
90  
91 4 1;
92  
93 =encoding utf8
94  
95 =head1 NAME
96  
97 Mojo::Log::Role::Color - Add colors to your mojo logs
98  
99 =head1 SYNOPSIS
100  
101 use Mojo::Log;
102 my $log = Mojo::Log->with_roles("+Color")->new;
103 $log->info("FYI: it happened again");
104  
105 =head1 DESCRIPTION
106  
107 L<Mojo::Log::Role::Color> is a role you can apply to your L<Mojo::Log> to get
108 colored log messages when running your application in interactive mode.
109  
110 It is also possible to set the C<MOJO_LOG_COLORS> environment variable to force
111 colored output.
112  
113 The coloring is based on the log level:
114  
115 debug: cyan text
116 info: green text
117 warn: yellow text
118 error: red text
119 fatal: white text on red background
120  
121 The colors can be customized by changing C<%Mojo::Log::Role::Color::COLORS>,
122 though this is not officially supported, and may break in a future release.
123  
124 =head1 EXPORTED FUNCTIONS
125  
126 use Mojo::Log::Role::Color -func;
127 l error => "too %s", "cool";
128  
129 use Mojo::Log::Role::Color -func => 'main::DEBUG';
130 main::DEBUG error => "too %s", "cool";
131  
132 $ MOJO_LOG_FORMAT="%hms %m" PERL5OPT="-MMojo::Log::Role::Color=-func" perl -le'::l error => "bad"'
133 $ MOJO_LOG_FORMAT="%ymdT%hms [%pid] [%level] %m" PERL5OPT="-MMojo::Log::Role::Color=-func" prove -vl t/test.t
134  
135 It is possible to import a logging function that provides a quick and dirty
136 logging interface.
137  
138 The C<-func> switch might change without warning. It's only supposed to be used
139 for quick debug output.
140  
141 =head1 ATTRIBUTES
142  
143 =head2 colored
144  
145 $bool = $log->colored;
146 $log = $log->colored(1);
147  
148 Check if colored output is enabled, or force it to a given state. Defaults to
149 C<MOJO_LOG_COLORS> environment variable, or will be set to "1" if
150 L<Mojo::Log/handle> is attached to a terminal.
151  
152 =head1 AUTHOR
153  
154 Jan Henning Thorsen
155  
156 =head1 COPYRIGHT AND LICENSE
157  
158 Copyright (C) Jan Henning Thorsen
159  
160 This program is free software, you can redistribute it and/or modify it under
161 the terms of the Artistic License version 2.0.
162  
163 =head1 SEE ALSO
164  
165 L<Mojo::Log>.
166  
167 =cut
↑ Summary