/usr/share/perl5/Paranoid/Log/File.pm is in libparanoid-perl 2.05-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 | # Paranoid::Log::File -- File Log support for paranoid programs
#
# (c) 2005 - 2017, Arthur Corliss <corliss@digitalmages.com>
#
# $Id: lib/Paranoid/Log/File.pm, 2.05 2017/02/06 01:48:57 acorliss Exp $
#
# This software is licensed under the same terms as Perl, itself.
# Please see http://dev.perl.org/licenses/ for more information.
#
#####################################################################
#####################################################################
#
# Environment definitions
#
#####################################################################
package Paranoid::Log::File;
use 5.008;
use strict;
use warnings;
use vars qw($VERSION);
use Paranoid::Debug qw(:all);
use Paranoid::Filesystem;
use Paranoid::Input;
use Paranoid::IO;
use Fcntl qw(:DEFAULT :flock :mode :seek);
($VERSION) = ( q$Revision: 2.05 $ =~ /(\d+(?:\.\d+)+)/sm );
#####################################################################
#
# Module code follows
#
#####################################################################
{
my $hostname;
my $pname;
sub _getHostname {
# Purpose: Returns the hostname, defaulting to localhost if
# /bin/hostname is unusable
# Returns: Hostname
# Usage: $hostname = _getHostname();
my $fd;
# Return cached result
return $hostname if defined $hostname;
# Get the current hostname
if ( -x '/bin/hostname' ) {
if ( open $fd, '-|', '/bin/hostname' ) {
chomp( $hostname = <$fd> );
close $fd;
}
}
# Do a little sanitizing...
if (defined $hostname and length $hostname) {
$hostname =~ s/\..*$//so;
} else {
$hostname = 'localhost';
}
return $hostname;
}
sub _timestamp {
# Purpose: Returns a syslog-stype timestamp string for the current or
# passed time
# Returns: String
# Usage: $timestamp = ptimestamp();
# Usage: $timestamp = ptimestamp($epoch);
my $utime = shift;
my @months = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
my @ctime;
@ctime = defined $utime ? ( localtime $utime ) : (localtime);
return sprintf
'%s %2d %02d:%02d:%02d',
$months[ $ctime[4] ],
@ctime[ 3, 2, 1, 0 ];
}
sub init {
_getHostname();
($pname) = ( $0 =~ m#^(?:.+/)?([^/]+)$#s );
return 1;
}
sub addLogger {
# Purpose: Opens a handle the requested file
# Returns: Boolean
# Usage: $rv = addLogger(%record);
my %record = @_;
my ( $mode, $perm, $rv );
pdebug( 'entering w/%s', PDLEVEL1, %record );
pIn();
# Get mode and permissions
$mode =
exists $record{options}{mode}
? $record{options}{mode}
: O_CREAT | O_APPEND | O_WRONLY;
$perm = $record{options}{perm} if exists $record{options}{perm};
pdebug( 'perm: %s mode: %s', PDLEVEL1, $perm, $mode );
if ( defined $record{options}{file}
and length $record{options}{file} ) {
$rv = defined popen( $record{options}{file}, $mode, $perm );
} else {
Paranoid::ERROR =
pdebug( 'invalid file name specified in options: %s',
PDLEVEL1, $record{options}{file} );
}
pOut();
pdebug( 'leaving w/rv: %s', PDLEVEL1, $rv );
return $rv;
}
sub delLogger {
# Purpose: Closes the requested file
# Returns: Return value of close
# Usage: $rv = delLogger(%record);
my %record = @_;
return pclose( $record{options}{file} );
}
sub logMsg {
# Purpose: Logs the passed message to the named file
# Returns: Return value of print()
# Usage: $rv = logMsg(%record);
my %record = @_;
my ( $fh, $message, $rv );
pdebug( 'entering w/%s', PDLEVEL1, %record );
pIn();
# Get the message and make sure it's terminated by a single newline
$message = $record{message};
$message =~ s/\n*$/\n/so;
if ( $record{options}{syslog} ) {
$message =~ s/\n//so;
$message = sprintf "%s %s %s[%d]: %s\n",
_timestamp( $record{msgtime} ), $hostname, $pname, $$,
substr $message, 0, 2048;
}
$rv = pappend( $record{options}{file}, $message );
pOut();
pdebug( 'leaving w/rv: %s', PDLEVEL1, $rv );
return $rv;
}
}
1;
__END__
=head1 NAME
Paranoid::Log::File - File Logging Functions
=head1 VERSION
$Id: lib/Paranoid/Log/File.pm, 2.05 2017/02/06 01:48:57 acorliss Exp $
=head1 SYNOPSIS
use Paranoid::Log;
startLogger('events', 'File', PL_DEBUG, PL_GE,
{
file => '/var/log/events.log',
mode => O_TRUNC | O_CREAT | O_RDWR,
perm => 0600,
syslog => 1,
});
=head1 DESCRIPTION
This provides a mechanism to log to log files. It will log arbitrarily long
text, but also provides a syslog mode which limits log lines to 2048 and
precedes text with the standard syslog preamble (date/time, host, process
name/PID).
The only mandatory option is the I<file> key/value pair. This module
leverages L<Paranoid::IO>'s I<popen>.
I<mode> defaults to O_CREAT | O_APPEND | O_WRONLY.'
I<perm> defaults to 0666 (umask still applies).
I<syslog> defaults to false. Enabling it causes every line to be formatted
akin to syslog, along with the 2048 byte limit on messages.
=head1 OPTIONS
The options recognized for use in the options hash are as follows:
Option Value Description
-----------------------------------------------------
file string file name of log file
mode integer file mode to open with
perm integer file permissions of newly
created log files
syslog boolean enable syslog-style format
=head1 SUBROUTINES/METHODS
B<NOTE>: Given that this module is not intended to be used directly nothing
is exported.
=head2 init
=head2 logMsg
=head2 addLogger
=head2 delLogger
=head1 DEPENDENCIES
=over
=item o
L<Fcntl>
=item o
L<Paranoid::Debug>
=item o
L<Paranoid::Filesystem>
=item o
L<Paranoid::Input>
=item o
L<Paranoid::IO>
=back
=head1 SEE ALSO
=over
=item o
L<Paranoid::Log>
=back
=head1 BUGS AND LIMITATIONS
This isn't a high performance module when dealing with a high logging rate
with high concurrency. This is due to the advisory locking requirement and
the seeks to the end of the file with every message. This facility is
intended as a kind of lowest-common denominator for programs that need some
kind of logging capability.
=head1 AUTHOR
Arthur Corliss (corliss@digitalmages.com)
=head1 LICENSE AND COPYRIGHT
This software is licensed under the same terms as Perl, itself.
Please see http://dev.perl.org/licenses/ for more information.
(c) 2005 - 2017, Arthur Corliss (corliss@digitalmages.com)
|