/[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 15 - (show annotations)
Sun Jul 17 10:42:23 2005 UTC (18 years, 9 months ago) by dpavlin
File size: 3825 byte(s)
WebPAC::Common cleanup, most code moved to WebPAC::Normalize. Added
documentation about order of data mungling when normalising data.

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

  ViewVC Help
Powered by ViewVC 1.1.26