--- trunk/lib/WebPAC/Common.pm 2007/09/03 15:26:44 886 +++ trunk/lib/WebPAC/Common.pm 2007/09/03 15:26:46 887 @@ -6,9 +6,10 @@ use Log::Log4perl qw/get_logger :levels/; use Time::HiRes qw/time/; use Data::Dump qw/dump/; +use File::Spec; -# If ture, enable logging debug -my $log_debug = 0; +use base qw/Class::Accessor/; +__PACKAGE__->mk_accessors( qw/log_debug no_log debug/ ); =head1 NAME @@ -16,17 +17,42 @@ =head1 VERSION -Version 0.04 +Version 0.05 =cut -our $VERSION = '0.04'; +our $VERSION = '0.05'; =head1 SYNOPSYS This module defines common functions, and is used as base for other, more specific modules. +my $o = WebPAC::Common->new({ + log_debug => 1, + no_log => 1, + debug => 1, +}); + +Options: + +=over 20 + +=item log_debug + +Generate additional debugging log on C + +=item no_log + +Disable all logging (useful for tests) + +=item debug + +Use debugging logger which dumps output only yo C + +=back + + =head1 FUNCTIONS =head2 progress_bar @@ -131,6 +157,19 @@ # # +=head2 var_path + + my $path = $self->var_path('data_dir', 'data_file', ... ); + +=cut + +sub var_path { + my $self = shift; + + return File::Spec->catfile('var', @_); +} + + =head1 INTERNAL METHODS Here is a quick list of internal methods, mostly useful to turn debugging @@ -190,9 +229,9 @@ my $name = (caller(2))[3] || caller; my $conf = q( ); - if ($self->{'no_log'}) { - warn "# $name disabled logging\n" if ($log_debug); - } elsif ($self->{'debug'}) { + if ($self->no_log) { + warn "# $name disabled logging\n" if $self->log_debug; + } elsif ($self->debug) { $conf = << '_log4perl_'; log4perl.rootLogger=INFO, SCREEN @@ -204,15 +243,15 @@ log4perl.appender.SCREEN.layout.ConversionPattern=%d %p> %F{1}:%L %M - %m%n _log4perl_ - warn "# $name is using debug logger\n" if ($log_debug); + warn "# $name is using debug logger\n" if $self->log_debug; } elsif ($name =~ m/Test::Exception/o) { - warn "# disabled logging for Text::Exception\n" if ($log_debug); + warn "# disabled logging for Text::Exception\n" if $self->log_debug; } elsif (-e $file) { - warn "# $name is using $file logger\n" if ($log_debug); + warn "# $name is using $file logger\n" if $self->log_debug; Log::Log4perl->init($file); return 1; } else { - warn "# $name is using null logger\n" if ($log_debug); + warn "# $name is using null logger\n" if $self->log_debug; } Log::Log4perl->init( \$conf ); @@ -238,7 +277,7 @@ # make name full my $f = ''; - if ($log_debug) { + if ( $self->log_debug ) { foreach ( 0 .. 5 ) { my $s = (caller($_))[3]; $f .= "#### $_ >> $s\n" if ($s); @@ -248,7 +287,7 @@ $self->{'_logger_'} ||= $self->_init_logger; my $log = get_logger( $name ); - 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})); $_logger_seen->{$name}++; return $log; }