line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# |
2
|
|
|
|
|
|
|
# (c) Jan Gehring |
3
|
|
|
|
|
|
|
# |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
=head1 NAME |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
Rex::Logger - Logging Module |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
=head1 DESCRIPTION |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
This module is the logging module. You can define custom logformats. |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
=head1 SYNOPSIS |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
$Rex::Logger::format = '[%D] %s'; |
16
|
|
|
|
|
|
|
# will output something like |
17
|
|
|
|
|
|
|
# [2012-04-12 18:35:12] Installing package vim |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
$Rex::Logger::format = '%h - %D - %s'; |
20
|
|
|
|
|
|
|
# will output something like |
21
|
|
|
|
|
|
|
# srv001 - 2012-04-12 18:35:12 - Installing package vim |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
=head1 VARIABLES |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
=over 4 |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
=cut |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
package Rex::Logger; |
30
|
|
|
|
|
|
|
|
31
|
106
|
|
|
106
|
|
1312
|
use v5.12.5; |
|
106
|
|
|
|
|
360
|
|
32
|
106
|
|
|
106
|
|
635
|
use warnings; |
|
106
|
|
|
|
|
224
|
|
|
106
|
|
|
|
|
4915
|
|
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
our $VERSION = '1.14.2.3'; # TRIAL VERSION |
35
|
|
|
|
|
|
|
|
36
|
106
|
|
|
106
|
|
25951
|
use English qw(-no_match_vars); |
|
106
|
|
|
|
|
185455
|
|
|
106
|
|
|
|
|
596
|
|
37
|
106
|
|
|
106
|
|
105886
|
use Term::ANSIColor; |
|
106
|
|
|
|
|
902195
|
|
|
106
|
|
|
|
|
8375
|
|
38
|
|
|
|
|
|
|
|
39
|
106
|
|
|
106
|
|
71010
|
use if $OSNAME eq 'MSWin32', 'Win32::Console::ANSI'; |
|
106
|
|
|
|
|
1471
|
|
|
106
|
|
|
|
|
738
|
|
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
our $no_color = 0; |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
my $has_syslog = 0; |
44
|
|
|
|
|
|
|
my $log_fh; |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
=item $debug |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
Setting this variable to 1 will enable debug logging. |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
$Rex::Logger::debug = 1; |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
=cut |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
our $debug = 0; |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
=item $silent |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
If you set this variable to 1 nothing will be logged. |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
$Rex::Logger::silent = 1; |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
=cut |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
our $silent = 0; |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
=item $format |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
You can define the logging format with the following parameters. |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
%D - Appends the current date yyyy-mm-dd HH:mm:ss |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
%h - The target host |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
%p - The pid of the running process |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
%l - Loglevel (INFO or DEBUG) |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
%s - The Logstring |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
Default is: [%D] %l - %s |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
=cut |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
our $format = "[%D] %l - %s"; |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
my $log_opened = 0; |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
sub init { |
89
|
76
|
100
|
|
76
|
0
|
2008
|
return if $silent; |
90
|
73
|
|
|
|
|
1047
|
eval { |
91
|
73
|
100
|
66
|
|
|
2698
|
die |
92
|
|
|
|
|
|
|
if ( Rex::Config->get_log_filename || !Rex::Config->get_log_facility ); |
93
|
50
|
50
|
|
|
|
2429
|
die if ( $OSNAME =~ m/^MSWin/ ); |
94
|
|
|
|
|
|
|
|
95
|
50
|
|
|
|
|
3129
|
Sys::Syslog->use; |
96
|
50
|
|
|
|
|
678
|
openlog( "rex", "ndelay,pid", Rex::Config->get_log_facility ); |
97
|
50
|
|
|
|
|
14023
|
$has_syslog = 1; |
98
|
|
|
|
|
|
|
}; |
99
|
|
|
|
|
|
|
|
100
|
73
|
|
|
|
|
380
|
$log_opened = 1; |
101
|
|
|
|
|
|
|
} |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
sub info { |
104
|
91
|
|
|
91
|
0
|
731
|
my ( $msg, $type ) = @_; |
105
|
91
|
|
|
|
|
770
|
my $color = 'green'; |
106
|
|
|
|
|
|
|
|
107
|
91
|
100
|
|
|
|
681
|
if ( defined($type) ) { |
108
|
|
|
|
|
|
|
CHECK_COLOR: { |
109
|
36
|
100
|
|
|
|
96
|
$type eq 'warn' && do { $color = 'yellow'; last CHECK_COLOR; }; |
|
36
|
|
|
|
|
188
|
|
|
14
|
|
|
|
|
61
|
|
|
14
|
|
|
|
|
66
|
|
110
|
22
|
50
|
|
|
|
80
|
$type eq 'error' && do { $color = 'red'; last CHECK_COLOR; }; |
|
22
|
|
|
|
|
81
|
|
|
22
|
|
|
|
|
73
|
|
111
|
|
|
|
|
|
|
} |
112
|
|
|
|
|
|
|
} |
113
|
|
|
|
|
|
|
|
114
|
91
|
100
|
|
|
|
416
|
return if $silent; |
115
|
|
|
|
|
|
|
|
116
|
87
|
100
|
|
|
|
639
|
if ( defined($type) ) { |
117
|
36
|
|
|
|
|
267
|
$msg = format_string( $msg, uc($type) ); |
118
|
|
|
|
|
|
|
} |
119
|
|
|
|
|
|
|
else { |
120
|
51
|
|
|
|
|
891
|
$msg = format_string( $msg, "INFO" ); |
121
|
|
|
|
|
|
|
} |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
# workaround for windows Sys::Syslog behaviour on forks |
124
|
|
|
|
|
|
|
# see: #6 |
125
|
87
|
100
|
|
|
|
426
|
unless ($log_opened) { |
126
|
42
|
|
|
|
|
208
|
init(); |
127
|
42
|
|
|
|
|
88
|
$log_opened = 2; |
128
|
|
|
|
|
|
|
} |
129
|
|
|
|
|
|
|
|
130
|
87
|
100
|
|
|
|
307
|
if ($has_syslog) { |
131
|
66
|
|
|
|
|
436
|
syslog( "info", $msg ); |
132
|
|
|
|
|
|
|
} |
133
|
|
|
|
|
|
|
|
134
|
87
|
100
|
|
|
|
16609
|
if ( Rex::Config->get_log_filename() ) { |
135
|
21
|
50
|
|
|
|
61
|
open( $log_fh, ">>", Rex::Config->get_log_filename() ) or die($OS_ERROR); |
136
|
21
|
|
|
|
|
229
|
flock( $log_fh, 2 ); |
137
|
21
|
50
|
|
|
|
112
|
print {$log_fh} "$msg\n" if ($log_fh); |
|
21
|
|
|
|
|
219
|
|
138
|
21
|
|
|
|
|
607
|
close($log_fh); |
139
|
|
|
|
|
|
|
} |
140
|
|
|
|
|
|
|
|
141
|
87
|
100
|
|
|
|
370
|
if ($no_color) { |
142
|
18
|
50
|
33
|
|
|
346
|
print STDERR "$msg\n" |
|
|
|
0
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
|
33
|
|
|
|
|
143
|
|
|
|
|
|
|
if ( |
144
|
|
|
|
|
|
|
( |
145
|
|
|
|
|
|
|
( defined $::QUIET && $::QUIET == 2 ) |
146
|
|
|
|
|
|
|
&& ( defined $type && $type ne 'info' ) |
147
|
|
|
|
|
|
|
) |
148
|
|
|
|
|
|
|
|| !defined $::QUIET |
149
|
|
|
|
|
|
|
); |
150
|
|
|
|
|
|
|
} |
151
|
|
|
|
|
|
|
else { |
152
|
69
|
50
|
33
|
|
|
1316
|
print STDERR colored( [$color], "$msg\n" ) |
|
|
|
0
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
|
33
|
|
|
|
|
153
|
|
|
|
|
|
|
if ( |
154
|
|
|
|
|
|
|
( |
155
|
|
|
|
|
|
|
( defined $::QUIET && $::QUIET == 2 ) |
156
|
|
|
|
|
|
|
&& ( defined $type && $type ne 'info' ) |
157
|
|
|
|
|
|
|
) |
158
|
|
|
|
|
|
|
|| !defined $::QUIET |
159
|
|
|
|
|
|
|
); |
160
|
|
|
|
|
|
|
} |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
# workaround for windows Sys::Syslog behaviour on forks |
163
|
|
|
|
|
|
|
# see: #6 |
164
|
87
|
100
|
|
|
|
436
|
if ( $log_opened == 2 ) { |
165
|
42
|
|
|
|
|
131
|
&shutdown(); |
166
|
|
|
|
|
|
|
} |
167
|
|
|
|
|
|
|
} |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
sub debug { |
170
|
6540
|
|
|
6540
|
0
|
45565
|
my ($msg) = @_; |
171
|
6540
|
100
|
|
|
|
18424
|
return if $silent; |
172
|
6404
|
100
|
|
|
|
19287
|
return unless $debug; |
173
|
|
|
|
|
|
|
|
174
|
2
|
|
|
|
|
6
|
$msg = format_string( $msg, "DEBUG" ); |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
# workaround for windows Sys::Syslog behaviour on forks |
177
|
|
|
|
|
|
|
# see: #6 |
178
|
2
|
50
|
|
|
|
9
|
unless ($log_opened) { |
179
|
2
|
|
|
|
|
6
|
init(); |
180
|
2
|
|
|
|
|
5
|
$log_opened = 2; |
181
|
|
|
|
|
|
|
} |
182
|
|
|
|
|
|
|
|
183
|
2
|
50
|
|
|
|
6
|
if ($has_syslog) { |
184
|
0
|
|
|
|
|
0
|
syslog( "debug", $msg ); |
185
|
|
|
|
|
|
|
} |
186
|
|
|
|
|
|
|
|
187
|
2
|
50
|
|
|
|
5
|
if ( Rex::Config->get_log_filename() ) { |
188
|
2
|
50
|
|
|
|
8
|
open( $log_fh, ">>", Rex::Config->get_log_filename() ) or die($OS_ERROR); |
189
|
2
|
|
|
|
|
23
|
flock( $log_fh, 2 ); |
190
|
2
|
50
|
|
|
|
9
|
print {$log_fh} "$msg\n" if ($log_fh); |
|
2
|
|
|
|
|
15
|
|
191
|
2
|
|
|
|
|
52
|
close($log_fh); |
192
|
|
|
|
|
|
|
} |
193
|
|
|
|
|
|
|
|
194
|
2
|
50
|
|
|
|
9
|
if ($no_color) { |
195
|
0
|
0
|
|
|
|
0
|
print STDERR "$msg\n" unless ($::QUIET); |
196
|
|
|
|
|
|
|
} |
197
|
|
|
|
|
|
|
else { |
198
|
2
|
50
|
|
|
|
7
|
print STDERR colored( ['red'], "$msg\n" ) unless ($::QUIET); |
199
|
|
|
|
|
|
|
} |
200
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
# workaround for windows Sys::Syslog behaviour on forks |
202
|
|
|
|
|
|
|
# see: #6 |
203
|
2
|
50
|
|
|
|
7
|
if ( $log_opened == 2 ) { |
204
|
2
|
|
|
|
|
5
|
&shutdown(); |
205
|
|
|
|
|
|
|
} |
206
|
|
|
|
|
|
|
} |
207
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
sub get_timestamp { |
209
|
89
|
|
|
89
|
0
|
4850
|
my ( $sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst ) = |
210
|
|
|
|
|
|
|
localtime(time); |
211
|
89
|
|
|
|
|
401
|
$mon++; |
212
|
89
|
|
|
|
|
388
|
$year += 1900; |
213
|
|
|
|
|
|
|
|
214
|
|
|
|
|
|
|
return |
215
|
89
|
|
|
|
|
1277
|
"$year-" |
216
|
|
|
|
|
|
|
. sprintf( "%02i", $mon ) . "-" |
217
|
|
|
|
|
|
|
. sprintf( "%02i", $mday ) . " " |
218
|
|
|
|
|
|
|
. sprintf( "%02i", $hour ) . ":" |
219
|
|
|
|
|
|
|
. sprintf( "%02i", $min ) . ":" |
220
|
|
|
|
|
|
|
. sprintf( "%02i", $sec ); |
221
|
|
|
|
|
|
|
} |
222
|
|
|
|
|
|
|
|
223
|
|
|
|
|
|
|
sub shutdown { |
224
|
75
|
100
|
|
75
|
0
|
1204
|
return if $silent; |
225
|
72
|
100
|
|
|
|
238
|
return unless $log_opened; |
226
|
|
|
|
|
|
|
|
227
|
71
|
100
|
|
|
|
279
|
if ($has_syslog) { |
228
|
48
|
|
|
|
|
1309
|
closelog(); |
229
|
|
|
|
|
|
|
} |
230
|
|
|
|
|
|
|
else { |
231
|
23
|
50
|
|
|
|
59
|
close($log_fh) if $log_fh; |
232
|
|
|
|
|
|
|
} |
233
|
|
|
|
|
|
|
|
234
|
71
|
|
|
|
|
3561
|
$log_opened = 0; |
235
|
|
|
|
|
|
|
|
236
|
|
|
|
|
|
|
} |
237
|
|
|
|
|
|
|
|
238
|
|
|
|
|
|
|
sub format_string { |
239
|
89
|
|
|
89
|
0
|
573
|
my ( $s, $level ) = @_; |
240
|
|
|
|
|
|
|
|
241
|
89
|
|
|
|
|
672
|
my $date = get_timestamp; |
242
|
|
|
|
|
|
|
my $host = |
243
|
|
|
|
|
|
|
Rex::get_current_connection() |
244
|
|
|
|
|
|
|
&& Rex::get_current_connection()->{conn}->server |
245
|
|
|
|
|
|
|
? Rex::get_current_connection()->{conn}->server |
246
|
89
|
50
|
33
|
|
|
769
|
: ""; |
247
|
|
|
|
|
|
|
|
248
|
89
|
|
|
|
|
741
|
my $line = $format; |
249
|
|
|
|
|
|
|
|
250
|
89
|
|
|
|
|
862
|
$line =~ s/\%D/$date/gms; |
251
|
89
|
|
|
|
|
319
|
$line =~ s/\%h/$host/gms; |
252
|
89
|
|
|
|
|
773
|
$line =~ s/\%s/$s/gms; |
253
|
89
|
|
|
|
|
609
|
$line =~ s/\%l/$level/gms; |
254
|
89
|
|
|
|
|
333
|
$line =~ s/\%p/$PID/gms; |
255
|
|
|
|
|
|
|
|
256
|
89
|
|
|
|
|
342
|
return $line; |
257
|
|
|
|
|
|
|
} |
258
|
|
|
|
|
|
|
|
259
|
|
|
|
|
|
|
sub masq { |
260
|
75
|
|
|
75
|
0
|
1373
|
my ( $format, @params ) = @_; |
261
|
|
|
|
|
|
|
|
262
|
75
|
50
|
|
|
|
343
|
return $format if scalar @params == 0; |
263
|
75
|
100
|
|
|
|
242
|
return $format if scalar( grep { defined } @params ) == 0; |
|
75
|
|
|
|
|
403
|
|
264
|
|
|
|
|
|
|
|
265
|
41
|
50
|
33
|
|
|
555
|
if ( exists $ENV{REX_DEBUG_INSECURE} && $ENV{REX_DEBUG_INSECURE} eq "1" ) { |
266
|
0
|
|
|
|
|
0
|
return sprintf( $format, @params ); |
267
|
|
|
|
|
|
|
} |
268
|
|
|
|
|
|
|
|
269
|
41
|
|
|
|
|
327
|
return sprintf( $format, ("**********") x @params ); |
270
|
|
|
|
|
|
|
} |
271
|
|
|
|
|
|
|
|
272
|
|
|
|
|
|
|
=back |
273
|
|
|
|
|
|
|
|
274
|
|
|
|
|
|
|
=cut |
275
|
|
|
|
|
|
|
|
276
|
|
|
|
|
|
|
1; |