blib/lib/Rex/Logger.pm

filter: Statementstmt 97.2% 105 / 108 Branchbran 72.2% 52 / 72 Conditioncond 30.3% 10 / 33 MC/DCmcdc 6.2% 1 / 16 Subroutinesub 100% 12 / 12 Podpod 0.0% 0 / 7 totaltotal 72.5% 180 / 248 CC 48 SCAR 29.0
CC
48
Cov
78%
info
34.7
debug
15.4
masq
5.8
CRAP 18.2
« lib/Rex/Helper/INI.pm ↑ Summary lib/Rex/Commands.pm »
1 #
2 # (c) Jan Gehring <jan.gehring@gmail.com>
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 109 ▶use v5.14.4;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 109 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34 109 our $VERSION = '1.16.1'; # VERSION
35  
36 109 ▶use English qw(-no_match_vars);
Subroutine BEGIN: called CC=1 SCAR=0.0
37 109 ▶use Term::ANSIColor;
Subroutine BEGIN: called CC=1 SCAR=0.0
38  
39 109 ▶use if $OSNAME eq 'MSWin32', 'Win32::Console::ANSI';
Subroutine BEGIN: called CC=1 SCAR=0.0
40  
41 109 our $no_color = 0;
42  
43 109 my $has_syslog = 0;
44 109 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 109 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 109 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 109 our $format = "[%D] %l - %s";
85  
86 109 my $log_opened = 0;
87  
88 90 ▶sub init {
Subroutine init: called CC=5 SCAR=16.2
Pod undocumented
89 90 ▶ return if $silent;
Branch: 1 branch
BranchTrueFalseTotal
if $Rex::Logger::silent 3 87 90
90 87 eval {
91 87 ▶ die
Branch: 1 branch
BranchTrueFalseTotal
if "Rex::Config"->get_log_filename or not "Rex::Config"->get_log_facility 25 62 87
Condition66%2/3
"Rex::Config"->get_log_filename or not "Rex::Config"->get_log_facility
l!l&&r!l&&!r
25062
MC/DC50%1/2
"Rex::Config"->get_log_filename or not "Rex::Config"->get_log_facility
"Rex::Config"->get_log_filename not "Rex::Config"->get_log_facility
Truth table66%2/3
A or not(B)
A= "Rex::Config"->get_log_filenameB= not "Rex::Config"->get_log_facility
ABresult
1X1
011
000
92 if ( Rex::Config->get_log_filename || !Rex::Config->get_log_facility );
93 62 ▶ die if ( $OSNAME =~ m/^MSWin/ );
Branch: 1 branch
BranchTrueFalseTotal
if $Rex::Logger::OSNAME =~ /^MSWin/u 0 62 62
94  
95 62 Sys::Syslog->use;
96 62 openlog( "rex", "ndelay,pid", Rex::Config->get_log_facility );
97 62 $has_syslog = 1;
98 };
99  
100 87 $log_opened = 1;
101 }
102  
103 105 ▶sub info {
Subroutine info: called CC=23 SCAR=35.5
Pod undocumented
104 105 my ( $msg, $type ) = @_;
105 105 my $color = 'green';
106  
107 105 ▶ if ( defined($type) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $type) 38 67 105
108 CHECK_COLOR: {
109 38 ▶ $type eq 'warn' && do { $color = 'yellow'; last CHECK_COLOR; };
Branch: 1 branch
BranchTrueFalseTotal
$type eq "warn" and do { $color = "yellow"; last CHECK_COLOR } 14 24 38
110 24 ▶ $type eq 'error' && do { $color = 'red'; last CHECK_COLOR; };
Branch: 1 branch
BranchTrueFalseTotal
$type eq "error" and do { $color = "red"; last CHECK_COLOR } 24 0 24
111 }
112 }
113  
114 105 ▶ return if $silent;
Branch: 1 branch
BranchTrueFalseTotal
if $Rex::Logger::silent 4 101 105
115  
116 101 ▶ if ( defined($type) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $type) { } 38 63 101
117 38 $msg = format_string( $msg, uc($type) );
118 }
119 else {
120 63 $msg = format_string( $msg, "INFO" );
121 }
122  
123 # workaround for windows Sys::Syslog behaviour on forks
124 # see: #6
125 101 ▶ unless ($log_opened) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($log_opened) 56 45 101
126 56 init();
127 56 $log_opened = 2;
128 }
129  
130 101 ▶ if ($has_syslog) {
Branch: 1 branch
BranchTrueFalseTotal
if ($has_syslog) 78 23 101
131 78 syslog( "info", $msg );
132 }
133  
134 101 ▶ if ( Rex::Config->get_log_filename() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ("Rex::Config"->get_log_filename) 23 78 101
135 23 ▶ open( $log_fh, ">>", Rex::Config->get_log_filename() ) or die($OS_ERROR);
Branch: 1 branch
BranchTrueFalseTotal
open $log_fh, ">>", "Rex::Config"->get_log_filename or die $Rex::Logger::OS_ERROR 23 0 23
136 23 flock( $log_fh, 2 );
137 23 ▶ print {$log_fh} "$msg\n" if ($log_fh);
Branch: 1 branch
BranchTrueFalseTotal
if $log_fh 23 0 23
138 23 close($log_fh);
139 }
140  
141 101 ▶ if ($no_color) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Rex::Logger::no_color) { } 20 81 101
142 20 ▶ print STDERR "$msg\n"
Branch: 1 branch
BranchTrueFalseTotal
if defined $main::QUIET and $main::QUIET == 2 and (defined $type and $type ne "info") or not defined $main::QUIET 20 0 20
Condition25%3/12
defined $main::QUIET and $main::QUIET == 2 and (defined $type and $type ne "info") or not defined $main::QUIET
l!l&&r!l&&!r
0200
defined $main::QUIET and $main::QUIET == 2 and (defined $type and $type ne "info")
!ll&&!rl&&r
2000
defined $main::QUIET and $main::QUIET == 2
!ll&&!rl&&r
2000
defined $type and $type ne "info"
!ll&&!rl&&r
000
MC/DC0%0/5
defined $main::QUIET and $main::QUIET == 2 and (defined $type and $type ne "info") or not defined $main::QUIET
defined $main::QUIET $main::QUIET == 2 defined $type $type ne "info" not defined $main::QUIET
Truth table11%1/9
A and B and C and D or not(E)
A= defined $main::QUIETB= $main::QUIET == 2C= defined $typeD= $type ne "info"E= not defined $main::QUIET
ABCDEresult
1111X1
0XXX11
10XX11
110X11
111011
0XXX00
10XX00
110X00
111000
143 if (
144 (
145 ( defined $::QUIET && $::QUIET == 2 )
146 && ( defined $type && $type ne 'info' )
147 )
148 || !defined $::QUIET
149 );
150 }
151 else {
152 81 ▶ print STDERR colored( [$color], "$msg\n" )
Branch: 1 branch
BranchTrueFalseTotal
if defined $main::QUIET and $main::QUIET == 2 and (defined $type and $type ne "info") or not defined $main::QUIET 0 81 81
Condition25%3/12
defined $main::QUIET and $main::QUIET == 2 and (defined $type and $type ne "info") or not defined $main::QUIET
l!l&&r!l&&!r
0081
defined $main::QUIET and $main::QUIET == 2 and (defined $type and $type ne "info")
!ll&&!rl&&r
8100
defined $main::QUIET and $main::QUIET == 2
!ll&&!rl&&r
0810
defined $type and $type ne "info"
!ll&&!rl&&r
000
MC/DC0%0/5
defined $main::QUIET and $main::QUIET == 2 and (defined $type and $type ne "info") or not defined $main::QUIET
defined $main::QUIET $main::QUIET == 2 defined $type $type ne "info" not defined $main::QUIET
Truth table11%1/9
A and B and C and D or not(E)
A= defined $main::QUIETB= $main::QUIET == 2C= defined $typeD= $type ne "info"E= not defined $main::QUIET
ABCDEresult
1111X1
0XXX11
10XX11
110X11
111011
0XXX00
10XX00
110X00
111000
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 101 ▶ if ( $log_opened == 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($log_opened == 2) 56 45 101
165 56 &shutdown();
166 }
167 }
168  
169 6520 ▶sub debug {
Subroutine debug: called CC=12 SCAR=27.3
Pod undocumented
170 6520 my ($msg) = @_;
171 6520 ▶ return if $silent;
Branch: 1 branch
BranchTrueFalseTotal
if $Rex::Logger::silent 136 6384 6520
172 6384 ▶ return unless $debug;
Branch: 1 branch
BranchTrueFalseTotal
unless $Rex::Logger::debug 6382 2 6384
173  
174 2 $msg = format_string( $msg, "DEBUG" );
175  
176 # workaround for windows Sys::Syslog behaviour on forks
177 # see: #6
178 2 ▶ unless ($log_opened) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($log_opened) 2 0 2
179 2 init();
180 2 $log_opened = 2;
181 }
182  
183 2 ▶ if ($has_syslog) {
Branch: 1 branch
BranchTrueFalseTotal
if ($has_syslog) 0 2 2
184 0 syslog( "debug", $msg );
185 }
186  
187 2 ▶ if ( Rex::Config->get_log_filename() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ("Rex::Config"->get_log_filename) 2 0 2
188 2 ▶ open( $log_fh, ">>", Rex::Config->get_log_filename() ) or die($OS_ERROR);
Branch: 1 branch
BranchTrueFalseTotal
open $log_fh, ">>", "Rex::Config"->get_log_filename or die $Rex::Logger::OS_ERROR 2 0 2
189 2 flock( $log_fh, 2 );
190 2 ▶ print {$log_fh} "$msg\n" if ($log_fh);
Branch: 1 branch
BranchTrueFalseTotal
if $log_fh 2 0 2
191 2 close($log_fh);
192 }
193  
194 2 ▶ if ($no_color) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Rex::Logger::no_color) { } 0 2 2
195 0 ▶ print STDERR "$msg\n" unless ($::QUIET);
Branch: 1 branch
BranchTrueFalseTotal
unless $main::QUIET 0 0 0
196 }
197 else {
198 2 ▶ print STDERR colored( ['red'], "$msg\n" ) unless ($::QUIET);
Branch: 1 branch
BranchTrueFalseTotal
unless $main::QUIET 0 2 2
199 }
200  
201 # workaround for windows Sys::Syslog behaviour on forks
202 # see: #6
203 2 ▶ if ( $log_opened == 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($log_opened == 2) 2 0 2
204 2 &shutdown();
205 }
206 }
207  
208 103 ▶sub get_timestamp {
Subroutine get_timestamp: called CC=1 SCAR=0.0
Pod undocumented
209 103 my ( $sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst ) =
210 localtime(time);
211 103 $mon++;
212 103 $year += 1900;
213  
214 return
215 103 "$year-"
216 . sprintf( "%02i", $mon ) . "-"
217 . sprintf( "%02i", $mday ) . " "
218 . sprintf( "%02i", $hour ) . ":"
219 . sprintf( "%02i", $min ) . ":"
220 . sprintf( "%02i", $sec );
221 }
222  
223 89 ▶sub shutdown {
Subroutine shutdown: called CC=5 SCAR=16.1
Pod undocumented
224 89 ▶ return if $silent;
Branch: 1 branch
BranchTrueFalseTotal
if $Rex::Logger::silent 3 86 89
225 86 ▶ return unless $log_opened;
Branch: 1 branch
BranchTrueFalseTotal
unless $log_opened 1 85 86
226  
227 85 ▶ if ($has_syslog) {
Branch: 1 branch
BranchTrueFalseTotal
if ($has_syslog) { } 60 25 85
228 60 closelog();
229 }
230 else {
231 25 ▶ close($log_fh) if $log_fh;
Branch: 1 branch
BranchTrueFalseTotal
if $log_fh 25 0 25
232 }
233  
234 85 $log_opened = 0;
235  
236 }
237  
238 103 ▶sub format_string {
Subroutine format_string: called CC=3 SCAR=11.2
Pod undocumented
239 103 my ( $s, $level ) = @_;
240  
241 103 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 103 ▶ : "<local>";
Branch: 1 branch
BranchTrueFalseTotal
Rex::get_current_connection() && Rex::get_current_connection()->{'conn'}->server ? : 103 0 103
Condition33%1/3
Rex::get_current_connection() and Rex::get_current_connection()->{"conn"}->server
!ll&&!rl&&r
00103
MC/DC0%0/2
Rex::get_current_connection() and Rex::get_current_connection()->{"conn"}->server
Rex::get_current_connection() Rex::get_current_connection()->{"conn"}->server
Truth table33%1/3
A and B
A= Rex::get_current_connection()B= Rex::get_current_connection()->{"conn"}->server
ABresult
0X0
100
111
247  
248 103 my $line = $format;
249  
250 103 $line =~ s/\%D/$date/gms;
251 103 $line =~ s/\%h/$host/gms;
252 103 $line =~ s/\%s/$s/gms;
253 103 $line =~ s/\%l/$level/gms;
254 103 $line =~ s/\%p/$PID/gms;
255  
256 103 return $line;
257 }
258  
259 75 ▶sub masq {
Subroutine masq: called CC=5 SCAR=17.5
Pod undocumented
260 75 my ( $format, @params ) = @_;
261  
262 75 ▶ return $format if scalar @params == 0;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @params == 0 0 75 75
263 75 ▶ return $format if scalar( grep { defined } @params ) == 0;
Branch: 1 branch
BranchTrueFalseTotal
if scalar grep({defined $_;} @params) == 0 34 41 75
264  
265 41 ▶ if ( exists $ENV{REX_DEBUG_INSECURE} && $ENV{REX_DEBUG_INSECURE} eq "1" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $ENV{"REX_DEBUG_INSECURE"} and $ENV{"REX_DEBUG_INSECURE"} eq 1) 0 41 41
Condition33%1/3
exists $ENV{"REX_DEBUG_INSECURE"} and $ENV{"REX_DEBUG_INSECURE"} eq 1
!ll&&!rl&&r
4100
MC/DC0%0/2
exists $ENV{"REX_DEBUG_INSECURE"} and $ENV{"REX_DEBUG_INSECURE"} eq 1
exists $ENV{"REX_DEBUG_INSECURE"} $ENV{"REX_DEBUG_INSECURE"} eq 1
Truth table33%1/3
A and B
A= exists $ENV{"REX_DEBUG_INSECURE"}B= $ENV{"REX_DEBUG_INSECURE"} eq 1
ABresult
0X0
100
111
266 0 return sprintf( $format, @params );
267 }
268  
269 41 return sprintf( $format, ("**********") x @params );
270 }
271  
272 =back
273  
274 =cut
275  
276 109 1;
« lib/Rex/Helper/INI.pm ↑ Summary lib/Rex/Commands.pm »