1 package WebPAC::Common;
10 use Log::Log4perl qw/get_logger :levels/;
11 use Time::HiRes qw/time/;
12 use Data::Dump qw/dump/;
16 use base qw/Class::Accessor/;
17 __PACKAGE__->mk_accessors( qw/log_debug no_log debug/ );
21 WebPAC::Common - internal methods called from other WebPAC modules
29 our $VERSION = '0.05';
33 This module defines common functions, and is used as base for other, more
36 my $o = WebPAC::Common->new({
48 Generate additional debugging log on C<STDERR>
52 Disable all logging (useful for tests)
56 Use debugging logger which dumps output only yo C<STDERR>
65 Draw progress bar on STDERR.
67 $webpac->progress_bar($current, $max);
76 my $log = $self->_get_logger();
78 $self->{last_pcnt_t} ||= time();
80 $log->logconfess("no current value!") if (! $curr);
81 $log->logconfess("no maximum value!") if (! $max);
85 $log->debug("overflow to $curr");
88 $self->{'last_pcnt'} ||= 1;
89 $self->{'start_t'} ||= time();
91 my $p = int($curr * 100 / $max) || 1;
94 if ($p < $self->{'last_pcnt'}) {
95 $self->{'last_pcnt'} = $p;
96 $self->{'start_t'} = time();
101 if ($p != $self->{'last_pcnt'} || ( $t - $self->{last_pcnt_t} ) > 2 ) {
103 my $rate = ($curr / ($t - $self->{'start_t'} || 1));
104 my $eta = ($max-$curr) / ($rate || 1);
105 printf STDERR ("%5d [%-38s] %-5d %0.1f/s %s\r",$curr,"=" x ($p/3)."$p%>", $max, $rate, $self->fmt_time($eta));
106 $self->{'last_pcnt'} = $p;
107 $self->{'last_curr'} = $curr;
108 $self->{last_pcnt_t} = $t;
110 print STDERR "\n" if ($p == 100);
115 Format time (in seconds) for display.
117 print $webpac->fmt_time(time());
119 This method is called by L<progress_bar> to display remaining time.
129 my ($ss,$mm,$hh) = gmtime($t);
130 $out .= "${hh}h" if ($hh);
131 $out .= sprintf("%02d:%02d", $mm,$ss);
132 $out .= " " if ($hh == 0);
138 Fill in variable names by values
140 print $webpac->fill_in( 'foo = $foo bar = $bar',
141 foo => 42, bar => 11,
149 my $format = shift || die "no format?";
152 foreach my $n ( keys %$d ) {
153 $format =~ s/\$\Q$n\E/$d->{$n}/gs;
156 die "unknown variables in '$format' input data = ", dump( $d ) if ( $format =~ m/\$\w+/ );
167 my $path = $self->var_path('data_dir', 'data_file', ... );
177 # $abs_path = abs_path( $0 );
178 # $abs_path =~ s!/WebPAC/Common\.pm!!;
179 $abs_path = '/data/webpac2';
182 return File::Spec->catfile($abs_path, 'var', @_);
185 =head1 EXPORTED NETHODS
189 my @array = force_array( $ref, sub {
190 warn "reference is undefined!";
196 my ( $what, $error ) = @_;
198 if ( ref( $what ) eq 'ARRAY' ) {
199 @result = @{ $what };
200 } elsif ( defined $what ) {
203 $error->() if ref($error) eq 'CODE';
209 =head1 INTERNAL METHODS
211 Here is a quick list of internal methods, mostly useful to turn debugging
212 on them (see L<LOGGING> below for explanation).
218 Internal function to eval code without C<strict 'subs'>.
225 my $code = shift || return;
227 my $log = $self->_get_logger();
230 my $ret = eval $code;
232 $log->error("problem with eval code [$code]: $@");
235 $log->debug("eval: ",$code," [",$ret,"]");
237 return $ret || undef;
242 This function will init C<Log::Log4perl> using provided configuration file.
244 $webpac->_init_logger('/path/to/log.conf');
246 If no path to configuration file is given, dummy empty configuration
247 will be created. If any mode which inherits from this one is called
248 with C<debug> flag, it will turn logging to debug level.
250 This function will also read C<log_conf> value from current object and try
251 to read that as configuration file if it exists, if it doesn't it will
252 fallback to default C<conf/log.conf>.
254 You can disable all logging by adding C<no_log> to constructor of WebPAC
255 object. Object in C<Test::Exception> class will disable logging
263 $file ||= $self->{'log_conf'};
264 $file = 'conf/log.conf';
265 my $name = (caller(2))[3] || caller;
269 warn "# $name disabled logging\n" if $self->log_debug;
270 $Log::Log4perl::Config::CONFIG_INTEGRITY_CHECK = 0;
271 } elsif ($self->debug) {
272 $conf = << '_log4perl_';
274 log4perl.rootLogger=INFO, SCREEN
276 log4perl.logger.WebPAC.=DEBUG
278 log4perl.appender.SCREEN=Log::Log4perl::Appender::Screen
279 log4perl.appender.SCREEN.layout=PatternLayout
280 log4perl.appender.SCREEN.layout.ConversionPattern=%d %p> %F{1}:%L %M - %m%n
283 warn "# $name is using debug logger\n" if $self->log_debug;
284 } elsif ($name =~ m/Test::Exception/o) {
285 warn "# disabled logging for Text::Exception\n" if $self->log_debug;
287 warn "# $name is using $file logger\n" if $self->log_debug;
288 Log::Log4perl->init($file);
291 warn "# $name is using null logger\n" if $self->log_debug;
293 Log::Log4perl->init( \$conf );
301 Get C<Log::Log4perl> object with a twist: domains are defined for each
304 my $log = $webpac->_get_logger();
313 my $name = (caller(1))[3] || caller;
317 if ( $self->log_debug ) {
319 my $s = (caller($_))[3];
320 $f .= "#### $_ >> $s\n" if ($s);
324 $self->{'_logger_'} ||= $self->_init_logger;
326 my $log = get_logger( $name );
327 warn "# get_logger( $name ) level ", $log->level, "\n$f" if ($self->log_debug && !defined($_logger_seen->{$name}));
328 $_logger_seen->{$name}++;
335 Quick cludge to make logging object available to scripts which
336 use webpac line this:
338 my $log = _new WebPAC::Common()->_get_logger();
345 bless($self, $class);
347 $self ? return $self : return undef;
352 Logging in WebPAC is performed by L<Log::Log4perl> with config file
355 Methods defined above have different levels of logging, so
356 it's descriptions will be useful to turn (mostry B<debug> logging) on
357 or off to see why WabPAC isn't perforing as you expect it (it might even
360 B<This is different from normal Log4perl behaviour>. To repeat, you can
361 also use method names, and not only classes (which are just few)