6 |
use Log::Log4perl qw/get_logger :levels/; |
use Log::Log4perl qw/get_logger :levels/; |
7 |
use Time::HiRes qw/time/; |
use Time::HiRes qw/time/; |
8 |
use Data::Dump qw/dump/; |
use Data::Dump qw/dump/; |
9 |
|
use File::Spec; |
10 |
|
|
11 |
# If ture, enable logging debug |
use base qw/Class::Accessor/; |
12 |
my $log_debug = 0; |
__PACKAGE__->mk_accessors( qw/log_debug no_log debug/ ); |
13 |
|
|
14 |
=head1 NAME |
=head1 NAME |
15 |
|
|
17 |
|
|
18 |
=head1 VERSION |
=head1 VERSION |
19 |
|
|
20 |
Version 0.04 |
Version 0.05 |
21 |
|
|
22 |
=cut |
=cut |
23 |
|
|
24 |
our $VERSION = '0.04'; |
our $VERSION = '0.05'; |
25 |
|
|
26 |
=head1 SYNOPSYS |
=head1 SYNOPSYS |
27 |
|
|
28 |
This module defines common functions, and is used as base for other, more |
This module defines common functions, and is used as base for other, more |
29 |
specific modules. |
specific modules. |
30 |
|
|
31 |
|
my $o = WebPAC::Common->new({ |
32 |
|
log_debug => 1, |
33 |
|
no_log => 1, |
34 |
|
debug => 1, |
35 |
|
}); |
36 |
|
|
37 |
|
Options: |
38 |
|
|
39 |
|
=over 20 |
40 |
|
|
41 |
|
=item log_debug |
42 |
|
|
43 |
|
Generate additional debugging log on C<STDERR> |
44 |
|
|
45 |
|
=item no_log |
46 |
|
|
47 |
|
Disable all logging (useful for tests) |
48 |
|
|
49 |
|
=item debug |
50 |
|
|
51 |
|
Use debugging logger which dumps output only yo C<STDERR> |
52 |
|
|
53 |
|
=back |
54 |
|
|
55 |
|
|
56 |
=head1 FUNCTIONS |
=head1 FUNCTIONS |
57 |
|
|
58 |
=head2 progress_bar |
=head2 progress_bar |
157 |
# |
# |
158 |
# |
# |
159 |
|
|
160 |
|
=head2 var_path |
161 |
|
|
162 |
|
my $path = $self->var_path('data_dir', 'data_file', ... ); |
163 |
|
|
164 |
|
=cut |
165 |
|
|
166 |
|
sub var_path { |
167 |
|
my $self = shift; |
168 |
|
|
169 |
|
return File::Spec->catfile('var', @_); |
170 |
|
} |
171 |
|
|
172 |
|
|
173 |
=head1 INTERNAL METHODS |
=head1 INTERNAL METHODS |
174 |
|
|
175 |
Here is a quick list of internal methods, mostly useful to turn debugging |
Here is a quick list of internal methods, mostly useful to turn debugging |
229 |
my $name = (caller(2))[3] || caller; |
my $name = (caller(2))[3] || caller; |
230 |
|
|
231 |
my $conf = q( ); |
my $conf = q( ); |
232 |
if ($self->{'no_log'}) { |
if ($self->no_log) { |
233 |
warn "# $name disabled logging\n" if ($log_debug); |
warn "# $name disabled logging\n" if $self->log_debug; |
234 |
} elsif ($self->{'debug'}) { |
} elsif ($self->debug) { |
235 |
$conf = << '_log4perl_'; |
$conf = << '_log4perl_'; |
236 |
|
|
237 |
log4perl.rootLogger=INFO, SCREEN |
log4perl.rootLogger=INFO, SCREEN |
243 |
log4perl.appender.SCREEN.layout.ConversionPattern=%d %p> %F{1}:%L %M - %m%n |
log4perl.appender.SCREEN.layout.ConversionPattern=%d %p> %F{1}:%L %M - %m%n |
244 |
|
|
245 |
_log4perl_ |
_log4perl_ |
246 |
warn "# $name is using debug logger\n" if ($log_debug); |
warn "# $name is using debug logger\n" if $self->log_debug; |
247 |
} elsif ($name =~ m/Test::Exception/o) { |
} elsif ($name =~ m/Test::Exception/o) { |
248 |
warn "# disabled logging for Text::Exception\n" if ($log_debug); |
warn "# disabled logging for Text::Exception\n" if $self->log_debug; |
249 |
} elsif (-e $file) { |
} elsif (-e $file) { |
250 |
warn "# $name is using $file logger\n" if ($log_debug); |
warn "# $name is using $file logger\n" if $self->log_debug; |
251 |
Log::Log4perl->init($file); |
Log::Log4perl->init($file); |
252 |
return 1; |
return 1; |
253 |
} else { |
} else { |
254 |
warn "# $name is using null logger\n" if ($log_debug); |
warn "# $name is using null logger\n" if $self->log_debug; |
255 |
} |
} |
256 |
Log::Log4perl->init( \$conf ); |
Log::Log4perl->init( \$conf ); |
257 |
|
|
277 |
|
|
278 |
# make name full |
# make name full |
279 |
my $f = ''; |
my $f = ''; |
280 |
if ($log_debug) { |
if ( $self->log_debug ) { |
281 |
foreach ( 0 .. 5 ) { |
foreach ( 0 .. 5 ) { |
282 |
my $s = (caller($_))[3]; |
my $s = (caller($_))[3]; |
283 |
$f .= "#### $_ >> $s\n" if ($s); |
$f .= "#### $_ >> $s\n" if ($s); |
287 |
$self->{'_logger_'} ||= $self->_init_logger; |
$self->{'_logger_'} ||= $self->_init_logger; |
288 |
|
|
289 |
my $log = get_logger( $name ); |
my $log = get_logger( $name ); |
290 |
warn "# get_logger( $name ) level ", $log->level, "\n$f" if ($log_debug && !defined($_logger_seen->{$name})); |
warn "# get_logger( $name ) level ", $log->level, "\n$f" if ($self->log_debug && !defined($_logger_seen->{$name})); |
291 |
$_logger_seen->{$name}++; |
$_logger_seen->{$name}++; |
292 |
return $log; |
return $log; |
293 |
} |
} |