OK, its working.

However, it appears tied filehandles have been either deprecated in
favor of custom layers, or just don't work from XS yet. In any event,
I switched to a custom layer test and things work OK.
(See below for my POD update w/ an example).

FYI: I ditched the separate logfp_needs_close flag for
a simple existance test on a new logfp_ref field
used to keep the refcnt'd io object.

I still have to add the logic to PurePerl, and do some
testing on non Win32 platforms, but hope to deliver the
update by Monday. Do you want to review it all before
I commit to svn ?

- Dean

=head2 Trace Output

Initially trace output is written to C<STDERR>.  Both the
C<$h-E<gt>trace> and C<DBI-E<gt>trace> methods take an optional
$trace_file parameter, which may be either the name of a file to be
openned by DBI in append mode, or a reference to an existing writable
(possibly layered) filehandle. If $trace_file is a filename,
and can be opened in append mode, or $trace_file is a writable
filehandle, then I<all> trace output (currently including that from
other handles) is redirected to that file. A warning is generated
if $trace_file can't be opened or is not writable.

Further calls to trace() without $trace_file do not alter where
the trace output is sent. If $trace_file is undefined, then
trace output is sent to C<STDERR> and, if the prior trace was openned with
$trace_file as a filename, the previous trace file is closed; if $trace_file was
a filehandle, the filehandle is B<not> closed.

B<NOTE>: If $trace_file is specified as a filehandle, the filehandle
should not be closed until all DBI operations are completed, or the
application has reset the trace file via another call to
C<trace()> that changes the trace file.

=head2 Tracing to Layered Filehandles

One application of tracing to existing filehandles is the use of layered
filehandles (using L<PerlIO::via> custom layering), thereby providing a
mechanism to adapt DBI's tracing to an existing logging facility. E.g.,

        package PerlIO::via::TraceDBI;

        my $logger;

        sub logger { $logger = shift; }

        sub PUSHED {
            my ($class,$mode,$fh) = @_;
            my $buf;
            return bless \$buf,$class;
        }

        sub WRITE {
            my ($obj,$buf,$fh) = @_;
            $logger->log($buf);
            return length($buf);
        }

        sub FLUSH {
            my ($obj,$fh) = @_;
            return 0;
        }

        sub CLOSE {
            $logger->close();
            return 0;
        }

        1;

        use PerlIO::via::TraceDBI;

        PerlIO::via::TraceDBI::logger(MyFancyLogger->new());

        #
        #       filename doesn't matter here
        #
        open TRACEFD, '>:via(TraceDBI)', 'dummyfile';

        ...setup your DBI connection...

        $dbh->trace('SQL', \*TRACEFD);

Reply via email to