package MasonX::Plugin::CompDebug;
use strict;
use warnings;
use base qw(HTML::Mason::Plugin);
use Time::HiRes;
use version; our $VERSION = qv('0.2.0');
use vars qw($DEBUG_URI);

$DEBUG_URI = qr/./;

sub start_request_hook {
    my ( $self, $c ) = @_;
    my $path = $c->request->apache_req->uri;
    return if $path !~ $DEBUG_URI;
    push @{ $self->{timers} }, [Time::HiRes::gettimeofday];
    $self->{chart} = "$path <elapsed:$path>";
    return;
}

sub end_request_hook {
    my ( $self, $c ) = @_;
    my $path = $c->request->apache_req->uri;
    return if $path !~ $DEBUG_URI;
    my $elapsed = sprintf '%.4f',
        Time::HiRes::tv_interval( pop @{ $self->{timers} } );
    $self->{chart} =~ s{<elapsed:$path>}{($elapsed)}xms;
    my $label = '[' . ( scalar localtime ) . '] [debug] ';
    $self->{chart} =~ s/^/$label/gmx;
    print STDERR $label, 'Mason execution chain:', "\n", $self->{chart}, "\n";
    return delete $self->{chart};
}

sub start_component_hook {
    my ( $self, $c ) = @_;
    push @{ $self->{timers} }, [Time::HiRes::gettimeofday];
    my $depth = $c->request->depth;
    my $path  = $c->comp->path;
    $self->{chart} .= "\n"
        . ( q{ } x ( 4 * $depth ) )
        . $c->comp->path
        . " <elapsed:$path>";
    return;
}

sub end_component_hook {
    my ( $self, $c ) = @_;
    my $elapsed = sprintf '%.4f',
        Time::HiRes::tv_interval( pop @{ $self->{timers} } );
    my $path = $c->comp->path;
    $self->{chart} =~ s{<elapsed:$path>}{($elapsed)}xms;
    return;
}

1;

__END__

=head1 NAME

MasonX::Plugin::CompDebug - log components execution chain


=head1 VERSION

This document describes MasonX::Plugin::CompDebug version 0.2.0


=head1 SYNOPSIS

 # basic method:
 PerlAddVar MasonPlugins MasonX::Plugin::CompDebug

 # advanced method:
 my $ah = HTML::Mason::ApacheHandler->new(
              # ...
              plugins => [ 'MasonX::Plugin::CompDebug' ],
              # ...
          );

 # sample output:
 [Thu Jan 19 21:33:08 2006] [debug] Mason execution chain:
 [Thu Jan 19 21:33:08 2006] [debug] /index.html (0.0004)
 [Thu Jan 19 21:33:08 2006] [debug]     /autohandler (0.0959)
 [Thu Jan 19 21:33:08 2006] [debug]         /index.html:_init (0.0005)
 [Thu Jan 19 21:33:08 2006] [debug]         /index.html:sidebar (0.0545)
 [Thu Jan 19 21:33:08 2006] [debug]         /_gfx (0.0005)
 [Thu Jan 19 21:33:08 2006] [debug]         /index.html (0.0968)
 [Thu Jan 19 21:33:08 2006] [debug]         /_toc (0.0010)
 [Thu Jan 19 21:33:08 2006] [debug]         /_ul (0.0018)
 [Thu Jan 19 21:33:08 2006] [debug]         /_search (0.0006)
 [Thu Jan 19 21:33:08 2006] [debug]         /_bc (0.0016)
 [Thu Jan 19 21:33:08 2006] [debug]             /autohandler:bc (0.0004)
 [Thu Jan 19 21:33:08 2006] [debug]         /_ul (0.0027)

  
=head1 DESCRIPTION

This L<HTML::Mason> plugin should be used by developers to get an
overview of a mason handler execution chain.


=head1 INTERFACE 

There is no public interface.
Loading this plugin is enough to do its job, logging its findings
to I<STDERR> (usually httpd's I<error_log>).


=head1 CONFIGURATION AND ENVIRONMENT

=head2 MasonX::Plugin::CompDebug::DEBUG_URI

This is the regular expression applied to C<< $r->uri >> to determine
if execution chain should be monitored or not. By default it's set to
C<< qr{.} >> — monitor every request.


=head1 DEPENDENCIES

L<HTML::Mason>, L<Time::HiRes>, L<version>.


=head1 INCOMPATIBILITIES

None reported.


=head1 BUGS AND LIMITATIONS

No bugs have been reported.

Please report any bugs or feature requests to C<< <altblue@n0i.net> >>.


=head1 AUTHOR

Marius FERARU  C<< <altblue@n0i.net> >>


=head1 LICENCE AND COPYRIGHT

Copyright (c) 2006, Marius FERARU C<< <altblue@n0i.net> >>. All rights reserved.

This module is free software; you can redistribute it and/or
modify it under the same terms as Perl itself. See L<perlartistic>.


=head1 DISCLAIMER OF WARRANTY

BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH
YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL
NECESSARY SERVICING, REPAIR, OR CORRECTION.

IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE
LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL,
OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE
THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
SUCH DAMAGES.
