| 1 | package Puzzle::Debug; | ||
| 2 | |||
| 3 | our $VERSION = '0.16'; | ||
| 4 | |||
| 5 | use base 'Class::Container'; | ||
| 6 | |||
| 7 | use Data::Dumper; | ||
| 8 | use HTML::Entities; | ||
| 9 | use Time::HiRes qw(gettimeofday tv_interval); | ||
| 10 | |||
| 11 | |||
| 12 | |||
| 13 | use HTML::Mason::MethodMaker( | ||
| 14 | read_only => [ qw(timer) ], | ||
| 15 | ); | ||
| 16 | |||
| 17 | sub sprint { | ||
| 18 | my $self = shift; | ||
| 19 | my $tmpl = $self->container->tmpl; | ||
| 20 | my $html = &Puzzle::Debug::debug_html_code(); | ||
| 21 | $tmpl->tmplfile(\$html); | ||
| 22 | return $tmpl->html({$self->internal_objects_dump_for_html}); | ||
| 23 | } | ||
| 24 | |||
| 25 | sub timer_reset { | ||
| 26 | my $self = shift; | ||
| 27 | $self->{timer} = [gettimeofday]; | ||
| 28 | } | ||
| 29 | |||
| 30 | sub internal_objects_dump_for_html { | ||
| 31 | my $self = shift; | ||
| 32 | my $glob = $self->all_mason_args_for_debug; | ||
| 33 | my %debug; | ||
| 34 | my $to_dump = sub { $_[0] =~ s/^\$VAR1\s*=\s*//; | ||
| 35 | $_[0] =~ s/(\'pw\'\s*\=\>\s*\'[^']+)/'pw' => '********/; | ||
| 36 | $_[0] =~ s/(\'password\'\s*\=\>\s*\'[^']+)/'password' => '********/; | ||
| 37 | $_[0] = encode_entities($_[0]); | ||
| 38 | $_[0] =~ s/\n/<br>/g; | ||
| 39 | $_[0] =~ s/\s/ /g; | ||
| 40 | return $_[0]}; | ||
| 41 | $debug{debug_elapsed} = tv_interval($self->timer); | ||
| 42 | foreach my $key (qw/conf post args session env/) { | ||
| 43 | delete $glob->{$key}->{container}; | ||
| 44 | foreach (sort {lc($a) cmp lc($b)} keys %{$glob->{$key}}) { | ||
| 45 | my $dumper = &$to_dump(Data::Dumper::Dumper($glob->{$key}->{$_})); | ||
| 46 | push @{$debug{"debug_$key"}},{ key => $_,value => $dumper}; | ||
| 47 | } | ||
| 48 | } | ||
| 49 | foreach (keys %{$self->container->post->args}) { | ||
| 50 | my $dumper = &$to_dump(Data::Dumper::Dumper($self->container->post->args->{$_})); | ||
| 51 | push @{$debug{"debug_http_post"}},{ key => $_,value => $dumper}; | ||
| 52 | } | ||
| 53 | push @{$debug{"debug_cache"}}, {key => 'size', | ||
| 54 | value => $self->container->_mason->cache(namespace=>$self->container->cfg->namespace)->size}; | ||
| 55 | my @cache_keys =$self->container->_mason->cache(namespace=>$self->container->cfg->namespace)->get_keys; | ||
| 56 | foreach (@cache_keys) { | ||
| 57 | push @{$debug{"debug_cache"}}, | ||
| 58 | {key => $_, value => &ParseDateString("epoch " . | ||
| 59 | $self->container->_mason->cache(namespace=>$self->container->cfg->namespace)->get_object($_)->get_expires_at())}; | ||
| 60 | } | ||
| 61 | $debug{'puzzle_dump'} = $to_dump->(Data::Dumper::Dumper($self->container)); | ||
| 62 | return %debug | ||
| 63 | } | ||
| 64 | |||
| 65 | sub all_mason_args { | ||
| 66 | # ritorna tutti i parametri globali | ||
| 67 | # alcuni normalizzati | ||
| 68 | my $self = shift; | ||
| 69 | my $puzzle = $self->container; | ||
| 70 | return { | ||
| 71 | %{$puzzle->cfg->as_hashref}, | ||
| 72 | %{&_struct2args($puzzle->session->internal_session)}, | ||
| 73 | %{&_struct2args($puzzle->post->args)}, | ||
| 74 | %{&_struct2args($puzzle->args->args)}, | ||
| 75 | title => $puzzle->page->title, | ||
| 76 | }; | ||
| 77 | } | ||
| 78 | |||
| 79 | sub all_mason_args_for_debug { | ||
| 80 | # ritorna tutti i parametri globali | ||
| 81 | # alcuni normalizzati | ||
| 82 | my $self = shift; | ||
| 83 | return { | ||
| 84 | conf => $self->container->cfg, | ||
| 85 | session => &_struct2args($self->container->session->internal_session), | ||
| 86 | post => &_struct2args($self->container->post->args) , | ||
| 87 | args => &_struct2args($self->container->args->args), | ||
| 88 | env => \%ENV | ||
| 89 | }; | ||
| 90 | } | ||
| 91 | |||
| 92 | sub _struct2args { | ||
| 93 | my $struct = shift; | ||
| 94 | my $buffer = {}; | ||
| 95 | &_struct2argsrec(\$buffer,$struct,''); | ||
| 96 | return $buffer; | ||
| 97 | } | ||
| 98 | |||
| 99 | sub _struct2argsrec { | ||
| 100 | my $buffer = shift; | ||
| 101 | my $struct = shift; | ||
| 102 | my $key = shift; | ||
| 103 | if (ref($struct) eq 'HASH') { | ||
| 104 | $key .= '.' unless ($key eq ''); | ||
| 105 | foreach (keys %$struct) { | ||
| 106 | &_struct2argsrec($buffer,$struct->{$_},"$key$_"); | ||
| 107 | } | ||
| 108 | } elsif (ref($struct) eq 'ARRAY') { | ||
| 109 | if (defined($struct->[0]) && ref($struct->[0]) eq '') { | ||
| 110 | # it's not an array of hashref | ||
| 111 | $$buffer->{"$key.array.count"} = scalar(@$struct); | ||
| 112 | $key = "$key.array."; | ||
| 113 | for (my $i=0;$i<scalar(@$struct);$i++) { | ||
| 114 | &_struct2argsrec($buffer,$struct->[$i],"$key$i"); | ||
| 115 | } | ||
| 116 | } else { | ||
| 117 | $$buffer->{$key} = $struct; | ||
| 118 | } | ||
| 119 | } elsif (ref($struct) eq '') { | ||
| 120 | $$buffer->{$key} = $struct; | ||
| 121 | } else { | ||
| 122 | die "_struct2argsrec: Unknown structure for key $key: " . ref($struct) . | ||
| 123 | Data::Dumper::Dumper($struct); | ||
| 124 | } | ||
| 125 | } | ||
| 126 | |||
| 127 | sub debug_html_code { | ||
| 128 | return <<EOF; | ||
| 129 | <!-- required debug.css and debug.js --> | ||
| 130 | |||
| 131 | <!-- INIT: DEBUG TABBED --> | ||
| 132 | <ul id="tablist"> | ||
| 133 | |||
| 134 | <li><a href="#" class="current" onClick="return expandcontent('sc1', this)">DEBUG MENU</a></li> | ||
| 135 | <li><a href="#" onClick="return expandcontent('sc2', this)" theme="#EAEAFF">POST HTTP</a></li> | ||
| 136 | <li><a href="#" onClick="return expandcontent('sc3', this)" theme="#EAEAFF">Al template</a></li> | ||
| 137 | <li><a href="#" onClick="return expandcontent('sc4', this)" theme="#FFE6E6">Al template dal POST HTTP</a></li> | ||
| 138 | <li><a href="#" onClick="return expandcontent('sc5', this)" theme="#DFFFDF">Sessione</a></li> | ||
| 139 | <li><a href="#" onClick="return expandcontent('sc6', this)" theme="#AFAFAF">Configurazione</a></li> | ||
| 140 | <li><a href="#" onClick="return expandcontent('sc7', this)" theme="#D0F0D0">Ambiente</a></li> | ||
| 141 | <li><a href="#" onClick="return expandcontent('sc8', this)" theme="#BFBFDF">Cache</a></li> | ||
| 142 | <li><a href="#" onClick="return expandcontent('sc9', this)" theme="#FF9F9F">Puzzle</a></li> | ||
| 143 | </ul> | ||
| 144 | |||
| 145 | <DIV id="tabcontentcontainer"> | ||
| 146 | |||
| 147 | <div id="sc1" class="tabcontent"> | ||
| 148 | Pagina valutata in <b>%debug_elapsed%</b> secondi.<br /> | ||
| 149 | </div> | ||
| 150 | |||
| 151 | <div id="sc2" class="tabcontent"> | ||
| 152 | <table border="0"> | ||
| 153 | <TMPL_LOOP NAME="debug_http_post"> | ||
| 154 | <tr> | ||
| 155 | <td valign="top" > | ||
| 156 | <font color="#0000FF" class="debugcella">%key%</font></td> | ||
| 157 | <td valign="top" class="debugcella"> | ||
| 158 | =></td> <td valign="top" class="debugcella"> | ||
| 159 | %value% </td> | ||
| 160 | </tr> | ||
| 161 | </TMPL_LOOP> | ||
| 162 | </table> | ||
| 163 | </div> | ||
| 164 | |||
| 165 | <div id="sc3" class="tabcontent"> | ||
| 166 | <table border="0"> | ||
| 167 | <TMPL_LOOP NAME="debug_args"> | ||
| 168 | <tr> | ||
| 169 | <td valign="top" > | ||
| 170 | <font color="#0000FF" class="debugcella">%key%</font></td> | ||
| 171 | <td valign="top" class="debugcella"> | ||
| 172 | =></td> <td valign="top" class="debugcella"> | ||
| 173 | %value% </td> | ||
| 174 | </tr> | ||
| 175 | </TMPL_LOOP> | ||
| 176 | </table> | ||
| 177 | |||
| 178 | </div> | ||
| 179 | |||
| 180 | <div id="sc4" class="tabcontent"> | ||
| 181 | <table border="0"> | ||
| 182 | <TMPL_LOOP NAME="debug_post"> | ||
| 183 | <tr> | ||
| 184 | <td valign="top" > | ||
| 185 | <font color="#0000FF" class="debugcella">%key%</font></td> | ||
| 186 | <td valign="top" class="debugcella"> | ||
| 187 | =></td> <td valign="top" class="debugcella"> | ||
| 188 | %value% </td> | ||
| 189 | </tr> | ||
| 190 | </TMPL_LOOP> | ||
| 191 | </table> | ||
| 192 | </div> | ||
| 193 | |||
| 194 | <div id="sc5" class="tabcontent"> | ||
| 195 | <table border="0"> | ||
| 196 | <TMPL_LOOP NAME="debug_session"> | ||
| 197 | <tr> | ||
| 198 | <td valign="top"> | ||
| 199 | <font color="#0000FF" class="debugcella">%key%</font></td> | ||
| 200 | <td valign="top" class="debugcella"> | ||
| 201 | =></td> <td valign="top" class="debugcella"> | ||
| 202 | %value%</td> | ||
| 203 | </tr> | ||
| 204 | </TMPL_LOOP> | ||
| 205 | </table> | ||
| 206 | |||
| 207 | </div> | ||
| 208 | <div id="sc6" class="tabcontent"> | ||
| 209 | <table border="0"> | ||
| 210 | <TMPL_LOOP NAME="debug_conf"> | ||
| 211 | <tr> | ||
| 212 | <td valign="top" > | ||
| 213 | <font color="#0000FF" class="debugcella">%key%</font></td> | ||
| 214 | <td valign="top" class="debugcella"> | ||
| 215 | =></td> <td valign="top" class="debugcella"> | ||
| 216 | %value% </td> | ||
| 217 | </tr> | ||
| 218 | </TMPL_LOOP> | ||
| 219 | </table> | ||
| 220 | </div> | ||
| 221 | |||
| 222 | <div id="sc7" class="tabcontent"> | ||
| 223 | <table border="0"> | ||
| 224 | <TMPL_LOOP NAME="debug_env"> | ||
| 225 | <tr> | ||
| 226 | <td valign="top" > | ||
| 227 | <font color="#0000FF" class="debugcella">%key%</font></td> | ||
| 228 | <td valign="top" class="debugcella"> | ||
| 229 | =></td> <td valign="top" class="debugcella"> | ||
| 230 | %value% </td> | ||
| 231 | </tr> | ||
| 232 | </TMPL_LOOP> | ||
| 233 | </table> | ||
| 234 | </div> | ||
| 235 | <div id="sc8" class="tabcontent"> | ||
| 236 | <table border="0"> | ||
| 237 | <TMPL_LOOP NAME="debug_cache"> | ||
| 238 | <tr> | ||
| 239 | <td valign="top" > | ||
| 240 | <font color="#0000FF" class="debugcella">%key%</font></td> | ||
| 241 | <td valign="top" class="debugcella"> | ||
| 242 | =></td> <td valign="top" class="debugcella"> | ||
| 243 | %value% </td> | ||
| 244 | </tr> | ||
| 245 | </TMPL_LOOP> | ||
| 246 | </table> | ||
| 247 | </div> | ||
| 248 | |||
| 249 | <div id="sc9" class="tabcontent"> | ||
| 250 | <font color="#000000" class="debugcella"> | ||
| 251 | <PRE> | ||
| 252 | %puzzle_dump% | ||
| 253 | </PRE> | ||
| 254 | </font> | ||
| 255 | </div> | ||
| 256 | |||
| 257 | |||
| 258 | </DIV> | ||
| 259 | EOF | ||
| 260 | } | ||
| 261 | |||
| 262 | 1; |