line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Log::Any::Plugin::ANSIColor; |
2
|
5
|
|
|
5
|
|
5467
|
use 5.008001; |
|
5
|
|
|
|
|
16
|
|
3
|
5
|
|
|
5
|
|
24
|
use strict; |
|
5
|
|
|
|
|
6
|
|
|
5
|
|
|
|
|
88
|
|
4
|
5
|
|
|
5
|
|
20
|
use warnings; |
|
5
|
|
|
|
|
8
|
|
|
5
|
|
|
|
|
225
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
our $VERSION = "0.02"; |
7
|
|
|
|
|
|
|
|
8
|
5
|
|
|
5
|
|
444
|
use Log::Any::Plugin::Util qw( get_old_method set_new_method ); |
|
5
|
|
|
|
|
7750
|
|
|
5
|
|
|
|
|
283
|
|
9
|
5
|
|
|
|
|
313
|
use Log::Any::Adapter::Util qw( |
10
|
|
|
|
|
|
|
log_level_aliases logging_aliases logging_methods |
11
|
5
|
|
|
5
|
|
28
|
); |
|
5
|
|
|
|
|
8
|
|
12
|
5
|
|
|
5
|
|
608
|
use Term::ANSIColor qw( colored colorvalid ); |
|
5
|
|
|
|
|
6842
|
|
|
5
|
|
|
|
|
2334
|
|
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
our %default = ( |
15
|
|
|
|
|
|
|
emergency => 'bold magenta', |
16
|
|
|
|
|
|
|
alert => 'magenta', |
17
|
|
|
|
|
|
|
critical => 'bold red', |
18
|
|
|
|
|
|
|
error => 'red', |
19
|
|
|
|
|
|
|
warning => 'yellow', |
20
|
|
|
|
|
|
|
debug => 'cyan', |
21
|
|
|
|
|
|
|
trace => 'blue', |
22
|
|
|
|
|
|
|
); |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
sub install { |
25
|
4
|
|
|
4
|
0
|
144
|
my ($class, $adapter_class, %color_map) = @_; |
26
|
|
|
|
|
|
|
|
27
|
4
|
100
|
100
|
|
|
26
|
if ((delete $color_map{default}) || (keys %color_map == 0)) { |
28
|
|
|
|
|
|
|
# Copy the default colors, leaving any the user has specified. |
29
|
2
|
|
|
|
|
8
|
for my $method (keys %default) { |
30
|
14
|
|
66
|
|
|
35
|
$color_map{$method} ||= $default{$method}; |
31
|
|
|
|
|
|
|
} |
32
|
|
|
|
|
|
|
} |
33
|
|
|
|
|
|
|
|
34
|
4
|
|
|
|
|
15
|
my %aliases = log_level_aliases(); |
35
|
4
|
|
|
|
|
34
|
for my $alias (keys %aliases) { |
36
|
20
|
|
|
|
|
26
|
my $method = $aliases{$alias}; |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
$color_map{$alias} = $color_map{$method} |
39
|
20
|
100
|
|
|
|
47
|
if exists $color_map{$method}; |
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
|
42
|
4
|
|
|
|
|
12
|
for my $method_name ( logging_methods(), logging_aliases() ) { |
43
|
56
|
100
|
|
|
|
280
|
if (my $color = delete $color_map{$method_name}) { |
44
|
|
|
|
|
|
|
# Specifying none as the color name disables colorisation for that |
45
|
|
|
|
|
|
|
# method. |
46
|
30
|
100
|
|
|
|
52
|
next if $color eq 'none'; |
47
|
|
|
|
|
|
|
|
48
|
28
|
100
|
|
|
|
65
|
if (!colorvalid($color)) { |
49
|
2
|
|
|
|
|
36
|
warn "Invalid color name \"$color\" for $method_name"; |
50
|
2
|
|
|
|
|
12
|
next; |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
|
53
|
26
|
|
|
|
|
408
|
my $old_method = get_old_method($adapter_class, $method_name); |
54
|
|
|
|
|
|
|
set_new_method($adapter_class, $method_name, sub { |
55
|
26
|
|
|
26
|
|
29639
|
my $self = shift; |
56
|
|
|
|
|
|
|
$self->$old_method( |
57
|
|
|
|
|
|
|
# Colorise non-ref arguments, leave ref args alone. |
58
|
26
|
50
|
|
|
|
87
|
map { ref $_ ? $_ : colored([$color], $_) } @_ |
|
26
|
|
|
|
|
102
|
|
59
|
|
|
|
|
|
|
); |
60
|
26
|
|
|
|
|
683
|
}); |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
|
64
|
4
|
100
|
|
|
|
46
|
if (my @remainder = sort keys %color_map) { |
65
|
1
|
|
|
|
|
15
|
warn 'Unknown logging methods: ', join(', ', @remainder); |
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
1; |
70
|
|
|
|
|
|
|
__END__ |