/[webpac2]/trunk/lib/WebPAC/Common.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /trunk/lib/WebPAC/Common.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 856 by dpavlin, Sun May 27 16:00:26 2007 UTC revision 942 by dpavlin, Wed Oct 31 14:11:44 2007 UTC
# Line 1  Line 1 
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;
# Line 6  use strict; Line 10  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    use Cwd qw/abs_path/;
15    
16  # If ture, enable logging debug  use base qw/Class::Accessor/;
17  my $log_debug = 0;  __PACKAGE__->mk_accessors( qw/log_debug no_log debug/ );
18    
19  =head1 NAME  =head1 NAME
20    
# Line 16  WebPAC::Common - internal methods called Line 22  WebPAC::Common - internal methods called
22    
23  =head1 VERSION  =head1 VERSION
24    
25  Version 0.04  Version 0.05
26    
27  =cut  =cut
28    
29  our $VERSION = '0.04';  our $VERSION = '0.05';
30    
31  =head1 SYNOPSYS  =head1 SYNOPSYS
32    
33  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
34  specific modules.  specific modules.
35    
36    my $o = WebPAC::Common->new({
37            log_debug => 1,
38            no_log => 1,
39            debug => 1,
40    });
41    
42    Options:
43    
44    =over 20
45    
46    =item log_debug
47    
48    Generate additional debugging log on C<STDERR>
49    
50    =item no_log
51    
52    Disable all logging (useful for tests)
53    
54    =item debug
55    
56    Use debugging logger which dumps output only yo C<STDERR>
57    
58    =back
59    
60    
61  =head1 FUNCTIONS  =head1 FUNCTIONS
62    
63  =head2 progress_bar  =head2 progress_bar
# Line 131  sub fill_in { Line 162  sub fill_in {
162  #  #
163  #  #
164    
165    =head2 var_path
166    
167      my $path = $self->var_path('data_dir', 'data_file', ... );
168    
169    =cut
170    
171    my $abs_path;
172    
173    sub var_path {
174            my $self = shift;
175    
176            if ( ! $abs_path ) {
177    #               $abs_path = abs_path( $0 );
178    #               $abs_path =~ s!/WebPAC/Common\.pm!!;
179                    $abs_path = '/data/webpac2';
180            }
181    
182            return File::Spec->catfile($abs_path, 'var', @_);
183    }
184    
185    =head1 EXPORTED NETHODS
186    
187    =head2 force_array
188    
189      my @array = force_array( $ref, sub {
190            warn "reference is undefined!";
191      });
192    
193    =cut
194    
195    sub force_array {
196            my ( $what, $error ) = @_;
197            my @result;
198            if ( ref( $what ) eq 'ARRAY' ) {
199                    @result = @{ $what };
200            } elsif ( defined $what ) {
201                    @result =  ( $what );
202            } else {
203                    $error->() if ref($error) eq 'CODE';
204            }
205            return @result;
206    }
207    
208    
209  =head1 INTERNAL METHODS  =head1 INTERNAL METHODS
210    
211  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
# Line 190  sub _init_logger { Line 265  sub _init_logger {
265          my $name = (caller(2))[3] || caller;          my $name = (caller(2))[3] || caller;
266    
267          my $conf = q( );          my $conf = q( );
268          if ($self->{'no_log'}) {          if ($self->no_log) {
269                  warn "# $name disabled logging\n" if ($log_debug);                  warn "# $name disabled logging\n" if $self->log_debug;
270          } elsif ($self->{'debug'}) {          } elsif ($self->debug) {
271                  $conf = << '_log4perl_';                  $conf = << '_log4perl_';
272    
273  log4perl.rootLogger=INFO, SCREEN  log4perl.rootLogger=INFO, SCREEN
# Line 204  log4perl.appender.SCREEN.layout=PatternL Line 279  log4perl.appender.SCREEN.layout=PatternL
279  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
280    
281  _log4perl_  _log4perl_
282                  warn "# $name is using debug logger\n" if ($log_debug);                  warn "# $name is using debug logger\n" if $self->log_debug;
283          } elsif ($name =~ m/Test::Exception/o) {          } elsif ($name =~ m/Test::Exception/o) {
284                  warn "# disabled logging for Text::Exception\n" if ($log_debug);                  warn "# disabled logging for Text::Exception\n" if $self->log_debug;
285          } elsif (-e $file) {          } elsif (-e $file) {
286                  warn "# $name is using $file logger\n" if ($log_debug);                  warn "# $name is using $file logger\n" if $self->log_debug;
287                  Log::Log4perl->init($file);                  Log::Log4perl->init($file);
288                  return 1;                  return 1;
289          } else {          } else {
290                  warn "# $name is using null logger\n" if ($log_debug);                  warn "# $name is using null logger\n" if $self->log_debug;
291          }          }
292          Log::Log4perl->init( \$conf );          Log::Log4perl->init( \$conf );
293    
# Line 238  sub _get_logger { Line 313  sub _get_logger {
313    
314          # make name full          # make name full
315          my $f = '';          my $f = '';
316          if ($log_debug) {          if ( $self->log_debug ) {
317                  foreach ( 0 .. 5 ) {                  foreach ( 0 .. 5 ) {
318                          my $s = (caller($_))[3];                          my $s = (caller($_))[3];
319                          $f .= "#### $_ >> $s\n" if ($s);                          $f .= "#### $_ >> $s\n" if ($s);
# Line 248  sub _get_logger { Line 323  sub _get_logger {
323          $self->{'_logger_'} ||= $self->_init_logger;          $self->{'_logger_'} ||= $self->_init_logger;
324    
325          my $log = get_logger( $name );          my $log = get_logger( $name );
326          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}));
327          $_logger_seen->{$name}++;          $_logger_seen->{$name}++;
328          return $log;          return $log;
329  }  }

Legend:
Removed from v.856  
changed lines
  Added in v.942

  ViewVC Help
Powered by ViewVC 1.1.26