This file is indexed.

/usr/share/perl5/Rex/Logger.pm is in rex 1.4.1-1.

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
#
# (c) Jan Gehring <jan.gehring@gmail.com>
#
# vim: set ts=2 sw=2 tw=0:
# vim: set expandtab:

=head1 NAME

Rex::Logger - Logging Module

=head1 DESCRIPTION

This module is the logging module. You can define custom logformats.

=head1 SYNOPSIS

 $Rex::Logger::format = '[%D] %s';
 # will output something like
 # [2012-04-12 18:35:12] Installing package vim
   
 $Rex::Logger::format = '%h - %D - %s';
 # will output something like
 # srv001 - 2012-04-12 18:35:12 - Installing package vim

=head1 VARIABLES

=over 4

=cut

package Rex::Logger;

use strict;
use warnings;

our $VERSION = '1.4.1'; # VERSION

#use Rex;

our $no_color = 0;
eval "use Term::ANSIColor";
if ($@) { $no_color = 1; }

if ( $^O =~ m/MSWin/ ) {
  eval "use Win32::Console::ANSI";
  if ($@) { $no_color = 1; }
}

my $has_syslog = 0;
my $log_fh;

=item $debug

Setting this variable to 1 will enable debug logging.

 $Rex::Logger::debug = 1;

=cut

our $debug = 0;

# we store the default handle to stderr
# so that we can restore the handle inside the logging functions
my $DEFAULT_STDERR;
open $DEFAULT_STDERR, ">&", STDERR;

=item $silent

If you set this variable to 1 nothing will be logged.

 $Rex::Logger::silent = 1;

=cut

our $silent = 0;

=item $format

You can define the logging format with the following parameters.

%D - Appends the current date yyyy-mm-dd HH:mm:ss

%h - The target host

%p - The pid of the running process

%l - Loglevel (INFO or DEBUG)

%s - The Logstring

Default is: [%D] %l - %s

=cut

our $format = "[%D] %l - %s";

my $log_opened = 0;

sub init {
  return if $silent;
  eval {
    die
      if ( Rex::Config->get_log_filename || !Rex::Config->get_log_facility );

    Sys::Syslog->use;
    openlog( "rex", "ndelay,pid", Rex::Config->get_log_facility );
    $has_syslog = 1;
  };

  $log_opened = 1;
}

sub info {
  my ( $msg, $type ) = @_;
  my $color = 'green';

  if ( defined($type) ) {
  CHECK_COLOR: {
      $type eq 'warn'  && do { $color = 'yellow'; last CHECK_COLOR; };
      $type eq 'error' && do { $color = 'red';    last CHECK_COLOR; };
    }
  }

  return if $silent;

  local *STDERR;
  open STDERR, ">&", $DEFAULT_STDERR;

  if ( defined($type) ) {
    $msg = format_string( $msg, uc($type) );
  }
  else {
    $msg = format_string( $msg, "INFO" );
  }

  # workaround for windows Sys::Syslog behaviour on forks
  # see: #6
  unless ($log_opened) {
    init();
    $log_opened = 2;
  }

  if ($has_syslog) {
    syslog( "info", $msg );
  }

  if ( Rex::Config->get_log_filename() ) {
    open( $log_fh, ">>", Rex::Config->get_log_filename() ) or die($!);
    flock( $log_fh, 2 );
    print {$log_fh} "$msg\n" if ($log_fh);
    close($log_fh);
  }

  if ($no_color) {
    print STDERR "$msg\n"
      if (
      (
           ( defined $::QUIET && $::QUIET == 2 )
        && ( defined $type && $type ne 'info' )
      )
      || !defined $::QUIET
      );
  }
  else {
    print STDERR colored( [$color], "$msg\n" )
      if (
      (
           ( defined $::QUIET && $::QUIET == 2 )
        && ( defined $type && $type ne 'info' )
      )
      || !defined $::QUIET
      );
  }

  # workaround for windows Sys::Syslog behaviour on forks
  # see: #6
  if ( $log_opened == 2 ) {
    &shutdown();
  }
}

sub debug {
  my ($msg) = @_;
  return if $silent;
  return unless $debug;

  local *STDERR;
  open STDERR, ">&", $DEFAULT_STDERR;

  $msg = format_string( $msg, "DEBUG" );

  # workaround for windows Sys::Syslog behaviour on forks
  # see: #6
  unless ($log_opened) {
    init();
    $log_opened = 2;
  }

  if ($has_syslog) {
    syslog( "debug", $msg );
  }

  if ( Rex::Config->get_log_filename() ) {
    open( $log_fh, ">>", Rex::Config->get_log_filename() ) or die($!);
    flock( $log_fh, 2 );
    print {$log_fh} "$msg\n" if ($log_fh);
    close($log_fh);
  }

  if ($no_color) {
    print STDERR "$msg\n" unless ($::QUIET);
  }
  else {
    print STDERR colored( ['red'], "$msg\n" ) unless ($::QUIET);
  }

  # workaround for windows Sys::Syslog behaviour on forks
  # see: #6
  if ( $log_opened == 2 ) {
    &shutdown();
  }
}

sub get_timestamp {
  my ( $sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst ) =
    localtime(time);
  $mon++;
  $year += 1900;

  return
      "$year-"
    . sprintf( "%02i", $mon ) . "-"
    . sprintf( "%02i", $mday ) . " "
    . sprintf( "%02i", $hour ) . ":"
    . sprintf( "%02i", $min ) . ":"
    . sprintf( "%02i", $sec );
}

sub shutdown {
  return if $silent;
  return unless $log_opened;

  if ($has_syslog) {
    closelog();
  }
  else {
    close($log_fh) if $log_fh;
  }

  $log_opened = 0;

}

# %D - Date
# %h - Host
# %s - Logstring
sub format_string {
  my ( $s, $level ) = @_;

  my $date = get_timestamp;
  my $host =
      Rex::get_current_connection()
    ? Rex::get_current_connection()->{conn}->server
    : "<local>";
  my $pid = $$;

  my $line = $format;

  $line =~ s/\%D/$date/gms;
  $line =~ s/\%h/$host/gms;
  $line =~ s/\%s/$s/gms;
  $line =~ s/\%l/$level/gms;
  $line =~ s/\%p/$pid/gms;

  return $line;
}

sub masq {
  my ( $format, @params ) = @_;

  return $format if scalar @params == 0;
  return $format if scalar( grep { defined } @params ) == 0;

  if ( exists $ENV{REX_DEBUG_INSECURE} && $ENV{REX_DEBUG_INSECURE} eq "1" ) {
    return sprintf( $format, @params );
  }

  return sprintf( $format, ("**********") x @params );
}

=back

=cut

1;