Programme Execution Tracing in Perl

Programmes run from the command line sometimes need to run silently and sometimes need to provide varying levels of of information. This simple sub-routine, plus a global variable, is all that is needed to do the job...

sub TraceScript
{
   my $P_REPORT_LEVEL = $_[0];
   my $P_CALLER = sprintf("%25s", $_[1]);
   my $P_MESSAGE = $_[2];

   my $V_CALL_ID = $P_CALLER . "==>  ";
   my $V_COMMAND;

   # --------------------------------------------------
   # If the passed level is less than or equal
   # to the preset reporting level, show the message...
   # --------------------------------------------------
   if( $P_REPORT_LEVEL <= $C_VERBOSE )
   {
      print $V_CALL_ID . $P_MESSAGE . "\n";
   }

   return;
}

In use, just set $C_VERBOSE to a suitable value and call the sub-routine anywhere as often as you like...

TraceScript $ReportLevel, $FunctionName, $Message;

The result will be a neatly formatted display of your script's progress. One of the nice things about this scheme is that you can set up various reporting levels that make sense in different circumstances. For example, this scheme has worked well for me in the past:

$Silent = 0;
$Console = 1;
$Normal = 2;
$Debug = 3;
$Paranoid = 4;

If you set up your programme so that you can dynamically alter the level of $C_VERBOSE this can become a very powerfull debugging tool indeed.


Find out more by searching Google here...

Google