| 1 | package Log::Saftpresse::Input::Stdin; | ||||||||||
| 2 | |||||||||||
| 3 | 1 | ▶ | use Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | |||||||||||
| 5 | # ABSTRACT: log input for reading STDIN | ||||||||||
| 6 | 1 | our $VERSION = '1.5'; # VERSION | |||||||||
| 7 | |||||||||||
| 8 | |||||||||||
| 9 | 1 | ▶ | use IO::Handle; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 1 | ▶ | use IO::Select; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | |||||||||||
| 12 | 1 | ▶ | use Sys::Hostname; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | 1 | ▶ | use Time::Piece; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | |||||||||||
| 15 | 1 | extends 'Log::Saftpresse::Input'; | |||||||||
| 16 | |||||||||||
| 17 | 1 | 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 | 0 | ▶ | my $fh = IO::Handle->new_from_fd(fileno(STDIN),"r"); | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 23 | 0 | $fh->blocking(0); | |||||||||
| 24 | 0 | return $fh; | |||||||||
| 25 | }, | ||||||||||
| 26 | 1 | handles => { | |||||||||
| 27 | 'eof' => 'eof', | ||||||||||
| 28 | }, | ||||||||||
| 29 | ); | ||||||||||
| 30 | |||||||||||
| 31 | # we only have one handle, just alias | ||||||||||
| 32 | 1 | *io_handles = \&stdin; | |||||||||
| 33 | |||||||||||
| 34 | has 'io_select' => ( | ||||||||||
| 35 | is => 'ro', isa => 'IO::Select', lazy => 1, | ||||||||||
| 36 | default => sub { | ||||||||||
| 37 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 38 | 0 | my $s = IO::Select->new(); | |||||||||
| 39 | 0 | $s->add( $self->stdin ); | |||||||||
| 40 | 0 | return $s; | |||||||||
| 41 | }, | ||||||||||
| 42 | 1 | ); | |||||||||
| 43 | |||||||||||
| 44 | sub read_events { | ||||||||||
| 45 | 0 | ▶ | my ( $self ) = @_; | ||||||||
|
Subroutine
read_events: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||
| 46 | 0 | my @events; | |||||||||
| 47 | 0 | my $cnt = 0; | |||||||||
| 48 | 0 | ▶ | while( defined( my $line = $self->stdin->getline ) ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 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 | ▶ | if( $cnt > $self->max_chunk_lines ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 58 | 0 | last; | |||||||||
| 59 | } | ||||||||||
| 60 | } | ||||||||||
| 61 | 0 | return @events; | |||||||||
| 62 | } | ||||||||||
| 63 | |||||||||||
| 64 | sub can_read { | ||||||||||
| 65 | 0 | ▶ | my ( $self ) = @_; | ||||||||
|
Subroutine
can_read: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 66 | 0 | my @can_read = $self->io_select->can_read(0); | |||||||||
| 67 | 0 | return( scalar @can_read ); | |||||||||
| 68 | } | ||||||||||
| 69 | |||||||||||
| 70 | 1 | 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.5 | ||||||||||
| 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 | ||||||||||