Log4perl/Appender/Socket.pm untested

filter: Statementstmt 0.0% 0 / 45 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 84 CC 10 SCAR 32.5
CC
10
Cov
0%
log
42.0
new
30.0
connect
2.0
CRAP 25.8
1 ##################################################
2 package Log::Log4perl::Appender::Socket;
3 ##################################################
4 our @ISA = qw(Log::Log4perl::Appender);
5  
6 use warnings;
7 use strict;
8  
9 use IO::Socket::INET;
10  
11 ##################################################
12 sub new {
13 ##################################################
14 my($class, @options) = @_;
15  
16 my $self = {
17 name => "unknown name",
18 silent_recovery => 0,
19 no_warning => 0,
20 PeerAddr => "localhost",
21 Proto => 'tcp',
22 Timeout => 5,
23 @options,
24 };
25  
26 bless $self, $class;
27  
28 unless ($self->{defer_connection}){
29 unless($self->connect(@options)) {
30 if($self->{silent_recovery}) {
31 if( ! $self->{no_warning}) {
32 warn "Connect to $self->{PeerAddr}:$self->{PeerPort} failed: $!";
33 }
34 return $self;
35 }
36 die "Connect to $self->{PeerAddr}:$self->{PeerPort} failed: $!";
37 }
38  
39 $self->{socket}->autoflush(1);
40 #autoflush has been the default behavior since 1997
41 }
42  
43 return $self;
44 }
45
46 ##################################################
47 sub connect {
48 ##################################################
49 my($self, @options) = @_;
50  
51 $self->{socket} = IO::Socket::INET->new(@options);
52  
53 return $self->{socket};
54 }
55  
56 ##################################################
57 sub log {
58 ##################################################
59 my($self, %params) = @_;
60  
61  
62 {
63 # If we were never able to establish
64 # a connection, try to establish one
65 # here. If it fails, return.
66 if(($self->{silent_recovery} or $self->{defer_connection}) and
67 !defined $self->{socket}) {
68 if(! $self->connect(%$self)) {
69 return undef;
70 }
71 }
72
73 # Try to send the message across
74 eval { $self->{socket}->send($params{message});
75 };
76  
77 if($@) {
78 warn "Send to " . ref($self) . " failed ($@), retrying once...";
79 if($self->connect(%$self)) {
80 redo;
81 }
82 if($self->{silent_recovery}) {
83 return undef;
84 }
85 warn "Reconnect to $self->{PeerAddr}:$self->{PeerPort} " .
86 "failed: $!";
87 return undef;
88 }
89 };
90  
91 return 1;
92 }
93  
94 ##################################################
95 sub DESTROY {
96 ##################################################
97 my($self) = @_;
98  
99 undef $self->{socket};
100 }
101  
102 1;
103  
104 __END__