The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

CGI::AppBuilder::Message - Display debug messages based on levels

SYNOPSIS

    my $self = bless {}, "main";
    use CGI::AppBuilder::Message;
    $self->debug_level(2);   # set debug level to 2
    # The level 3 message will not be displayed
    $self->echo_msg("This is level 1 message.", 1);
    $self->echo_msg("This is level 2 message.", 2);
    $self->echo_msg("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.

new (ifn => 'file.cfg', opt => 'hvS:')

This is a inherited method from CGI::AppBuilder. See the same method in CGI::AppBuilder for more details.

debug_level($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_level(2);     # set the message level to 2
  print $self->debug_level;  # print current message level

Return: the debug level or set the debug level.

echo_msg($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_level - get debug level.

How to use:

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

Return: None.

This method will display message or a hash array based on debug_level level. If debug_level is set to '0', no message or array will be displayed. If debug_level 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 echo_msg:

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

If debug_level 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:

  echo_msg - print debug messages
  debug_level   - set debug level
  disp_param - recusively called

How to use:

  use CGI::AppBuilder::Message 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.

set_param($var, $ar[,$val])

Input variables:

  $var - variable name
  $ar  - parameter hash or array ref 
  $val - value to be added or assigned

Variables used or routines called:

  None 

How to use:

  use CGI::AppBuilder::Message qw(set_param);
  my $ar = {a=>1,b=>25};
  my $br = [1,2,5,10];
  # for hash ref
  my $va = $self->set_param('a',$ar);  # set $va = 1
  my $v1 = $self->set_param('v1',$ar); # set $v1 = "" 
  my $v2 = $self->set_param('b',$ar);  # set $v2 = 25
  # for array ref
  my $v3 = $self->set_param(0,$br);    # set $v3 = 1
  my $v4 = $self->set_param(3,$br);    # set $v4 = 10
  # add or assign values and return array ref
  $self->set_param('c',$ar,30);        # set $ar->{c} = 30
  $self->set_param(5,$br,50);          # set $br->[5] = 50

Return: $r - the value in the hash or empty string or array ref.

CODING HISTORY

  • Version 0.10

    Extracted methods debug_level, echo_msg, disp_param, and set_param from Debug::EchoMessage.

  • Version 0.11

    Some minor changes to echo_msg.

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)