| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 46 | 38 | ▶ | if ( defined( $self->{'logfile'} ) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%6/6
not ref $logfile and $logfile ne 0 and $logfile ne 1
not ref $logfile and $logfile ne 0
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
| |||||||||||||||||||||||||||||||||||||||||||
| 51 | 3 | ▶ | if ( $logfile =~ /^stderr$/i ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 68 | 0 | require Win32::EventLog; | |||||||||||||||||||||||||||||||||||||||||
| 69 | 0 | ▶ | $self->{'eventLog'} = Win32::EventLog->new( ref($self), '' ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 76 | 0 | die "Cannot open Syslog: $@"; | |||||||||||||||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||||||||||||||||
| 78 | 15 | ▶ | if ( $^O ne 'solaris' | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
$^O ne "solaris" and $^O ne "freebsd" and eval {
do {
Sys::Syslog::_PATH_LOG()
}
}
$^O ne "solaris" and $^O ne "freebsd"
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()
}
}
| |||||||||||||||||||||||||||||||||||||||||||
| 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
$self->{'facility'} || 'daemon'
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
Truth table50%1/2
A
A= $self->{'facility'}
| |||||||||||||||||||||||||||||||||||||||||||
| 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
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
| |||||||||||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||||||||||
| 95 | 36 | my $tid = ''; | |||||||||||||||||||||||||||||||||||||||||
| 96 | 36 | ▶ | if ( ref($self) && $self->{'mode'} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $self and $self->{"mode"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||
| 97 | 33 | ▶ | if ( $self->{'mode'} eq 'ithreads' ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 98 | 0 | ▶ | if ( my $sthread = threads->self() ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 99 | 0 | $tid = $sthread->tid() . ", "; | |||||||||||||||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||||||||||||||||
| 103 | 36 | ▶ | if ($logfile) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 104 | 3 | my $logtime = $self->LogTime(); | |||||||||||||||||||||||||||||||||||||||||
| 105 | 3 | ▶ | if ( ref($logfile) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
not ref $self or $self->{"debug"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||||||||||||||||