SPUG: Last night's meeting

Lee Wilson devnull at devnullsoftware.com
Wed Mar 17 14:17:42 CST 2004


On Wed, 17 Mar 2004, Michael R. Wolf wrote:
> Tim Maher <tim at consultix-inc.com> writes:
> 
> [...]
> 
> > IMHO, the humble print statement has a significant advantage over
> > any interactive-debugging based strategy, which is simply that it
> > will still be there if/when the program breaks again, and it can
> > easily be re-activated to deliver its benefits again.
> 
> I like that. They read like assertions. I know that Tim doesn't like
> the "backwards" if statement, but I code them like this:
> 
> I've seen and heard of debugging environments that had huge "profiles"
> and "support scripts". They were an IDE in and of themselves. I'm not
> confident that they were usable. I haven't seen them lately, but that
> doesn't mean that they don't exist. I think that automated regression
> tests can serve the same purpose as automated debugging environments.
[snip]


For my recent-ish web project, I created a debug module that I just
include everywhere.  It allows me to debug by levels and by logical code
areas, is semi-efficient, and acts like print statements.  This is useful
for web-based CGI because there is no interactive debugging.  It's not
ideal currently because it uses both files and a DB lookup at various
times, but it works for my purposes.  Modifying it for your own purposes
would be relatively easy.

Any time I want to put a debug statement in code, I just do a call to 
PrintDebugString.  e.g. 

&PrintDebugString( "xmldebug", 2, "red", "", "blah debug blag" );

When I want to turn off the debug, I just delete the files for that 
area/level.  

Anyway, here's the code - it's relatively short.



package DebugLib;

require Exporter;
@DebugLib::ISA = qw( Exporter );
@DebugLib::EXPORT =
qw(
    GetDebugFlag
    PrintDebugString
    SetDebugFlag
);

use lib "../src";

use CGI qw( :standard escapeHTML );
use CGI::Carp qw ( fatalsToBrowser );
use DBI;
use db;
use strict;

##########################################
# Global Variables
##########################################
my %hDebugOutput = ();


sub GetDebugFlag
{
    my ( $strDebugType, $iLevel ) = @_;

    if ( $hDebugOutput {"$strDebugType.$iLevel"} eq "" )
    {
	    my $iTempLevel = 3;
	    my $iFoundLevel	= 0;

        while ( $iTempLevel > 0 )
        {
            if ( $iFoundLevel > 0 )
            {
                $hDebugOutput { "$strDebugType.$iTempLevel" } = "1";
            }
            else
            {
                if ( -e 
"$main::GLOBAL_ServerBasePath/cgi-bin/Debug/debug.$strDebugType.$iTempLevel")
                {
                    $iFoundLevel = $iTempLevel;
                    $hDebugOutput { "$strDebugType.$iTempLevel" } = "1";
                }
                else
                {
                    $hDebugOutput { "$strDebugType.$iTempLevel" } = "0";
                }
            }
            $iTempLevel--;
        }
    }
    return $hDebugOutput {"$strDebugType.$iLevel"};
}


sub SetDebugFlag
{
    my ( $dbhref, $strFlagName, $iLevel ) = @_;
    my $dbh = $$dbhref;
    my $sql;
    my $sth;
    my $i;
    
    $sql = "DELETE FROM DebugFlags WHERE Name=" . $dbh->quote( 
$strFlagName );
    $sth = $dbh->do ( $sql );

    $sql = "INSERT INTO DebugFlags ( Name, CurrentValue ) VALUES ( " . 
$dbh->quote( $strFlagName ) . ", $iLevel )";
    $sth = $dbh->do ( $sql );

    if ( $iLevel > 0 )
    {
        system( "touch 
$main::GLOBAL_ServerBasePath/cgi-bin/Debug/debug.$strFlagName.$iLevel" );
        for ( $i = 1; $i <= 3; $i++ )
        {
            if ( $i <= $iLevel )
            {
                $hDebugOutput {"$strFlagName.$i"} = "1";
            }
            else
            {
                $hDebugOutput {"$strFlagName.$i"} = "0";
            }
        }
    }
    else
    {
        system ( "rm -f 
$main::GLOBAL_ServerBasePath/cgi-bin/Debug/debug.$strFlagName.*" );
        for ( $i = 1; $i <= 3; $i++ )
        {
            $hDebugOutput {"$strFlagName.$i"} = "";
        }
    }
}


sub PrintDebugString
{
    my ( $strDebugType, $iLevel, $strFontColor, $strLog, $strDebugString ) 
= @_;

    if ( &GetDebugFlag ( $strDebugType, $iLevel ) eq "1" )
    {
        if( $strLog ne "" )
        {
            print $strLog "$strDebugString\n";
        }
        else
        {
            if ( $strFontColor ne "" )
            {
                print "<font 
color=\"$strFontColor\">$strDebugString</font><br>\n";
            }
            else
            {
                print "$strDebugString<br>\n";
            }
        }
    }
  
    1;
}


1;



==============================================================================
Lee Wilson - INTP                               http://www.devnullsoftware.com
Software Developer / RealNetworks                    http://www.realarcade.com
==============================================================================
              There are 10 kinds of people in the world: 
                the people who understand ternary, 
                  the people who dont, but care, 
            and the people who don't understand or care.
==============================================================================




More information about the spug-list mailing list