# ====================================================================
# Gossamer Threads Module Library - http://gossamer-threads.com/
#
# GT::SQL::Driver::debug
# Author: Jason Rhinelander
# CVS Info : 087,071,086,086,085
# $Id: debug.pm,v 2.1 2007/12/18 23:13:41 brewt Exp $
#
# Copyright (c) 2004 Gossamer Threads Inc. All Rights Reserved.
# ====================================================================
#
# Description:
# GT::SQL::Driver debugging module
#
package GT::SQL::Driver::debug;
use strict;
use strict;
use GT::AutoLoader;
use vars qw/$LAST_QUERY @QUERY_STACK @STACK_TRACE $QUERY_STACK_SIZE @ISA/;
@ISA = qw(GT::Base);
$QUERY_STACK_SIZE = 100;
$COMPILE{last_query} = __LINE__ . <<'END_OF_SUB';
sub last_query {
# -------------------------------------------------------------------
# Get, or set the last query.
#
my $self = shift;
return $self->error('NEEDDEBUG', 'WARN') if (! $self->{_debug});
@_ > 0 or return $LAST_QUERY || '';
$LAST_QUERY = shift;
$LAST_QUERY = GT::SQL::Driver::debug->replace_placeholders($LAST_QUERY, @_) if (@_);
# Display stack traces if requested via debug level.
my $stack = '';
if ($self->{_debug} > 2) {
($stack, $LAST_QUERY) = js_stack(3, $LAST_QUERY);
}
elsif ($self->{_debug} > 1) {
package DB;
my $i = 2;
my $ls = defined $ENV{REQUEST_METHOD} ? '
' : "\n";
my $spc = defined $ENV{REQUEST_METHOD} ? ' ' : ' ';
while (my ($file, $line, $sub, $args) = (caller($i++))[1,2,3,4]) {
my @args;
for (@DB::args) {
eval { my $a = $_ }; # workaround for a reference that doesn't think it's a reference
my $print = $@ ? \$_ : $_;
push @args, defined $print ? $print : '[undef]';
}
if (@args) {
my $args = join ", ", @args;
$args =~ s/\n\s*\n/\n/g;
$args =~ s/\n/\n$spc$spc$spc$spc/g;
$stack .= qq!$sub called at $file line $line with arguments $ls$spc$spc ($args).$ls!;
}
else {
$stack .= qq!$sub called at $file line $line with no arguments.$ls!;
}
}
}
push @QUERY_STACK, $LAST_QUERY;
push @STACK_TRACE, "
\n" . $stack . "\n\n" if ($self->{_debug} and $stack); # Pesistance such as Mod_Perl @QUERY_STACK > $QUERY_STACK_SIZE and shift @QUERY_STACK; @STACK_TRACE > $QUERY_STACK_SIZE and shift @STACK_TRACE; return $LAST_QUERY || ''; } END_OF_SUB $COMPILE{js_stack} = __LINE__ . <<'END_OF_SUB'; sub js_stack { # ------------------------------------------------------------------- # Create a nicely formatted javascript browser that (unfortunately) # only works in ie, netscape sucks. # my ($sp, $title) = @_; my $nb = @QUERY_STACK; my ($stack, $dump_out); { package DB; require GT::Dumper; my $i = 0; while (my ($file, $line, $sub, $args) = (caller($sp++))[1,2,3,4]) { if (@DB::args) { $args = "with arguments
$dump~; $i++; } chop $args; chop $args; } else { $args = "with no arguments"; } $stack .= qq!