Some more optimisations
[spider.git] / perl / DXDebug.pm
index dbeab595045870a1ee74e360ea5a5af5bf4f9aac..5bf2470dbf8526282902c7592eab9234654f4ade 100644 (file)
@@ -19,6 +19,7 @@ use vars qw(%dbglevel $fp $callback $cleandays $keepdays);
 use DXUtil;
 use DXLog ();
 use Carp ();
+use POSIX qw(isatty);
 
 %dbglevel = ();
 $fp = undef;
@@ -26,6 +27,8 @@ $callback = undef;
 $keepdays = 10;
 $cleandays = 100;
 
+our $no_stdout;                                        # set if not running in a terminal
+
 # Avoid generating "subroutine redefined" warnings with the following
 # hack (from CGI::Carp):
 if (!defined $DB::VERSION) {
@@ -66,7 +69,7 @@ sub dbg($)
                my @l = split /\n/, $r;
                for (@l) {
                        s/([\x00-\x08\x0B-\x1f\x7f-\xff])/uc sprintf("%%%02x",ord($1))/eg;
-                       print "$_\n" if defined \*STDOUT;
+                       print "$_\n" if defined \*STDOUT && !$no_stdout;
                        my $str = "$t^$_";
                        &$callback($str) if $callback;
                        $fp->writeunix($t, $str); 
@@ -79,7 +82,7 @@ sub dbginit
        $callback = shift;
        
        # add sig{__DIE__} handling
-       if (!defined $DB::VERSION) {
+       unless (defined $DB::VERSION) {
                $SIG{__WARN__} = sub { 
                        if ($_[0] =~ /Deep\s+recursion/i) {
                                dbg($@);
@@ -92,6 +95,13 @@ sub dbginit
                };
                
                $SIG{__DIE__} = sub { dbg($@); dbg(Carp::longmess(@_)); };
+
+               # switch off STDOUT printing if we are not talking to a TTY
+               unless ($^O =~ /^MS/ || $^O =~ /^OS-2/) {
+                       unless (isatty(STDOUT->fileno)) {
+                               ++$no_stdout;
+                       }
+               }
        }
 
        $fp = DXLog::new('debug', 'dat', 'd');