| 1 | package Log::Syslog::Fast::PP; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 9 | ▶ | use 5.006002; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 9 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 9 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 9 | ▶ | use Log::Syslog::Fast::Constants ':all'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 9 | require Exporter; | |||||||||||||||||||||||||||
| 9 | 9 | our @ISA = qw(Exporter); | |||||||||||||||||||||||||||
| 10 | 9 | our @EXPORT = qw(); | |||||||||||||||||||||||||||
| 11 | 9 | our %EXPORT_TAGS = %Log::Syslog::Fast::Constants::EXPORT_TAGS; | |||||||||||||||||||||||||||
| 12 | 9 | our @EXPORT_OK = @Log::Syslog::Fast::Constants::EXPORT_OK; | |||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 9 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 9 | ▶ | use POSIX 'strftime'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 9 | ▶ | use IO::Socket::IP; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | 9 | ▶ | use IO::Socket::UNIX; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | 9 | ▶ | use Socket; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 0 | ▶ | sub DESTROY { } | ||||||||||||||||||||||||||
|
Subroutine
DESTROY: not called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 9 | ▶ | use constant PRIORITY => 0; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 23 | 9 | ▶ | use constant SENDER => 1; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 24 | 9 | ▶ | use constant NAME => 2; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 25 | 9 | ▶ | use constant PID => 3; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 26 | 9 | ▶ | use constant SOCK => 4; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 27 | 9 | ▶ | use constant LAST_TIME => 5; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 28 | 9 | ▶ | use constant PREFIX => 6; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 29 | 9 | ▶ | use constant PREFIX_LEN => 7; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 30 | 9 | ▶ | use constant FORMAT => 8; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 39 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=7 SCAR=19.5
Pod
undocumented
| |||||||||||||||||||||||||||||
| 33 | 39 | my $ref = shift; | |||||||||||||||||||||||||||
| 34 | 39 | ▶ | $ref = __PACKAGE__ unless defined $ref; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 35 | 39 | ▶ | my $class = ref $ref || $ref; | ||||||||||||||||||||||||||
|
Condition33%1/3
ref $ref || $ref
MC/DC0%0/2
ref $ref || $ref
ref $ref
$ref
Truth table33%1/3
A || B
A= ref $refB= $ref
| |||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 39 | my ($proto, $hostname, $port, $facility, $severity, $sender, $name) = @_; | |||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 39 | ▶ | croak "hostname required" unless defined $hostname; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 40 | 38 | ▶ | croak "sender required" unless defined $sender; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 41 | 37 | ▶ | croak "name required" unless defined $name; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 36 | my $self = bless [ | |||||||||||||||||||||||||||
| 44 | ($facility << 3) | $severity, # prio | ||||||||||||||||||||||||||||
| 45 | $sender, # sender | ||||||||||||||||||||||||||||
| 46 | $name, # name | ||||||||||||||||||||||||||||
| 47 | $$, # pid | ||||||||||||||||||||||||||||
| 48 | undef, # sock | ||||||||||||||||||||||||||||
| 49 | undef, # last_time | ||||||||||||||||||||||||||||
| 50 | undef, # prefix | ||||||||||||||||||||||||||||
| 51 | undef, # prefix_len | ||||||||||||||||||||||||||||
| 52 | LOG_RFC3164, # format | ||||||||||||||||||||||||||||
| 53 | ], $class; | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 36 | $self->update_prefix(time()); | |||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 36 | eval { $self->set_receiver($proto, $hostname, $port) }; | |||||||||||||||||||||||||||
| 58 | 36 | ▶ | die "Error in ->new: $@" if $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | 26 | return $self; | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 86 | ▶ | sub update_prefix { | ||||||||||||||||||||||||||
|
Subroutine
update_prefix: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 63 | 86 | my $self = shift; | |||||||||||||||||||||||||||
| 64 | 86 | my $t = shift; | |||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 86 | $self->[LAST_TIME] = $t; | |||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 86 | my $timestr = strftime("%h %e %T", localtime $t); | |||||||||||||||||||||||||||
| 69 | 86 | ▶ | if ($self->[FORMAT] == LOG_RFC5424) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | 4 | $timestr = strftime("%Y-%m-%dT%H:%M:%S%z", localtime $t); | |||||||||||||||||||||||||||
| 71 | 4 | $timestr =~ s/(\d{2})$/:$1/; # see http://tools.ietf.org/html/rfc3339#section-5.6 time-numoffset | |||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 86 | $self->[PREFIX] = sprintf "<%d>%s %s %s[%d]: ", | |||||||||||||||||||||||||||
| 75 | $self->[PRIORITY], $timestr, $self->[SENDER], $self->[NAME], $self->[PID]; | ||||||||||||||||||||||||||||
| 76 | 86 | ▶ | if ($self->[FORMAT] == LOG_RFC5424) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | 4 | $self->[PREFIX] = sprintf "<%d>1 %s %s %s %d - - ", | |||||||||||||||||||||||||||
| 78 | $self->[PRIORITY], $timestr, $self->[SENDER], $self->[NAME], $self->[PID]; | ||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | 86 | ▶ | if ($self->[FORMAT] == LOG_RFC3164_LOCAL) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | 8 | $self->[PREFIX] = sprintf "<%d>%s %s[%d]: ", | |||||||||||||||||||||||||||
| 82 | $self->[PRIORITY], $timestr, $self->[NAME], $self->[PID]; | ||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 145 | ▶ | sub set_receiver { | ||||||||||||||||||||||||||
|
Subroutine
set_receiver: called CC=8 SCAR=20.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 87 | 145 | my $self = shift; | |||||||||||||||||||||||||||
| 88 | 145 | ▶ | croak("hostname required") unless defined $_[1]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 144 | my ($proto, $hostname, $port) = @_; | |||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 144 | ▶ | if ($proto == LOG_TCP) { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 93 | 110 | $self->[SOCK] = IO::Socket::IP->new( | |||||||||||||||||||||||||||
| 94 | Proto => 'tcp', | ||||||||||||||||||||||||||||
| 95 | PeerHost => $hostname, | ||||||||||||||||||||||||||||
| 96 | PeerPort => $port, | ||||||||||||||||||||||||||||
| 97 | ); | ||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | elsif ($proto == LOG_UDP) { | ||||||||||||||||||||||||||||
| 100 | 14 | $self->[SOCK] = IO::Socket::IP->new( | |||||||||||||||||||||||||||
| 101 | Proto => 'udp', | ||||||||||||||||||||||||||||
| 102 | PeerHost => $hostname, | ||||||||||||||||||||||||||||
| 103 | PeerPort => $port, | ||||||||||||||||||||||||||||
| 104 | ); | ||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | elsif ($proto == LOG_UNIX) { | ||||||||||||||||||||||||||||
| 107 | 20 | eval { | |||||||||||||||||||||||||||
| 108 | 20 | $self->[SOCK] = IO::Socket::UNIX->new( | |||||||||||||||||||||||||||
| 109 | Type => SOCK_STREAM, | ||||||||||||||||||||||||||||
| 110 | Peer => $hostname, | ||||||||||||||||||||||||||||
| 111 | ); | ||||||||||||||||||||||||||||
| 112 | }; | ||||||||||||||||||||||||||||
| 113 | 20 | ▶ | if ($@ || !$self->[SOCK]) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$@ or not $self->[4]
MC/DC50%1/2
$@ or not $self->[4]
$@
not $self->[4]
Truth table66%2/3
A or not(B)
A= $@B= not $self->[4]
| |||||||||||||||||||||||||||||
| 114 | 13 | $self->[SOCK] = IO::Socket::UNIX->new( | |||||||||||||||||||||||||||
| 115 | Type => SOCK_DGRAM, | ||||||||||||||||||||||||||||
| 116 | Peer => $hostname, | ||||||||||||||||||||||||||||
| 117 | ); | ||||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 144 | ▶ | die "Error in ->set_receiver: $!" unless $self->[SOCK]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 11 | ▶ | sub set_priority { | ||||||||||||||||||||||||||
|
Subroutine
set_priority: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 125 | 11 | my $self = shift; | |||||||||||||||||||||||||||
| 126 | 11 | my ($facility, $severity) = @_; | |||||||||||||||||||||||||||
| 127 | 11 | $self->[PRIORITY] = ($facility << 3) | $severity; | |||||||||||||||||||||||||||
| 128 | 11 | $self->update_prefix(time); | |||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | 1 | ▶ | sub set_facility { | ||||||||||||||||||||||||||
|
Subroutine
set_facility: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 132 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 133 | 1 | $self->set_priority(shift, $self->get_severity); | |||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 1 | ▶ | sub set_severity { | ||||||||||||||||||||||||||
|
Subroutine
set_severity: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 137 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 138 | 1 | $self->set_priority($self->get_facility, shift); | |||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | 10 | ▶ | sub set_sender { | ||||||||||||||||||||||||||
|
Subroutine
set_sender: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 142 | 10 | my $self = shift; | |||||||||||||||||||||||||||
| 143 | 10 | ▶ | croak("sender required") unless defined $_[0]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 144 | 9 | $self->[SENDER] = shift; | |||||||||||||||||||||||||||
| 145 | 9 | $self->update_prefix(time); | |||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | 10 | ▶ | sub set_name { | ||||||||||||||||||||||||||
|
Subroutine
set_name: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 149 | 10 | my $self = shift; | |||||||||||||||||||||||||||
| 150 | 10 | ▶ | croak("name required") unless defined $_[0]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 151 | 9 | $self->[NAME] = shift; | |||||||||||||||||||||||||||
| 152 | 9 | $self->update_prefix(time); | |||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | 9 | ▶ | sub set_pid { | ||||||||||||||||||||||||||
|
Subroutine
set_pid: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 156 | 9 | my $self = shift; | |||||||||||||||||||||||||||
| 157 | 9 | $self->[PID] = shift; | |||||||||||||||||||||||||||
| 158 | 9 | $self->update_prefix(time); | |||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | 12 | ▶ | sub set_format { | ||||||||||||||||||||||||||
|
Subroutine
set_format: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 162 | 12 | my $self = shift; | |||||||||||||||||||||||||||
| 163 | 12 | $self->[FORMAT] = shift; | |||||||||||||||||||||||||||
| 164 | 12 | $self->update_prefix(time); | |||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | 33 | ▶ | sub send { | ||||||||||||||||||||||||||
|
Subroutine
send: called CC=4 SCAR=14.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 168 | 33 | ▶ | my $now = $_[2] || time; | ||||||||||||||||||||||||||
|
Condition100%2/2
$_[2] || time
MC/DC100%1/1
$_[2] || time
$_[2]
Truth table100%2/2
A
A= $_[2]
| |||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | # update the prefix if seconds have rolled over | ||||||||||||||||||||||||||||
| 171 | 33 | ▶ | if ($now != $_[0][LAST_TIME]) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 172 | 0 | $_[0]->update_prefix($now); | |||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | 33 | ▶ | send($_[0][SOCK], $_[0][PREFIX] . $_[1], 0) || die "Error while sending: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | #no warnings 'redefine'; | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | 3 | ▶ | sub get_priority { | ||||||||||||||||||||||||||
|
Subroutine
get_priority: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 181 | 3 | my $self = shift; | |||||||||||||||||||||||||||
| 182 | 3 | return $self->[PRIORITY]; | |||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | 3 | ▶ | sub get_facility { | ||||||||||||||||||||||||||
|
Subroutine
get_facility: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 186 | 3 | my $self = shift; | |||||||||||||||||||||||||||
| 187 | 3 | return $self->[PRIORITY] >> 3; | |||||||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | 3 | ▶ | sub get_severity { | ||||||||||||||||||||||||||
|
Subroutine
get_severity: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 191 | 3 | my $self = shift; | |||||||||||||||||||||||||||
| 192 | 3 | return $self->[PRIORITY] & 7; | |||||||||||||||||||||||||||
| 193 | } | ||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | 2 | ▶ | sub get_sender { | ||||||||||||||||||||||||||
|
Subroutine
get_sender: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 196 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 197 | 2 | return $self->[SENDER]; | |||||||||||||||||||||||||||
| 198 | } | ||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | 2 | ▶ | sub get_name { | ||||||||||||||||||||||||||
|
Subroutine
get_name: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 201 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 202 | 2 | return $self->[NAME]; | |||||||||||||||||||||||||||
| 203 | } | ||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | 2 | ▶ | sub get_pid { | ||||||||||||||||||||||||||
|
Subroutine
get_pid: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 206 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 207 | 2 | return $self->[PID]; | |||||||||||||||||||||||||||
| 208 | } | ||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | 0 | ▶ | sub get_format { | ||||||||||||||||||||||||||
|
Subroutine
get_format: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 211 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 212 | 0 | return $self->[FORMAT]; | |||||||||||||||||||||||||||
| 213 | } | ||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||
| 215 | 2 | ▶ | sub _get_sock { | ||||||||||||||||||||||||||
|
Subroutine
_get_sock: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 216 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 217 | 2 | return $self->[SOCK]->fileno; | |||||||||||||||||||||||||||
| 218 | } | ||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | 9 | 1; | |||||||||||||||||||||||||||
| 221 | __END__ | ||||||||||||||||||||||||||||