Passing/Output/File.pm

filter: Statementstmt 50.0% 14 / 28 Branchbran 0.0% 0 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 42.8% 3 / 7 Podpod 50.0% 1 / 2 totaltotal 41.8% 18 / 43 SCAR 19.8
CC
4
Cov
41%
_build_fh
20.0
__ANON__
2.0
BUILD
2.0
CRAP 7.3
« Passing/Filter/ToLogstash.pm ↑ Summary Passing/Input/FileTail.pm »
1 package Message::Passing::Output::File;
2 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use MooX::Types::MooseLike::Base qw/ Str Bool /;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use namespace::clean -except => 'meta';
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 with 'Message::Passing::Role::Output';
7  
8 1 has filename => (
9 isa => Str,
10 is => 'ro',
11 predicate => '_has_filename',
12 );
13  
14 1 has fh => (
15 is => 'ro',
16 lazy => 1,
17 builder => '_build_fh',
18 );
19  
20 has append => (
21 is => 'ro',
22 isa => Bool,
23 0 ▶ default => sub { 1 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
24 1 );
25  
26 sub _build_fh {
27 0 ▶ my $self = shift;
Subroutine _build_fh: not called CC=4 SCAR=30.0
28 0 ▶ confess("Need a filename to output to") unless $self->_has_filename;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_has_filename 0 0 0
29 0 ▶ my $mode = $self->append ? '>>' : '>';
Branch: 1 branch
BranchTrueFalseTotal
$self->append ? : 0 0 0
30 0 ▶ open(my $fh, $mode, $self->filename) or confess("Could not open ".
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, $mode, $self->filename or confess('Could not open ' . $self->filename . " for writing: $!") 0 0 0
31 $self->filename . " for writing: $!");
32 0 $fh;
33 }
34  
35 sub BUILD {
36 0 ▶ my $self = shift;
Subroutine BUILD: not called CC=1 SCAR=6.9
Pod undocumented
37 0 $self->fh;
38 }
39  
40 sub consume {
41 0 ▶ my $self = shift;
Subroutine consume: not called CC=1 SCAR=6.9
Pod documented
42 0 my $saved = select($self->fh);
43 0 local $|=1;
44 0 print shift() . "\n";
45 0 select($saved);
46 0 return 1;
47 }
48  
49  
50 1 1;
51  
52 =head1 NAME
53  
54 Message::Passing::Output::File - File output
55  
56 =head1 SYNOPSIS
57  
58 message-pass --input STDIN --output File --output_options '{"filename": "/tmp/my.log"}'
59 {"foo": "bar"}
60 {"foo":"bar"}
61  
62 =head1 DESCRIPTION
63  
64 Output messages to File
65  
66 =head1 METHODS
67  
68 =head2 append
69  
70 A boolean attribute for if the output file should be re-created, or
71 appended to. Default true.
72  
73 =head2 filename
74  
75 An attribute for the file name to write to.
76  
77 =head2 consume
78  
79 Consumes a message by JSON encoding it and printing it, followed by \n
80  
81 =head1 SEE ALSO
82  
83 L<Message::Passing>
84  
85 =head1 SPONSORSHIP
86  
87 This module exists due to the wonderful people at Suretec Systems Ltd.
88 <http://www.suretecsystems.com/> who sponsored its development for its
89 VoIP division called SureVoIP <http://www.surevoip.co.uk/> for use with
90 the SureVoIP API -
91 <http://www.surevoip.co.uk/support/wiki/api_documentation>
92  
93 =head1 AUTHOR, COPYRIGHT AND LICENSE
94  
95 See L<Message::Passing>.
96  
97 =cut
« Passing/Filter/ToLogstash.pm ↑ Summary Passing/Input/FileTail.pm »