1 |
package WebPAC::Common; |
package WebPAC::Common; |
2 |
|
use Exporter 'import'; |
3 |
|
@EXPORT = qw/ |
4 |
|
force_array |
5 |
|
/; |
6 |
|
|
7 |
use warnings; |
use warnings; |
8 |
use strict; |
use strict; |
10 |
use Log::Log4perl qw/get_logger :levels/; |
use Log::Log4perl qw/get_logger :levels/; |
11 |
use Time::HiRes qw/time/; |
use Time::HiRes qw/time/; |
12 |
use Data::Dump qw/dump/; |
use Data::Dump qw/dump/; |
13 |
|
use File::Spec; |
14 |
|
|
15 |
# If ture, enable logging debug |
use base qw/Class::Accessor/; |
16 |
my $log_debug = 0; |
__PACKAGE__->mk_accessors( qw/log_debug no_log debug/ ); |
17 |
|
|
18 |
=head1 NAME |
=head1 NAME |
19 |
|
|
21 |
|
|
22 |
=head1 VERSION |
=head1 VERSION |
23 |
|
|
24 |
Version 0.04 |
Version 0.05 |
25 |
|
|
26 |
=cut |
=cut |
27 |
|
|
28 |
our $VERSION = '0.04'; |
our $VERSION = '0.05'; |
29 |
|
|
30 |
=head1 SYNOPSYS |
=head1 SYNOPSYS |
31 |
|
|
32 |
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 |
33 |
specific modules. |
specific modules. |
34 |
|
|
35 |
|
my $o = WebPAC::Common->new({ |
36 |
|
log_debug => 1, |
37 |
|
no_log => 1, |
38 |
|
debug => 1, |
39 |
|
}); |
40 |
|
|
41 |
|
Options: |
42 |
|
|
43 |
|
=over 20 |
44 |
|
|
45 |
|
=item log_debug |
46 |
|
|
47 |
|
Generate additional debugging log on C<STDERR> |
48 |
|
|
49 |
|
=item no_log |
50 |
|
|
51 |
|
Disable all logging (useful for tests) |
52 |
|
|
53 |
|
=item debug |
54 |
|
|
55 |
|
Use debugging logger which dumps output only yo C<STDERR> |
56 |
|
|
57 |
|
=back |
58 |
|
|
59 |
|
|
60 |
=head1 FUNCTIONS |
=head1 FUNCTIONS |
61 |
|
|
62 |
=head2 progress_bar |
=head2 progress_bar |
161 |
# |
# |
162 |
# |
# |
163 |
|
|
164 |
|
=head2 var_path |
165 |
|
|
166 |
|
my $path = $self->var_path('data_dir', 'data_file', ... ); |
167 |
|
|
168 |
|
=cut |
169 |
|
|
170 |
|
sub var_path { |
171 |
|
my $self = shift; |
172 |
|
|
173 |
|
return File::Spec->catfile('var', @_); |
174 |
|
} |
175 |
|
|
176 |
|
=head1 EXPORTED NETHODS |
177 |
|
|
178 |
|
=head2 force_array |
179 |
|
|
180 |
|
my @array = force_array( $ref, sub { |
181 |
|
warn "reference is undefined!"; |
182 |
|
}); |
183 |
|
|
184 |
|
=cut |
185 |
|
|
186 |
|
sub force_array { |
187 |
|
my ( $what, $error ) = @_; |
188 |
|
my @result; |
189 |
|
if ( ref( $what ) eq 'ARRAY' ) { |
190 |
|
@result = @{ $what }; |
191 |
|
} elsif ( defined $what ) { |
192 |
|
@result = ( $what ); |
193 |
|
} else { |
194 |
|
$error->() if ref($error) eq 'CODE'; |
195 |
|
} |
196 |
|
return @result; |
197 |
|
} |
198 |
|
|
199 |
|
|
200 |
=head1 INTERNAL METHODS |
=head1 INTERNAL METHODS |
201 |
|
|
202 |
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 |
256 |
my $name = (caller(2))[3] || caller; |
my $name = (caller(2))[3] || caller; |
257 |
|
|
258 |
my $conf = q( ); |
my $conf = q( ); |
259 |
if ($self->{'no_log'}) { |
if ($self->no_log) { |
260 |
warn "# $name disabled logging\n" if ($log_debug); |
warn "# $name disabled logging\n" if $self->log_debug; |
261 |
} elsif ($self->{'debug'}) { |
} elsif ($self->debug) { |
262 |
$conf = << '_log4perl_'; |
$conf = << '_log4perl_'; |
263 |
|
|
264 |
log4perl.rootLogger=INFO, SCREEN |
log4perl.rootLogger=INFO, SCREEN |
270 |
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 |
271 |
|
|
272 |
_log4perl_ |
_log4perl_ |
273 |
warn "# $name is using debug logger\n" if ($log_debug); |
warn "# $name is using debug logger\n" if $self->log_debug; |
274 |
} elsif ($name =~ m/Test::Exception/o) { |
} elsif ($name =~ m/Test::Exception/o) { |
275 |
warn "# disabled logging for Text::Exception\n" if ($log_debug); |
warn "# disabled logging for Text::Exception\n" if $self->log_debug; |
276 |
} elsif (-e $file) { |
} elsif (-e $file) { |
277 |
warn "# $name is using $file logger\n" if ($log_debug); |
warn "# $name is using $file logger\n" if $self->log_debug; |
278 |
Log::Log4perl->init($file); |
Log::Log4perl->init($file); |
279 |
return 1; |
return 1; |
280 |
} else { |
} else { |
281 |
warn "# $name is using null logger\n" if ($log_debug); |
warn "# $name is using null logger\n" if $self->log_debug; |
282 |
} |
} |
283 |
Log::Log4perl->init( \$conf ); |
Log::Log4perl->init( \$conf ); |
284 |
|
|
304 |
|
|
305 |
# make name full |
# make name full |
306 |
my $f = ''; |
my $f = ''; |
307 |
if ($log_debug) { |
if ( $self->log_debug ) { |
308 |
foreach ( 0 .. 5 ) { |
foreach ( 0 .. 5 ) { |
309 |
my $s = (caller($_))[3]; |
my $s = (caller($_))[3]; |
310 |
$f .= "#### $_ >> $s\n" if ($s); |
$f .= "#### $_ >> $s\n" if ($s); |
314 |
$self->{'_logger_'} ||= $self->_init_logger; |
$self->{'_logger_'} ||= $self->_init_logger; |
315 |
|
|
316 |
my $log = get_logger( $name ); |
my $log = get_logger( $name ); |
317 |
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})); |
318 |
$_logger_seen->{$name}++; |
$_logger_seen->{$name}++; |
319 |
return $log; |
return $log; |
320 |
} |
} |