line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Log::StdLog; |
2
|
|
|
|
|
|
|
|
3
|
15
|
|
|
15
|
|
114245
|
use version; $VERSION = qv('0.0.3'); |
|
15
|
|
|
|
|
54820
|
|
|
15
|
|
|
|
|
98
|
|
4
|
|
|
|
|
|
|
|
5
|
15
|
|
|
15
|
|
1697
|
use warnings; |
|
15
|
|
|
|
|
29
|
|
|
15
|
|
|
|
|
502
|
|
6
|
15
|
|
|
15
|
|
76
|
use strict; |
|
15
|
|
|
|
|
38
|
|
|
15
|
|
|
|
|
434
|
|
7
|
15
|
|
|
15
|
|
80
|
use Carp; |
|
15
|
|
|
|
|
27
|
|
|
15
|
|
|
|
|
1751
|
|
8
|
|
|
|
|
|
|
|
9
|
15
|
|
|
15
|
|
130
|
use base 'IO::File'; |
|
15
|
|
|
|
|
28
|
|
|
15
|
|
|
|
|
19299
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
my @levels = qw( all trace debug user info warn error fatal none ); |
12
|
|
|
|
|
|
|
my %severity; @severity{@levels} = 1..@levels; |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
# Aliases... |
15
|
|
|
|
|
|
|
$severity{warning} = $severity{warn}; |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
sub _make_formatter { |
19
|
14
|
|
|
14
|
|
33
|
my ($format) = @_; |
20
|
|
|
|
|
|
|
return sub { |
21
|
49
|
|
|
49
|
|
120
|
my ($time, $source, $type, @msg) = @_; |
22
|
49
|
|
|
|
|
111
|
my $msg = join q{}, @msg; |
23
|
49
|
|
|
|
|
240
|
return sprintf $format, $time, $source, $type, $msg; |
24
|
|
|
|
|
|
|
} |
25
|
14
|
|
|
|
|
99
|
} |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
sub import { |
28
|
15
|
|
|
15
|
|
286
|
my ($package, $opt_ref) = @_; |
29
|
15
|
|
|
|
|
56
|
my ($caller, $file) = caller; |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
|
32
|
15
|
50
|
66
|
|
|
172
|
croak "Usage: use $package { file=>\$filename, level=>\$level, format=>sub{...} }\n " |
33
|
|
|
|
|
|
|
if $opt_ref && not ref $opt_ref eq 'HASH'; |
34
|
|
|
|
|
|
|
|
35
|
15
|
100
|
|
|
|
79
|
if (not exists $opt_ref->{file}) { |
36
|
2
|
|
|
|
|
9
|
$opt_ref->{file} = "$file.log"; |
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
|
39
|
15
|
100
|
|
|
|
79
|
if (not exists $opt_ref->{format}) { |
|
|
50
|
|
|
|
|
|
40
|
14
|
|
|
|
|
46
|
$opt_ref->{format} = _make_formatter("[%s] [%s] [%s] %s"); |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
elsif (not ref $opt_ref->{format}) { |
43
|
0
|
|
|
|
|
0
|
$opt_ref->{format} = _make_formatter($opt_ref->{format}); |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
|
46
|
15
|
100
|
|
|
|
64
|
if (not exists $opt_ref->{level}) { |
47
|
1
|
|
|
|
|
3
|
$opt_ref->{level} = 'user'; |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
|
50
|
15
|
|
|
15
|
|
232895
|
no strict 'refs'; |
|
15
|
|
|
|
|
42
|
|
|
15
|
|
|
|
|
2792
|
|
51
|
15
|
|
|
|
|
31
|
tie *{$caller.'::STDLOG'}, $package, $opt_ref; |
|
15
|
|
|
|
|
196
|
|
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
sub TIEHANDLE { |
55
|
15
|
|
|
15
|
|
34
|
my ($package, $opt_ref) = @_; |
56
|
|
|
|
|
|
|
|
57
|
15
|
|
50
|
|
|
3202
|
return bless { file => $opt_ref->{file}, |
|
|
|
33
|
|
|
|
|
58
|
|
|
|
|
|
|
handle => $opt_ref->{handle}, |
59
|
|
|
|
|
|
|
formatter => $opt_ref->{format}, |
60
|
|
|
|
|
|
|
min_severity_name => $opt_ref->{level} || 'user', |
61
|
|
|
|
|
|
|
min_severity => $severity{$opt_ref->{level}} |
62
|
|
|
|
|
|
|
|| $severity{user}, |
63
|
|
|
|
|
|
|
}; |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
|
66
|
15
|
|
|
15
|
|
84
|
use Fcntl ':flock'; |
|
15
|
|
|
|
|
35
|
|
|
15
|
|
|
|
|
11150
|
|
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
sub PRINT { |
69
|
86
|
100
|
|
86
|
|
1029
|
my ($self, $level, @msg) |
|
|
100
|
|
|
|
|
|
70
|
|
|
|
|
|
|
= @_ == 1 ? ( $_[0], $_[0]->{min_severity_name}, $_ ) |
71
|
|
|
|
|
|
|
: @_ == 2 ? ( $_[0], $_[0]->{min_severity_name}, $_[1] ) |
72
|
|
|
|
|
|
|
: ( @_ ) |
73
|
|
|
|
|
|
|
; |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
# No-op if message isn't important enough... |
76
|
86
|
|
66
|
|
|
286
|
my $severity = $severity{$level} || $severity{user}; |
77
|
86
|
100
|
|
|
|
638
|
return 0 if $self->{min_severity} > $severity; |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
# Format message early to get accurate time-stamp... |
80
|
54
|
|
|
|
|
156
|
my ($sec,$min,$hour,$day,$mon,$year) = localtime; |
81
|
54
|
|
|
|
|
383
|
$year+=1900; |
82
|
54
|
|
|
|
|
94
|
$mon++; |
83
|
54
|
|
|
|
|
536
|
my $time = sprintf("%04d%02d%02d.%02d%02d%02d", |
84
|
|
|
|
|
|
|
$year, $mon, $day, $hour, $min, $sec); |
85
|
54
|
50
|
|
|
|
324
|
$msg[-1] =~ s/\n\z// if @msg; |
86
|
54
|
|
|
|
|
187
|
my $log_msg = $self->{formatter}->($time, $$, $level, @msg); |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
# Create connection to log file, if necessary... |
89
|
54
|
100
|
|
|
|
197
|
if (not $self->{handle}) { |
90
|
12
|
50
|
|
12
|
|
499
|
open $self->{handle}, '>>', $self->{file} |
|
12
|
|
|
|
|
126
|
|
|
12
|
|
|
|
|
27
|
|
|
12
|
|
|
|
|
104
|
|
91
|
|
|
|
|
|
|
or croak "Unable to open log file '$self->{file}'"; |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
# Synchronize writing to file via advisory locking... |
95
|
54
|
|
|
|
|
22949
|
flock($self->{handle}, LOCK_EX); |
96
|
54
|
|
|
|
|
721
|
my $result = $self->{handle}->print($log_msg."\n"); |
97
|
54
|
|
|
|
|
508
|
flock($self->{handle}, LOCK_UN); |
98
|
|
|
|
|
|
|
|
99
|
54
|
|
|
|
|
237
|
return $result; |
100
|
|
|
|
|
|
|
} |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
sub CLOSE { |
103
|
0
|
|
|
0
|
|
0
|
my ($self) = @_; |
104
|
0
|
|
|
|
|
0
|
$self->{handle}->close(); |
105
|
|
|
|
|
|
|
} |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
1; # Magic true value required at end of module |
108
|
|
|
|
|
|
|
__END__ |