| blib/lib/CGI/Application/Plugin/DebugMessage.pm | |||
|---|---|---|---|
| Criterion | Covered | Total | % | 
| statement | 59 | 78 | 75.6 | 
| branch | 10 | 38 | 26.3 | 
| condition | 6 | 20 | 30.0 | 
| subroutine | 10 | 12 | 83.3 | 
| pod | 2 | 5 | 40.0 | 
| total | 87 | 153 | 56.8 | 
| line | stmt | bran | cond | sub | pod | time | code | 
|---|---|---|---|---|---|---|---|
| 1 | package CGI::Application::Plugin::DebugMessage; | ||||||
| 2 | |||||||
| 3 | 2 | 2 | 58703 | use 5.006; | |||
| 2 | 6 | ||||||
| 2 | 61 | ||||||
| 4 | 2 | 2 | 9 | use strict; | |||
| 2 | 3 | ||||||
| 2 | 55 | ||||||
| 5 | 2 | 2 | 8 | use warnings; | |||
| 2 | 7 | ||||||
| 2 | 71 | ||||||
| 6 | |||||||
| 7 | 2 | 2 | 13 | use CGI::Application 3.21; | |||
| 2 | 23 | ||||||
| 2 | 46 | ||||||
| 8 | 2 | 2 | 8 | use Carp qw(croak); | |||
| 2 | 3 | ||||||
| 2 | 1861 | ||||||
| 9 | |||||||
| 10 | require Exporter; | ||||||
| 11 | |||||||
| 12 | our @ISA = qw(Exporter); | ||||||
| 13 | |||||||
| 14 | our %EXPORT_TAGS = ( 'all' => [ qw() ] ); | ||||||
| 15 | our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); | ||||||
| 16 | our @EXPORT = qw( | ||||||
| 17 | debug | ||||||
| 18 | debug_ocode | ||||||
| 19 | ); | ||||||
| 20 | our $VERSION = '0.01'; | ||||||
| 21 | my $prefix = "CAP_DeubgMessage"; | ||||||
| 22 | |||||||
| 23 | sub import { | ||||||
| 24 | 2 | 2 | 18 | my $caller = scalar(caller); | |||
| 25 | 2 | 17 | $caller->add_callback('postrun', 'CGI::Application::Plugin::DebugMessage::log2footer'); | ||||
| 26 | 2 | 1667 | goto &Exporter::import; | ||||
| 27 | } | ||||||
| 28 | |||||||
| 29 | sub debug { | ||||||
| 30 | 2 | 2 | 1 | 878 | my $self = shift; | ||
| 31 | 2 | 5 | my @added = @_; | ||||
| 32 | 2 | 50 | 6 | if (@added) { | |||
| 33 | 2 | 100 | 11 | my $footer = $self->param("${prefix}_footer") || []; | |||
| 34 | 2 | 52 | my $caller = bless([caller(0)], "${prefix}::Caller"); | ||||
| 35 | 2 | 5 | @added = map { [$caller, $_] } @added; | ||||
| 2 | 6 | ||||||
| 36 | 2 | 3 | push(@{$footer}, @added); | ||||
| 2 | 2 | ||||||
| 37 | 2 | 8 | $self->param("${prefix}_footer" => $footer) | ||||
| 38 | } | ||||||
| 39 | } | ||||||
| 40 | |||||||
| 41 | sub debug_ocode { | ||||||
| 42 | 0 | 0 | 1 | 0 | my $self = shift; | ||
| 43 | 0 | 0 | my $code = shift; | ||||
| 44 | 0 | 0 | 0 | $self->param("${prefix}_code" => $code) if (UNIVERSAL::can($self, 'param')); | |||
| 45 | } | ||||||
| 46 | |||||||
| 47 | sub log2footer { | ||||||
| 48 | 1 | 1 | 0 | 22952 | my $self = shift; | ||
| 49 | 1 | 3 | my $ref = shift; | ||||
| 50 | 1 | 50 | 7 | my $footer = $self->param("${prefix}_footer") ? $self->param("${prefix}_footer") : []; | |||
| 51 | 1 | 50 | 33 | 43 | return unless ($footer and ref($footer) eq 'ARRAY' and @{$footer}); | ||
| 1 | 33 | 5 | |||||
| 52 | 1 | 10 | my $html = " \n" . $self->dump_html() . " Debug Messages:\n 
 | ||||
| 53 | 1 | 2672 | foreach my $message (@{$footer}) { | ||||
| 1 | 4 | ||||||
| 54 | 2 | 1063 | my $string = ''; | ||||
| 55 | 2 | 4 | my $caller = undef; | ||||
| 56 | 2 | 50 | 33 | 11 | ($caller, $message) = @{$message} if (ref($message) eq 'ARRAY' and @{$message} and ref($message->[0]) eq "${prefix}::Caller"); | ||
| 2 | 33 | 6 | |||||
| 2 | 21 | ||||||
| 57 | 2 | 50 | 14 | $caller = sprintf("[%s(%s)] ", $caller->[0], $caller->[2]) if ($caller); | |||
| 58 | # HTML escape and dump (if necessary) | ||||||
| 59 | 2 | 100 | 24 | if (ref($message)) { | |||
| 60 | 1 | 4 | $string = CGI::Application::Plugin::DebugMessage::dump_pretty($self, $message); | ||||
| 61 | 1 | 19 | $string = CGI->pre($string); | ||||
| 62 | } else { | ||||||
| 63 | 1 | 26 | $string = CGI->escapeHTML($message); | ||||
| 64 | } | ||||||
| 65 | 2 | 50 | 440 | $string = CGI::Application::Plugin::DebugMessage::convert_code($self, $string) if ($self->param("${prefix}_code")); | |||
| 66 | 2 | 72 | $html .= CGI->li($caller . $string) . "\n"; | ||||
| 67 | } | ||||||
| 68 | 1 | 45 | $html .= "\n"; | ||||
| 69 | 1 | 62 | $$ref =~ s/(<\/html>|$)/$html$1/i; | ||||
| 70 | } | ||||||
| 71 | |||||||
| 72 | sub dump_pretty { | ||||||
| 73 | 1 | 1 | 0 | 2 | my $self = shift; | ||
| 74 | 1 | 1 | 71 | eval ' | |||
| 1 | 2466 | ||||||
| 1 | 6946 | ||||||
| 1 | 90 | ||||||
| 75 | use Data::Dumper; | ||||||
| 76 | local $Data::Dumper::Indent = 1; | ||||||
| 77 | local $Data::Dumper::Sortkeys = 1; | ||||||
| 78 | local $Data::Dumper::Terse = 1; | ||||||
| 79 | '; | ||||||
| 80 | 1 | 50 | 6 | return join(", ", @_) if ($@); | |||
| 81 | 1 | 50 | 5 | return unless (@_); | |||
| 82 | 1 | 4 | my $dump = Dumper(@_); | ||||
| 83 | 1 | 108 | return $dump; | ||||
| 84 | } | ||||||
| 85 | |||||||
| 86 | sub convert_code { | ||||||
| 87 | 0 | 0 | 0 | 0 | my $self = shift; | ||
| 88 | 0 | 0 | my $str = shift; | ||||
| 89 | 0 | 0 | 0 | my $ref = ref($str) ? $str : \$str; | |||
| 90 | 0 | 0 | 0 | my $class = ref($self) ? ref($self) : $self; | |||
| 91 | 0 | 0 | my $ocode = $self->param("${prefix}_code"); | ||||
| 92 | 0 | 0 | 0 | return $str unless (length($str)); | |||
| 93 | 0 | 0 | 0 | return $str unless ($ocode); | |||
| 94 | # Use Jcode | ||||||
| 95 | 0 | 0 | eval "use Jcode"; | ||||
| 96 | 0 | 0 | 0 | return $str if ($@); | |||
| 97 | # Guess input code | ||||||
| 98 | 0 | 0 | my ($icode, $match) = Jcode::getcode($$ref); | ||||
| 99 | 0 | 0 | 0 | 0 | $icode = 'euc' if ($icode eq undef and $match > 0); | ||
| 100 | 0 | 0 | 0 | if ($icode eq 'euc') { | |||
| 101 | 0 | 0 | my $re_sjis = '[\201-\237\340-\374][\100-\176\200-\374]|[\241-\337]|[\x00-\x7F]'; | ||||
| 102 | 0 | 0 | my $re_euc = '[\241-\376][\241-\376]|\216[\241-\337]|\217[\241-\376][\241-\376]|[\x00-\x7F]'; | ||||
| 103 | 0 | 0 | 0 | 0 | $icode = 'sjis' if ($$ref !~ /^(?:$re_euc)*$/o and $str =~ /^(?:$re_sjis)*$/o); | ||
| 104 | } | ||||||
| 105 | # Convert | ||||||
| 106 | 0 | 0 | 0 | $$ref = Jcode::jcode($ref, $icode)->$ocode if ($icode ne $ocode); | |||
| 107 | } | ||||||
| 108 | |||||||
| 109 | 1; | ||||||
| 110 | __END__ |