Helm/Log/Channel/file.pm untested

filter: Statementstmt 0.0% 0 / 64 Branchbran 0.0% 0 / 6 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 11 totaltotal 0.0% 0 / 107 CC 4 SCAR 11.8
CC
4
Cov
0%
forked
6.0
finalize
6.0
_prefix
6.0
CRAP 3.3
1 package Helm::Log::Channel::file;
2 use strict;
3 use warnings;
4 use Moose;
5 use namespace::autoclean;
6 use DateTime;
7  
8 extends 'Helm::Log::Channel';
9 has fh => (is => 'ro', writer => '_fh', isa => 'FileHandle|Undef');
10  
11 sub initialize {
12 my ($self, $helm) = @_;
13  
14 # file the file and open it for appending
15 my $file = $self->uri->file;
16 Helm->debug("Opening file $file for logging");
17 open(my $fh, '>>', $file) or $helm->die("Could not open file $file for appending: $@");
18 $self->_fh($fh);
19 }
20  
21 # nothing to do
22 sub parallelize { }
23  
24 sub forked {
25 my ($self, $type) = @_;
26  
27 # close the existing fh
28 my $fh = $self->fh;
29 close($fh) if $fh;
30  
31 # re-open it for appending so that each child process has it's own distinct FH
32 my $file = $self->uri->file;
33 Helm->debug("Re-opening file $file for logging after fork");
34 open($fh, '>>', $file) or CORE::die("Could not re-open file $file for appending: $@");
35 $self->_fh($fh);
36  
37 }
38  
39 sub finalize {
40 my ($self, $helm) = @_;
41  
42 # close our FH
43 if( my $fh = $self->fh ) {
44 $self->_current_server(undef);
45 print $fh $self->_prefix . "HELM execution ended\n";
46 Helm->debug("Closing logging file handle");
47 close($self->fh);
48 $self->_fh(undef);
49 }
50 }
51  
52 sub start_server {
53 my ($self, $server) = @_;
54 $self->SUPER::start_server($server);
55 my $fh = $self->fh;
56 print $fh $self->_prefix . "BEGIN TASK ON $server\n";
57 }
58  
59 sub end_server {
60 my ($self, $server) = @_;
61 $self->SUPER::end_server($server);
62 my $fh = $self->fh;
63 print $fh $self->_prefix . "END TASK ON $server\n";
64 }
65  
66 sub debug {
67 my ($self, $msg) = @_;
68 my $fh = $self->fh;
69 print $fh $self->_prefix . "[debug] $msg\n";
70 }
71  
72 sub info {
73 my ($self, $msg) = @_;
74 my $fh = $self->fh;
75 print $fh $self->_prefix . "$msg\n";
76 }
77  
78 sub warn {
79 my ($self, $msg) = @_;
80 my $fh = $self->fh;
81 print $fh $self->_prefix . "[warn] $msg\n";
82 }
83  
84 sub error {
85 my ($self, $msg) = @_;
86 my $fh = $self->fh;
87 print $fh $self->_prefix . "[error] $msg\n";
88 }
89  
90 sub _prefix {
91 my $self = shift;
92 my $prefix = '[' . DateTime->now->strftime('%a %b %d %H:%M:%S %Y') . '] ';
93  
94 if( $self->current_server ) {
95 # use the current server's name as part of the prefix
96 $prefix = "$prefix\{" . $self->current_server->name . '} ';
97 }
98  
99 return $prefix;
100 }
101  
102 __PACKAGE__->meta->make_immutable;
103  
104 1;