/[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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 16 - (show annotations)
Sun Jul 17 11:37:07 2005 UTC (18 years, 10 months ago) by dpavlin
File size: 3851 byte(s)
Added Output::WebPAC::TT to produce Template Toolkit output.

1 package WebPAC::Common;
2
3 use warnings;
4 use strict;
5
6 use Log::Log4perl qw/get_logger :levels/;
7 use Time::HiRes qw/time/;
8
9 =head1 NAME
10
11 WebPAC::Common - internal methods called from other WebPAC modules
12
13 =head1 VERSION
14
15 Version 0.01
16
17 =cut
18
19 our $VERSION = '0.01';
20
21 =head1 SYNOPSYS
22
23 This module defines common functions, and is used as base for other, more
24 specific modules.
25
26 =head1 FUNCTIONS
27
28 =head2 progress_bar
29
30 Draw progress bar on STDERR.
31
32 $webpac->progress_bar($current, $max);
33
34 =cut
35
36 sub progress_bar {
37 my $self = shift;
38
39 my ($curr,$max) = @_;
40
41 my $log = $self->_get_logger();
42
43 $log->logconfess("no current value!") if (! $curr);
44 $log->logconfess("no maximum value!") if (! $max);
45
46 if ($curr > $max) {
47 $max = $curr;
48 $log->debug("overflow to $curr");
49 }
50
51 $self->{'last_pcnt'} ||= 1;
52 $self->{'start_t'} ||= time();
53
54 my $p = int($curr * 100 / $max) || 1;
55
56 # reset on re-run
57 if ($p < $self->{'last_pcnt'}) {
58 $self->{'last_pcnt'} = $p;
59 $self->{'start_t'} = time();
60 }
61
62 if ($p != $self->{'last_pcnt'}) {
63
64 my $t = time();
65 my $rate = ($curr / ($t - $self->{'start_t'} || 1));
66 my $eta = ($max-$curr) / ($rate || 1);
67 printf STDERR ("%5d [%-38s] %-5d %0.1f/s %s\r",$curr,"=" x ($p/3)."$p%>", $max, $rate, $self->fmt_time($eta));
68 $self->{'last_pcnt'} = $p;
69 $self->{'last_curr'} = $curr;
70 }
71 print STDERR "\n" if ($p == 100);
72 }
73
74 =head2 fmt_time
75
76 Format time (in seconds) for display.
77
78 print $webpac->fmt_time(time());
79
80 This method is called by L<progress_bar> to display remaining time.
81
82 =cut
83
84 sub fmt_time {
85 my $self = shift;
86
87 my $t = shift || 0;
88 my $out = "";
89
90 my ($ss,$mm,$hh) = gmtime($t);
91 $out .= "${hh}h" if ($hh);
92 $out .= sprintf("%02d:%02d", $mm,$ss);
93 $out .= " " if ($hh == 0);
94 return $out;
95 }
96
97 #
98 #
99 #
100
101 =head1 INTERNAL METHODS
102
103 Here is a quick list of internal methods, mostly useful to turn debugging
104 on them (see L<LOGGING> below for explanation).
105
106 =cut
107
108 =head2 _eval
109
110 Internal function to eval code without C<strict 'subs'>.
111
112 =cut
113
114 sub _eval {
115 my $self = shift;
116
117 my $code = shift || return;
118
119 my $log = $self->_get_logger();
120
121 no strict 'subs';
122 my $ret = eval $code;
123 if ($@) {
124 $log->error("problem with eval code [$code]: $@");
125 }
126
127 $log->debug("eval: ",$code," [",$ret,"]");
128
129 return $ret || undef;
130 }
131
132 =head2 _init_logger
133
134 This function will init C<Log::Log4perl> using provided configuration file.
135
136 $webpac->_init_logger('/path/to/log.conf');
137
138 If no path to configuration file is given, dummy empty configuration
139 will be created. If any mode which inherits from this one is called
140 with C<debug> flag, it will turn logging to debug level.
141
142 =cut
143
144 sub _init_logger {
145 my $self = shift;
146 my $file = shift;
147 if ($file) {
148 Log::Log4perl->init($file);
149 } else {
150 my $conf = q( );
151 if ($self->{'debug'}) {
152 $conf = << '_log4perl_';
153
154 log4perl.rootLogger=INFO, SCREEN
155
156 log4perl.logger.WebPAC.=DEBUG
157
158 log4perl.appender.SCREEN=Log::Log4perl::Appender::Screen
159 log4perl.appender.SCREEN.layout=PatternLayout
160 log4perl.appender.SCREEN.layout.ConversionPattern=%d %p> %F{1}:%L %M - %m%n
161
162 _log4perl_
163 }
164 Log::Log4perl->init( \$conf );
165 }
166 }
167
168
169 =head2 _get_logger
170
171 Get C<Log::Log4perl> object with a twist: domains are defined for each
172 method
173
174 my $log = $webpac->_get_logger();
175
176 =cut
177
178 sub _get_logger {
179 my $self = shift;
180
181 $self->{'_logger_ok'} ||= $self->_init_logger;
182
183 my $name = (caller(1))[3] || caller;
184 return get_logger($name);
185 }
186
187
188 =head1 LOGGING
189
190 Logging in WebPAC is performed by L<Log::Log4perl> with config file
191 C<log.conf>.
192
193 Methods defined above have different levels of logging, so
194 it's descriptions will be useful to turn (mostry B<debug> logging) on
195 or off to see why WabPAC isn't perforing as you expect it (it might even
196 be a bug!).
197
198 B<This is different from normal Log4perl behaviour>. To repeat, you can
199 also use method names, and not only classes (which are just few)
200 to filter logging.
201
202

  ViewVC Help
Powered by ViewVC 1.1.26