| 1 | package Debug::EchoMessage; | ||
| 2 | |||
| 3 | use warnings; | ||
| 4 | use Carp; | ||
| 5 | use IO::File; | ||
| 6 | use POSIX qw(strftime); | ||
| 7 | |||
| 8 | # require Exporter; | ||
| 9 | @ISA = qw(Exporter); | ||
| 10 | our @EXPORT = qw(echoMSG debug disp_param); | ||
| 11 | our @EXPORT_OK = qw(debug echoMSG disp_param start_log end_log | ||
| 12 | ); | ||
| 13 | our %EXPORT_TAGS = ( | ||
| 14 | all => [@EXPORT_OK], | ||
| 15 | echo_msg => [qw(debug echoMSG disp_param)], | ||
| 16 | log => [qw(start_log end_log)], | ||
| 17 | ); | ||
| 18 | $Debug::EchoMessage::VERSION = 1.03; | ||
| 19 | |||
| 20 | =head1 NAME | ||
| 21 | |||
| 22 | Debug::EchoMessage - Display debug messages based on levels | ||
| 23 | |||
| 24 | =head1 SYNOPSIS | ||
| 25 | |||
| 26 | my $self = bless {}, "main"; | ||
| 27 | use Debug::EchoMessage; | ||
| 28 | $self->debug(2); # set debug level to 2 | ||
| 29 | # The level 3 message will not be displayed | ||
| 30 | $self->echoMSG("This is level 1 message.", 1); | ||
| 31 | $self->echoMSG("This is level 2 message.", 2); | ||
| 32 | $self->echoMSG("This is level 3 message.", 3); | ||
| 33 | |||
| 34 | =head1 DESCRIPTION | ||
| 35 | |||
| 36 | The package contains the modules can be used for debuging or displaying | ||
| 37 | contents of your runtime state. You would first define the level of | ||
| 38 | each message in your program, then define a debug level that you would | ||
| 39 | like to see in your runtime. | ||
| 40 | |||
| 41 | { # Encapsulated class data | ||
| 42 | _debug =>0, # debug level | ||
| 43 | } | ||
| 44 | |||
| 45 | =head2 debug($n) | ||
| 46 | |||
| 47 | Input variables: | ||
| 48 | |||
| 49 | $n - a number between 0 and 100. It specifies the | ||
| 50 | level of messages that you would like to | ||
| 51 | display. The higher the number, the more | ||
| 52 | detailed messages that you will get. | ||
| 53 | |||
| 54 | Variables used or routines called: None. | ||
| 55 | |||
| 56 | How to use: | ||
| 57 | |||
| 58 | $self->debug(2); # set the message level to 2 | ||
| 59 | print $self->debug; # print current message level | ||
| 60 | |||
| 61 | Return: the debug level or set the debug level. | ||
| 62 | |||
| 63 | =cut | ||
| 64 | |||
| 65 | sub debug { | ||
| 66 | # my ($c_pkg,$c_fn,$c_ln) = caller; | ||
| 67 | # my $s = ref($_[0])?shift:(bless {}, $c_pkg); | ||
| 68 | my $s = shift; | ||
| 69 | croak "ERR: Too many args to debug." if @_ > 1; | ||
| 70 | @_ ? ($s->{_debug}=shift) : return $s->{_debug}; | ||
| 71 | } | ||
| 72 | |||
| 73 | =head2 echoMSG($msg, $lvl, $fh) | ||
| 74 | |||
| 75 | Input variables: | ||
| 76 | |||
| 77 | $msg - the message to be displayed. No newline | ||
| 78 | is needed in the end of the message. It | ||
| 79 | will add the newline code at the end of | ||
| 80 | the message. | ||
| 81 | $lvl - the message level is assigned to the message. | ||
| 82 | If it is higher than the debug level, then | ||
| 83 | the message will not be displayed. | ||
| 84 | $fh - file handler, or set the file hanlder in this parameter | ||
| 85 | $ENV{FH_DEBUG_LOG} | ||
| 86 | |||
| 87 | Variables used or routines called: | ||
| 88 | |||
| 89 | debug - get debug level. | ||
| 90 | |||
| 91 | How to use: | ||
| 92 | |||
| 93 | # default msg level to 0 | ||
| 94 | $self->echoMSG('This is a test"); | ||
| 95 | # set the msg level to 2 | ||
| 96 | $self->echoMSG('This is a test", 2); | ||
| 97 | |||
| 98 | Return: None. | ||
| 99 | |||
| 100 | This method will display message or a hash array based on I<debug> | ||
| 101 | level. If I<debug> is set to '0', no message or array will be | ||
| 102 | displayed. If I<debug> is set to '2', it will only display the message | ||
| 103 | level ($lvl) is less than or equal to '2'. If you call this | ||
| 104 | method without providing a message level, the message level ($lvl) is | ||
| 105 | default to '0'. Of course, if no message is provided to the method, | ||
| 106 | it will be quietly returned. | ||
| 107 | |||
| 108 | This is how you can call I<echoMSG>: | ||
| 109 | |||
| 110 | my $df = DataFax->new; | ||
| 111 | $df->echoMSG("This is a test"); # default the msg to level 0 | ||
| 112 | $df->echoMSG("This is a test",1); # assign the msg as level 1 msg | ||
| 113 | $df->echoMSG("Test again",2); # assign the msg as level 2 msg | ||
| 114 | $df->echoMSG($hrf,1); # assign $hrf as level 1 msg | ||
| 115 | $df->echoMSG($hrf,2); # assign $hrf as level 2 msg | ||
| 116 | |||
| 117 | If I<debug> is set to '1', all the messages with default message level, | ||
| 118 | i.e., 0, and '1' will be displayed. The higher level messages | ||
| 119 | will not be displayed. | ||
| 120 | |||
| 121 | This method displays or writes the message based on debug level. | ||
| 122 | The filehandler is provided through $fh or $ENV{FH_DEBUG_LOG}, and | ||
| 123 | the outputs are written to the file. | ||
| 124 | |||
| 125 | =cut | ||
| 126 | |||
| 127 | sub echoMSG { | ||
| 128 | # my ($c_pkg,$c_fn,$c_ln) = caller; | ||
| 129 | # my $self = ref($_[0])?shift:(bless {},$c_pkg); | ||
| 130 | my $self = shift; | ||
| 131 | my ($msg,$lvl, $fh) = @_; | ||
| 132 | $fh = (exists $ENV{FH_DEBUG_LOG})?$ENV{FH_DEBUG_LOG}:""; | ||
| 133 | $fh = "" if !$fh || ($fh && ref($fh) !~ /(IO::File|GLOB)/); | ||
| 134 | if (!defined($msg)) { return; } # return if no msg | ||
| 135 | if (!defined($lvl)) { $lvl = 0; } # default level to 0 | ||
| 136 | my $class = ref($self)||$self; # get class name | ||
| 137 | my $dbg = $self->debug; # get debug level | ||
| 138 | if (!$dbg) { return; } # return if not debug | ||
| 139 | my $ref = ref($msg); | ||
| 140 | if ($ref eq $class) { | ||
| 141 | if ($lvl <= $dbg) { $self->disp_param($msg); } | ||
| 142 | } else { | ||
| 143 | $msg = "<h2>$msg</h2>" if exists $ENV{QUERY_STRING} && | ||
| 144 | $msg =~ /^\s*\d+\.\s+\w+/; | ||
| 145 | $msg = "$msg\n"; | ||
| 146 | $msg =~ s/\n/<br>\n/gm if exists $ENV{QUERY_STRING}; | ||
| 147 | if ($lvl <= $dbg) { | ||
| 148 | if ($fh) { print $fh $msg; } else { print $msg; } | ||
| 149 | } | ||
| 150 | } | ||
| 151 | } | ||
| 152 | |||
| 153 | =head2 disp_param($arf,$lzp, $fh) | ||
| 154 | |||
| 155 | Input variables: | ||
| 156 | |||
| 157 | $arf - array reference | ||
| 158 | $lzp - number of blank space indented in left | ||
| 159 | $fh - file handler | ||
| 160 | |||
| 161 | Variables used or routines called: | ||
| 162 | |||
| 163 | echoMSG - print debug messages | ||
| 164 | debug - set debug level | ||
| 165 | disp_param - recusively called | ||
| 166 | |||
| 167 | How to use: | ||
| 168 | |||
| 169 | use Debug::EchoMessage qw(:echo_msg); | ||
| 170 | my $self= bless {}, "main"; | ||
| 171 | $self->disp_param($arf); | ||
| 172 | |||
| 173 | Return: Display the content of the array. | ||
| 174 | |||
| 175 | This method recursively displays the contents of an array. If a | ||
| 176 | filehandler is provided through $fh or $ENV{FH_DEBUG_LOG}, the outputs | ||
| 177 | are written to the file. | ||
| 178 | |||
| 179 | =cut | ||
| 180 | |||
| 181 | sub disp_param { | ||
| 182 | my ($self, $hrf, $lzp, $fh) = @_; | ||
| 183 | $self->echoMSG(" -- displaying parameters..."); | ||
| 184 | $fh = (exists $ENV{FH_DEBUG_LOG})?$ENV{FH_DEBUG_LOG}:""; | ||
| 185 | $fh = "" if !$fh || ($fh && ref($fh) !~ /(IO::File|GLOB)/); | ||
| 186 | if (!$lzp) { $lzp = 15; } else { $lzp +=4; } | ||
| 187 | my $fmt; | ||
| 188 | if (exists $ENV{QUERY_STRING}) { | ||
| 189 | $fmt = "%${lzp}s = %-30s<br>\n"; | ||
| 190 | } else { | ||
| 191 | $fmt = "%${lzp}s = %-30s\n"; | ||
| 192 | } | ||
| 193 | if (!$hrf) { | ||
| 194 | print "Please specify an array ref.\n"; | ||
| 195 | return; | ||
| 196 | } | ||
| 197 | # print join "|", $self, "HRF", $hrf, ref($hrf), "\n"; | ||
| 198 | my ($v); | ||
| 199 | if (ref($hrf) eq 'HASH'|| $hrf =~ /.*=HASH/) { | ||
| 200 | foreach my $k (sort keys %{$hrf}) { | ||
| 201 | if (!defined(${$hrf}{$k})) { $v = ""; | ||
| 202 | } else { $v = ${$hrf}{$k}; } | ||
| 203 | if ($v =~ /([-\w_]+)\/(\w+)\@(\w+)/) { | ||
| 204 | $v =~ s{(\w+)/(\w+)\@}{$1/\*\*\*\@}; | ||
| 205 | } | ||
| 206 | if ($fh) { printf $fh $fmt, $k, $v; | ||
| 207 | } else { printf $fmt, $k, $v; } | ||
| 208 | if (ref($v) =~ /^(HASH|ARRAY)$/ || | ||
| 209 | $v =~ /.*=(HASH|ARRAY)/) { | ||
| 210 | my $db1 = $self->debug; | ||
| 211 | $self->debug(0); | ||
| 212 | # print "$k = ${$hrf}{$k}: @{${$hrf}{$k}}\n"; | ||
| 213 | $self->disp_param(${$hrf}{$k},$lzp); | ||
| 214 | $self->debug($db1); | ||
| 215 | if ($fh) { print $fh "\n"; } else { print "\n"; } | ||
| 216 | } | ||
| 217 | } | ||
| 218 | } elsif (ref($hrf) eq 'ARRAY' || $hrf =~ /.*=ARRAY/) { | ||
| 219 | foreach my $i (0..$#{$hrf}) { | ||
| 220 | if (!defined(${$hrf}[$i])) { $v = ""; | ||
| 221 | } else { $v = ${$hrf}[$i]; } | ||
| 222 | if ($v =~ /([-\w_]+)\/(\w+)\@(\w+)/) { | ||
| 223 | $v =~ s{(\w+)/(\w+)\@}{$1/\*\*\*\@}; | ||
| 224 | } | ||
| 225 | if ($fh) { printf $fh $fmt, $i, $v; | ||
| 226 | } else { printf $fmt, $i, $v; } | ||
| 227 | if (ref($v) =~ /^(HASH|ARRAY)$/ || | ||
| 228 | $v =~ /.*=(HASH|ARRAY)/) { | ||
| 229 | my $db1 = $self->debug; | ||
| 230 | $self->debug(0); | ||
| 231 | $self->disp_param(${$hrf}[$i],$lzp); | ||
| 232 | $self->debug($db1); | ||
| 233 | if ($fh) { print $fh "\n"; } else { print "\n"; } | ||
| 234 | } | ||
| 235 | } | ||
| 236 | } | ||
| 237 | } | ||
| 238 | |||
| 239 | =head2 start_log($dtl, $brf, $cns) | ||
| 240 | |||
| 241 | Input variables: | ||
| 242 | |||
| 243 | $dtl - file name for detailed log | ||
| 244 | $brf - file name for brief log | ||
| 245 | $cns - a list of fields which are stored in brief log | ||
| 246 | $arg - command line arguments | ||
| 247 | |||
| 248 | Variables used or routines called: | ||
| 249 | |||
| 250 | echoMSG - print debug messages | ||
| 251 | |||
| 252 | How to use: | ||
| 253 | |||
| 254 | use Debug::EchoMessage qw(:log); | ||
| 255 | my $self= bless {}, "main"; | ||
| 256 | my $ar = $self->start_log('details.log','brief.log', | ||
| 257 | 'start_time,end_time,elapsed_time,file_tranferred,status'); | ||
| 258 | |||
| 259 | Return: a hash array containing the fields in $cns. | ||
| 260 | |||
| 261 | This method creates log files if they do not exist and prepare a | ||
| 262 | hash array to store needed fields for end_log. The hash array has | ||
| 263 | the following elements: | ||
| 264 | |||
| 265 | cns - a list of field names separated by commas | ||
| 266 | fld - a hash array containing the field defined in cns. | ||
| 267 | fn_brf - file name for brief log | ||
| 268 | fh_brf - file handler for brief log | ||
| 269 | fn_dtl - file name for detail log | ||
| 270 | fh_dtl - file handler for detail log | ||
| 271 | |||
| 272 | If the I<cns> is not specifed, then it defaults to | ||
| 273 | start_time,end_time,elapsed_time,user,args,result. | ||
| 274 | |||
| 275 | =cut | ||
| 276 | |||
| 277 | sub start_log { | ||
| 278 | my $s = shift; | ||
| 279 | my ($dtl, $brf, $cns,$arg) = @_; | ||
| 280 | my $ar = bless {}, ref($s); | ||
| 281 | return $ar if ! $dtl; | ||
| 282 | |||
| 283 | $s->echoMSG(" -- start logging in $dtl..."); | ||
| 284 | $cns='start_time,end_time,elapsed_time,user,args,result' if !$cns; | ||
| 285 | foreach my $k (split /,/, $cns) { | ||
| 286 | $k = lc $k; ${$ar}{fld}{$k} = ""; | ||
| 287 | } | ||
| 288 | ${$ar}{user} = `whoami`; | ||
| 289 | ${$ar}{args} = (exists $ENV{QUERY_STRING})? | ||
| 290 | $ENV{QUERY_STRING}:$arg; | ||
| 291 | my ($tx1, $txt, $cn1); | ||
| 292 | my $fh_dtl = new IO::File ">> $dtl"; | ||
| 293 | croak "ERR: could not write to $dtl: $!\n" if !defined($fh_dtl); | ||
| 294 | ${$ar}{start_time} = time; | ||
| 295 | $ENV{FH_DEBUG_LOG} = $fh_dtl; | ||
| 296 | ${$ar}{cns} = $cns; | ||
| 297 | ${$ar}{fn_dtl} = $dtl; | ||
| 298 | ${$ar}{fh_dtl} = $fh_dtl; | ||
| 299 | my $stm = strftime "%a %b %e %H:%M:%S %Y", | ||
| 300 | localtime(${$ar}{start_time}); | ||
| 301 | $tx1 = "# File Name: $dtl\n# Start at $stm\n"; | ||
| 302 | print $fh_dtl $tx1; | ||
| 303 | return $ar if ! $brf; | ||
| 304 | |||
| 305 | my ($pkg, $fn, $line, $subroutine, $hasargs, $wantarray, | ||
| 306 | $evaltext, $is_require, $hints, $bitmask) = caller(3); | ||
| 307 | $tx1 = "# File Name: $brf\n# Generated By: $subroutine\n"; | ||
| 308 | $tx1 .= "# Fields: (elapsed times are in seconds)\n"; | ||
| 309 | $cn1 = $cns; $cn1 =~ s/,/\|/g; | ||
| 310 | $tx1 .= "# $cn1\n"; | ||
| 311 | $txt = $tx1 if ! -f $brf; | ||
| 312 | my $dbg = $s->debug; | ||
| 313 | $s->debug(1) if !$dbg; # we at least log message at level 1 | ||
| 314 | my $fh_brf = new IO::File ">> $brf"; | ||
| 315 | print $fh_brf "$txt" if $txt; | ||
| 316 | ${$ar}{fn_brf} = $brf; | ||
| 317 | ${$ar}{fh_brf} = $fh_brf; | ||
| 318 | return $ar; | ||
| 319 | } | ||
| 320 | |||
| 321 | =head2 end_log($ar) | ||
| 322 | |||
| 323 | Input variables: | ||
| 324 | |||
| 325 | $ar - array ref returned from start_log. The elements can | ||
| 326 | be populated in before end_log. | ||
| 327 | |||
| 328 | Variables used or routines called: | ||
| 329 | |||
| 330 | strftime - time formater from POSIX | ||
| 331 | disp_param - display parameters | ||
| 332 | |||
| 333 | How to use: | ||
| 334 | |||
| 335 | use Debug::EchoMessage qw(:log); | ||
| 336 | my $self= bless {}, "main"; | ||
| 337 | my $ar = $self->start_log('details.log','brief.log'); | ||
| 338 | $self->end_log($ar); | ||
| 339 | |||
| 340 | Return: none. | ||
| 341 | |||
| 342 | =cut | ||
| 343 | |||
| 344 | sub end_log { | ||
| 345 | my $s = shift; | ||
| 346 | my ($ar) = @_; | ||
| 347 | |||
| 348 | my %b = %{${$ar}{fld}}; | ||
| 349 | my $f = "%a %b %e %H:%M:%S %Y"; | ||
| 350 | my $fh1 = ${$ar}{fh_brf}; | ||
| 351 | my $fh2 = ${$ar}{fh_dtl}; | ||
| 352 | my $cns = ${$ar}{cns}; | ||
| 353 | |||
| 354 | $b{end_time} = time; | ||
| 355 | $b{elapsed_time} = $b{end_time} - $b{start_time}; | ||
| 356 | $b{start_time} = strftime $f, localtime($b{start_time}); | ||
| 357 | $b{end_time} = strftime $f, localtime($b{end_time}); | ||
| 358 | $b{result} = 'OK'; | ||
| 359 | |||
| 360 | my ($txt) = (""); | ||
| 361 | foreach my $k (split /,/, $cns) { $txt .= "$b{$k}|"; } | ||
| 362 | $txt =~ s/\|$//; | ||
| 363 | |||
| 364 | $self->disp_param(\%b); | ||
| 365 | print $fh1 $txt; | ||
| 366 | print $fh2 "# End at $b{end_time} $b{result}\n"; | ||
| 367 | undef $fh1; # close breif file hanlder | ||
| 368 | undef $fh2; # close detail file handler | ||
| 369 | } | ||
| 370 | |||
| 371 | =head1 CODING HISTORY | ||
| 372 | |||
| 373 | =over 4 | ||
| 374 | |||
| 375 | =item * Version 0.01 | ||
| 376 | |||
| 377 | 04/15/2000 (htu) - Initial coding | ||
| 378 | |||
| 379 | =item * Version 0.02 | ||
| 380 | |||
| 381 | 04/16/2001 (htu) - finished debug and echoMSG | ||
| 382 | |||
| 383 | =item * Version 0.03 | ||
| 384 | |||
| 385 | 05/19/2001 (htu) - added disp_param | ||
| 386 | |||
| 387 | =item * Version 1.00 | ||
| 388 | |||
| 389 | 06/25/2002 (htu) - added HTML format in disp_param | ||
| 390 | |||
| 391 | =item * Version 1.01 | ||
| 392 | |||
| 393 | 04/25/2005 (htu) - fixed the NAME title | ||
| 394 | |||
| 395 | =item * Version 1.02 | ||
| 396 | |||
| 397 | 05/06/2005 (htu) - added file handler parameter so that messages can | ||
| 398 | be logged. The file handler can be passed through $ENV{FH_DEBUG_LOG}. | ||
| 399 | |||
| 400 | =item * Version 1.03 | ||
| 401 | |||
| 402 | This version adds the start_log and end_log routines. | ||
| 403 | |||
| 404 | =back | ||
| 405 | |||
| 406 | =head1 FUTURE IMPLEMENTATION | ||
| 407 | |||
| 408 | =over 4 | ||
| 409 | |||
| 410 | =item * no plan yet | ||
| 411 | |||
| 412 | =back | ||
| 413 | |||
| 414 | =head1 AUTHOR | ||
| 415 | |||
| 416 | Copyright (c) 2004 Hanming Tu. All rights reserved. | ||
| 417 | |||
| 418 | This package is free software and is provided "as is" without express | ||
| 419 | or implied warranty. It may be used, redistributed and/or modified | ||
| 420 | under the terms of the Perl Artistic License (see | ||
| 421 | http://www.perl.com/perl/misc/Artistic.html) | ||
| 422 | |||
| 423 | =cut | ||
| 424 |