| 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.09 2021/12/28 15:46:49 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
|
|
708
|
use 5.008; |
|
|
13
|
|
|
|
|
51
|
|
|
35
|
|
|
|
|
|
|
|
|
36
|
13
|
|
|
13
|
|
67
|
use strict; |
|
|
13
|
|
|
|
|
170
|
|
|
|
13
|
|
|
|
|
356
|
|
|
37
|
13
|
|
|
13
|
|
77
|
use warnings; |
|
|
13
|
|
|
|
|
26
|
|
|
|
13
|
|
|
|
|
525
|
|
|
38
|
13
|
|
|
13
|
|
92
|
use vars qw($VERSION); |
|
|
13
|
|
|
|
|
27
|
|
|
|
13
|
|
|
|
|
664
|
|
|
39
|
13
|
|
|
13
|
|
65
|
use Paranoid::Debug qw(:all); |
|
|
13
|
|
|
|
|
26
|
|
|
|
13
|
|
|
|
|
2869
|
|
|
40
|
13
|
|
|
13
|
|
7153
|
use Paranoid::Filesystem; |
|
|
13
|
|
|
|
|
38
|
|
|
|
13
|
|
|
|
|
1429
|
|
|
41
|
13
|
|
|
13
|
|
98
|
use Paranoid::Input; |
|
|
13
|
|
|
|
|
37
|
|
|
|
13
|
|
|
|
|
625
|
|
|
42
|
13
|
|
|
13
|
|
77
|
use Paranoid::IO; |
|
|
13
|
|
|
|
|
25
|
|
|
|
13
|
|
|
|
|
1095
|
|
|
43
|
13
|
|
|
13
|
|
79
|
use Fcntl qw(:DEFAULT :flock :mode :seek); |
|
|
13
|
|
|
|
|
31
|
|
|
|
13
|
|
|
|
|
16347
|
|
|
44
|
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
($VERSION) = ( q$Revision: 2.09 $ =~ /(\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
|
|
|
|
54
|
return $hostname if defined $hostname; |
|
69
|
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
# Get the current hostname |
|
71
|
12
|
50
|
|
|
|
372
|
if ( -x '/bin/hostname' ) { |
|
72
|
12
|
50
|
|
|
|
34170
|
if ( open $fd, '-|', '/bin/hostname' ) { |
|
73
|
12
|
|
|
|
|
7338
|
chomp( $hostname = <$fd> ); |
|
74
|
12
|
|
|
|
|
1704
|
close $fd; |
|
75
|
|
|
|
|
|
|
} |
|
76
|
|
|
|
|
|
|
} |
|
77
|
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
# Do a little sanitizing... |
|
79
|
12
|
50
|
33
|
|
|
552
|
if (defined $hostname and length $hostname) { |
|
80
|
12
|
|
|
|
|
138
|
$hostname =~ s/\..*$//so; |
|
81
|
|
|
|
|
|
|
} else { |
|
82
|
0
|
|
|
|
|
0
|
$hostname = 'localhost'; |
|
83
|
|
|
|
|
|
|
} |
|
84
|
|
|
|
|
|
|
|
|
85
|
12
|
|
|
|
|
288
|
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
|
|
1401
|
my $utime = shift; |
|
97
|
512
|
|
|
|
|
2424
|
my @months = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec); |
|
98
|
512
|
|
|
|
|
791
|
my @ctime; |
|
99
|
|
|
|
|
|
|
|
|
100
|
512
|
50
|
|
|
|
16278
|
@ctime = defined $utime ? ( localtime $utime ) : (localtime); |
|
101
|
|
|
|
|
|
|
|
|
102
|
512
|
|
|
|
|
9135
|
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
|
42
|
_getHostname(); |
|
110
|
12
|
|
|
|
|
342
|
($pname) = ( $0 =~ m#^(?:.+/)?([^/]+)$#s ); |
|
111
|
12
|
|
|
|
|
2814
|
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
|
105
|
my %record = @_; |
|
121
|
28
|
|
|
|
|
93
|
my ( $mode, $perm, $rv ); |
|
122
|
|
|
|
|
|
|
|
|
123
|
28
|
|
|
|
|
275
|
pdebug( 'entering w/%s', PDLEVEL1, %record ); |
|
124
|
28
|
|
|
|
|
367
|
pIn(); |
|
125
|
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
# Get mode and permissions |
|
127
|
|
|
|
|
|
|
$mode = |
|
128
|
|
|
|
|
|
|
exists $record{options}{mode} |
|
129
|
|
|
|
|
|
|
? $record{options}{mode} |
|
130
|
28
|
100
|
|
|
|
121
|
: O_CREAT | O_APPEND | O_WRONLY; |
|
131
|
28
|
100
|
|
|
|
92
|
$perm = $record{options}{perm} if exists $record{options}{perm}; |
|
132
|
28
|
|
|
|
|
87
|
pdebug( 'perm: %s mode: %s', PDLEVEL1, $perm, $mode ); |
|
133
|
28
|
100
|
66
|
|
|
200
|
if ( defined $record{options}{file} |
|
134
|
|
|
|
|
|
|
and length $record{options}{file} ) { |
|
135
|
16
|
|
|
|
|
232
|
$rv = defined popen( $record{options}{file}, $mode, $perm ); |
|
136
|
|
|
|
|
|
|
} else { |
|
137
|
|
|
|
|
|
|
Paranoid::ERROR = |
|
138
|
|
|
|
|
|
|
pdebug( 'invalid file name specified in options: %s', |
|
139
|
12
|
|
|
|
|
144
|
PDLEVEL1, $record{options}{file} ); |
|
140
|
|
|
|
|
|
|
} |
|
141
|
|
|
|
|
|
|
|
|
142
|
28
|
|
|
|
|
127
|
pOut(); |
|
143
|
28
|
|
|
|
|
83
|
pdebug( 'leaving w/rv: %s', PDLEVEL1, $rv ); |
|
144
|
|
|
|
|
|
|
|
|
145
|
28
|
|
|
|
|
157
|
return $rv; |
|
146
|
|
|
|
|
|
|
} |
|
147
|
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
sub delLogger { |
|
149
|
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
# Purpose: Closes the requested file |
|
151
|
|
|
|
|
|
|
# Returns: Return value of close |
|
152
|
|
|
|
|
|
|
# Usage: $rv = delLogger(%record); |
|
153
|
|
|
|
|
|
|
|
|
154
|
4
|
|
|
4
|
1
|
37
|
my %record = @_; |
|
155
|
|
|
|
|
|
|
|
|
156
|
4
|
|
|
|
|
22
|
return pclose( $record{options}{file} ); |
|
157
|
|
|
|
|
|
|
} |
|
158
|
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
sub logMsg { |
|
160
|
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
# Purpose: Logs the passed message to the named file |
|
162
|
|
|
|
|
|
|
# Returns: Return value of print() |
|
163
|
|
|
|
|
|
|
# Usage: $rv = logMsg(%record); |
|
164
|
|
|
|
|
|
|
|
|
165
|
512
|
|
|
512
|
1
|
2837
|
my %record = @_; |
|
166
|
512
|
|
|
|
|
1138
|
my ( $fh, $message, $rv ); |
|
167
|
|
|
|
|
|
|
|
|
168
|
512
|
|
|
|
|
2112
|
pdebug( 'entering w/%s', PDLEVEL1, %record ); |
|
169
|
512
|
|
|
|
|
1660
|
pIn(); |
|
170
|
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
# Get the message and make sure it's terminated by a single newline |
|
172
|
512
|
|
|
|
|
1089
|
$message = $record{message}; |
|
173
|
512
|
|
|
|
|
5083
|
$message =~ s/\n*$/\n/so; |
|
174
|
|
|
|
|
|
|
|
|
175
|
512
|
50
|
|
|
|
1635
|
if ( $record{options}{syslog} ) { |
|
176
|
512
|
|
|
|
|
1669
|
$message =~ s/\n//so; |
|
177
|
|
|
|
|
|
|
$message = sprintf "%s %s %s[%d]: %s\n", |
|
178
|
512
|
|
|
|
|
2322
|
_timestamp( $record{msgtime} ), $hostname, $pname, $$, |
|
179
|
|
|
|
|
|
|
substr $message, 0, 2048; |
|
180
|
|
|
|
|
|
|
} |
|
181
|
|
|
|
|
|
|
|
|
182
|
512
|
|
|
|
|
3480
|
$rv = pappend( $record{options}{file}, $message ); |
|
183
|
|
|
|
|
|
|
|
|
184
|
512
|
|
|
|
|
1602
|
pOut(); |
|
185
|
512
|
|
|
|
|
1649
|
pdebug( 'leaving w/rv: %s', PDLEVEL1, $rv ); |
|
186
|
|
|
|
|
|
|
|
|
187
|
512
|
|
|
|
|
3923
|
return $rv; |
|
188
|
|
|
|
|
|
|
} |
|
189
|
|
|
|
|
|
|
} |
|
190
|
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
1; |
|
192
|
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
__END__ |