File Coverage

blib/lib/Log/Dispatch/Socket.pm
Criterion Covered Total %
statement 18 49 36.7
branch 0 12 0.0
condition 0 3 0.0
subroutine 6 11 54.5
pod 2 2 100.0
total 26 77 33.7


line stmt bran cond sub pod time code
1             #
2             # This file is part of Log-Dispatch-Socket
3             #
4             # This software is copyright (c) 2012 by Loïc TROCHET.
5             #
6             # This is free software; you can redistribute it and/or modify it under
7             # the same terms as the Perl 5 programming language system itself.
8             #
9             package Log::Dispatch::Socket;
10             {
11             $Log::Dispatch::Socket::VERSION = '0.130020';
12             }
13             # ABSTRACT: Subclass of Log::Dispatch::Output that log messages to a socket
14              
15 1     1   28320 use strict;
  1         2  
  1         39  
16 1     1   5 use warnings;
  1         2  
  1         29  
17              
18 1     1   1078 use IO::Socket::INET;
  1         30050  
  1         11  
19 1     1   2143 use Params::Validate qw(validate SCALAR);
  1         11728  
  1         82  
20              
21 1     1   956 use Log::Dispatch::Output;
  1         6180  
  1         33  
22 1     1   983 use parent qw(Log::Dispatch::Output);
  1         280  
  1         6  
23              
24             Params::Validate::validation_options( allow_extra => 1 );
25              
26              
27             sub new
28             {
29 0     0 1   my $this = shift;
30 0   0       my $class = ref $this || $this;
31              
32 0           my $self = validate
33             (
34             @_
35             , {
36             PeerHost => { type => SCALAR, default => 'localhost' }
37             , PeerPort => { type => SCALAR }
38             , Proto => { type => SCALAR, default => 'tcp' }
39             }
40             );
41              
42 0           bless $self, $class;
43              
44 0           $self->_basic_init(%$self);
45              
46 0           $self->{Attempt} = 0;
47 0           $self->{Socket} = undef;
48              
49 0 0         die "Connect to '$self->{PeerHost}:$self->{PeerPort}' failed: $!"
50             unless $self->_connect(%$self);
51              
52 0           return $self;
53             }
54              
55             sub _connect
56             {
57 0     0     my $self = shift;
58 0           return $self->{Socket} = IO::Socket::INET->new(@_);
59             }
60              
61             sub _disconnect
62             {
63 0     0     my $self = shift;
64              
65 0 0         if (defined $self->{Socket})
66             {
67 0           eval { close $self->{Socket}; };
  0            
68 0           undef $self->{Socket};
69             }
70             }
71              
72              
73             sub log_message
74             {
75 0     0 1   my ($self, %params) = @_;
76              
77             RETRY:
78             {
79 0 0         unless (defined $self->{Socket})
  0            
80             {
81 0 0         return if $self->{Attempt};
82 0           $self->{Attempt} += 1;
83 0 0         unless ($self->_connect(%$self))
84             {
85 0           die "Disconnect from '$self->{PeerHost}:$self->{PeerPort}'";
86 0           return;
87             }
88 0           $self->{Attempt} = 0;
89             }
90              
91 0           eval { $self->{Socket}->send($params{message}); };
  0            
92              
93 0 0         if ($@)
94             {
95 0           $self->_disconnect;
96 0           redo RETRY;
97             }
98             }
99             }
100              
101              
102             sub DESTROY
103             {
104 0     0     $_[0]->_disconnect;
105             }
106              
107             1;
108              
109             __END__