The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Debug::EchoMessage - Display debug messages based on levels

SYNOPSIS

    my $self = bless {}, "main";
    use Debug::EchoMessage;
    $self->debug(2);   # set debug level to 2
    # The level 3 message will not be displayed
    $self->echoMSG("This is level 1 message.", 1);
    $self->echoMSG("This is level 2 message.", 2);
    $self->echoMSG("This is level 3 message.", 3);  

DESCRIPTION

The package contains the modules can be used for debuging or displaying contents of your runtime state. You would first define the level of each message in your program, then define a debug level that you would like to see in your runtime.

{ # Encapsulated class data _debug =>0, # debug level }

debug($n)

Input variables:

  $n   - a number between 0 and 100. It specifies the
         level of messages that you would like to
         display. The higher the number, the more
         detailed messages that you will get.

Variables used or routines called: None.

How to use:

  $self->debug(2);     # set the message level to 2
  print $self->debug;  # print current message level

Return: the debug level or set the debug level.

echoMSG($msg, $lvl, $fh)

Input variables:

  $msg - the message to be displayed. No newline
         is needed in the end of the message. It
         will add the newline code at the end of
         the message.
  $lvl - the message level is assigned to the message.
         If it is higher than the debug level, then
         the message will not be displayed.
  $fh  - file handler, or set the file hanlder in this parameter
         $ENV{FH_DEBUG_LOG}

Variables used or routines called:

  debug - get debug level.

How to use:

  # default msg level to 0
  $self->echoMSG('This is a test");
  # set the msg level to 2
  $self->echoMSG('This is a test", 2);

Return: None.

This method will display message or a hash array based on debug level. If debug is set to '0', no message or array will be displayed. If debug is set to '2', it will only display the message level ($lvl) is less than or equal to '2'. If you call this method without providing a message level, the message level ($lvl) is default to '0'. Of course, if no message is provided to the method, it will be quietly returned.

This is how you can call echoMSG:

  my $df = DataFax->new;
     $df->echoMSG("This is a test");   # default the msg to level 0
     $df->echoMSG("This is a test",1); # assign the msg as level 1 msg
     $df->echoMSG("Test again",2);     # assign the msg as level 2 msg
     $df->echoMSG($hrf,1);             # assign $hrf as level 1 msg
     $df->echoMSG($hrf,2);             # assign $hrf as level 2 msg

If debug is set to '1', all the messages with default message level, i.e., 0, and '1' will be displayed. The higher level messages will not be displayed.

This method displays or writes the message based on debug level. The filehandler is provided through $fh or $ENV{FH_DEBUG_LOG}, and the outputs are written to the file.

disp_param($arf,$lzp, $fh)

Input variables:

  $arf - array reference
  $lzp - number of blank space indented in left
  $fh  - file handler

Variables used or routines called:

  echoMSG - print debug messages
  debug   - set debug level
  disp_param - recusively called

How to use:

  use Debug::EchoMessage qw(:echo_msg);
  my $self= bless {}, "main";
  $self->disp_param($arf);

Return: Display the content of the array.

This method recursively displays the contents of an array. If a filehandler is provided through $fh or $ENV{FH_DEBUG_LOG}, the outputs are written to the file.

start_log($dtl, $brf, $cns)

Input variables:

  $dtl - file name for detailed log
  $brf - file name for brief log
  $cns - a list of fields which are stored in brief log
  $arg - command line arguments

Variables used or routines called:

  echoMSG - print debug messages

How to use:

  use Debug::EchoMessage qw(:log);
  my $self= bless {}, "main";
  my $ar = $self->start_log('details.log','brief.log',
    'start_time,end_time,elapsed_time,file_tranferred,status');

Return: a hash array containing the fields in $cns.

This method creates log files if they do not exist and prepare a hash array to store needed fields for end_log. The hash array has the following elements:

  cns    - a list of field names separated by commas
  fld    - a hash array containing the field defined in cns.
  fn_brf - file name for brief log
  fh_brf - file handler for brief log
  fn_dtl - file name for detail log
  fh_dtl - file handler for detail log

If the cns is not specifed, then it defaults to start_time,end_time,elapsed_time,user,args,result.

end_log($ar)

Input variables:

  $ar  - array ref returned from start_log. The elements can
         be populated in before end_log.

Variables used or routines called:

  strftime - time formater from POSIX
  disp_param - display parameters

How to use:

  use Debug::EchoMessage qw(:log);
  my $self= bless {}, "main";
  my $ar = $self->start_log('details.log','brief.log');
  $self->end_log($ar);

Return: none.

CODING HISTORY

  • Version 0.01

    04/15/2000 (htu) - Initial coding

  • Version 0.02

    04/16/2001 (htu) - finished debug and echoMSG

  • Version 0.03

    05/19/2001 (htu) - added disp_param

  • Version 1.00

    06/25/2002 (htu) - added HTML format in disp_param

  • Version 1.01

    04/25/2005 (htu) - fixed the NAME title

  • Version 1.02

    05/06/2005 (htu) - added file handler parameter so that messages can be logged. The file handler can be passed through $ENV{FH_DEBUG_LOG}.

  • Version 1.03

    This version adds the start_log and end_log routines.

FUTURE IMPLEMENTATION

  • no plan yet

AUTHOR

Copyright (c) 2004 Hanming Tu. All rights reserved.

This package is free software and is provided "as is" without express or implied warranty. It may be used, redistributed and/or modified under the terms of the Perl Artistic License (see http://www.perl.com/perl/misc/Artistic.html)