1 package WebPAC::Common;
6 use Log::Log4perl qw/get_logger :levels/;
7 use Time::HiRes qw/time/;
8 use Data::Dump qw/dump/;
11 use base qw/Class::Accessor/;
12 __PACKAGE__->mk_accessors( qw/log_debug no_log debug/ );
16 WebPAC::Common - internal methods called from other WebPAC modules
24 our $VERSION = '0.05';
28 This module defines common functions, and is used as base for other, more
31 my $o = WebPAC::Common->new({
43 Generate additional debugging log on C<STDERR>
47 Disable all logging (useful for tests)
51 Use debugging logger which dumps output only yo C<STDERR>
60 Draw progress bar on STDERR.
62 $webpac->progress_bar($current, $max);
71 my $log = $self->_get_logger();
73 $self->{last_pcnt_t} ||= time();
75 $log->logconfess("no current value!") if (! $curr);
76 $log->logconfess("no maximum value!") if (! $max);
80 $log->debug("overflow to $curr");
83 $self->{'last_pcnt'} ||= 1;
84 $self->{'start_t'} ||= time();
86 my $p = int($curr * 100 / $max) || 1;
89 if ($p < $self->{'last_pcnt'}) {
90 $self->{'last_pcnt'} = $p;
91 $self->{'start_t'} = time();
96 if ($p != $self->{'last_pcnt'} || ( $t - $self->{last_pcnt_t} ) > 2 ) {
98 my $rate = ($curr / ($t - $self->{'start_t'} || 1));
99 my $eta = ($max-$curr) / ($rate || 1);
100 printf STDERR ("%5d [%-38s] %-5d %0.1f/s %s\r",$curr,"=" x ($p/3)."$p%>", $max, $rate, $self->fmt_time($eta));
101 $self->{'last_pcnt'} = $p;
102 $self->{'last_curr'} = $curr;
103 $self->{last_pcnt_t} = $t;
105 print STDERR "\n" if ($p == 100);
110 Format time (in seconds) for display.
112 print $webpac->fmt_time(time());
114 This method is called by L<progress_bar> to display remaining time.
124 my ($ss,$mm,$hh) = gmtime($t);
125 $out .= "${hh}h" if ($hh);
126 $out .= sprintf("%02d:%02d", $mm,$ss);
127 $out .= " " if ($hh == 0);
133 Fill in variable names by values
135 print $webpac->fill_in( 'foo = $foo bar = $bar',
136 foo => 42, bar => 11,
144 my $format = shift || die "no format?";
147 foreach my $n ( keys %$d ) {
148 $format =~ s/\$\Q$n\E/$d->{$n}/gs;
151 die "unknown variables in '$format' input data = ", dump( $d ) if ( $format =~ m/\$\w+/ );
162 my $path = $self->var_path('data_dir', 'data_file', ... );
169 return File::Spec->catfile('var', @_);
173 =head1 INTERNAL METHODS
175 Here is a quick list of internal methods, mostly useful to turn debugging
176 on them (see L<LOGGING> below for explanation).
182 Internal function to eval code without C<strict 'subs'>.
189 my $code = shift || return;
191 my $log = $self->_get_logger();
194 my $ret = eval $code;
196 $log->error("problem with eval code [$code]: $@");
199 $log->debug("eval: ",$code," [",$ret,"]");
201 return $ret || undef;
206 This function will init C<Log::Log4perl> using provided configuration file.
208 $webpac->_init_logger('/path/to/log.conf');
210 If no path to configuration file is given, dummy empty configuration
211 will be created. If any mode which inherits from this one is called
212 with C<debug> flag, it will turn logging to debug level.
214 This function will also read C<log_conf> value from current object and try
215 to read that as configuration file if it exists, if it doesn't it will
216 fallback to default C<conf/log.conf>.
218 You can disable all logging by adding C<no_log> to constructor of WebPAC
219 object. Object in C<Test::Exception> class will disable logging
227 $file ||= $self->{'log_conf'};
228 $file = 'conf/log.conf';
229 my $name = (caller(2))[3] || caller;
233 warn "# $name disabled logging\n" if $self->log_debug;
234 } elsif ($self->debug) {
235 $conf = << '_log4perl_';
237 log4perl.rootLogger=INFO, SCREEN
239 log4perl.logger.WebPAC.=DEBUG
241 log4perl.appender.SCREEN=Log::Log4perl::Appender::Screen
242 log4perl.appender.SCREEN.layout=PatternLayout
243 log4perl.appender.SCREEN.layout.ConversionPattern=%d %p> %F{1}:%L %M - %m%n
246 warn "# $name is using debug logger\n" if $self->log_debug;
247 } elsif ($name =~ m/Test::Exception/o) {
248 warn "# disabled logging for Text::Exception\n" if $self->log_debug;
250 warn "# $name is using $file logger\n" if $self->log_debug;
251 Log::Log4perl->init($file);
254 warn "# $name is using null logger\n" if $self->log_debug;
256 Log::Log4perl->init( \$conf );
264 Get C<Log::Log4perl> object with a twist: domains are defined for each
267 my $log = $webpac->_get_logger();
276 my $name = (caller(1))[3] || caller;
280 if ( $self->log_debug ) {
282 my $s = (caller($_))[3];
283 $f .= "#### $_ >> $s\n" if ($s);
287 $self->{'_logger_'} ||= $self->_init_logger;
289 my $log = get_logger( $name );
290 warn "# get_logger( $name ) level ", $log->level, "\n$f" if ($self->log_debug && !defined($_logger_seen->{$name}));
291 $_logger_seen->{$name}++;
298 Quick cludge to make logging object available to scripts which
299 use webpac line this:
301 my $log = _new WebPAC::Common()->_get_logger();
308 bless($self, $class);
310 $self ? return $self : return undef;
315 Logging in WebPAC is performed by L<Log::Log4perl> with config file
318 Methods defined above have different levels of logging, so
319 it's descriptions will be useful to turn (mostry B<debug> logging) on
320 or off to see why WabPAC isn't perforing as you expect it (it might even
323 B<This is different from normal Log4perl behaviour>. To repeat, you can
324 also use method names, and not only classes (which are just few)