File Coverage

blib/lib/Paranoid/Log/File.pm
Criterion Covered Total %
statement 67 68 98.5
branch 12 18 66.6
condition 3 6 50.0
subroutine 15 15 100.0
pod 4 4 100.0
total 101 111 90.9


line stmt bran cond sub pod time code
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             # as published by the
10             # Free Software Foundation ; either version 1
11             # , or any later version
12             # , or
13             # b) the Artistic License 2.0
14             # ,
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 13     13   567 use 5.008;
  13         34  
35              
36 13     13   59 use strict;
  13         19  
  13         214  
37 13     13   46 use warnings;
  13         31  
  13         592  
38 13     13   75 use vars qw($VERSION);
  13         20  
  13         530  
39 13     13   59 use Paranoid::Debug qw(:all);
  13         25  
  13         2573  
40 13     13   6035 use Paranoid::Filesystem;
  13         32  
  13         1033  
41 13     13   84 use Paranoid::Input;
  13         19  
  13         536  
42 13     13   66 use Paranoid::IO;
  13         19  
  13         915  
43 13     13   72 use Fcntl qw(:DEFAULT :flock :mode :seek);
  13         25  
  13         12906  
44              
45             ($VERSION) = ( q$Revision: 2.10 $ =~ /(\d+(?:\.\d+)+)/sm );
46              
47             #####################################################################
48             #
49             # Module code follows
50             #
51             #####################################################################
52              
53             {
54              
55             my $hostname;
56             my $pname;
57              
58             sub _getHostname {
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     12   18 my $fd;
66              
67             # Return cached result
68 12 50       36 return $hostname if defined $hostname;
69              
70             # Get the current hostname
71 12 50       252 if ( -x '/bin/hostname' ) {
72 12 50       27150 if ( open $fd, '-|', '/bin/hostname' ) {
73 12         5910 chomp( $hostname = <$fd> );
74 12         1182 close $fd;
75             }
76             }
77              
78             # Do a little sanitizing...
79 12 50 33     606 if ( defined $hostname and length $hostname ) {
80 12         114 $hostname =~ s/\..*$//so;
81             } else {
82 0         0 $hostname = 'localhost';
83             }
84              
85 12         234 return $hostname;
86             }
87              
88             sub _timestamp {
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     512   1233 my $utime = shift;
97 512         2717 my @months = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
98 512         617 my @ctime;
99              
100 512 50       15353 @ctime = defined $utime ? ( localtime $utime ) : (localtime);
101              
102 512         7907 return sprintf
103             '%s %2d %02d:%02d:%02d',
104             $months[ $ctime[4] ],
105             @ctime[ 3, 2, 1, 0 ];
106             }
107              
108             sub init {
109 12     12 1 30 _getHostname();
110 12         216 ($pname) = ( $0 =~ m#^(?:.+/)?([^/]+)$#s );
111 12         1842 return 1;
112             }
113              
114             sub addLogger {
115              
116             # Purpose: Opens a handle the requested file
117             # Returns: Boolean
118             # Usage: $rv = addLogger(%record);
119              
120 28     28 1 130 my %record = @_;
121 28         79 my ( $mode, $perm, $rv );
122              
123 28         249 subPreamble( PDLEVEL1, '%', %record );
124              
125             # Get mode and permissions
126             $mode =
127             exists $record{options}{mode}
128             ? $record{options}{mode}
129 28 100       80 : O_CREAT | O_APPEND | O_WRONLY;
130 28 100       109 $perm = $record{options}{perm} if exists $record{options}{perm};
131 28         81 pdebug( 'perm: %s mode: %s', PDLEVEL1, $perm, $mode );
132 28 100 66     173 if ( defined $record{options}{file}
133             and length $record{options}{file} ) {
134 16         221 $rv = defined popen( $record{options}{file}, $mode, $perm );
135             } else {
136             Paranoid::ERROR =
137             pdebug( 'invalid file name specified in options: %s',
138 12         42 PDLEVEL1, $record{options}{file} );
139             }
140              
141 28         115 subPostamble( PDLEVEL1, '$', $rv );
142              
143 28         108 return $rv;
144             }
145              
146             sub delLogger {
147              
148             # Purpose: Closes the requested file
149             # Returns: Return value of close
150             # Usage: $rv = delLogger(%record);
151              
152 4     4 1 32 my %record = @_;
153              
154 4         19 return pclose( $record{options}{file} );
155             }
156              
157             sub logMsg {
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     512 1 2574 my %record = @_;
164 512         937 my ( $message, $rv );
165              
166 512         1780 subPreamble( PDLEVEL1, '%', %record );
167              
168             # Get the message and make sure it's terminated by a single newline
169 512         1392 $message = $record{message};
170 512         4181 $message =~ s/\n*$/\n/so;
171              
172 512 50       1628 if ( $record{options}{syslog} ) {
173 512         1560 $message =~ s/\n//so;
174             $message = sprintf "%s %s %s[%d]: %s\n",
175 512         2098 _timestamp( $record{msgtime} ), $hostname, $pname, $$,
176             substr $message, 0, 2048;
177             }
178              
179 512         2891 $rv = pappend( $record{options}{file}, $message );
180              
181 512         1578 subPostamble( PDLEVEL1, '$', $rv );
182              
183 512         4732 return $rv;
184             }
185             }
186              
187             1;
188              
189             __END__