blib/lib/Message.pm untested

filter: Statementstmt 0.0% 0 / 149 Branchbran 0.0% 0 / 106 Conditioncond 0.0% 0 / 51 MC/DCmcdc 0.0% 0 / 34 Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 357 CC 54 SCAR 58.3
CC
54
Cov
0%
echo_msg
600.0
disp_param
342.0
set_param
110.0
CRAP 339.7
1 package CGI::AppBuilder::Message;
2  
3 use warnings;
4 use Carp;
5 use IO::File;
6 use POSIX qw(strftime);
7 use CGI::AppBuilder;
8  
9 # require Exporter;
10 @ISA = qw(Exporter CGI::AppBuilder);
11 # @ISA = qw(Exporter);
12 our @EXPORT = qw();
13 our @EXPORT_OK = qw(disp_param set_param echo_msg debug_level echoMSG
14 debug get_params
15 );
16 our %EXPORT_TAGS = (
17 all => [@EXPORT_OK],
18 echo_msg => [qw(disp_param echo_msg debug_level)],
19 );
20 our $VERSION = 1.0001;
21  
22 =head1 NAME
23  
24 CGI::AppBuilder::Message - Display debug messages based on levels
25  
26 =head1 SYNOPSIS
27  
28 my $self = bless {}, "main";
29 use CGI::AppBuilder::Message;
30 $self->debug_level(2); # set debug level to 2
31 # The level 3 message will not be displayed
32 $self->echo_msg("This is level 1 message.", 1);
33 $self->echo_msg("This is level 2 message.", 2);
34 $self->echo_msg("This is level 3 message.", 3);
35  
36 =head1 DESCRIPTION
37  
38 The package contains the modules can be used for debuging or displaying
39 contents of your runtime state. You would first define the level of
40 each message in your program, then define a debug level that you would
41 like to see in your runtime.
42  
43 =head2 new (ifn => 'file.cfg', opt => 'hvS:')
44  
45 This is a inherited method from CGI::AppBuilder. See the same method
46 in CGI::AppBuilder for more details.
47  
48 =cut
49  
50 sub new {
51 my ($s, %args) = @_;
52 return $s->SUPER::new(%args);
53 }
54  
55 =head2 debug_level($n)
56  
57 Input variables:
58  
59 $n - a number between 0 and 100. It specifies the
60 level of messages that you would like to
61 display. The higher the number, the more
62 detailed messages that you will get.
63  
64 Variables used or routines called: None.
65  
66 How to use:
67  
68 $self->debug_level(2); # set the message level to 2
69 print $self->debug_level; # print current message level
70  
71 Return: the debug level or set the debug level.
72  
73 =cut
74  
75 *debug = \&CGI::AppBuilder::Message::debug_level;
76  
77 sub debug_level {
78 # my ($c_pkg,$c_fn,$c_ln) = caller;
79 # my $s = ref($_[0])?shift:(bless {}, $c_pkg);
80 my $s = shift;
81 croak "ERR: Too many args to debug_level." if @_ > 1;
82 @_ ? ($s->{_debug_level}=shift) : return $s->{_debug_level};
83 }
84  
85 =head2 echo_msg($msg, $lvl, $fh)
86  
87 Input variables:
88  
89 $msg - the message to be displayed. No newline
90 is needed in the end of the message. It
91 will add the newline code at the end of
92 the message.
93 $lvl - the message level is assigned to the message.
94 If it is higher than the debug level, then
95 the message will not be displayed.
96 $fh - file handler, or set the file hanlder in this parameter
97 $ENV{FH_DEBUG_LOG}
98  
99 Variables used or routines called:
100  
101 debug_level - get debug level.
102  
103 How to use:
104  
105 # default msg level to 0
106 $self->echo_msg('This is a test");
107 # set the msg level to 2
108 $self->echo_msg('This is a test", 2);
109  
110 Return: None.
111  
112 This method will display message or a hash array based on
113 I<debug_level> level. If I<debug_level> is set to '0', no message
114 or array will be displayed. If I<debug_level> is set to '2', it
115 will only display the message level ($lvl) is less than or equal
116 to '2'. If you call this method without providing a message level,
117 the message level ($lvl) is default to '0'. Of course, if no message
118 is provided to the method, it will be quietly returned.
119  
120 This is how you can call I<echo_msg>:
121  
122 my $df = CGI::AppBuilder::Message->new;
123 $df->echo_msg("This is a test"); # default the msg to level 0
124 $df->echo_msg("This is a test",1); # assign the msg as level 1 msg
125 $df->echo_msg("Test again",2); # assign the msg as level 2 msg
126 $df->echo_msg($hrf,1); # assign $hrf as level 1 msg
127 $df->echo_msg($hrf,2); # assign $hrf as level 2 msg
128  
129 If I<debug_level> is set to '1', all the messages with default message
130 level, i.e., 0, and '1' will be displayed. The higher level messages
131 will not be displayed.
132  
133 This method displays or writes the message based on debug level.
134 The filehandler is provided through $fh or $ENV{FH_DEBUG_LOG}, and
135 the outputs are written to the file.
136  
137 =cut
138  
139 *echoMSG = \&CGI::AppBuilder::Message::echo_msg;
140  
141 sub echo_msg {
142 # my ($c_pkg,$c_fn,$c_ln) = caller;
143 # my $self = ref($_[0])?shift:(bless {},$c_pkg);
144 my $self = shift;
145 my ($msg,$lvl, $fh) = @_;
146 $fh = (exists $ENV{FH_DEBUG_LOG})?$ENV{FH_DEBUG_LOG}:"";
147 $fh = "" if !$fh || ($fh && ref($fh) !~ /(IO::File|GLOB)/);
148 if (!defined($msg)) { return; } # return if no msg
149 if (!defined($lvl)) { $lvl = 0; } # default level to 0
150 my $class = ref($self)||$self; # get class name
151 my $dbg = $self->debug_level; # get debug level
152 if (!$dbg) { return; } # return if not debug
153 my $ref = ref($msg);
154 if ($ref eq $class || $ref =~ /(ARRAY|HASH)/) {
155 if ($lvl <= $dbg) { $self->disp_param($msg); }
156 return;
157 }
158 my $wbf = (exists $ENV{QUERY_STRING}||exists $ENV{HTTP_HOST})?1:0;
159 $msg =~ s/\/(\w+)\@/\/****\@/g if $msg =~ /(\w+)\/(\w+)\@(\w+)/;
160 my $f1 = "<h2>%s</h2>";
161 my $f2 = "<font color=%s>%s</font>";
162 my $f3 = "<pre>\n%s</pre>";
163 if ($wbf) {
164 $msg = sprintf $f1, $msg if $msg =~ /^\s*\d+\.\s+\w+/;
165 $msg = sprintf $f2, 'red', $msg if $msg =~ /^ERR:/i;
166 $msg = sprintf $f2, 'orange', $msg if $msg =~ /^WARN:/i;
167 $msg = sprintf $f2, 'cyan', $msg if $msg =~ /^INFO:/i;
168 $msg = sprintf $f2, 'blue', $msg if $msg =~ /^CMD:/i;
169 $msg = sprintf $f3, $msg if $msg =~ /^CODE:/i;
170 $msg .= "<br>" if $msg !~ /^\s*\d+\.\s+\w+/;
171 }
172 $msg = "$msg\n";
173 # $msg =~ s/\n/<br>\n/gm if $wbf;
174 if ($lvl <= $dbg) {
175 if (defined wantarray) { # assign to a variable
176 return $msg; # return the value
177 } else { # print the msg
178 if ($fh) { print $fh $msg; } else { print $msg; }
179 }
180 }
181 if (exists $self->{write_log} && $self->{write_log}) {
182 my $fh2 = (exists $self->{fh_dtl}) ? $self->{fh_dtl} : "";
183 return if ! $fh2;
184 print $fh2 "$msg";
185 }
186 }
187  
188 =head2 disp_param($arf,$lzp, $fh)
189  
190 Input variables:
191  
192 $arf - array reference
193 $lzp - number of blank space indented in left
194 $fh - file handler
195  
196 Variables used or routines called:
197  
198 echo_msg - print debug messages
199 debug_level - set debug level
200 disp_param - recusively called
201  
202 How to use:
203  
204 use CGI::AppBuilder::Message qw(:echo_msg);
205 my $self= bless {}, "main";
206 $self->disp_param($arf);
207  
208 Return: Display the content of the array.
209  
210 This method recursively displays the contents of an array. If a
211 filehandler is provided through $fh or $ENV{FH_DEBUG_LOG}, the outputs
212 are written to the file.
213  
214 =cut
215  
216 sub disp_param {
217 my ($self, $hrf, $lzp, $fh) = @_;
218  
219 $self->echo_msg(" -- displaying parameters...",3);
220 $fh = (exists $ENV{FH_DEBUG_LOG})?$ENV{FH_DEBUG_LOG}:"";
221 $fh = "" if !$fh || ($fh && ref($fh) !~ /(IO::File|GLOB)/);
222 if (!$lzp) { $lzp = 15; } else { $lzp +=4; }
223 my $fmt;
224 if (exists $ENV{QUERY_STRING}) {
225 # $fmt = "%${lzp}s = %-30s<br>\n";
226 $fmt = ("&nbsp;" x $lzp) . "%s = %-30s<br>\n";
227 } else {
228 $fmt = "%${lzp}s = %-30s\n";
229 }
230 if (!$hrf) {
231 print "Please specify an array ref.\n";
232 return;
233 }
234 # print join "|", $self, "HRF", $hrf, ref($hrf), "\n";
235  
236 my ($v);
237 if (ref($hrf) eq 'HASH'|| $hrf =~ /.*=HASH/) {
238 foreach my $k (sort keys %$hrf) {
239 if (!defined(${$hrf}{$k})) { $v = "";
240 } else { $v = $hrf->{$k}; }
241 if ($v =~ /([-\w_]+)\/(\w+)\@(\w+)/) {
242 $v =~ s{(\w+)/(\w+)\@}{$1/\*\*\*\@}g;
243 }
244 chomp $v;
245 if ($fh) { printf $fh $fmt, $k, $v;
246 } else { printf $fmt, $k, $v; }
247 if (ref($v) =~ /^(HASH|ARRAY)$/ ||
248 $v =~ /.*=(HASH|ARRAY)/) {
249 my $db1 = $self->debug_level;
250 $self->debug_level(0);
251 # print "$k = ${$hrf}{$k}: @{${$hrf}{$k}}\n";
252 $self->disp_param(${$hrf}{$k},$lzp);
253 $self->debug_level($db1);
254 if ($fh) { print $fh "\n"; } else { print "\n"; }
255 }
256 }
257 } elsif (ref($hrf) eq 'ARRAY' || $hrf =~ /.*=ARRAY/) {
258 foreach my $i (0..$#{$hrf}) {
259 if (!defined(${$hrf}[$i])) { $v = "";
260 } else { $v = ${$hrf}[$i]; }
261 if ($v =~ /([-\w_]+)\/(\w+)\@(\w+)/) {
262 $v =~ s{(\w+)/(\w+)\@}{$1/\*\*\*\@}g;
263 }
264 chomp $v;
265 if ($fh) { printf $fh $fmt, $i, $v;
266 } else { printf $fmt, $i, $v; }
267 if (ref($v) =~ /^(HASH|ARRAY)$/ ||
268 $v =~ /.*=(HASH|ARRAY)/) {
269 my $db1 = $self->debug_level;
270 $self->debug_level(0);
271 $self->disp_param(${$hrf}[$i],$lzp);
272 $self->debug_level($db1);
273 if ($fh) { print $fh "\n"; } else { print "\n"; }
274 }
275 }
276 }
277 }
278  
279 =head2 set_param($var, $ar[,$val])
280  
281 Input variables:
282  
283 $var - variable name
284 $ar - parameter hash or array ref
285 $val - value to be added or assigned
286  
287 Variables used or routines called:
288  
289 None
290  
291 How to use:
292  
293 use CGI::AppBuilder::Message qw(set_param);
294 my $ar = {a=>1,b=>25};
295 my $br = [1,2,5,10];
296 # for hash ref
297 my $va = $self->set_param('a',$ar); # set $va = 1
298 my $v1 = $self->set_param('v1',$ar); # set $v1 = ""
299 my $v2 = $self->set_param('b',$ar); # set $v2 = 25
300 # for array ref
301 my $v3 = $self->set_param(0,$br); # set $v3 = 1
302 my $v4 = $self->set_param(3,$br); # set $v4 = 10
303 # add or assign values and return array ref
304 $self->set_param('c',$ar,30); # set $ar->{c} = 30
305 $self->set_param(5,$br,50); # set $br->[5] = 50
306  
307 Return: $r - the value in the hash or empty string or array ref.
308  
309 =cut
310  
311 sub set_param {
312 my ($s, $v, $r) = @_;
313 # return blank if no $v or $r is not array, hash nor object
314 return "" if $v =~ /^\s*$/ || ! ref($r);
315 if ($#_>2) { # there is a third input
316 $r->[$v] = $_[3] if $v =~ /^\d+$/ && ref($r) =~ /ARRAY/;
317 $r->{$v} = $_[3] if ref($r) =~ /HASH/ && ref $r;
318 return ;
319 }
320 return "" if $v !~ /^\d+$/ && ref($r) =~ /ARRAY/;
321 return (exists $r->[$v])?$r->[$v]:"" if ref($r) =~ /^ARRAY/;
322 # if $r = $s, then ref $r will make it sure to catch that as well
323 return (exists $r->{$v})?$r->{$v}:"" if ref($r) =~ /^HASH/||ref $r;
324 return ""; # catch all
325 }
326  
327 =head2 get_params($vs, $ar)
328  
329 Input variables:
330  
331 $vs - a list of variable names separated by comma
332 $ar - parameter hash or array ref
333  
334 Variables used or routines called:
335  
336 set_param - get individual parameter
337  
338 How to use:
339  
340 use CGI::AppBuilder::Message;
341 my $ar = {a=>1,b=>25};
342 my ($va, $vb) = $self->get_params('a,b',$ar);
343  
344 Return: array or array ref
345  
346 This method gets multiple values for listed variables.
347  
348 =cut
349  
350 sub get_params {
351 my $s = shift;
352 my ($vs, $r) = @_;
353 return () if ! $vs;
354 my $p = [];
355 $vs =~ s/\s+//g; # remove any spaces
356 foreach my $k (split /,/, $vs) {
357 push @$p, $s->set_param($k, $r);
358 }
359 return wantarray ? @$p : $p;
360 }
361  
362  
363 1;
364  
365 =head1 CODING HISTORY
366  
367 =over 4
368  
369 =item * Version 0.10
370  
371 Extracted methods debug_level, echo_msg, disp_param, and set_param
372 from Debug::EchoMessage.
373  
374 =item * Version 0.11
375  
376 Some minor changes to echo_msg.
377  
378 =item * Version 0.12
379  
380 Added get_params method.
381  
382 =item * Version 1.0001
383  
384 Bring the version to 1.0001 after 10 years!
385  
386 =back
387  
388 =head1 FUTURE IMPLEMENTATION
389  
390 =over 4
391  
392 =item * no plan yet
393  
394 =back
395  
396 =head1 AUTHOR
397  
398 Copyright (c) 2004 Hanming Tu. All rights reserved.
399  
400 This package is free software and is provided "as is" without express
401 or implied warranty. It may be used, redistributed and/or modified
402 under the terms of the Perl Artistic License (see
403 http://www.perl.com/perl/misc/Artistic.html)
404  
405 =cut
406