line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Log::Dump; |
2
|
|
|
|
|
|
|
|
3
|
20
|
|
|
20
|
|
658252
|
use strict; |
|
20
|
|
|
|
|
50
|
|
|
20
|
|
|
|
|
719
|
|
4
|
20
|
|
|
20
|
|
100
|
use warnings; |
|
20
|
|
|
|
|
39
|
|
|
20
|
|
|
|
|
522
|
|
5
|
20
|
|
|
20
|
|
20304
|
use Sub::Install qw( install_sub ); |
|
20
|
|
|
|
|
45822
|
|
|
20
|
|
|
|
|
120
|
|
6
|
20
|
|
|
20
|
|
2747
|
use Scalar::Util qw( blessed ); |
|
20
|
|
|
|
|
44
|
|
|
20
|
|
|
|
|
5911
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
our $VERSION = '0.14'; |
9
|
|
|
|
|
|
|
our @CARP_NOT = qw/Log::Dump Log::Dump::Class Log::Dump::Functions/; |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
sub import { |
12
|
23
|
|
|
23
|
|
931
|
my $class = shift; |
13
|
23
|
|
|
|
|
57
|
my $caller = caller; |
14
|
|
|
|
|
|
|
|
15
|
23
|
50
|
|
|
|
114
|
return if $caller eq 'main'; |
16
|
|
|
|
|
|
|
|
17
|
23
|
|
|
|
|
150
|
my @methods = qw/logger logfilter logfile logcolor logtime log/; |
18
|
23
|
|
|
|
|
55
|
for my $method (@methods) { |
19
|
138
|
|
|
|
|
752
|
install_sub({ |
20
|
|
|
|
|
|
|
as => $method, |
21
|
|
|
|
|
|
|
into => $caller, |
22
|
138
|
|
|
|
|
7288
|
code => \&{$method}, |
23
|
|
|
|
|
|
|
}); |
24
|
|
|
|
|
|
|
} |
25
|
|
|
|
|
|
|
} |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
sub logger { |
28
|
232
|
|
|
232
|
1
|
40227
|
my $self = shift; |
29
|
|
|
|
|
|
|
|
30
|
232
|
|
|
|
|
319
|
my $logger = $_[0]; |
31
|
232
|
100
|
|
|
|
814
|
if ( blessed $self ) { |
32
|
103
|
100
|
|
|
|
477
|
@_ ? $self->{_logger} = $logger : $self->{_logger}; |
33
|
|
|
|
|
|
|
} |
34
|
|
|
|
|
|
|
else { |
35
|
20
|
|
|
20
|
|
114
|
no strict 'refs'; |
|
20
|
|
|
|
|
38
|
|
|
20
|
|
|
|
|
16366
|
|
36
|
129
|
100
|
|
|
|
330
|
@_ ? ${"$self\::_logger"} = $logger : ${"$self\::_logger"}; |
|
23
|
|
|
|
|
173
|
|
|
106
|
|
|
|
|
704
|
|
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
} |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
sub logfilter { |
41
|
240
|
|
|
240
|
1
|
35378
|
my $self = shift; |
42
|
|
|
|
|
|
|
|
43
|
240
|
|
|
|
|
345
|
my $filter = undef; |
44
|
240
|
100
|
66
|
|
|
620
|
if (@_ && $_[0]) { |
45
|
12
|
|
|
|
|
48
|
$filter = {pos => [], neg => []}; |
46
|
12
|
|
|
|
|
31
|
for (@_) { |
47
|
32
|
100
|
|
|
|
83
|
if (substr($_, 0, 1) eq '!') { |
48
|
16
|
|
|
|
|
21
|
push @{$filter->{neg}}, substr($_, 1); |
|
16
|
|
|
|
|
57
|
|
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
else { |
51
|
16
|
|
|
|
|
18
|
push @{$filter->{pos}}, $_; |
|
16
|
|
|
|
|
103
|
|
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
} |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
|
56
|
240
|
100
|
|
|
|
696
|
if ( blessed $self ) { |
57
|
117
|
100
|
|
|
|
506
|
@_ ? $self->{_logfilter} = $filter : $self->{_logfilter}; |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
else { |
60
|
20
|
|
|
20
|
|
186
|
no strict 'refs'; |
|
20
|
|
|
|
|
57
|
|
|
20
|
|
|
|
|
3285
|
|
61
|
123
|
100
|
|
|
|
266
|
@_ ? ${"$self\::_logfilter"} = $filter : ${"$self\::_logfilter"}; |
|
12
|
|
|
|
|
69
|
|
|
111
|
|
|
|
|
692
|
|
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
sub logfile { |
66
|
126
|
|
|
126
|
1
|
9332
|
my $self = shift; |
67
|
|
|
|
|
|
|
|
68
|
126
|
|
|
|
|
149
|
my $logfile_ref; |
69
|
126
|
100
|
|
|
|
388
|
if ( blessed $self ) { |
70
|
61
|
|
|
|
|
189
|
$logfile_ref = \($self->{_logfile}); |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
else { |
73
|
20
|
|
|
20
|
|
120
|
no strict 'refs'; |
|
20
|
|
|
|
|
40
|
|
|
20
|
|
|
|
|
5048
|
|
74
|
65
|
|
|
|
|
83
|
$logfile_ref = \(${"$self\::_logfile"}); |
|
65
|
|
|
|
|
302
|
|
75
|
|
|
|
|
|
|
} |
76
|
|
|
|
|
|
|
|
77
|
126
|
100
|
66
|
|
|
703
|
if ( @_ && $_[0] ) { |
|
|
100
|
66
|
|
|
|
|
78
|
4
|
50
|
|
|
|
27
|
push @_, 'w' if @_ == 1; |
79
|
4
|
|
|
|
|
1906
|
require IO::File; |
80
|
4
|
50
|
|
|
|
2161
|
my $fh = IO::File->new(@_) or $self->log( fatal => $! ); |
81
|
4
|
|
|
|
|
696
|
$$logfile_ref = $fh; |
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
elsif ( @_ && !$_[0] ) { |
84
|
4
|
50
|
|
|
|
36
|
$$logfile_ref->close if $$logfile_ref; |
85
|
4
|
|
|
|
|
312
|
$$logfile_ref = undef; |
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
else { |
88
|
118
|
|
|
|
|
640
|
$$logfile_ref; |
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
} |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
sub logtime { |
93
|
136
|
|
|
136
|
1
|
9311
|
my $self = shift; |
94
|
|
|
|
|
|
|
|
95
|
136
|
|
|
|
|
155
|
my $logtime_ref; |
96
|
136
|
100
|
|
|
|
423
|
if ( blessed $self ) { |
97
|
65
|
|
|
|
|
144
|
$logtime_ref = \($self->{_logtime}); |
98
|
|
|
|
|
|
|
} |
99
|
|
|
|
|
|
|
else { |
100
|
20
|
|
|
20
|
|
113
|
no strict 'refs'; |
|
20
|
|
|
|
|
52
|
|
|
20
|
|
|
|
|
5541
|
|
101
|
71
|
|
|
|
|
95
|
$logtime_ref = \(${"$self\::_logtime"}); |
|
71
|
|
|
|
|
310
|
|
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
|
104
|
136
|
100
|
66
|
|
|
720
|
if ( @_ && $_[0] ) { |
|
|
100
|
66
|
|
|
|
|
105
|
8
|
|
|
|
|
12
|
eval { require Time::Piece }; |
|
8
|
|
|
|
|
50
|
|
106
|
8
|
50
|
|
|
|
25
|
return $$logtime_ref = undef if $@; |
107
|
|
|
|
|
|
|
|
108
|
8
|
100
|
|
|
|
30
|
my $format = $_[0] =~ /%/ ? $_[0] : '%Y-%m-%d %H:%M:%S'; |
109
|
8
|
|
|
8
|
|
41
|
$$logtime_ref = sub { Time::Piece->new(shift)->strftime($format) }; |
|
8
|
|
|
|
|
41
|
|
110
|
|
|
|
|
|
|
} |
111
|
|
|
|
|
|
|
elsif ( @_ && !$_[0] ) { |
112
|
8
|
|
|
|
|
26
|
$$logtime_ref = undef; |
113
|
|
|
|
|
|
|
} |
114
|
|
|
|
|
|
|
else { |
115
|
120
|
|
|
|
|
358
|
$$logtime_ref; |
116
|
|
|
|
|
|
|
} |
117
|
|
|
|
|
|
|
} |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
sub logcolor { |
120
|
128
|
|
|
128
|
1
|
7636
|
my $self = shift; |
121
|
|
|
|
|
|
|
|
122
|
128
|
|
|
|
|
153
|
my $logcolor_ref; |
123
|
128
|
100
|
|
|
|
499
|
if ( blessed $self ) { |
124
|
61
|
|
|
|
|
158
|
$logcolor_ref = \($self->{_logcolor}); |
125
|
|
|
|
|
|
|
} |
126
|
|
|
|
|
|
|
else { |
127
|
20
|
|
|
20
|
|
113
|
no strict 'refs'; |
|
20
|
|
|
|
|
41
|
|
|
20
|
|
|
|
|
16083
|
|
128
|
67
|
|
|
|
|
91
|
$logcolor_ref = \(${"$self\::_logcolor"}); |
|
67
|
|
|
|
|
746
|
|
129
|
|
|
|
|
|
|
} |
130
|
|
|
|
|
|
|
|
131
|
128
|
100
|
|
|
|
419
|
unless ( defined $$logcolor_ref ) { |
132
|
30
|
|
|
|
|
60
|
eval { require Term::ANSIColor }; |
|
30
|
|
|
|
|
16063
|
|
133
|
30
|
50
|
|
|
|
125192
|
$$logcolor_ref = $@ ? 0 : {}; |
134
|
|
|
|
|
|
|
|
135
|
30
|
50
|
|
|
|
244
|
eval { require Win32::Console::ANSI } if $^O eq 'MSWin32'; |
|
0
|
|
|
|
|
0
|
|
136
|
|
|
|
|
|
|
} |
137
|
128
|
50
|
|
|
|
345
|
return unless $$logcolor_ref; |
138
|
|
|
|
|
|
|
|
139
|
128
|
100
|
66
|
|
|
714
|
if ( @_ == 1 && $_[0] ) { |
|
|
100
|
66
|
|
|
|
|
|
|
50
|
|
|
|
|
|
140
|
120
|
|
|
|
|
509
|
$$logcolor_ref->{$_[0]}; |
141
|
|
|
|
|
|
|
} |
142
|
|
|
|
|
|
|
elsif ( @_ && !$_[0] ) { |
143
|
4
|
|
|
|
|
13
|
$$logcolor_ref = {}; |
144
|
|
|
|
|
|
|
} |
145
|
|
|
|
|
|
|
elsif ( @_ % 2 == 0 ) { |
146
|
4
|
|
|
|
|
8
|
$$logcolor_ref = { %{ $$logcolor_ref }, @_ }; |
|
4
|
|
|
|
|
26
|
|
147
|
|
|
|
|
|
|
} |
148
|
|
|
|
|
|
|
} |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
sub log { |
151
|
164
|
|
|
164
|
1
|
106981
|
my $self = shift; |
152
|
|
|
|
|
|
|
|
153
|
164
|
|
|
|
|
490
|
my $logger = $self->logger; |
154
|
|
|
|
|
|
|
|
155
|
164
|
100
|
100
|
|
|
1818
|
if ( defined $logger and !$logger ) { |
|
|
100
|
66
|
|
|
|
|
|
|
|
66
|
|
|
|
|
156
|
12
|
|
|
|
|
34
|
return; |
157
|
|
|
|
|
|
|
} |
158
|
|
|
|
|
|
|
elsif ( $logger and $logger =~ /^[A-Za-z]/ && $logger->can('log') ) { |
159
|
8
|
|
|
|
|
33
|
$logger->log(@_); |
160
|
|
|
|
|
|
|
} |
161
|
|
|
|
|
|
|
else { |
162
|
144
|
|
|
|
|
224
|
my $label = shift; |
163
|
|
|
|
|
|
|
|
164
|
144
|
100
|
|
|
|
439
|
if ($self->logfilter) { |
165
|
44
|
100
|
|
|
|
53
|
if (my @neg = @{ $self->logfilter->{neg} }) { |
|
44
|
|
|
|
|
85
|
|
166
|
32
|
100
|
|
|
|
58
|
return if grep { $label eq $_ } @neg; |
|
64
|
|
|
|
|
248
|
|
167
|
|
|
|
|
|
|
} |
168
|
28
|
100
|
|
|
|
49
|
if (my @pos = @{ $self->logfilter->{pos} }) { |
|
28
|
|
|
|
|
56
|
|
169
|
24
|
100
|
|
|
|
36
|
return if !grep { $label eq $_ } @pos; |
|
48
|
|
|
|
|
215
|
|
170
|
|
|
|
|
|
|
} |
171
|
|
|
|
|
|
|
} |
172
|
|
|
|
|
|
|
|
173
|
120
|
|
|
|
|
17195
|
require Data::Dump; |
174
|
120
|
100
|
|
|
|
107166
|
my $msg = join '', map { ref $_ ? Data::Dump::dump($_) : $_ } @_; |
|
132
|
|
|
|
|
606
|
|
175
|
120
|
|
|
|
|
1092
|
my $colored_msg = $msg; |
176
|
120
|
100
|
|
|
|
521
|
if ( my $color = $self->logcolor($label) ) { |
177
|
8
|
|
|
|
|
13
|
eval { $colored_msg = Term::ANSIColor::colored($msg, $color) }; |
|
8
|
|
|
|
|
32
|
|
178
|
8
|
50
|
|
|
|
346
|
$colored_msg = $msg if $@; |
179
|
|
|
|
|
|
|
} |
180
|
120
|
|
|
|
|
221
|
my $time = ''; |
181
|
120
|
100
|
|
|
|
477
|
if (my $func = $self->logtime) { |
182
|
8
|
|
|
|
|
43
|
$time = $func->(time) . " "; |
183
|
|
|
|
|
|
|
} |
184
|
|
|
|
|
|
|
|
185
|
120
|
100
|
100
|
|
|
1790
|
if ( $label eq 'fatal' ) { |
|
|
100
|
|
|
|
|
|
186
|
6
|
|
|
|
|
29
|
require Carp; |
187
|
6
|
|
|
|
|
1366
|
Carp::croak $time."[$label] $colored_msg"; |
188
|
|
|
|
|
|
|
} |
189
|
|
|
|
|
|
|
elsif ( $label eq 'error' or $label eq 'warn' ) { |
190
|
14
|
|
|
|
|
72
|
require Carp; |
191
|
14
|
|
|
|
|
3633
|
Carp::carp $time."[$label] $colored_msg"; |
192
|
14
|
50
|
|
|
|
592
|
$self->logfile->print(Carp::shortmess($time."[$label] $msg"), "\n") if $self->logfile; |
193
|
|
|
|
|
|
|
} |
194
|
|
|
|
|
|
|
else { |
195
|
100
|
|
|
|
|
2179
|
print STDERR $time."[$label] $colored_msg\n"; |
196
|
100
|
100
|
|
|
|
1311
|
$self->logfile->print($time."[$label] $msg\n") if $self->logfile; |
197
|
|
|
|
|
|
|
} |
198
|
|
|
|
|
|
|
} |
199
|
|
|
|
|
|
|
} |
200
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
1; |
202
|
|
|
|
|
|
|
|
203
|
|
|
|
|
|
|
__END__ |