line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package File::Tail::Lite; |
2
|
2
|
|
|
2
|
|
44292
|
use strict; |
|
2
|
|
|
|
|
6
|
|
|
2
|
|
|
|
|
106
|
|
3
|
2
|
|
|
2
|
|
12
|
use warnings; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
1114
|
|
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
our $VERSION = '0.02'; |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
sub new { |
8
|
1
|
|
|
1
|
1
|
15
|
my $pkg = shift; |
9
|
1
|
|
|
|
|
4
|
my $self = {@_}; |
10
|
1
|
|
|
|
|
3
|
bless $self, $pkg; |
11
|
|
|
|
|
|
|
|
12
|
1
|
50
|
33
|
|
|
43
|
unless ( defined $self->{filename} and -e $self->{filename} ) { |
13
|
0
|
|
|
|
|
0
|
$self->{error} = 1; |
14
|
0
|
|
|
|
|
0
|
$self->{errinfo} = "error input filename"; |
15
|
0
|
|
|
|
|
0
|
return $self; |
16
|
|
|
|
|
|
|
} |
17
|
|
|
|
|
|
|
|
18
|
1
|
50
|
|
|
|
52
|
unless ( open $self->{fh}, "<", $self->{filename} ) { |
19
|
0
|
|
|
|
|
0
|
$self->{error} = 1; |
20
|
0
|
|
|
|
|
0
|
$self->{errinfo} = "error open file"; |
21
|
0
|
|
|
|
|
0
|
return $self; |
22
|
|
|
|
|
|
|
} |
23
|
|
|
|
|
|
|
|
24
|
1
|
|
|
|
|
2
|
my $seek_position; |
25
|
|
|
|
|
|
|
my $seek_whence; # 0:begin, 1:current, 2:eof |
26
|
1
|
50
|
|
|
|
5
|
if ( !defined $self->{seekpos} ) { |
27
|
0
|
|
|
|
|
0
|
$seek_position = 0; |
28
|
0
|
|
|
|
|
0
|
$seek_whence = 2; |
29
|
|
|
|
|
|
|
} |
30
|
|
|
|
|
|
|
else { |
31
|
1
|
50
|
33
|
|
|
13
|
if ( $self->{seekpos} eq 'begin' |
|
|
0
|
33
|
|
|
|
|
|
|
0
|
0
|
|
|
|
|
32
|
|
|
|
|
|
|
or $self->{seekpos} eq 'new' |
33
|
|
|
|
|
|
|
or $self->{seekpos} eq 'start' ) |
34
|
|
|
|
|
|
|
{ |
35
|
1
|
|
|
|
|
3
|
$seek_position = 0; |
36
|
1
|
|
|
|
|
2
|
$seek_whence = 0; |
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
elsif ( $self->{seekpos} eq 'end' or $self->{seekpos} eq 'eof' ) { |
39
|
0
|
|
|
|
|
0
|
$seek_position = 0; |
40
|
0
|
|
|
|
|
0
|
$seek_whence = 2; |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
elsif ( $self->{seekpos} =~ /^[0-9]+$/ ) { |
43
|
0
|
|
|
|
|
0
|
$seek_position = $self->{seekpos}; |
44
|
0
|
|
|
|
|
0
|
$seek_whence = 0; |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
else { |
47
|
0
|
|
|
|
|
0
|
$self->{error} = 1; |
48
|
0
|
|
|
|
|
0
|
$self->{errinfo} = "error seekpos"; |
49
|
0
|
|
|
|
|
0
|
return $self; |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
} |
52
|
1
|
50
|
|
|
|
7
|
unless ( sysseek( $self->{fh}, $seek_position, $seek_whence ) ) { |
53
|
0
|
|
|
|
|
0
|
$self->{error} = 1; |
54
|
0
|
|
|
|
|
0
|
$self->{errinfo} = "error seek file"; |
55
|
0
|
|
|
|
|
0
|
return $self; |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
|
58
|
1
|
0
|
33
|
|
|
10
|
$self->{maxbuf} = 1024 |
|
|
|
33
|
|
|
|
|
59
|
|
|
|
|
|
|
if !$self->{maxbuf} |
60
|
|
|
|
|
|
|
or int( $self->{maxbuf} ) < 1 |
61
|
|
|
|
|
|
|
or int( $self->{maxbuf} ) > 1024 * 1024; |
62
|
|
|
|
|
|
|
|
63
|
1
|
|
|
|
|
5
|
return $self; |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
sub readline { |
67
|
18
|
|
|
18
|
1
|
145
|
my $self = shift; |
68
|
18
|
50
|
|
|
|
64
|
if ( $self->{error} ) { |
69
|
0
|
|
|
|
|
0
|
return $self->{errinfo}; |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
|
72
|
18
|
|
|
|
|
20
|
my $line; |
73
|
18
|
|
|
|
|
22
|
while (1) { |
74
|
4492
|
|
|
|
|
3404
|
my $buf; |
75
|
4492
|
|
|
|
|
12147
|
my $ret = sysread( $self->{fh}, $buf, 1 ); |
76
|
4492
|
50
|
|
|
|
5469
|
if ($ret) { |
77
|
4492
|
|
|
|
|
3498
|
$line .= $buf; |
78
|
4492
|
100
|
66
|
|
|
17736
|
if ( $buf =~ /[\r\n]/ or length($line) >= $self->{maxbuf} ) { |
79
|
18
|
|
|
|
|
308
|
return ( sysseek( $self->{fh}, 0, 1 ), $line ); |
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
else { |
83
|
0
|
|
|
|
|
|
sleep 1; |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
1; |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
__END__ |