Daemon/Log.pm

filter: Statementstmt 67.1% 49 / 73 Branchbran 58.3% 21 / 36 Conditioncond 61.5% 16 / 26 MC/DCmcdc 40.0% 6 / 15 Subroutinesub 77.7% 7 / 9 Podpod 0.0% 0 / 6 totaltotal 60.0% 99 / 165 CC 28 SCAR 28.8
CC
28
Cov
64%
Log
24.2
OpenLog
22.6
Debug
3.0
CRAP 17.9
« Daemon/Test.pm ↑ Summary
1 ############################################################################
2 #
3 # Net::Daemon::Log
4 #
5 # Copyright (C) 1998, Jochen Wiedmann
6 # Am Eisteich 9
7 # 72555 Metzingen
8 # Germany
9 #
10 # Phone: +49 7123 14887
11 # Email: joe@ispsoft.de
12 #
13 # All rights reserved.
14 #
15 # You may distribute this package under the terms of either the GNU
16 # General Public License or the Artistic License, as specified in the
17 # Perl README file.
18 #
19 ############################################################################
20  
21 57 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 57 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 package Net::Daemon::Log;
25  
26 57 our $VERSION = '0.02';
27  
28 57 ▶use Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 ############################################################################
31 #
32 # Name: Log (Instance method)
33 #
34 # Purpose: Does logging
35 #
36 # Inputs: $self - Server instance
37 #
38 # Result: TRUE, if the client has successfully authorized, FALSE
39 # otherwise.
40 #
41 ############################################################################
42  
43 sub OpenLog($) {
44 38 ▶ my $self = shift;
Subroutine OpenLog: called CC=16 SCAR=31.2
Pod undocumented
45 38 ▶ return 1 unless ref($self);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 0 38 38
46 38 ▶ if ( defined( $self->{'logfile'} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"logfile"}) 23 15 38
47 23 my $logfile = $self->{'logfile'};
48 # If logfile is a plain string (not a reference, not 0/1),
49 # treat it as a filename to open or "STDERR" special case.
50 23 ▶ if ( !ref($logfile) && $logfile ne '0' && $logfile ne '1' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $logfile and $logfile ne 0 and $logfile ne 1) 3 20 23
Condition100%6/6
not ref $logfile and $logfile ne 0 and $logfile ne 1
!ll&&!rl&&r
1913
not ref $logfile and $logfile ne 0
!ll&&!rl&&r
1184
MC/DC100%3/3
not ref $logfile and $logfile ne 0 and $logfile ne 1
not ref $logfile $logfile ne 0 $logfile ne 1
Truth table100%4/4
not(A) and B and C
A= not ref $logfileB= $logfile ne 0C= $logfile ne 1
ABCresult
0XX0
10X0
1100
1111
51 3 ▶ if ( $logfile =~ /^stderr$/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($logfile =~ /^stderr$/i) { } 2 1 3
52 # 1 is the stderr sentinel used by Log(): truthy + non-ref
53 # triggers the "printf STDERR" branch (see Log() below).
54 2 $self->{'logfile'} = 1;
55 }
56 else {
57 1 require Symbol;
58 1 my $fh = Symbol::gensym();
59 1 ▶ open( $fh, '>>', $logfile )
Branch: 1 branch
BranchTrueFalseTotal
open $fh, ">>", $logfile or die "Cannot open logfile ${logfile}: $!" 0 1 1
60 or die "Cannot open logfile $logfile: $!";
61 1 select((select($fh), $| = 1)[0]);
62 1 $self->{'logfile'} = $fh;
63 }
64 }
65 23 return $self->{'logfile'};
66 }
67 15 ▶ if ( $Config::Config{'archname'} =~ /win32/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Config::Config{'archname'} =~ /win32/i) { } 0 15 15
68 0 require Win32::EventLog;
69 0 ▶ $self->{'eventLog'} = Win32::EventLog->new( ref($self), '' )
Branch: 1 branch
BranchTrueFalseTotal
$self->{"eventLog"} = "Win32::EventLog"->new(ref $self, "") or die 'Cannot open EventLog:' . Win32::GetLastError() 0 0 0
70 or die "Cannot open EventLog:" . Win32::GetLastError();
71 0 $self->{'$eventId'} = 0;
72 }
73 else {
74 15 eval { require Sys::Syslog };
75 15 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 15 15
76 0 die "Cannot open Syslog: $@";
77 }
78 15 ▶ if ( $^O ne 'solaris'
Branch: 1 branch
BranchTrueFalseTotal
if ($^O ne "solaris" and $^O ne "freebsd" and eval { do { Sys::Syslog::_PATH_LOG() } }) 15 0 15
Condition33%2/6
$^O ne "solaris" and $^O ne "freebsd" and eval { do { Sys::Syslog::_PATH_LOG() } }
!ll&&!rl&&r
0015
$^O ne "solaris" and $^O ne "freebsd"
!ll&&!rl&&r
0015
MC/DC0%0/3
$^O ne "solaris" and $^O ne "freebsd" and eval { do { Sys::Syslog::_PATH_LOG() } }
$^O ne "solaris" $^O ne "freebsd" eval { do { Sys::Syslog::_PATH_LOG() } }
Truth table25%1/4
A and B and C
A= $^O ne "solaris"B= $^O ne "freebsd"C= eval { do { Sys::Syslog::_PATH_LOG() } }
ABCresult
0XX0
10X0
1100
1111
79 && $^O ne 'freebsd'
80 15 && eval { Sys::Syslog::_PATH_LOG() } ) {
81 15 Sys::Syslog::setlogsock('unix');
82 }
83 Sys::Syslog::openlog(
84 $self->{'logname'} || ref($self), 'pid',
85 15 ▶ $self->{'facility'} || 'daemon'
Condition40%2/5
$self->{'logname'} || ref $self
l!l&&r!l&&!r
0150
$self->{'facility'} || 'daemon'
l!l
015
MC/DC0%0/2
$self->{'logname'} || ref $self
$self->{'logname'} ref $self
MC/DC0%0/1
$self->{'facility'} || 'daemon'
$self->{'facility'}
Truth table33%1/3
A || B
A= $self->{'logname'}B= ref $self
ABresult
1X1
011
000
Truth table50%1/2
A
A= $self->{'facility'}
Aresult
11
00
86 );
87 }
88 15 $self->{'logfile'} = 0;
89 }
90  
91 sub Log ($$$;@) {
92 36 ▶ my ( $self, $level, $format, @args ) = @_;
Subroutine Log: called CC=11 SCAR=31.8
Pod undocumented
93 36 ▶ my $logfile = !ref($self) || $self->OpenLog();
Condition66%2/3
!ref($self) || $self->OpenLog
l!l&&r!l&&!r
0333
MC/DC50%1/2
!ref($self) || $self->OpenLog
!ref($self) $self->OpenLog
Truth table66%2/3
not(A) || B
A= !ref($self)B= $self->OpenLog
ABresult
1X1
011
000
94  
95 36 my $tid = '';
96 36 ▶ if ( ref($self) && $self->{'mode'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self and $self->{"mode"}) 33 3 36
Condition66%2/3
ref $self and $self->{"mode"}
!ll&&!rl&&r
0333
MC/DC50%1/2
ref $self and $self->{"mode"}
ref $self $self->{"mode"}
Truth table66%2/3
A and B
A= ref $selfB= $self->{"mode"}
ABresult
0X0
100
111
97 33 ▶ if ( $self->{'mode'} eq 'ithreads' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"mode"} eq "ithreads") 0 33 33
98 0 ▶ if ( my $sthread = threads->self() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $sthread = "threads"->self) 0 0 0
99 0 $tid = $sthread->tid() . ", ";
100 }
101 }
102 }
103 36 ▶ if ($logfile) {
Branch: 2 branches
BranchTrueFalseTotal
if ($logfile) { } 3 33 36
elsif (my $eventLog = $self->{'eventLog'}) { } 0 33 33
104 3 my $logtime = $self->LogTime();
105 3 ▶ if ( ref($logfile) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $logfile) { } 2 1 3
106 2 $logfile->print( sprintf( "$logtime $level, $tid$format\n", @args ) );
107 }
108 else {
109 1 printf STDERR ( "$logtime $level, $tid$format\n", @args );
110 }
111 }
112 elsif ( my $eventLog = $self->{'eventLog'} ) {
113 0 my ( $type, $category );
114 0 ▶ if ( $level eq 'debug' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($level eq 'debug') { } 0 0 0
elsif ($level eq 'notice') { } 0 0 0
115 0 $type = Win32::EventLog::EVENTLOG_INFORMATION_TYPE();
116 0 $category = 10;
117 }
118 elsif ( $level eq 'notice' ) {
119 0 $type = Win32::EventLog::EVENTLOG_INFORMATION_TYPE();
120 0 $category = 20;
121 }
122 else {
123 0 $type = Win32::EventLog::EVENTLOG_ERROR_TYPE();
124 0 $category = 50;
125 }
126 $eventLog->Report(
127 {
128 'Category' => $category,
129 'EventType' => $type,
130 0 'EventID' => ++$self->{'eventId'},
131 'Strings' => sprintf( $format, @args ),
132 'Data' => $tid
133 }
134 );
135 }
136 else {
137 33 Sys::Syslog::syslog( $level, "$tid$format", @args );
138 }
139 }
140  
141 sub Debug ($$;@) {
142 157 ▶ my $self = shift;
Subroutine Debug: called CC=3 SCAR=11.0
Pod undocumented
143 157 ▶ if ( !ref($self) || $self->{'debug'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $self or $self->{"debug"}) 18 139 157
Condition66%2/3
not ref $self or $self->{"debug"}
l!l&&r!l&&!r
018139
MC/DC50%1/2
not ref $self or $self->{"debug"}
not ref $self $self->{"debug"}
Truth table66%2/3
not(A) or B
A= not ref $selfB= $self->{"debug"}
ABresult
1X1
011
000
144 18 my $fmt = shift;
145 18 $self->Log( 'debug', $fmt, @_ );
146 }
147 }
148  
149 sub Error ($$;@) {
150 0 ▶ my $self = shift;
Subroutine Error: not called CC=1 SCAR=6.9
Pod undocumented
151 0 my $fmt = shift;
152 0 $self->Log( 'err', $fmt, @_ );
153 }
154  
155 sub Fatal ($$;@) {
156 0 ▶ my $self = shift;
Subroutine Fatal: not called CC=1 SCAR=6.9
Pod undocumented
157 0 my $fmt = shift;
158 0 my $msg = sprintf( $fmt, @_ );
159 0 $self->Log( 'err', $msg );
160 0 my ( $package, $filename, $line ) = caller();
161 0 die "$msg at $filename line $line.";
162 }
163  
164 3 ▶sub LogTime { scalar(localtime) }
Subroutine LogTime: called CC=1 SCAR=0.0
Pod undocumented
165  
166 57 1;
167  
168 __END__
« Daemon/Test.pm ↑ Summary