1 package WebPAC::Common;
6 use Log::Log4perl qw/get_logger :levels/;
7 use Time::HiRes qw/time/;
9 # If ture, enable logging debug
14 WebPAC::Common - internal methods called from other WebPAC modules
22 our $VERSION = '0.01';
26 This module defines common functions, and is used as base for other, more
33 Draw progress bar on STDERR.
35 $webpac->progress_bar($current, $max);
44 my $log = $self->_get_logger();
46 $log->logconfess("no current value!") if (! $curr);
47 $log->logconfess("no maximum value!") if (! $max);
51 $log->debug("overflow to $curr");
54 $self->{'last_pcnt'} ||= 1;
55 $self->{'start_t'} ||= time();
57 my $p = int($curr * 100 / $max) || 1;
60 if ($p < $self->{'last_pcnt'}) {
61 $self->{'last_pcnt'} = $p;
62 $self->{'start_t'} = time();
65 if ($p != $self->{'last_pcnt'}) {
68 my $rate = ($curr / ($t - $self->{'start_t'} || 1));
69 my $eta = ($max-$curr) / ($rate || 1);
70 printf STDERR ("%5d [%-38s] %-5d %0.1f/s %s\r",$curr,"=" x ($p/3)."$p%>", $max, $rate, $self->fmt_time($eta));
71 $self->{'last_pcnt'} = $p;
72 $self->{'last_curr'} = $curr;
74 print STDERR "\n" if ($p == 100);
79 Format time (in seconds) for display.
81 print $webpac->fmt_time(time());
83 This method is called by L<progress_bar> to display remaining time.
93 my ($ss,$mm,$hh) = gmtime($t);
94 $out .= "${hh}h" if ($hh);
95 $out .= sprintf("%02d:%02d", $mm,$ss);
96 $out .= " " if ($hh == 0);
104 =head1 INTERNAL METHODS
106 Here is a quick list of internal methods, mostly useful to turn debugging
107 on them (see L<LOGGING> below for explanation).
113 Internal function to eval code without C<strict 'subs'>.
120 my $code = shift || return;
122 my $log = $self->_get_logger();
125 my $ret = eval $code;
127 $log->error("problem with eval code [$code]: $@");
130 $log->debug("eval: ",$code," [",$ret,"]");
132 return $ret || undef;
137 This function will init C<Log::Log4perl> using provided configuration file.
139 $webpac->_init_logger('/path/to/log.conf');
141 If no path to configuration file is given, dummy empty configuration
142 will be created. If any mode which inherits from this one is called
143 with C<debug> flag, it will turn logging to debug level.
145 This function will also read C<log_conf> value from current object and try
146 to read that as configuration file if it exists, if it doesn't it will
147 fallback to default C<conf/log.conf>.
149 You can disable all logging by adding C<no_log> to constructor of WebPAC
150 object. Object in C<Test::Exception> class will disable logging
158 $file ||= $self->{'log_conf'};
159 $file = 'conf/log.conf';
160 my $name = (caller(2))[3] || caller;
163 if ($self->{'no_log'}) {
164 warn "# $name disabled logging\n" if ($log_debug);
165 } elsif ($self->{'debug'}) {
166 $conf = << '_log4perl_';
168 log4perl.rootLogger=INFO, SCREEN
170 log4perl.logger.WebPAC.=DEBUG
172 log4perl.appender.SCREEN=Log::Log4perl::Appender::Screen
173 log4perl.appender.SCREEN.layout=PatternLayout
174 log4perl.appender.SCREEN.layout.ConversionPattern=%d %p> %F{1}:%L %M - %m%n
177 warn "# $name is using debug logger\n" if ($log_debug);
178 } elsif ($name =~ m/Test::Exception/o) {
179 warn "# disabled logging for Text::Exception\n" if ($log_debug);
181 warn "# $name is using $file logger\n" if ($log_debug);
182 Log::Log4perl->init($file);
185 warn "# $name is using null logger\n" if ($log_debug);
187 Log::Log4perl->init( \$conf );
195 Get C<Log::Log4perl> object with a twist: domains are defined for each
198 my $log = $webpac->_get_logger();
205 my $name = (caller(2))[3] || caller;
206 $self->{'_logger_'} ||= $self->_init_logger;
208 warn "# get_logger( $name )\n" if ($log_debug);
210 return get_logger($name);
216 Logging in WebPAC is performed by L<Log::Log4perl> with config file
219 Methods defined above have different levels of logging, so
220 it's descriptions will be useful to turn (mostry B<debug> logging) on
221 or off to see why WabPAC isn't perforing as you expect it (it might even
224 B<This is different from normal Log4perl behaviour>. To repeat, you can
225 also use method names, and not only classes (which are just few)