File Coverage

blib/lib/Log/Saftpresse/Input/Stdin.pm
Criterion Covered Total %
statement 15 29 51.7
branch 0 2 0.0
condition n/a
subroutine 5 7 71.4
pod 0 2 0.0
total 20 40 50.0


line stmt bran cond sub pod time code
1             package Log::Saftpresse::Input::Stdin;
2              
3 1     1   1576 use Moose;
  1         3  
  1         9  
4              
5             # ABSTRACT: log input for reading STDIN
6             our $VERSION = '1.6'; # VERSION
7              
8              
9 1     1   6039 use IO::Handle;
  1         3  
  1         55  
10 1     1   4 use IO::Select;
  1         1  
  1         39  
11              
12 1     1   4 use Sys::Hostname;
  1         2  
  1         44  
13 1     1   5 use Time::Piece;
  1         1  
  1         11  
14              
15             extends 'Log::Saftpresse::Input';
16              
17             has 'max_chunk_lines' => ( is => 'rw', isa => 'Int', default => 1024 );
18              
19             has 'stdin' => (
20             is => 'ro', isa => 'IO::Handle', lazy => 1,
21             default => sub {
22             my $fh = IO::Handle->new_from_fd(fileno(STDIN),"r");
23             $fh->blocking(0);
24             return $fh;
25             },
26             handles => {
27             'eof' => 'eof',
28             },
29             );
30              
31             # we only have one handle, just alias
32             *io_handles = \&stdin;
33              
34             has 'io_select' => (
35             is => 'ro', isa => 'IO::Select', lazy => 1,
36             default => sub {
37             my $self = shift;
38             my $s = IO::Select->new();
39             $s->add( $self->stdin );
40             return $s;
41             },
42             );
43              
44             sub read_events {
45 0     0 0   my ( $self ) = @_;
46 0           my @events;
47 0           my $cnt = 0;
48 0           while( defined( my $line = $self->stdin->getline ) ) {
49 0           chomp( $line );
50 0           my $event = {
51             'host' => hostname,
52             'time' => Time::Piece->new,
53             'message' => $line,
54             };
55 0           push( @events, $event );
56 0           $cnt++;
57 0 0         if( $cnt > $self->max_chunk_lines ) {
58 0           last;
59             }
60             }
61 0           return @events;
62             }
63              
64             sub can_read {
65 0     0 0   my ( $self ) = @_;
66 0           my @can_read = $self->io_select->can_read(0);
67 0           return( scalar @can_read );
68             }
69              
70             1;
71              
72             __END__
73              
74             =pod
75              
76             =encoding UTF-8
77              
78             =head1 NAME
79              
80             Log::Saftpresse::Input::Stdin - log input for reading STDIN
81              
82             =head1 VERSION
83              
84             version 1.6
85              
86             =head1 Description
87              
88             This input plugins reads events from STDIN.
89              
90             =head1 Synopsis
91              
92             <Input stdin>
93             module = "Stdin"
94             </Input>
95              
96             =head1 Options
97              
98             =over
99              
100             =item max_chunk_lines (default: 1024)
101              
102             Maximum number of file to read in one chunk.
103              
104             =back
105              
106             =head1 Input Format
107              
108             For each line the plugin will generate an event with the following fields:
109              
110             =over
111              
112             =item message
113              
114             The content of the line.
115              
116             =item host
117              
118             The hostname of the system.
119              
120             =item time
121              
122             The current time.
123              
124             =back
125              
126             =head1 AUTHOR
127              
128             Markus Benning <ich@markusbenning.de>
129              
130             =head1 COPYRIGHT AND LICENSE
131              
132             This software is Copyright (c) 1998 by James S. Seymour, 2015 by Markus Benning.
133              
134             This is free software, licensed under:
135              
136             The GNU General Public License, Version 2, June 1991
137              
138             =cut