File Coverage

blib/lib/Log/Dispatch/Screen/Color.pm
Criterion Covered Total %
statement 48 48 100.0
branch 10 12 83.3
condition 6 9 66.6
subroutine 9 9 100.0
pod 0 2 0.0
total 73 80 91.2


line stmt bran cond sub pod time code
1             package Log::Dispatch::Screen::Color;
2 5     5   48014 use strict;
  5         12  
  5         191  
3 5     5   27 use warnings;
  5         10  
  5         160  
4 5     5   26 use base 'Log::Dispatch::Screen';
  5         8  
  5         4907  
5             our $VERSION = '0.04';
6              
7 5     5   112467 use Params::Validate qw(validate HASHREF BOOLEAN);
  5         14  
  5         313  
8             Params::Validate::validation_options( allow_extra => 1 );
9              
10 5     5   1483 use Term::ANSIColor ();
  5         12242  
  5         3390  
11             require Win32::Console::ANSI if $^O eq 'MSWin32';
12              
13             our $DEFAULT_COLOR = {
14             debug => {},
15             info => {
16             text => 'blue',
17             background => undef,
18             },
19             notice => {
20             text => 'green',
21             background => undef,
22             },
23             warning => {
24             text => 'black',
25             background => 'yellow',
26             },
27             error => {
28             text => 'red',
29             background => 'yellow',
30             },
31             critical => {
32             text => 'black',
33             background => 'red',
34             },
35             alert => {
36             text => 'white',
37             background => 'red',
38             bold => 1,
39             },
40             emergency => {
41             text => 'yellow',
42             background => 'red',
43             bold => 1,
44             },
45             };
46             $DEFAULT_COLOR->{err} = $DEFAULT_COLOR->{error};
47             $DEFAULT_COLOR->{crit} = $DEFAULT_COLOR->{critical};
48             $DEFAULT_COLOR->{emerg} = $DEFAULT_COLOR->{emergency};
49              
50              
51             sub new {
52 5     5 0 187 my $proto = shift;
53 5         60 my $self = $proto->SUPER::new(@_);
54              
55 5         1492 my %p = validate( @_, {
56             color => {
57             type => HASHREF,
58             optional => 1,
59             default => +{},
60             },
61             newline => {
62             type => BOOLEAN,
63             optional => 1,
64             default => 0,
65             },
66             });
67              
68             # generate color table
69 5         52 my $color = {};
70 5         14 while (my($level, $val) = each %{ $DEFAULT_COLOR }) {
  60         204  
71 55   66     206 my $obj = $p{color}->{$level} || $val;
72 55         273 $color->{$level} = {
73             text => $obj->{text},
74             background => $obj->{background},
75             bold => $obj->{bold},
76             };
77             }
78 5         17 $self->{color} = $color;
79              
80             # inject color callback
81 5         30 my @callbacks = $self->_get_callbacks(%p);
82 5     35   77 $self->{callbacks} = [ sub { $self->colored(@_) }, @callbacks ];
  35         2702  
83              
84             # newline
85 5 100       26 if ($p{newline}) {
86 1         3 push @{$self->{callbacks}}, \&_add_newline_callback;
  1         3  
87             }
88              
89 5         31 $self;
90             }
91              
92             my $RESET = Term::ANSIColor::color('reset');
93             my $BOLD = Term::ANSIColor::color('bold');
94             my %COLOR_CACHE;
95             sub colored {
96 35     35 0 118 my($self, %p) = @_;
97 35         60 my $message = $p{message};
98 35         52 my $level = $p{level};
99 35 50       80 return $message unless $level;
100 35         83 my $map = $self->{color}->{$level};
101 35 50       72 return $message unless $map;
102              
103 35 100       95 if (my $name = $map->{text}) {
104 32   66     112 my $color = $COLOR_CACHE{$name} ||= Term::ANSIColor::color($name);
105 32         237 $message = join '', $color, $message, $RESET;
106             }
107 35 100       97 if (my $name = $map->{background}) {
108 30   66     98 my $color = $COLOR_CACHE{"on_$name"} ||= Term::ANSIColor::color("on_$name");
109 30         136 $message = join '', $color, $message, $RESET;
110             }
111 35 100       88 if ($map->{bold}) {
112 3         6 $message = join '', $BOLD, $message, $RESET;
113             }
114              
115 35         143 return $message;
116             }
117              
118             sub _add_newline_callback {
119 1     1   9 my %p = @_;
120 1         118 return $p{message} . "\n";
121             }
122              
123              
124             1;
125             __END__