| 1 | # Paranoid::Log::File -- File Log support for paranoid programs | ||||||||||||||||||||||||||||
| 2 | # | ||||||||||||||||||||||||||||
| 3 | # $Id: lib/Paranoid/Log/File.pm, 2.10 2022/03/08 00:01:04 acorliss Exp $ | ||||||||||||||||||||||||||||
| 4 | # | ||||||||||||||||||||||||||||
| 5 | # This software is free software. Similar to Perl, you can redistribute it | ||||||||||||||||||||||||||||
| 6 | # and/or modify it under the terms of either: | ||||||||||||||||||||||||||||
| 7 | # | ||||||||||||||||||||||||||||
| 8 | # a) the GNU General Public License | ||||||||||||||||||||||||||||
| 9 | # <https://www.gnu.org/licenses/gpl-1.0.html> as published by the | ||||||||||||||||||||||||||||
| 10 | # Free Software Foundation <http://www.fsf.org/>; either version 1 | ||||||||||||||||||||||||||||
| 11 | # <https://www.gnu.org/licenses/gpl-1.0.html>, or any later version | ||||||||||||||||||||||||||||
| 12 | # <https://www.gnu.org/licenses/license-list.html#GNUGPL>, or | ||||||||||||||||||||||||||||
| 13 | # b) the Artistic License 2.0 | ||||||||||||||||||||||||||||
| 14 | # <https://opensource.org/licenses/Artistic-2.0>, | ||||||||||||||||||||||||||||
| 15 | # | ||||||||||||||||||||||||||||
| 16 | # subject to the following additional term: No trademark rights to | ||||||||||||||||||||||||||||
| 17 | # "Paranoid" have been or are conveyed under any of the above licenses. | ||||||||||||||||||||||||||||
| 18 | # However, "Paranoid" may be used fairly to describe this unmodified | ||||||||||||||||||||||||||||
| 19 | # software, in good faith, but not as a trademark. | ||||||||||||||||||||||||||||
| 20 | # | ||||||||||||||||||||||||||||
| 21 | # (c) 2005 - 2020, Arthur Corliss (corliss@digitalmages.com) | ||||||||||||||||||||||||||||
| 22 | # (tm) 2008 - 2020, Paranoid Inc. (www.paranoid.com) | ||||||||||||||||||||||||||||
| 23 | # | ||||||||||||||||||||||||||||
| 24 | ##################################################################### | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | ##################################################################### | ||||||||||||||||||||||||||||
| 27 | # | ||||||||||||||||||||||||||||
| 28 | # Environment definitions | ||||||||||||||||||||||||||||
| 29 | # | ||||||||||||||||||||||||||||
| 30 | ##################################################################### | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | package Paranoid::Log::File; | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 14 | ▶ | use 5.008; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 14 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 37 | 14 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 38 | 14 | ▶ | use vars qw($VERSION); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 39 | 14 | ▶ | use Paranoid::Debug qw(:all); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 40 | 14 | ▶ | use Paranoid::Filesystem; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 41 | 14 | ▶ | use Paranoid::Input; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 42 | 14 | ▶ | use Paranoid::IO; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 43 | 14 | ▶ | use Fcntl qw(:DEFAULT :flock :mode :seek); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 14 | ($VERSION) = ( q$Revision: 2.10 $ =~ /(\d+(?:\.\d+)+)/sm ); | |||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | ##################################################################### | ||||||||||||||||||||||||||||
| 48 | # | ||||||||||||||||||||||||||||
| 49 | # Module code follows | ||||||||||||||||||||||||||||
| 50 | # | ||||||||||||||||||||||||||||
| 51 | ##################################################################### | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | { | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 14 | my $hostname; | |||||||||||||||||||||||||||
| 56 | 14 | my $pname; | |||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 12 | ▶ | sub _getHostname { | ||||||||||||||||||||||||||
|
Subroutine
_getHostname: called CC=6 SCAR=19.9
| |||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | # Purpose: Returns the hostname, defaulting to localhost if | ||||||||||||||||||||||||||||
| 61 | # /bin/hostname is unusable | ||||||||||||||||||||||||||||
| 62 | # Returns: Hostname | ||||||||||||||||||||||||||||
| 63 | # Usage: $hostname = _getHostname(); | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 12 | my $fd; | |||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | # Return cached result | ||||||||||||||||||||||||||||
| 68 | 12 | ▶ | return $hostname if defined $hostname; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | # Get the current hostname | ||||||||||||||||||||||||||||
| 71 | 12 | ▶ | if ( -x '/bin/hostname' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | 12 | ▶ | if ( open $fd, '-|', '/bin/hostname' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | 12 | chomp( $hostname = <$fd> ); | |||||||||||||||||||||||||||
| 74 | 12 | close $fd; | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | # Do a little sanitizing... | ||||||||||||||||||||||||||||
| 79 | 12 | ▶ | if ( defined $hostname and length $hostname ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $hostname and length $hostname
MC/DC0%0/2
defined $hostname and length $hostname
defined $hostname
length $hostname
Truth table33%1/3
A and B
A= defined $hostnameB= length $hostname
| |||||||||||||||||||||||||||||
| 80 | 12 | $hostname =~ s/\..*$//so; | |||||||||||||||||||||||||||
| 81 | } else { | ||||||||||||||||||||||||||||
| 82 | 0 | $hostname = 'localhost'; | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 12 | return $hostname; | |||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 512 | ▶ | sub _timestamp { | ||||||||||||||||||||||||||
|
Subroutine
_timestamp: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | # Purpose: Returns a syslog-stype timestamp string for the current or | ||||||||||||||||||||||||||||
| 91 | # passed time | ||||||||||||||||||||||||||||
| 92 | # Returns: String | ||||||||||||||||||||||||||||
| 93 | # Usage: $timestamp = ptimestamp(); | ||||||||||||||||||||||||||||
| 94 | # Usage: $timestamp = ptimestamp($epoch); | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 512 | my $utime = shift; | |||||||||||||||||||||||||||
| 97 | 512 | my @months = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec); | |||||||||||||||||||||||||||
| 98 | 512 | my @ctime; | |||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 512 | ▶ | @ctime = defined $utime ? ( localtime $utime ) : (localtime); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 512 | return sprintf | |||||||||||||||||||||||||||
| 103 | '%s %2d %02d:%02d:%02d', | ||||||||||||||||||||||||||||
| 104 | $months[ $ctime[4] ], | ||||||||||||||||||||||||||||
| 105 | @ctime[ 3, 2, 1, 0 ]; | ||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 12 | ▶ | sub init { | ||||||||||||||||||||||||||
|
Subroutine
init: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 109 | 12 | _getHostname(); | |||||||||||||||||||||||||||
| 110 | 12 | ($pname) = ( $0 =~ m#^(?:.+/)?([^/]+)$#s ); | |||||||||||||||||||||||||||
| 111 | 12 | return 1; | |||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 28 | ▶ | sub addLogger { | ||||||||||||||||||||||||||
|
Subroutine
addLogger: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | # Purpose: Opens a handle the requested file | ||||||||||||||||||||||||||||
| 117 | # Returns: Boolean | ||||||||||||||||||||||||||||
| 118 | # Usage: $rv = addLogger(%record); | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 28 | my %record = @_; | |||||||||||||||||||||||||||
| 121 | 28 | my ( $mode, $perm, $rv ); | |||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 28 | subPreamble( PDLEVEL1, '%', %record ); | |||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | # Get mode and permissions | ||||||||||||||||||||||||||||
| 126 | $mode = | ||||||||||||||||||||||||||||
| 127 | exists $record{options}{mode} | ||||||||||||||||||||||||||||
| 128 | ? $record{options}{mode} | ||||||||||||||||||||||||||||
| 129 | 28 | ▶ | : O_CREAT | O_APPEND | O_WRONLY; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 130 | 28 | ▶ | $perm = $record{options}{perm} if exists $record{options}{perm}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 131 | 28 | pdebug( 'perm: %s mode: %s', PDLEVEL1, $perm, $mode ); | |||||||||||||||||||||||||||
| 132 | 28 | ▶ | if ( defined $record{options}{file} | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $record{"options"}{"file"} and length $record{"options"}{"file"}
MC/DC50%1/2
defined $record{"options"}{"file"} and length $record{"options"}{"file"}
defined $record{"options"}{"file"}
length $record{"options"}{"file"}
Truth table66%2/3
A and B
A= defined $record{"options"}{"file"}B= length $record{"options"}{"file"}
| |||||||||||||||||||||||||||||
| 133 | and length $record{options}{file} ) { | ||||||||||||||||||||||||||||
| 134 | 16 | $rv = defined popen( $record{options}{file}, $mode, $perm ); | |||||||||||||||||||||||||||
| 135 | } else { | ||||||||||||||||||||||||||||
| 136 | Paranoid::ERROR = | ||||||||||||||||||||||||||||
| 137 | pdebug( 'invalid file name specified in options: %s', | ||||||||||||||||||||||||||||
| 138 | 12 | PDLEVEL1, $record{options}{file} ); | |||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | 28 | subPostamble( PDLEVEL1, '$', $rv ); | |||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | 28 | return $rv; | |||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | 4 | ▶ | sub delLogger { | ||||||||||||||||||||||||||
|
Subroutine
delLogger: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | # Purpose: Closes the requested file | ||||||||||||||||||||||||||||
| 149 | # Returns: Return value of close | ||||||||||||||||||||||||||||
| 150 | # Usage: $rv = delLogger(%record); | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | 4 | my %record = @_; | |||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | 4 | return pclose( $record{options}{file} ); | |||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | 512 | ▶ | sub logMsg { | ||||||||||||||||||||||||||
|
Subroutine
logMsg: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | # Purpose: Logs the passed message to the named file | ||||||||||||||||||||||||||||
| 160 | # Returns: Return value of print() | ||||||||||||||||||||||||||||
| 161 | # Usage: $rv = logMsg(%record); | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | 512 | my %record = @_; | |||||||||||||||||||||||||||
| 164 | 512 | my ( $message, $rv ); | |||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | 512 | subPreamble( PDLEVEL1, '%', %record ); | |||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | # Get the message and make sure it's terminated by a single newline | ||||||||||||||||||||||||||||
| 169 | 512 | $message = $record{message}; | |||||||||||||||||||||||||||
| 170 | 512 | $message =~ s/\n*$/\n/so; | |||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | 512 | ▶ | if ( $record{options}{syslog} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 173 | 512 | $message =~ s/\n//so; | |||||||||||||||||||||||||||
| 174 | $message = sprintf "%s %s %s[%d]: %s\n", | ||||||||||||||||||||||||||||
| 175 | 512 | _timestamp( $record{msgtime} ), $hostname, $pname, $$, | |||||||||||||||||||||||||||
| 176 | substr $message, 0, 2048; | ||||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | 512 | $rv = pappend( $record{options}{file}, $message ); | |||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | 512 | subPostamble( PDLEVEL1, '$', $rv ); | |||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | 512 | return $rv; | |||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | 14 | 1; | |||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | __END__ | ||||||||||||||||||||||||||||