--- trunk/lib/WebPAC/Common.pm 2005/07/16 14:44:38 6 +++ trunk/lib/WebPAC/Common.pm 2005/07/16 23:56:14 13 @@ -17,11 +17,6 @@ our $VERSION = '0.01'; -#my $LOOKUP_REGEX = '\[[^\[\]]+\]'; -#my $LOOKUP_REGEX_SAVE = '\[([^\[\]]+)\]'; -my $LOOKUP_REGEX = 'lookup{[^\{\}]+}'; -my $LOOKUP_REGEX_SAVE = 'lookup{([^\{\}]+)}'; - =head1 SYNOPSYS This module defines common functions, and is used as base for other, more @@ -109,8 +104,7 @@ $log->debug("filter result: $format"); } # do we have lookups? - if ($format =~ /$LOOKUP_REGEX/o) { - $log->debug("format '$format' has lookup"); + if ($self->{'lookup'}) { return $self->lookup($format); } else { return $format; @@ -171,74 +165,108 @@ } -=head1 INTERNAL METHODS +=head2 progress_bar -Here is a quick list of internal methods, mostly useful to turn debugging -on them (see L below for explanation). +Draw progress bar on STDERR. -=cut - -=head2 _eval - -Internal function to eval code without C. + $webpac->progress_bar($current, $max); =cut -sub _eval { +sub progress_bar { my $self = shift; - my $code = shift || return; + my ($curr,$max) = @_; my $log = $self->_get_logger(); - no strict 'subs'; - my $ret = eval $code; - if ($@) { - $log->error("problem with eval code [$code]: $@"); + $log->logconfess("no current value!") if (! $curr); + $log->logconfess("no maximum value!") if (! $max); + + if ($curr > $max) { + $max = $curr; + $log->debug("overflow to $curr"); } - $log->debug("eval: ",$code," [",$ret,"]"); + $self->{'last_pcnt'} ||= 1; + $self->{'start_t'} ||= time(); - return $ret || undef; + my $p = int($curr * 100 / $max) || 1; + + # reset on re-run + if ($p < $self->{'last_pcnt'}) { + $self->{'last_pcnt'} = $p; + $self->{'start_t'} = time(); + } + + if ($p != $self->{'last_pcnt'}) { + + my $t = time(); + my $rate = ($curr / ($t - $self->{'start_t'} || 1)); + my $eta = ($max-$curr) / ($rate || 1); + printf STDERR ("%5d [%-38s] %-5d %0.1f/s %s\r",$curr,"=" x ($p/3)."$p%>", $max, $rate, $self->fmt_time($eta)); + $self->{'last_pcnt'} = $p; + $self->{'last_curr'} = $curr; + } + print STDERR "\n" if ($p == 100); } -=head2 _sort_by_order +=head2 fmt_time -Sort xml tags data structure accoding to C attribute. +Format time (in seconds) for display. + + print $webpac->fmt_time(time()); + +This method is called by L to display remaining time. =cut -sub _sort_by_order { +sub fmt_time { my $self = shift; - my $va = $self->{'import_xml'}->{'indexer'}->{$a}->{'order'} || - $self->{'import_xml'}->{'indexer'}->{$a}; - my $vb = $self->{'import_xml'}->{'indexer'}->{$b}->{'order'} || - $self->{'import_xml'}->{'indexer'}->{$b}; + my $t = shift || 0; + my $out = ""; - return $va <=> $vb; + my ($ss,$mm,$hh) = gmtime($t); + $out .= "${hh}h" if ($hh); + $out .= sprintf("%02d:%02d", $mm,$ss); + $out .= " " if ($hh == 0); + return $out; } -=head2 _x +# +# +# -Convert string from UTF-8 to code page defined in C. +=head1 INTERNAL METHODS - my $text = $webpac->_x('utf8 text'); +Here is a quick list of internal methods, mostly useful to turn debugging +on them (see L below for explanation). + +=cut -Default application code page is C. You will probably want to -change that when creating new instance of object based on this one. +=head2 _eval + +Internal function to eval code without C. =cut -sub _x { +sub _eval { my $self = shift; - my $utf8 = shift || return; - # create UTF-8 convertor for import_xml files - $self->{'utf2cp'} ||= Text::Iconv->new('UTF-8' ,$self->{'code_page'} || 'ISO-8859-2'); + my $code = shift || return; + + my $log = $self->_get_logger(); - return $self->{'utf2cp'}->convert($utf8) || - $self->_get_logger()->logwarn("can't convert '$utf8'"); + no strict 'subs'; + my $ret = eval $code; + if ($@) { + $log->error("problem with eval code [$code]: $@"); + } + + $log->debug("eval: ",$code," [",$ret,"]"); + + return $ret || undef; } =head2 _init_logger @@ -248,7 +276,8 @@ $webpac->_init_logger('/path/to/log.conf'); If no path to configuration file is given, dummy empty configuration -will be create. +will be created. If any mode which inherits from this one is called +with C flag, it will turn logging to debug level. =cut @@ -259,6 +288,19 @@ Log::Log4perl->init($file); } else { my $conf = q( ); + if ($self->{'debug'}) { + $conf = << '_log4perl_'; + +log4perl.rootLogger=INFO, SCREEN + +log4perl.logger.WebPAC.=DEBUG + +log4perl.appender.SCREEN=Log::Log4perl::Appender::Screen +log4perl.appender.SCREEN.layout=PatternLayout +log4perl.appender.SCREEN.layout.ConversionPattern=%d %p> %F{1}:%L %M - %m%n + +_log4perl_ + } Log::Log4perl->init( \$conf ); } }