line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Logging::Simple; |
2
|
16
|
|
|
16
|
|
144956
|
use 5.007; |
|
16
|
|
|
|
|
45
|
|
3
|
16
|
|
|
16
|
|
65
|
use strict; |
|
16
|
|
|
|
|
17
|
|
|
16
|
|
|
|
|
299
|
|
4
|
16
|
|
|
16
|
|
58
|
use warnings; |
|
16
|
|
|
|
|
17
|
|
|
16
|
|
|
|
|
486
|
|
5
|
|
|
|
|
|
|
|
6
|
16
|
|
|
16
|
|
59
|
use Carp qw(croak confess); |
|
16
|
|
|
|
|
25
|
|
|
16
|
|
|
|
|
891
|
|
7
|
16
|
|
|
16
|
|
7335
|
use POSIX qw(strftime); |
|
16
|
|
|
|
|
90938
|
|
|
16
|
|
|
|
|
71
|
|
8
|
16
|
|
|
16
|
|
23692
|
use Time::HiRes qw(time); |
|
16
|
|
|
|
|
26254
|
|
|
16
|
|
|
|
|
55
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
our $VERSION = '1.03'; |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
BEGIN { |
13
|
|
|
|
|
|
|
|
14
|
129
|
|
|
129
|
|
296
|
sub _sub_names { return [qw(_0 _1 _2 _3 _4 _5 _6 _7)]; }; |
15
|
16
|
|
|
16
|
|
42
|
my $sub_names = _sub_names(); |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
# build the level subs dynamically. The code in this BEGIN block represents |
18
|
|
|
|
|
|
|
# all logging subs |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
{ |
21
|
16
|
|
|
16
|
|
3394
|
no strict 'refs'; |
|
16
|
|
|
|
|
20
|
|
|
16
|
|
|
|
|
3074
|
|
|
16
|
|
|
|
|
37
|
|
22
|
|
|
|
|
|
|
|
23
|
16
|
|
|
|
|
34
|
for (@$sub_names) { |
24
|
128
|
|
|
|
|
95
|
my $sub = $_; |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
*$_ = sub { |
27
|
165
|
|
|
165
|
|
4189
|
my ($self, $msg) = @_; |
28
|
|
|
|
|
|
|
|
29
|
165
|
100
|
|
|
|
243
|
return if $self->level == -1; |
30
|
|
|
|
|
|
|
|
31
|
149
|
100
|
|
|
|
245
|
$self->level($ENV{LS_LEVEL}) if defined $ENV{LS_LEVEL}; |
32
|
|
|
|
|
|
|
|
33
|
149
|
50
|
|
|
|
455
|
if ($sub =~ /^_(\d)$/) { |
34
|
149
|
100
|
|
|
|
185
|
if (defined $self->_log_only) { |
35
|
32
|
100
|
|
|
|
38
|
return if $1 != $self->_log_only; |
36
|
|
|
|
|
|
|
} |
37
|
121
|
100
|
|
|
|
164
|
return if $1 > $self->level; |
38
|
|
|
|
|
|
|
} |
39
|
|
|
|
|
|
|
|
40
|
106
|
|
|
|
|
606
|
my $proc = join '|', (caller(0))[1..2]; |
41
|
|
|
|
|
|
|
|
42
|
106
|
|
|
|
|
278
|
my %log_entry = ( |
43
|
|
|
|
|
|
|
label => $sub, |
44
|
|
|
|
|
|
|
proc => $proc, |
45
|
|
|
|
|
|
|
msg => $msg, |
46
|
|
|
|
|
|
|
); |
47
|
106
|
|
|
|
|
230
|
$self->_generate_entry(%log_entry); |
48
|
|
|
|
|
|
|
} |
49
|
128
|
|
|
|
|
24090
|
} |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
sub new { |
53
|
51
|
|
|
51
|
1
|
35579
|
my ($class, %args) = @_; |
54
|
|
|
|
|
|
|
|
55
|
51
|
|
|
|
|
111
|
my $self = bless {}, $class; |
56
|
|
|
|
|
|
|
|
57
|
51
|
100
|
|
|
|
127
|
if (defined $args{level}) { |
58
|
12
|
|
|
|
|
24
|
$self->level($args{level}); |
59
|
|
|
|
|
|
|
} |
60
|
|
|
|
|
|
|
else { |
61
|
39
|
100
|
|
|
|
99
|
my $lvl = defined $ENV{LS_LEVEL} ? $ENV{LS_LEVEL} : 4; |
62
|
39
|
|
|
|
|
99
|
$self->level($lvl); |
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
|
65
|
51
|
100
|
|
|
|
110
|
if ($args{file}){ |
66
|
5
|
|
|
|
|
13
|
$self->file($args{file}, $args{write_mode}); |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
|
69
|
51
|
100
|
|
|
|
111
|
my $print = defined $args{print} ? $args{print} : 1; |
70
|
51
|
|
|
|
|
106
|
$self->print($print); |
71
|
|
|
|
|
|
|
|
72
|
51
|
|
|
|
|
148
|
$self->display( |
73
|
|
|
|
|
|
|
time => 1, |
74
|
|
|
|
|
|
|
label => 1, |
75
|
|
|
|
|
|
|
name => 1, |
76
|
|
|
|
|
|
|
pid => 0, |
77
|
|
|
|
|
|
|
proc => 0, |
78
|
|
|
|
|
|
|
); |
79
|
|
|
|
|
|
|
|
80
|
51
|
100
|
|
|
|
96
|
if (defined $args{display}){ |
81
|
3
|
|
|
|
|
8
|
$self->display($args{display}); |
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
|
84
|
51
|
|
|
|
|
147
|
$self->name($args{name}); |
85
|
|
|
|
|
|
|
|
86
|
51
|
|
|
|
|
121
|
return $self; |
87
|
|
|
|
|
|
|
} |
88
|
|
|
|
|
|
|
sub level { |
89
|
367
|
|
|
367
|
1
|
1728
|
my ($self, $level) = @_; |
90
|
|
|
|
|
|
|
|
91
|
367
|
|
|
|
|
487
|
my %levels = $self->levels; |
92
|
|
|
|
|
|
|
|
93
|
367
|
100
|
|
|
|
748
|
$self->{level} = $ENV{LS_LEVEL} if defined $ENV{LS_LEVEL}; |
94
|
367
|
|
|
|
|
243
|
my $lvl; |
95
|
|
|
|
|
|
|
|
96
|
367
|
100
|
100
|
|
|
987
|
if (defined $level && $level =~ /^-1$/){ |
|
|
100
|
|
|
|
|
|
97
|
2
|
|
|
|
|
4
|
$self->{level} = $level; |
98
|
|
|
|
|
|
|
} |
99
|
|
|
|
|
|
|
elsif (defined $level){ |
100
|
69
|
|
|
|
|
72
|
my $log_only; |
101
|
|
|
|
|
|
|
|
102
|
69
|
100
|
|
|
|
159
|
if ($level =~ /^=/){ |
103
|
4
|
|
|
|
|
10
|
$level =~ s/=//; |
104
|
4
|
|
|
|
|
4
|
$log_only = 1; |
105
|
|
|
|
|
|
|
} |
106
|
69
|
100
|
66
|
|
|
404
|
if ($level =~ /^\d$/ && defined $levels{$level}){ |
107
|
68
|
|
|
|
|
101
|
$self->{level} = $level; |
108
|
|
|
|
|
|
|
} |
109
|
|
|
|
|
|
|
else { |
110
|
1
|
|
|
|
|
13
|
warn "invalid level $level specified, using default of 4\n"; |
111
|
|
|
|
|
|
|
} |
112
|
|
|
|
|
|
|
|
113
|
69
|
100
|
|
|
|
101
|
if ($log_only){ |
114
|
4
|
|
|
|
|
5
|
$self->_log_only($self->{level}); |
115
|
|
|
|
|
|
|
} |
116
|
|
|
|
|
|
|
else { |
117
|
65
|
|
|
|
|
131
|
$self->_log_only(-1); |
118
|
|
|
|
|
|
|
} |
119
|
|
|
|
|
|
|
} |
120
|
|
|
|
|
|
|
|
121
|
367
|
|
|
|
|
1061
|
return $self->{level}; |
122
|
|
|
|
|
|
|
} |
123
|
|
|
|
|
|
|
sub file { |
124
|
27
|
|
|
27
|
1
|
4220
|
my ($self, $file, $mode) = @_; |
125
|
|
|
|
|
|
|
|
126
|
27
|
100
|
|
|
|
52
|
if (! defined $file){ |
127
|
1
|
|
|
|
|
4
|
return $self->{file}; |
128
|
|
|
|
|
|
|
} |
129
|
26
|
100
|
|
|
|
61
|
if ($file =~ /^0$/){ |
130
|
7
|
50
|
|
|
|
110
|
if (tell($self->{fh}) != -1) { |
131
|
7
|
|
|
|
|
88
|
close $self->{fh}; |
132
|
|
|
|
|
|
|
} |
133
|
7
|
|
|
|
|
10
|
delete $self->{file}; |
134
|
7
|
|
|
|
|
14
|
delete $self->{fh}; |
135
|
7
|
|
|
|
|
10
|
return; |
136
|
|
|
|
|
|
|
} |
137
|
19
|
100
|
33
|
|
|
75
|
if (defined $file && $self->{file} && $file ne $self->{file}){ |
|
|
|
66
|
|
|
|
|
138
|
8
|
|
|
|
|
173
|
close $self->{fh}; |
139
|
|
|
|
|
|
|
} |
140
|
19
|
100
|
|
|
|
34
|
$mode = 'a' if ! defined $mode; |
141
|
19
|
100
|
|
|
|
75
|
my $op = $mode =~ /^a/ ? '>>' : '>'; |
142
|
|
|
|
|
|
|
|
143
|
19
|
50
|
|
|
|
803
|
open $self->{fh}, $op, $file or croak "can't open log file for writing: $!"; |
144
|
19
|
|
|
|
|
31
|
$self->{file} = $file; |
145
|
|
|
|
|
|
|
|
146
|
19
|
|
|
|
|
36
|
return $self->{file}; |
147
|
|
|
|
|
|
|
} |
148
|
|
|
|
|
|
|
sub name { |
149
|
304
|
|
|
304
|
1
|
315
|
my ($self, $name) = @_; |
150
|
304
|
100
|
|
|
|
392
|
$self->{name} = $name if defined $name; |
151
|
304
|
|
|
|
|
525
|
return $self->{name}; |
152
|
|
|
|
|
|
|
} |
153
|
|
|
|
|
|
|
sub timestamp { |
154
|
51
|
|
|
51
|
1
|
118
|
my $t = time; |
155
|
51
|
|
|
|
|
4156
|
my $date = strftime "%Y-%m-%d %H:%M:%S", localtime $t; |
156
|
51
|
|
|
|
|
301
|
$date .= sprintf ".%03d", ($t-int($t))*1000; # without rounding |
157
|
51
|
|
|
|
|
129
|
return $date; |
158
|
|
|
|
|
|
|
} |
159
|
|
|
|
|
|
|
sub levels { |
160
|
490
|
|
|
490
|
1
|
7217
|
my ($self, $lvl) = @_; |
161
|
|
|
|
|
|
|
|
162
|
490
|
|
|
|
|
589
|
my %levels = $self->_levels; |
163
|
|
|
|
|
|
|
|
164
|
490
|
100
|
|
|
|
1036
|
return $levels{$lvl} if defined $lvl; |
165
|
372
|
|
|
|
|
1428
|
return %levels; |
166
|
|
|
|
|
|
|
} |
167
|
|
|
|
|
|
|
sub labels { |
168
|
2
|
|
|
2
|
1
|
5900
|
my ($self, $labels) = @_; |
169
|
2
|
|
|
|
|
6
|
$self->_levels($labels); |
170
|
|
|
|
|
|
|
} |
171
|
|
|
|
|
|
|
sub display { |
172
|
632
|
|
|
632
|
1
|
3787
|
my $self = shift; |
173
|
632
|
|
|
|
|
346
|
my ($tag, %tags); |
174
|
|
|
|
|
|
|
|
175
|
632
|
100
|
|
|
|
713
|
if (@_ == 1){ |
176
|
568
|
|
|
|
|
396
|
$tag = shift; |
177
|
|
|
|
|
|
|
} |
178
|
|
|
|
|
|
|
else { |
179
|
64
|
|
|
|
|
198
|
%tags = @_; |
180
|
|
|
|
|
|
|
} |
181
|
|
|
|
|
|
|
|
182
|
632
|
100
|
|
|
|
762
|
if (defined $tag){ |
183
|
568
|
100
|
|
|
|
781
|
if ($tag =~ /^0$/){ |
184
|
6
|
|
|
|
|
8
|
for (keys %{ $self->{display} }){ |
|
6
|
|
|
|
|
17
|
|
185
|
30
|
|
|
|
|
26
|
$self->{display}{$_} = 0; |
186
|
|
|
|
|
|
|
} |
187
|
6
|
|
|
|
|
12
|
return 0; |
188
|
|
|
|
|
|
|
} |
189
|
562
|
100
|
|
|
|
641
|
if ($tag =~ /^1$/){ |
190
|
4
|
|
|
|
|
5
|
for (keys %{ $self->{display} }){ |
|
4
|
|
|
|
|
12
|
|
191
|
20
|
|
|
|
|
17
|
$self->{display}{$_} = 1; |
192
|
|
|
|
|
|
|
} |
193
|
4
|
|
|
|
|
12
|
return 1; |
194
|
|
|
|
|
|
|
} |
195
|
|
|
|
|
|
|
|
196
|
558
|
|
|
|
|
1284
|
return $self->{display}{$tag}; |
197
|
|
|
|
|
|
|
} |
198
|
|
|
|
|
|
|
|
199
|
64
|
|
|
|
|
179
|
my %valid = ( |
200
|
|
|
|
|
|
|
name => 0, |
201
|
|
|
|
|
|
|
time => 0, |
202
|
|
|
|
|
|
|
label => 0, |
203
|
|
|
|
|
|
|
pid => 0, |
204
|
|
|
|
|
|
|
proc => 0, |
205
|
|
|
|
|
|
|
); |
206
|
|
|
|
|
|
|
|
207
|
64
|
|
|
|
|
139
|
for (keys %tags) { |
208
|
267
|
100
|
|
|
|
356
|
if (! defined $valid{$_}){ |
209
|
1
|
|
|
|
|
18
|
warn "$_ is an invalid tag...skipping\n"; |
210
|
1
|
|
|
|
|
14
|
next; |
211
|
|
|
|
|
|
|
} |
212
|
266
|
|
|
|
|
294
|
$self->{display}{$_} = $tags{$_}; |
213
|
|
|
|
|
|
|
} |
214
|
|
|
|
|
|
|
|
215
|
64
|
|
|
|
|
84
|
return %{ $self->{display} }; |
|
64
|
|
|
|
|
179
|
|
216
|
|
|
|
|
|
|
} |
217
|
|
|
|
|
|
|
sub print { |
218
|
167
|
100
|
|
167
|
1
|
740
|
$_[0]->{print} = $_[1] if defined $_[1]; |
219
|
167
|
|
|
|
|
501
|
return $_[0]->{print}; |
220
|
|
|
|
|
|
|
} |
221
|
|
|
|
|
|
|
sub child { |
222
|
29
|
|
|
29
|
1
|
135
|
my ($self, $name) = @_; |
223
|
29
|
|
|
|
|
116
|
my $child = bless { %$self }, ref $self; |
224
|
29
|
100
|
|
|
|
39
|
$name = $self->name . ".$name" if defined $self->name; |
225
|
29
|
|
|
|
|
31
|
$child->name($name); |
226
|
29
|
|
|
|
|
31
|
return $child; |
227
|
|
|
|
|
|
|
} |
228
|
|
|
|
|
|
|
sub custom_display { |
229
|
114
|
|
|
114
|
1
|
489
|
my ($self, $disp) = @_; |
230
|
|
|
|
|
|
|
|
231
|
114
|
100
|
|
|
|
168
|
if (defined $disp) { |
232
|
2
|
100
|
|
|
|
8
|
if ($disp =~ /^0$/) { |
233
|
1
|
|
|
|
|
3
|
delete $self->{custom_display}; |
234
|
1
|
|
|
|
|
2
|
return 0; |
235
|
|
|
|
|
|
|
} |
236
|
|
|
|
|
|
|
else { |
237
|
1
|
|
|
|
|
2
|
$self->{custom_display} = $disp; |
238
|
|
|
|
|
|
|
} |
239
|
|
|
|
|
|
|
} |
240
|
113
|
|
|
|
|
183
|
return $self->{custom_display}; |
241
|
|
|
|
|
|
|
} |
242
|
|
|
|
|
|
|
sub fatal { |
243
|
1
|
|
|
1
|
1
|
6
|
my ($self, $msg) = @_; |
244
|
1
|
|
|
|
|
2
|
$self->display(1); |
245
|
1
|
|
|
|
|
5
|
confess("\n" . $self->_0("$msg")); |
246
|
|
|
|
|
|
|
} |
247
|
|
|
|
|
|
|
sub _generate_entry { |
248
|
|
|
|
|
|
|
# builds/formats the log entry line |
249
|
|
|
|
|
|
|
|
250
|
111
|
|
|
111
|
|
113
|
my $self = shift; |
251
|
111
|
|
|
|
|
150
|
my %entry = @_; |
252
|
|
|
|
|
|
|
|
253
|
111
|
|
|
|
|
109
|
my $label = $entry{label}; |
254
|
111
|
|
|
|
|
103
|
my $proc = $entry{proc}; |
255
|
111
|
|
|
|
|
90
|
my $msg = $entry{msg}; |
256
|
|
|
|
|
|
|
|
257
|
111
|
|
|
|
|
157
|
my $subs = $self->_sub_names; |
258
|
111
|
100
|
|
|
|
132
|
if (! grep { $label eq $_ } @$subs){ |
|
888
|
|
|
|
|
892
|
|
259
|
1
|
|
|
|
|
136
|
croak "_generate_entry() requires a label => sub/label param\n"; |
260
|
|
|
|
|
|
|
} |
261
|
|
|
|
|
|
|
|
262
|
110
|
|
|
|
|
243
|
$label =~ s/_//; |
263
|
110
|
|
|
|
|
175
|
$label = $self->levels($label); |
264
|
|
|
|
|
|
|
|
265
|
110
|
100
|
|
|
|
178
|
$msg = $msg ? "$msg\n" : "\n"; |
266
|
|
|
|
|
|
|
|
267
|
110
|
|
|
|
|
86
|
my $log_entry; |
268
|
110
|
100
|
|
|
|
144
|
$log_entry .= $self->custom_display if defined $self->custom_display; |
269
|
110
|
100
|
|
|
|
140
|
$log_entry .= "[".$self->timestamp()."]" if $self->display('time'); |
270
|
110
|
100
|
|
|
|
159
|
$log_entry .= "[$label]" if $self->display('label'); |
271
|
110
|
100
|
100
|
|
|
137
|
$log_entry .= "[".$self->name."]" if $self->display('name') && $self->name; |
272
|
110
|
100
|
|
|
|
138
|
$log_entry .= "[$$]" if $self->display('pid'); |
273
|
110
|
100
|
|
|
|
123
|
$log_entry .= "[$proc]" if $self->display('proc'); |
274
|
110
|
100
|
|
|
|
176
|
$log_entry .= " " if $log_entry; |
275
|
110
|
|
|
|
|
82
|
$log_entry .= $msg; |
276
|
|
|
|
|
|
|
|
277
|
110
|
100
|
|
|
|
128
|
return $log_entry if ! $self->print; |
278
|
|
|
|
|
|
|
|
279
|
47
|
100
|
|
|
|
56
|
if ($self->{fh}){ |
280
|
45
|
|
|
|
|
26
|
print { $self->{fh} } $log_entry; |
|
45
|
|
|
|
|
216
|
|
281
|
|
|
|
|
|
|
} |
282
|
|
|
|
|
|
|
else { |
283
|
2
|
|
|
|
|
544
|
print $log_entry; |
284
|
|
|
|
|
|
|
} |
285
|
|
|
|
|
|
|
} |
286
|
|
|
|
|
|
|
sub _levels { |
287
|
|
|
|
|
|
|
# manages the level labels |
288
|
|
|
|
|
|
|
|
289
|
492
|
|
|
492
|
|
356
|
my ($self, $labels) = @_; |
290
|
|
|
|
|
|
|
|
291
|
492
|
100
|
|
|
|
793
|
if (ref $labels eq 'ARRAY'){ |
292
|
1
|
50
|
|
|
|
3
|
croak "must supply exactly 8 custom labels\n" if @$labels != 8; |
293
|
1
|
|
|
|
|
3
|
my %custom_levels = map {$_ => $labels->[$_]} (0..7); |
|
8
|
|
|
|
|
16
|
|
294
|
1
|
|
|
|
|
3
|
$self->{labels} = \%custom_levels; |
295
|
|
|
|
|
|
|
} |
296
|
|
|
|
|
|
|
|
297
|
492
|
100
|
100
|
|
|
2130
|
if (defined $labels && $labels == 0 || ! defined $self->{labels}) { |
|
|
|
100
|
|
|
|
|
298
|
|
|
|
|
|
|
$self->{labels} = { |
299
|
52
|
|
|
|
|
371
|
0 => 'lvl 0', |
300
|
|
|
|
|
|
|
1 => 'lvl 1', |
301
|
|
|
|
|
|
|
2 => 'lvl 2', |
302
|
|
|
|
|
|
|
3 => 'lvl 3', |
303
|
|
|
|
|
|
|
4 => 'lvl 4', |
304
|
|
|
|
|
|
|
5 => 'lvl 5', |
305
|
|
|
|
|
|
|
6 => 'lvl 6', |
306
|
|
|
|
|
|
|
7 => 'lvl 7', |
307
|
|
|
|
|
|
|
}; |
308
|
|
|
|
|
|
|
} |
309
|
492
|
|
|
|
|
298
|
return %{ $self->{labels} }; |
|
492
|
|
|
|
|
2190
|
|
310
|
|
|
|
|
|
|
} |
311
|
|
|
|
|
|
|
sub _log_only { |
312
|
|
|
|
|
|
|
# are we logging only one level or not? |
313
|
|
|
|
|
|
|
|
314
|
250
|
|
|
250
|
|
205
|
my ($self, $level) = @_; |
315
|
250
|
100
|
100
|
|
|
530
|
if (defined $level && $level == -1){ |
316
|
65
|
|
|
|
|
103
|
$self->{log_only} = undef; |
317
|
|
|
|
|
|
|
} |
318
|
|
|
|
|
|
|
else { |
319
|
185
|
100
|
|
|
|
234
|
$self->{log_only} = $level if defined $level; |
320
|
|
|
|
|
|
|
} |
321
|
250
|
|
|
|
|
673
|
return $self->{log_only}; |
322
|
|
|
|
|
|
|
} |
323
|
|
|
|
|
|
|
|
324
|
|
|
|
|
|
|
1; |
325
|
|
|
|
|
|
|
__END__ |