File Coverage

CGI/AppBuilder/Message.pm
Criterion Covered Total %
statement 13 15 86.6
branch n/a
condition n/a
subroutine 5 5 100.0
pod n/a
total 18 20 90.0


line stmt bran cond sub pod time code
1             package CGI::AppBuilder::Message;
2              
3 1     1   23697 use warnings;
  1         2  
  1         33  
4 1     1   5 use Carp;
  1         2  
  1         98  
5 1     1   1006 use IO::File;
  1         11635  
  1         165  
6 1     1   1068 use POSIX qw(strftime);
  1         6321  
  1         6  
7 1     1   3211 use CGI::AppBuilder;
  0            
  0            
8              
9             # require Exporter;
10             @ISA = qw(Exporter CGI::AppBuilder);
11             our @EXPORT = qw();
12             our @EXPORT_OK = qw(disp_param set_param echo_msg debug_level echoMSG
13             debug
14             );
15             our %EXPORT_TAGS = (
16             all => [@EXPORT_OK],
17             echo_msg => [qw(disp_param echo_msg debug_level)],
18             );
19             $CGI::AppBuilder::Message::VERSION = 0.12;
20              
21             =head1 NAME
22              
23             CGI::AppBuilder::Message - Display debug messages based on levels
24              
25             =head1 SYNOPSIS
26              
27             my $self = bless {}, "main";
28             use CGI::AppBuilder::Message;
29             $self->debug_level(2); # set debug level to 2
30             # The level 3 message will not be displayed
31             $self->echo_msg("This is level 1 message.", 1);
32             $self->echo_msg("This is level 2 message.", 2);
33             $self->echo_msg("This is level 3 message.", 3);
34              
35             =head1 DESCRIPTION
36              
37             The package contains the modules can be used for debuging or displaying
38             contents of your runtime state. You would first define the level of
39             each message in your program, then define a debug level that you would
40             like to see in your runtime.
41              
42             =head2 new (ifn => 'file.cfg', opt => 'hvS:')
43              
44             This is a inherited method from CGI::AppBuilder. See the same method
45             in CGI::AppBuilder for more details.
46              
47             =cut
48              
49             sub new {
50             my ($s, %args) = @_;
51             return $s->SUPER::new(%args);
52             }
53              
54             =head2 debug_level($n)
55              
56             Input variables:
57              
58             $n - a number between 0 and 100. It specifies the
59             level of messages that you would like to
60             display. The higher the number, the more
61             detailed messages that you will get.
62              
63             Variables used or routines called: None.
64              
65             How to use:
66              
67             $self->debug_level(2); # set the message level to 2
68             print $self->debug_level; # print current message level
69              
70             Return: the debug level or set the debug level.
71              
72             =cut
73              
74             *debug = \&CGI::AppBuilder::Message::debug_level;
75              
76             sub debug_level {
77             # my ($c_pkg,$c_fn,$c_ln) = caller;
78             # my $s = ref($_[0])?shift:(bless {}, $c_pkg);
79             my $s = shift;
80             croak "ERR: Too many args to debug_level." if @_ > 1;
81             @_ ? ($s->{_debug_level}=shift) : return $s->{_debug_level};
82             }
83              
84             =head2 echo_msg($msg, $lvl, $fh)
85              
86             Input variables:
87              
88             $msg - the message to be displayed. No newline
89             is needed in the end of the message. It
90             will add the newline code at the end of
91             the message.
92             $lvl - the message level is assigned to the message.
93             If it is higher than the debug level, then
94             the message will not be displayed.
95             $fh - file handler, or set the file hanlder in this parameter
96             $ENV{FH_DEBUG_LOG}
97              
98             Variables used or routines called:
99              
100             debug_level - get debug level.
101              
102             How to use:
103              
104             # default msg level to 0
105             $self->echo_msg('This is a test");
106             # set the msg level to 2
107             $self->echo_msg('This is a test", 2);
108              
109             Return: None.
110              
111             This method will display message or a hash array based on
112             I level. If I is set to '0', no message
113             or array will be displayed. If I is set to '2', it
114             will only display the message level ($lvl) is less than or equal
115             to '2'. If you call this method without providing a message level,
116             the message level ($lvl) is default to '0'. Of course, if no message
117             is provided to the method, it will be quietly returned.
118              
119             This is how you can call I:
120              
121             my $df = DataFax->new;
122             $df->echo_msg("This is a test"); # default the msg to level 0
123             $df->echo_msg("This is a test",1); # assign the msg as level 1 msg
124             $df->echo_msg("Test again",2); # assign the msg as level 2 msg
125             $df->echo_msg($hrf,1); # assign $hrf as level 1 msg
126             $df->echo_msg($hrf,2); # assign $hrf as level 2 msg
127              
128             If I is set to '1', all the messages with default message
129             level, i.e., 0, and '1' will be displayed. The higher level messages
130             will not be displayed.
131              
132             This method displays or writes the message based on debug level.
133             The filehandler is provided through $fh or $ENV{FH_DEBUG_LOG}, and
134             the outputs are written to the file.
135              
136             =cut
137              
138             *echoMSG = \&CGI::AppBuilder::Message::echo_msg;
139              
140             sub echo_msg {
141             # my ($c_pkg,$c_fn,$c_ln) = caller;
142             # my $self = ref($_[0])?shift:(bless {},$c_pkg);
143             my $self = shift;
144             my ($msg,$lvl, $fh) = @_;
145             $fh = (exists $ENV{FH_DEBUG_LOG})?$ENV{FH_DEBUG_LOG}:"";
146             $fh = "" if !$fh || ($fh && ref($fh) !~ /(IO::File|GLOB)/);
147             if (!defined($msg)) { return; } # return if no msg
148             if (!defined($lvl)) { $lvl = 0; } # default level to 0
149             my $class = ref($self)||$self; # get class name
150             my $dbg = $self->debug_level; # get debug level
151             if (!$dbg) { return; } # return if not debug
152             my $ref = ref($msg);
153             if ($ref eq $class || $ref =~ /(ARRAY|HASH)/) {
154             if ($lvl <= $dbg) { $self->disp_param($msg); }
155             return;
156             }
157             my $wbf = (exists $ENV{QUERY_STRING}||exists $ENV{HTTP_HOST})?1:0;
158             $msg = "

$msg

" if $wbf && $msg =~ /^\s*\d+\.\s+\w+/;
159             $msg =~ s/\/(\w+)\@/\/****\@/g if $msg =~ /(\w+)\/(\w+)\@(\w+)/;
160             $msg = "$msg\n";
161             $msg =~ s/\n/
\n/gm if $wbf;
162             if ($lvl <= $dbg) {
163             if ($fh) { print $fh $msg; } else { print $msg; }
164             }
165             }
166              
167             =head2 disp_param($arf,$lzp, $fh)
168              
169             Input variables:
170              
171             $arf - array reference
172             $lzp - number of blank space indented in left
173             $fh - file handler
174              
175             Variables used or routines called:
176              
177             echo_msg - print debug messages
178             debug_level - set debug level
179             disp_param - recusively called
180              
181             How to use:
182              
183             use CGI::AppBuilder::Message qw(:echo_msg);
184             my $self= bless {}, "main";
185             $self->disp_param($arf);
186              
187             Return: Display the content of the array.
188              
189             This method recursively displays the contents of an array. If a
190             filehandler is provided through $fh or $ENV{FH_DEBUG_LOG}, the outputs
191             are written to the file.
192              
193             =cut
194              
195             sub disp_param {
196             my ($self, $hrf, $lzp, $fh) = @_;
197             $self->echo_msg(" -- displaying parameters...");
198             $fh = (exists $ENV{FH_DEBUG_LOG})?$ENV{FH_DEBUG_LOG}:"";
199             $fh = "" if !$fh || ($fh && ref($fh) !~ /(IO::File|GLOB)/);
200             if (!$lzp) { $lzp = 15; } else { $lzp +=4; }
201             my $fmt;
202             if (exists $ENV{QUERY_STRING}) {
203             # $fmt = "%${lzp}s = %-30s
\n";
204             $fmt = (" " x $lzp) . "%s = %-30s
\n";
205             } else {
206             $fmt = "%${lzp}s = %-30s\n";
207             }
208             if (!$hrf) {
209             print "Please specify an array ref.\n";
210             return;
211             }
212             # print join "|", $self, "HRF", $hrf, ref($hrf), "\n";
213             my ($v);
214             if (ref($hrf) eq 'HASH'|| $hrf =~ /.*=HASH/) {
215             foreach my $k (sort keys %{$hrf}) {
216             if (!defined(${$hrf}{$k})) { $v = "";
217             } else { $v = ${$hrf}{$k}; }
218             if ($v =~ /([-\w_]+)\/(\w+)\@(\w+)/) {
219             $v =~ s{(\w+)/(\w+)\@}{$1/\*\*\*\@}g;
220             }
221             chomp $v;
222             if ($fh) { printf $fh $fmt, $k, $v;
223             } else { printf $fmt, $k, $v; }
224             if (ref($v) =~ /^(HASH|ARRAY)$/ ||
225             $v =~ /.*=(HASH|ARRAY)/) {
226             my $db1 = $self->debug_level;
227             $self->debug_level(0);
228             # print "$k = ${$hrf}{$k}: @{${$hrf}{$k}}\n";
229             $self->disp_param(${$hrf}{$k},$lzp);
230             $self->debug_level($db1);
231             if ($fh) { print $fh "\n"; } else { print "\n"; }
232             }
233             }
234             } elsif (ref($hrf) eq 'ARRAY' || $hrf =~ /.*=ARRAY/) {
235             foreach my $i (0..$#{$hrf}) {
236             if (!defined(${$hrf}[$i])) { $v = "";
237             } else { $v = ${$hrf}[$i]; }
238             if ($v =~ /([-\w_]+)\/(\w+)\@(\w+)/) {
239             $v =~ s{(\w+)/(\w+)\@}{$1/\*\*\*\@}g;
240             }
241             chomp $v;
242             if ($fh) { printf $fh $fmt, $i, $v;
243             } else { printf $fmt, $i, $v; }
244             if (ref($v) =~ /^(HASH|ARRAY)$/ ||
245             $v =~ /.*=(HASH|ARRAY)/) {
246             my $db1 = $self->debug_level;
247             $self->debug_level(0);
248             $self->disp_param(${$hrf}[$i],$lzp);
249             $self->debug_level($db1);
250             if ($fh) { print $fh "\n"; } else { print "\n"; }
251             }
252             }
253             }
254             }
255              
256             =head2 set_param($var, $ar[,$val])
257              
258             Input variables:
259              
260             $var - variable name
261             $ar - parameter hash or array ref
262             $val - value to be added or assigned
263              
264             Variables used or routines called:
265              
266             None
267              
268             How to use:
269              
270             use CGI::AppBuilder::Message qw(set_param);
271             my $ar = {a=>1,b=>25};
272             my $br = [1,2,5,10];
273             # for hash ref
274             my $va = $self->set_param('a',$ar); # set $va = 1
275             my $v1 = $self->set_param('v1',$ar); # set $v1 = ""
276             my $v2 = $self->set_param('b',$ar); # set $v2 = 25
277             # for array ref
278             my $v3 = $self->set_param(0,$br); # set $v3 = 1
279             my $v4 = $self->set_param(3,$br); # set $v4 = 10
280             # add or assign values and return array ref
281             $self->set_param('c',$ar,30); # set $ar->{c} = 30
282             $self->set_param(5,$br,50); # set $br->[5] = 50
283              
284             Return: $r - the value in the hash or empty string or array ref.
285              
286             =cut
287              
288             sub set_param {
289             my ($s, $v, $r) = @_;
290             # return blank if no $v or $r is not array, hash nor object
291             return "" if $v =~ /^\s*$/ || ! ref($r);
292             if ($#_>2) { # there is a third input
293             $r->[$v] = $_[3] if $v =~ /^\d+$/ && ref($r) =~ /ARRAY/;
294             $r->{$v} = $_[3] if ref($r) =~ /HASH/ || ref $r;
295             return ;
296             }
297             return "" if $v !~ /^\d+$/ && ref($r) =~ /ARRAY/;
298             return (exists $r->[$v])?$r->[$v]:"" if ref($r) =~ /^ARRAY/;
299             # if $r = $s, then ref $r will make it sure to catch that as well
300             return (exists $r->{$v})?$r->{$v}:"" if ref($r) =~ /^HASH/||ref $r;
301             return ""; # catch all
302             }
303              
304             1;
305              
306             =head1 CODING HISTORY
307              
308             =over 4
309              
310             =item * Version 0.10
311              
312             Extracted methods debug_level, echo_msg, disp_param, and set_param
313             from Debug::EchoMessage.
314              
315             =item * Version 0.11
316              
317             Some minor changes to echo_msg.
318              
319             =back
320              
321             =head1 FUTURE IMPLEMENTATION
322              
323             =over 4
324              
325             =item * no plan yet
326              
327             =back
328              
329             =head1 AUTHOR
330              
331             Copyright (c) 2004 Hanming Tu. All rights reserved.
332              
333             This package is free software and is provided "as is" without express
334             or implied warranty. It may be used, redistributed and/or modified
335             under the terms of the Perl Artistic License (see
336             http://www.perl.com/perl/misc/Artistic.html)
337              
338             =cut
339