Saftpresse/Input/FileTail.pm

filter: Statementstmt 36.3% 20 / 55 Branchbran 0.0% 0 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 38.4% 5 / 13 Podpod 0.0% 0 / 5 totaltotal 31.6% 25 / 79 SCAR 25.3
CC
5
Cov
33%
_open_file
6.0
read_events
6.0
eof
6.0
CRAP 12.6
« Saftpresse/Plugin/SyslogFile.pm ↑ Summary Saftpresse/App.pm »
1 package Log::Saftpresse::Input::FileTail;
2  
3 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 # ABSTRACT: log input for following a file
6 1 our $VERSION = '1.5'; # VERSION
7  
8  
9 1 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use File::stat;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 ▶use Time::Piece;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Sys::Hostname;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 1 extends 'Log::Saftpresse::Input';
16  
17 has 'path' => ( is => 'ro', isa => 'Str',
18 default => sub {
19 0 ▶ my $self = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
20 0 return $self->name;
21 },
22 1 );
23  
24 has 'file' => ( is => 'rw', isa => 'IO::File', lazy => 1,
25 default => sub {
26 0 ▶ my $self = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
27 0 return $self->_open_file;
28 },
29 1 );
30  
31 sub _open_file {
32 0 ▶ my $self = shift;
Subroutine _open_file: not called CC=2 SCAR=17.9
33 0 my $f = IO::File->new($self->path,"r");
34 0 ▶ if( ! defined $f ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $f) 0 0 0
35 0 die('could not open '.$self->path.' for input: '.$!);
36 }
37 0 $f->blocking(0);
38 0 $f->seek(0,2); # seek to end of file
39 0 return $f;
40 }
41  
42 sub reopen_file {
43 0 ▶ my $self = shift;
Subroutine reopen_file: not called CC=1 SCAR=6.9
Pod undocumented
44 0 $self->file->close;
45 0 $self->file( $self->_open_file );
46 0 return;
47 }
48  
49 sub io_handles {
50 0 ▶ my $self = shift;
Subroutine io_handles: not called CC=1 SCAR=6.9
Pod undocumented
51 0 return;
52 }
53  
54 sub read_events {
55 0 ▶ my ( $self ) = @_;
Subroutine read_events: not called CC=2 SCAR=17.9
Pod undocumented
56 0 my @events;
57 0 foreach my $line ( $self->file->getlines ) {
58 0 chomp( $line );
59 0 my $event = {
60 'host' => hostname,
61 'time' => Time::Piece->new,
62 'message' => $line,
63 };
64 0 push( @events, $event );
65 }
66 0 $self->file->seek(0,1); # clear eof flag
67 0 return @events;
68 }
69  
70 sub eof {
71 0 ▶ my $self = shift;
Subroutine eof: not called CC=2 SCAR=17.9
Pod undocumented
72 0 ▶ if( stat($self->file)->nlink == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&Log::Saftpresse::Input::FileTail::stat(scalar $self->file)->nlink == 0) 0 0 0
73 # file has been deleted (logrotate?)
74 0 $self->reopen_file;
75 }
76 0 return 0; # we dont signal eof, we're almost always eof.
77 }
78  
79 sub can_read {
80 0 ▶ my $self = shift;
Subroutine can_read: not called CC=2 SCAR=17.9
Pod undocumented
81 0 my $mypos = $self->file->tell;
82 0 my $size = stat($self->file)->size;
83 0 ▶ if( $size > $mypos ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($size > $mypos) 0 0 0
84 0 return 1;
85 }
86 0 return 0;
87 }
88  
89  
90 1 1;
91  
92 __END__
93  
94 =pod
95  
96 =encoding UTF-8
97  
98 =head1 NAME
99  
100 Log::Saftpresse::Input::FileTail - log input for following a file
101  
102 =head1 VERSION
103  
104 version 1.5
105  
106 =head1 Description
107  
108 This input watches a file for newly appended lines.
109  
110 =head1 Synopsis
111  
112 <Input maillog>
113 module = "FileTail"
114 path = "/var/log/mail.log"
115 </Input>
116  
117 =head1 Format
118  
119 Foreach line appended to the file a event with the following fields is generated:
120  
121 =over
122  
123 =item message
124  
125 Content of the line.
126  
127 =item host
128  
129 The hostname of the system.
130  
131 =item time
132  
133 The current time.
134  
135 =back
136  
137 =head1 AUTHOR
138  
139 Markus Benning <ich@markusbenning.de>
140  
141 =head1 COPYRIGHT AND LICENSE
142  
143 This software is Copyright (c) 1998 by James S. Seymour, 2015 by Markus Benning.
144  
145 This is free software, licensed under:
146  
147 The GNU General Public License, Version 2, June 1991
148  
149 =cut
« Saftpresse/Plugin/SyslogFile.pm ↑ Summary Saftpresse/App.pm »