/[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 31 - (show annotations)
Sun Jul 24 15:03:11 2005 UTC (18 years, 8 months ago) by dpavlin
File size: 4745 byte(s)
re-worked logging, added no_log option to disable logging

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, if it doesn't it will
147 fallback to default C<conf/log.conf>.
148
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
151 automatically.
152
153 =cut
154
155 sub _init_logger {
156 my $self = shift;
157 my $file = shift;
158 $file ||= $self->{'log_conf'};
159 $file = 'conf/log.conf';
160 my $name = (caller(2))[3] || caller;
161
162 my $conf = q( );
163 if ($self->{'no_log'}) {
164 warn "# $name disabled logging\n" if ($log_debug);
165 } elsif ($self->{'debug'}) {
166 $conf = << '_log4perl_';
167
168 log4perl.rootLogger=INFO, SCREEN
169
170 log4perl.logger.WebPAC.=DEBUG
171
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
175
176 _log4perl_
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";
180 } elsif ($file) {
181 warn "# $name is using $file logger\n" if ($log_debug);
182 Log::Log4perl->init($file);
183 } else {
184 warn "# $name is using null logger\n" if ($log_debug);
185 }
186 Log::Log4perl->init( \$conf );
187
188 return 1;
189 }
190
191
192 =head2 _get_logger
193
194 Get C<Log::Log4perl> object with a twist: domains are defined for each
195 method
196
197 my $log = $webpac->_get_logger();
198
199 =cut
200
201 sub _get_logger {
202 my $self = shift;
203
204 my $name = (caller(2))[3] || caller;
205 $self->{'_logger_'} ||= $self->_init_logger;
206
207 warn "# get_logger( $name )\n" if ($log_debug);
208
209 return get_logger($name);
210 }
211
212
213 =head1 LOGGING
214
215 Logging in WebPAC is performed by L<Log::Log4perl> with config file
216 C<log.conf>.
217
218 Methods defined above have different levels of logging, so
219 it's descriptions will be useful to turn (mostry B<debug> logging) on
220 or off to see why WabPAC isn't perforing as you expect it (it might even
221 be a bug!).
222
223 B<This is different from normal Log4perl behaviour>. To repeat, you can
224 also use method names, and not only classes (which are just few)
225 to filter logging.
226
227

  ViewVC Help
Powered by ViewVC 1.1.26