| 1 | package FirePHP::Dispatcher; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | BEGIN { require 5.008001; } | ||
| 7 | |||
| 8 | use version; | ||
| 9 | our $VERSION = '0.02_01'; | ||
| 10 | |||
| 11 | =head1 NAME | ||
| 12 | |||
| 13 | FirePHP::Dispatcher - sends log messages to a FirePHP console | ||
| 14 | |||
| 15 | =head1 SYNOPSIS | ||
| 16 | |||
| 17 | use FirePHP::Dispatcher; | ||
| 18 | |||
| 19 | my $fire_php = FirePHP::Dispatcher->new( | ||
| 20 | $reference_to_http_headers_of_current_request | ||
| 21 | ); | ||
| 22 | |||
| 23 | $fire_php->log( 'Hello world' ); | ||
| 24 | |||
| 25 | $fire_php->start_group( 'Levels:' ); | ||
| 26 | $fire_php->info ( 'Log informational message' ); | ||
| 27 | $fire_php->warn ( 'Log warning message' ); | ||
| 28 | $fire_php->error( 'Log error message' ); | ||
| 29 | $fire_php->end_group; | ||
| 30 | |||
| 31 | $fire_php->start_group( 'Propably empty:' ); | ||
| 32 | $fire_php->dismiss_group; | ||
| 33 | |||
| 34 | $fire_php->finalize; | ||
| 35 | |||
| 36 | =head1 DESCRIPTION | ||
| 37 | |||
| 38 | B<FirePHP::Dispatcher> implements the basic interface | ||
| 39 | for logging to a FirePHP console. It is no logger on its own | ||
| 40 | but rather a basic API that can be used by front-end loggers to | ||
| 41 | divert or copy messages to a FirePHP console. | ||
| 42 | |||
| 43 | =cut | ||
| 44 | |||
| 45 | use base qw/Class::Accessor::Fast/; | ||
| 46 | |||
| 47 | use Carp; | ||
| 48 | use Scalar::Util qw/looks_like_number blessed/; | ||
| 49 | use JSON::Any; | ||
| 50 | |||
| 51 | |||
| 52 | __PACKAGE__->mk_accessors( | ||
| 53 | qw/http_headers message_index stash json group_stack/ | ||
| 54 | ); | ||
| 55 | |||
| 56 | |||
| 57 | =head1 GENERAL METHODS | ||
| 58 | |||
| 59 | =head2 $class->new( $http_headers ) | ||
| 60 | |||
| 61 | Creates a new instance of C<FirePHP::Dispatcher> and binds it | ||
| 62 | to the L<HTTP::Headers> object given as parameter. | ||
| 63 | |||
| 64 | Returns: a new C<FirePHP::Dispatcher> object | ||
| 65 | |||
| 66 | =cut | ||
| 67 | |||
| 68 | sub new { | ||
| 69 | my ( $class, $http_headers ) = @_; | ||
| 70 | croak "FirePHP::Dispatcher needs a HTTP::Headers object" | ||
| 71 | unless blessed( $http_headers ) and $http_headers->isa( 'HTTP::Headers' ); | ||
| 72 | $class->SUPER::new({ | ||
| 73 | http_headers => $http_headers, | ||
| 74 | message_index => 0, | ||
| 75 | group_stack => [], | ||
| 76 | stash => {}, | ||
| 77 | json => JSON::Any->new(), | ||
| 78 | }); | ||
| 79 | } | ||
| 80 | |||
| 81 | |||
| 82 | =head2 $self->finalize | ||
| 83 | |||
| 84 | Add the needed protocol headers and meta infos to the | ||
| 85 | L<HTTP::Headers> object if anything has been logged to it. | ||
| 86 | Without C<finalize>, FirePHP will ignore all messages. | ||
| 87 | |||
| 88 | =cut | ||
| 89 | |||
| 90 | sub finalize { | ||
| 91 | my $self = shift; | ||
| 92 | my $http = $self->http_headers or return; | ||
| 93 | return unless $self->message_index; | ||
| 94 | |||
| 95 | $http->header( | ||
| 96 | 'X-Wf-Protocol-1' => 'http://meta.wildfirehq.org/' . | ||
| 97 | 'Protocol/JsonStream/0.2', | ||
| 98 | 'X-Wf-1-Plugin-1' => 'http://meta.firephp.org/' . | ||
| 99 | 'Wildfire/Plugin/FirePHP/Library-FirePHPCore/0.2.0', | ||
| 100 | 'X-Wf-1-Structure-1' => 'http://meta.firephp.org/' . | ||
| 101 | 'Wildfire/Structure/FirePHP/FirebugConsole/0.1', | ||
| 102 | 'X-Wf-1-Index' => $self->message_index, | ||
| 103 | ); | ||
| 104 | } | ||
| 105 | |||
| 106 | |||
| 107 | =head1 LOGGING METHODS | ||
| 108 | |||
| 109 | =head2 $self->log( $message ) | ||
| 110 | |||
| 111 | Log a plain message to the FirePHP console | ||
| 112 | |||
| 113 | =cut | ||
| 114 | |||
| 115 | sub log { | ||
| 116 | my ( $self, $message ) = @_; | ||
| 117 | $self->send_headers( $self->format_message({ Type => 'LOG' }, $message )); | ||
| 118 | } | ||
| 119 | |||
| 120 | |||
| 121 | =head2 $self->info( $message ) | ||
| 122 | |||
| 123 | Log a informational message to the FirePHP console | ||
| 124 | |||
| 125 | Returns: Return value | ||
| 126 | |||
| 127 | =cut | ||
| 128 | |||
| 129 | sub info { | ||
| 130 | my ( $self, $message ) = @_; | ||
| 131 | $self->send_headers( $self->format_message({ Type => 'INFO' }, $message )); | ||
| 132 | } | ||
| 133 | |||
| 134 | |||
| 135 | =head2 $self->warn( $message ) | ||
| 136 | |||
| 137 | Log a warning message to the FirePHP console | ||
| 138 | |||
| 139 | =cut | ||
| 140 | |||
| 141 | sub warn { | ||
| 142 | my ( $self, $message ) = @_; | ||
| 143 | $self->send_headers( $self->format_message({ Type => 'WARN' }, $message )); | ||
| 144 | } | ||
| 145 | |||
| 146 | |||
| 147 | =head2 $self->error( $message ) | ||
| 148 | |||
| 149 | Log a error message to the FirePHP console | ||
| 150 | |||
| 151 | =cut | ||
| 152 | |||
| 153 | sub error { | ||
| 154 | my ( $self, $message ) = @_; | ||
| 155 | $self->send_headers( $self->format_message({ Type => 'ERROR' }, $message )); | ||
| 156 | } | ||
| 157 | |||
| 158 | =head1 TABLE METHODS | ||
| 159 | |||
| 160 | =head2 $self->table( $label, $table ) | ||
| 161 | |||
| 162 | Prints the L<FirePHP::SimpleTable> or L<Text::SimpleTable> object | ||
| 163 | to the FirePHP console | ||
| 164 | |||
| 165 | =cut | ||
| 166 | |||
| 167 | sub table { | ||
| 168 | my ( $self, $label, $table ) = @_; | ||
| 169 | $label = '' unless defined $label; | ||
| 170 | |||
| 171 | my $report; | ||
| 172 | if ( blessed $table and $table->isa( 'Text::SimpleTable' ) ) { | ||
| 173 | if ( not $table->isa('FirePHP::SimpleTable') ) { | ||
| 174 | require FirePHP::SimpleTable; | ||
| 175 | bless $table, 'FirePHP::SimpleTable'; | ||
| 176 | } | ||
| 177 | $report = $table->draw; | ||
| 178 | } elsif ( ref $table eq 'ARRAY' ) { | ||
| 179 | $report = $table; | ||
| 180 | } else { | ||
| 181 | die "$table is neither an instance of Text::SimpleTable nor an array ref"; | ||
| 182 | } | ||
| 183 | |||
| 184 | $self->send_headers( | ||
| 185 | $self->format_message({ Type => 'TABLE' }, [ $label, $report ] ) | ||
| 186 | ); | ||
| 187 | } | ||
| 188 | |||
| 189 | |||
| 190 | =head1 GROUPING METHODS | ||
| 191 | |||
| 192 | =head2 $self->start_group( $name ) | ||
| 193 | |||
| 194 | Starts a new, collapsable logging group named C<$name>. | ||
| 195 | Nesting groups is entirly possible. | ||
| 196 | |||
| 197 | =cut | ||
| 198 | |||
| 199 | sub start_group { | ||
| 200 | my ( $self, $label ) = @_; | ||
| 201 | croak 'A group needs a label' unless $label; | ||
| 202 | my $http = $self->http_headers | ||
| 203 | or return; | ||
| 204 | my $hdr = $self->next_message_header; | ||
| 205 | push @{ $self->group_stack }, $self->message_index; | ||
| 206 | my $msg = $self->format_message({ Type => 'GROUP_START', Label => $label }); | ||
| 207 | $http->header( $hdr => sprintf( '%d|%s|', length( $msg ), $msg ) ); | ||
| 208 | } | ||
| 209 | |||
| 210 | |||
| 211 | =head2 $self->dismiss_group | ||
| 212 | |||
| 213 | Dismisses the current group. In later versions this will most propable | ||
| 214 | delete contained messages. Right now just a warning is issued and the current | ||
| 215 | group is closed with C<end_group>. | ||
| 216 | |||
| 217 | =cut | ||
| 218 | |||
| 219 | sub dismiss_group { | ||
| 220 | my $self = shift; | ||
| 221 | my $current_group = ${ $self->group_stack }[ -1 ] | ||
| 222 | or return; | ||
| 223 | if ( $current_group < $self->message_index ) { | ||
| 224 | carp "Dismissing a group with content is not implemented right now, " . | ||
| 225 | "just closing it instead now"; | ||
| 226 | $self->end_group; | ||
| 227 | } else { | ||
| 228 | $self->rollback_last_message; | ||
| 229 | } | ||
| 230 | pop @{ $self->group_stack }; | ||
| 231 | } | ||
| 232 | |||
| 233 | |||
| 234 | =head2 $self->end_group | ||
| 235 | |||
| 236 | Closes the current group and reenter the parent group if available. | ||
| 237 | |||
| 238 | =cut | ||
| 239 | |||
| 240 | sub end_group { | ||
| 241 | my $self = shift; | ||
| 242 | my $current_group = ${ $self->group_stack }[ -1 ] | ||
| 243 | or die "no current group"; | ||
| 244 | my $http = $self->http_headers | ||
| 245 | or return; | ||
| 246 | my $hdr = $self->next_message_header; | ||
| 247 | my $msg = $self->format_message({ Type => 'GROUP_END' }); | ||
| 248 | $http->header( $hdr => sprintf( '%d|%s|', length( $msg ), $msg ) ); | ||
| 249 | pop @{ $self->group_stack }; | ||
| 250 | } | ||
| 251 | |||
| 252 | |||
| 253 | =head2 $self->end_or_dismiss_group | ||
| 254 | |||
| 255 | Close the current group if it containes messages, otherwise just dismiss it. | ||
| 256 | |||
| 257 | =cut | ||
| 258 | |||
| 259 | sub end_or_dismiss_group { | ||
| 260 | my $self = shift; | ||
| 261 | my $current_group = ${ $self->group_stack }[ -1 ] | ||
| 262 | or die "no current group"; | ||
| 263 | if ( $current_group < $self->message_index ) { | ||
| 264 | $self->end_group; | ||
| 265 | } else { | ||
| 266 | $self->dismiss_group; | ||
| 267 | } | ||
| 268 | } | ||
| 269 | |||
| 270 | |||
| 271 | =head1 INTERNAL METHODS | ||
| 272 | |||
| 273 | =head2 $self->format_message( $attr, $message ) | ||
| 274 | |||
| 275 | Renders the message with the given attributs into a | ||
| 276 | message string that is understood by FirePHP. In | ||
| 277 | version 0.2 of the FirePHP protocol this means just | ||
| 278 | an ordered L<JSON> dump. | ||
| 279 | |||
| 280 | =cut | ||
| 281 | |||
| 282 | sub format_message { | ||
| 283 | my ( $self, $attr, $message ) = @_; | ||
| 284 | $self->json->objToJson( [ $attr, $message ] ); | ||
| 285 | } | ||
| 286 | |||
| 287 | |||
| 288 | =head2 $self->next_message_header | ||
| 289 | |||
| 290 | Iterator for FirePHP headers. Calling it advances | ||
| 291 | the internal message cursor so ensure that you either | ||
| 292 | fill it or rollback the message. | ||
| 293 | |||
| 294 | Returns: the next header field name for messages | ||
| 295 | |||
| 296 | =cut | ||
| 297 | |||
| 298 | sub next_message_header { | ||
| 299 | my $self = shift; | ||
| 300 | return sprintf( "X-Wf-1-1-1-%d", ++$self->{message_index} ); | ||
| 301 | } | ||
| 302 | |||
| 303 | |||
| 304 | =head2 $self->rollback_last_message | ||
| 305 | |||
| 306 | Rolls back the last message and decreases the message cursor. | ||
| 307 | This can be used to dismiss groups and delete recent messages | ||
| 308 | from the stack. | ||
| 309 | |||
| 310 | CAVEAT: currently doesn't work correctly for multi-part messages | ||
| 311 | that contain more than 5000 characters. | ||
| 312 | |||
| 313 | =cut | ||
| 314 | |||
| 315 | sub rollback_last_message { | ||
| 316 | my $self = shift; | ||
| 317 | my $http = $self->http_headers or return; | ||
| 318 | return unless $self->{message_index}; | ||
| 319 | my $hdr = sprintf( "X-Wf-1-1-1-%d", $self->{message_index}-- ); | ||
| 320 | $http->remove_header( $hdr ); | ||
| 321 | } | ||
| 322 | |||
| 323 | |||
| 324 | =head2 %headers = $self->build_message_headers( $message ) | ||
| 325 | |||
| 326 | Builds the full header structure for the given message string | ||
| 327 | automatically splitting it into multipart messages when the | ||
| 328 | character limit of 5000 is reached. The message cursor will be | ||
| 329 | advanced accordingly. | ||
| 330 | |||
| 331 | Returns: a hash containing all HTTP headers representing the given message | ||
| 332 | |||
| 333 | =cut | ||
| 334 | |||
| 335 | sub build_message_headers { | ||
| 336 | my ( $self, $message ) = @_; | ||
| 337 | my $len = length $message; | ||
| 338 | |||
| 339 | # split message into handable chunks | ||
| 340 | my @parts = grep{$_} split /(.{5000})/, $message; | ||
| 341 | |||
| 342 | my %headers; | ||
| 343 | for ( 0 .. $#parts ) { | ||
| 344 | $headers{ $self->next_message_header } = | ||
| 345 | (!$_ ? $len : '') . '|' . $parts[$_] . '|' . ($_ < $#parts ? '\\' : ''); | ||
| 346 | } | ||
| 347 | |||
| 348 | return %headers; | ||
| 349 | } | ||
| 350 | |||
| 351 | |||
| 352 | =head2 $self->send_headers( $message ) | ||
| 353 | |||
| 354 | Just a small wrapper that builds and sends all headers for the given message. | ||
| 355 | |||
| 356 | =cut | ||
| 357 | |||
| 358 | sub send_headers { | ||
| 359 | my ( $self, $message ) = @_; | ||
| 360 | my $http = $self->http_headers or return; | ||
| 361 | $http->header( $self->build_message_headers( $message ) ); | ||
| 362 | } | ||
| 363 | |||
| 364 | 1; | ||
| 365 | |||
| 366 | __END__ |