line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Class::Component::Component::Plaggerize::Log; |
2
|
1
|
|
|
1
|
|
6
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
42
|
|
3
|
1
|
|
|
1
|
|
6
|
use warnings; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
38
|
|
4
|
|
|
|
|
|
|
|
5
|
1
|
|
|
1
|
|
8
|
use Encode (); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
31
|
|
6
|
1
|
|
|
1
|
|
4633
|
my $TERM_ANSICOLOR_ENABLED = eval { use Term::ANSIColor; 1; }; |
|
1
|
|
|
|
|
13192
|
|
|
1
|
|
|
|
|
1593
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
my %levels = ( |
10
|
|
|
|
|
|
|
debug => 1, |
11
|
|
|
|
|
|
|
warn => 2, |
12
|
|
|
|
|
|
|
info => 3, |
13
|
|
|
|
|
|
|
error => 4, |
14
|
|
|
|
|
|
|
); |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
sub setup_config { |
17
|
1
|
|
|
1
|
0
|
3
|
my $class = shift; |
18
|
1
|
|
|
|
|
7
|
my $config = $class->NEXT( setup_config => @_ ); |
19
|
|
|
|
|
|
|
|
20
|
1
|
50
|
|
|
|
7
|
$config->{global} = {} unless $config->{global}; |
21
|
1
|
50
|
|
|
|
7
|
$config->{global}->{log} = {} unless $config->{global}->{log}; |
22
|
1
|
50
|
|
|
|
8
|
$config->{global}->{log}->{level} = 'debug' unless $config->{global}->{log}->{level}; |
23
|
|
|
|
|
|
|
|
24
|
1
|
50
|
|
|
|
7
|
%levels = %{ $config->{global}->{log}->{levels} } if ref($config->{global}->{log}->{levels}) eq 'HASH'; |
|
0
|
|
|
|
|
0
|
|
25
|
|
|
|
|
|
|
|
26
|
1
|
|
|
|
|
8
|
$config; |
27
|
|
|
|
|
|
|
} |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
sub log { |
30
|
0
|
|
|
0
|
0
|
|
my ($self, $level, $msg, %opt) = @_; |
31
|
0
|
|
|
|
|
|
$self->NEXT( log => @_ ); |
32
|
|
|
|
|
|
|
|
33
|
0
|
|
|
|
|
|
my $conf = $self->conf->{global}->{log}; |
34
|
0
|
0
|
0
|
|
|
|
return unless ( $levels{$level} || 0 ) >= ( $levels{$conf->{level}} || 0 ); |
|
|
|
0
|
|
|
|
|
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
# hack to get the original caller as Plugin |
37
|
0
|
|
|
|
|
|
my $caller = $opt{caller}; |
38
|
0
|
0
|
|
|
|
|
unless ($caller) { |
39
|
0
|
|
|
|
|
|
my $i = 0; |
40
|
0
|
|
|
|
|
|
while (my $c = caller($i++)) { |
41
|
0
|
0
|
|
|
|
|
last if $c !~ /Plugin/; |
42
|
0
|
|
|
|
|
|
$caller = $c; |
43
|
|
|
|
|
|
|
} |
44
|
0
|
|
0
|
|
|
|
$caller ||= caller(3); |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
|
47
|
0
|
0
|
|
|
|
|
my $fh = defined($conf->{fh}) ? $conf->{fh} : \*STDOUT; |
48
|
0
|
0
|
|
|
|
|
my $ansicolor = defined($conf->{ansicolor}) ? $conf->{ansicolor} : 'red'; |
49
|
|
|
|
|
|
|
|
50
|
0
|
|
|
|
|
|
chomp($msg); |
51
|
0
|
0
|
|
|
|
|
if ( $conf->{encoding} ) { |
52
|
0
|
0
|
|
|
|
|
$msg = Encode::decode_utf8($msg) unless utf8::is_utf8($msg); |
53
|
0
|
|
|
|
|
|
$msg = Encode::encode( $conf->{encoding}, $msg ); |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
|
56
|
0
|
|
|
|
|
|
local $| = 1; |
57
|
0
|
0
|
0
|
|
|
|
print $fh Term::ANSIColor::color($ansicolor) if $ansicolor && $TERM_ANSICOLOR_ENABLED; |
58
|
0
|
|
|
|
|
|
print $fh "$caller [$level] $msg\n"; |
59
|
0
|
0
|
0
|
|
|
|
print $fh Term::ANSIColor::color("reset") if $ansicolor && $TERM_ANSICOLOR_ENABLED; |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
1; |