/[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 29 - (show annotations)
Sun Jul 24 11:17:44 2005 UTC (18 years, 9 months ago) by dpavlin
File size: 4358 byte(s)
some logging improvements and sample configuration file

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

  ViewVC Help
Powered by ViewVC 1.1.26