line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Tie::LogFile; |
2
|
|
|
|
|
|
|
# |
3
|
|
|
|
|
|
|
# $Id: LogFile.pm,v 1.1.1.1 2002/10/16 10:08:08 ctriv Exp $ |
4
|
|
|
|
|
|
|
# |
5
|
5
|
|
|
5
|
|
117333
|
use 5.006; |
|
5
|
|
|
|
|
19
|
|
|
5
|
|
|
|
|
199
|
|
6
|
5
|
|
|
5
|
|
24
|
use strict; |
|
5
|
|
|
|
|
10
|
|
|
5
|
|
|
|
|
132
|
|
7
|
5
|
|
|
5
|
|
28
|
use warnings; |
|
5
|
|
|
|
|
13
|
|
|
5
|
|
|
|
|
167
|
|
8
|
5
|
|
|
5
|
|
64
|
use Carp; |
|
5
|
|
|
|
|
9
|
|
|
5
|
|
|
|
|
471
|
|
9
|
5
|
|
|
5
|
|
4185
|
use Symbol (); |
|
5
|
|
|
|
|
4647
|
|
|
5
|
|
|
|
|
4700
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
our $VERSION = 0.1; |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
our %defaults = ( |
14
|
|
|
|
|
|
|
'format' => '[%d] %m', |
15
|
|
|
|
|
|
|
'tformat' => '', |
16
|
|
|
|
|
|
|
'mode' => '>>', |
17
|
|
|
|
|
|
|
'force_newline' => 1, |
18
|
|
|
|
|
|
|
'autoflush' => 0, |
19
|
|
|
|
|
|
|
); |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
our %formats = ( |
22
|
|
|
|
|
|
|
'd' => sub { $_[0]->fmtime }, |
23
|
|
|
|
|
|
|
'm' => sub { $_[1] }, |
24
|
|
|
|
|
|
|
'p' => sub { $$ }, |
25
|
|
|
|
|
|
|
'c' => sub { $_[0]->count }, |
26
|
|
|
|
|
|
|
'%' => sub { '%' }, |
27
|
|
|
|
|
|
|
); |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
sub TIEHANDLE { |
31
|
10
|
|
|
10
|
|
1255
|
my $class = shift; |
32
|
10
|
|
50
|
|
|
45
|
my $filename = shift || return; |
33
|
|
|
|
|
|
|
|
34
|
10
|
50
|
|
|
|
47
|
if (@_ % 2 != 0) { |
35
|
0
|
|
|
|
|
0
|
carp "$class must be passed an even number of arguments."; |
36
|
0
|
|
|
|
|
0
|
return; |
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
|
39
|
10
|
|
|
|
|
184
|
my %options = @_; |
40
|
|
|
|
|
|
|
|
41
|
10
|
50
|
66
|
|
|
76
|
if ($options{'format'} && $options{'format'} !~ m/%m/) { |
42
|
0
|
|
|
|
|
0
|
carp "Syntax error in 'format' option, must contain a message tag (%m)\n"; |
43
|
0
|
|
|
|
|
0
|
return; |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
|
46
|
10
|
50
|
66
|
|
|
59
|
if ($options{'mode'} && $options{'mode'} !~ m/^>>?$/) { |
47
|
|
|
|
|
|
|
# We don't support no stinking reading, at least for now |
48
|
0
|
|
|
|
|
0
|
carp __PACKAGE__ . " only supports writing file modes.\n"; |
49
|
0
|
|
|
|
|
0
|
return; |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
|
52
|
10
|
|
|
|
|
49
|
my $self = Symbol::gensym(); #The lazy way... :) |
53
|
|
|
|
|
|
|
|
54
|
10
|
|
|
|
|
166
|
bless($self, $class); |
55
|
|
|
|
|
|
|
|
56
|
10
|
|
|
|
|
115
|
$$self = { |
57
|
|
|
|
|
|
|
%defaults, |
58
|
|
|
|
|
|
|
%options, |
59
|
|
|
|
|
|
|
count => 0, |
60
|
|
|
|
|
|
|
}; |
61
|
|
|
|
|
|
|
|
62
|
10
|
50
|
|
|
|
1205
|
open($self, $$self->{'mode'} , $filename) || return; |
63
|
|
|
|
|
|
|
|
64
|
10
|
100
|
|
|
|
42
|
if ($$self->{'autoflush'}) { |
65
|
1
|
|
|
|
|
4
|
$self->autoflush($$self->{'autoflush'}); |
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
|
68
|
10
|
|
|
|
|
90
|
return $self; |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
sub PRINT { |
72
|
21
|
|
|
21
|
|
4003
|
my $self = shift; |
73
|
21
|
|
|
|
|
63
|
$self->_print(@_); |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
sub PRINTF { |
77
|
1
|
|
|
1
|
|
3
|
my $self = shift; |
78
|
1
|
|
|
|
|
2
|
my $fmt = shift; |
79
|
1
|
|
|
|
|
5
|
my $line = sprintf($fmt, @_); |
80
|
1
|
|
|
|
|
3
|
$self->_print($line); |
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
sub CLOSE { |
84
|
10
|
|
|
10
|
|
486
|
my $self = shift; |
85
|
10
|
|
|
|
|
707
|
close($self); |
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
sub UNTIE { |
89
|
0
|
|
|
0
|
|
0
|
my $self = shift; |
90
|
0
|
|
|
|
|
0
|
close($self); |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
sub _print { |
94
|
22
|
|
|
22
|
|
34
|
my $self = shift; |
95
|
22
|
|
|
|
|
56
|
my $msg = "@_"; |
96
|
22
|
|
|
|
|
48
|
my $line = $$self->{'format'}; |
97
|
|
|
|
|
|
|
|
98
|
22
|
|
|
|
|
35
|
$$self->{'count'}++; |
99
|
|
|
|
|
|
|
# This isn't the fastest way to do things, but it's flexible and easy. |
100
|
|
|
|
|
|
|
# If people start ccontacting me and asking for more speed, I have a few |
101
|
|
|
|
|
|
|
# ideas, but I don't see that happening. |
102
|
22
|
50
|
|
|
|
133
|
$line =~ s/%(_?[%a-zA-Z])/$formats{$1} ? $formats{$1}->($self, $msg) : ''/eg; |
|
46
|
|
|
|
|
237
|
|
103
|
|
|
|
|
|
|
|
104
|
22
|
100
|
|
|
|
81
|
if ($$self->{'force_newline'}) { |
105
|
20
|
50
|
|
|
|
69
|
$line .= "\n" if $line !~ m/\n$/; |
106
|
|
|
|
|
|
|
} |
107
|
|
|
|
|
|
|
|
108
|
22
|
|
|
|
|
307
|
print $self $line; |
109
|
|
|
|
|
|
|
} |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
sub autoflush { |
112
|
1
|
|
|
1
|
1
|
2
|
my ($self, $af) = @_; |
113
|
|
|
|
|
|
|
|
114
|
1
|
50
|
|
|
|
5
|
if (defined $af) { |
115
|
|
|
|
|
|
|
# set self to autoflush. |
116
|
1
|
|
|
|
|
8
|
select((select($self), $| = $af)[0]); |
117
|
1
|
|
|
|
|
3
|
$$self->{'autoflush'} = $af; |
118
|
|
|
|
|
|
|
} |
119
|
|
|
|
|
|
|
|
120
|
1
|
|
|
|
|
3
|
return $$self->{'autoflush'}; |
121
|
|
|
|
|
|
|
} |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
sub fmtime { |
125
|
10
|
|
|
10
|
0
|
16
|
my $self = shift; |
126
|
|
|
|
|
|
|
|
127
|
10
|
100
|
|
|
|
33
|
if ($$self->{'tformat'}) { |
128
|
1
|
|
|
|
|
6
|
return Tie::LogFile::misc::time2str($$self->{'tformat'}); |
129
|
|
|
|
|
|
|
} |
130
|
|
|
|
|
|
|
|
131
|
9
|
|
|
|
|
642
|
return scalar localtime; |
132
|
|
|
|
|
|
|
} |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
sub count { |
135
|
10
|
|
|
10
|
0
|
12
|
my $self = shift; |
136
|
10
|
|
|
|
|
45
|
return $$self->{'count'}; |
137
|
|
|
|
|
|
|
} |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
sub format { |
140
|
0
|
|
|
0
|
1
|
0
|
my $self = shift; |
141
|
0
|
0
|
|
|
|
0
|
if (@_) { |
142
|
0
|
|
|
|
|
0
|
my $new_fmt = shift; |
143
|
0
|
|
|
|
|
0
|
$$self->{'format'} = $new_fmt; |
144
|
|
|
|
|
|
|
} |
145
|
0
|
|
|
|
|
0
|
return $$self->{'format'}; |
146
|
|
|
|
|
|
|
} |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
package Tie::LogFile::misc; |
149
|
|
|
|
|
|
|
|
150
|
5
|
|
|
5
|
|
33
|
use strict; |
|
5
|
|
|
|
|
10
|
|
|
5
|
|
|
|
|
189
|
|
151
|
5
|
|
|
5
|
|
24
|
use warnings; |
|
5
|
|
|
|
|
12
|
|
|
5
|
|
|
|
|
206
|
|
152
|
5
|
|
|
5
|
|
36
|
use Carp; |
|
5
|
|
|
|
|
10
|
|
|
5
|
|
|
|
|
7712
|
|
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
our $loaded = 0; |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
sub time2str { |
157
|
3
|
|
|
3
|
|
13
|
my $fmt = shift; |
158
|
3
|
100
|
|
|
|
15
|
if (!$loaded) { |
159
|
2
|
50
|
|
|
|
8
|
load_date_mod() || croak "Couldn't load a date module (Tried Data::Format and POSIX)\n"; |
160
|
|
|
|
|
|
|
} |
161
|
|
|
|
|
|
|
|
162
|
3
|
|
|
|
|
36
|
return _time_formater($fmt, time); |
163
|
|
|
|
|
|
|
} |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
sub load_date_mod { |
166
|
|
|
|
|
|
|
|
167
|
2
|
50
|
|
2
|
|
161
|
if (eval "require Date::Format") { |
168
|
0
|
|
|
|
|
0
|
$loaded = 1; |
169
|
0
|
|
|
|
|
0
|
*_time_formater = \&Date::Format::time2str; |
170
|
0
|
|
|
|
|
0
|
return 1; |
171
|
|
|
|
|
|
|
} |
172
|
|
|
|
|
|
|
|
173
|
2
|
50
|
|
|
|
229
|
if (eval "require POSIX") { |
174
|
2
|
|
|
|
|
20423
|
$loaded = 1; |
175
|
2
|
|
|
3
|
|
19
|
*_time_formater = sub { POSIX::strftime($_[0], localtime($_[1])); }; |
|
3
|
|
|
|
|
601
|
|
176
|
2
|
|
|
|
|
13
|
return 1; |
177
|
|
|
|
|
|
|
} |
178
|
|
|
|
|
|
|
|
179
|
0
|
|
|
|
|
|
return; |
180
|
|
|
|
|
|
|
} |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
1; |
185
|
|
|
|
|
|
|
__END__ |