From: Guillermo Roditi Date: Mon, 23 Jun 2008 20:58:50 +0000 (+0000) Subject: Add Catalyst::Exception and Catalyst::Log to the Moosified bunch. X-Git-Tag: 5.8000_03~127 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FCatalyst-Runtime.git;a=commitdiff_plain;h=9f6c5fdc8776506a0183270cfe64f4442e4ced13 Add Catalyst::Exception and Catalyst::Log to the Moosified bunch. r16975@martha (orig r7498): konobi | 2008-03-14 21:58:53 -0400 --- diff --git a/TODO b/TODO index 68132aa..34b771a 100644 --- a/TODO +++ b/TODO @@ -7,6 +7,8 @@ * Catalyst::Action * Catalyst::ActionChain * Catalyst::ActionContainer + * Catalyst::Log + * Catalyst::Exception - Make classes immutable at setup() time diff --git a/lib/Catalyst/Exception.pm b/lib/Catalyst/Exception.pm index ee85e1e..02610a4 100644 --- a/lib/Catalyst/Exception.pm +++ b/lib/Catalyst/Exception.pm @@ -1,15 +1,10 @@ package Catalyst::Exception; -use strict; -use vars qw[@ISA $CATALYST_EXCEPTION_CLASS]; - -BEGIN { - push( @ISA, $CATALYST_EXCEPTION_CLASS || 'Catalyst::Exception::Base' ); -} +# XXX: See bottom of file for Exception implementation package Catalyst::Exception::Base; -use strict; +use Moose; use Carp (); =head1 NAME @@ -49,6 +44,10 @@ sub throw { Carp::croak($message); } +=head2 meta + +Provided by Moose + =head1 AUTHOR Sebastian Riedel, C @@ -61,4 +60,13 @@ it under the same terms as Perl itself. =cut +package Catalyst::Exception; + +use Moose; +use vars qw[$CATALYST_EXCEPTION_CLASS]; + +BEGIN { + extends($CATALYST_EXCEPTION_CLASS || 'Catalyst::Exception::Base'); +} + 1; diff --git a/lib/Catalyst/Log.pm b/lib/Catalyst/Log.pm index 822ea32..01ff75f 100644 --- a/lib/Catalyst/Log.pm +++ b/lib/Catalyst/Log.pm @@ -1,14 +1,20 @@ package Catalyst::Log; use strict; -use base 'Class::Accessor::Fast'; +#use base 'Class::Accessor::Fast'; use Data::Dump; our %LEVELS = (); -__PACKAGE__->mk_accessors('level'); -__PACKAGE__->mk_accessors('body'); -__PACKAGE__->mk_accessors('abort'); +use Moose; + +has level => (is => 'rw'); +has _body => (is => 'rw'); +has abort => (is => 'rw'); + +#__PACKAGE__->mk_accessors('level'); +#__PACKAGE__->mk_accessors('body'); +#__PACKAGE__->mk_accessors('abort'); { my @levels = qw[ debug info warn error fatal ]; @@ -25,14 +31,14 @@ __PACKAGE__->mk_accessors('abort'); *{$name} = sub { my $self = shift; - if ( $self->{level} & $level ) { + if ( $self->level & $level ) { $self->_log( $name, @_ ); } }; *{"is_$name"} = sub { my $self = shift; - return $self->{level} & $level; + return $self->level & $level; }; } } @@ -52,12 +58,20 @@ sub levels { sub enable { my ( $self, @levels ) = @_; - $self->{level} |= $_ for map { $LEVELS{$_} } @levels; + my $level = $self->level; + for(map { $LEVELS{$_} } @levels){ + $level |= $_; + } + $self->level($level); } sub disable { my ( $self, @levels ) = @_; - $self->{level} &= ~$_ for map { $LEVELS{$_} } @levels; + my $level = $self->level; + for(map { $LEVELS{$_} } @levels){ + $level &= ~$_; + } + $self->level($level); } sub _dump { @@ -70,18 +84,20 @@ sub _log { my $level = shift; my $message = join( "\n", @_ ); $message .= "\n" unless $message =~ /\n$/; - $self->{body} .= sprintf( "[%s] %s", $level, $message ); + my $body = $self->_body; + $body .= sprintf( "[%s] %s", $level, $message ); + $self->_body($body); } sub _flush { my $self = shift; - if ( $self->abort || !$self->body ) { + if ( $self->abort || !$self->_body ) { $self->abort(undef); } else { - $self->_send_to_log( $self->body ); + $self->_send_to_log( $self->_body ); } - $self->body(undef); + $self->_body(undef); } sub _send_to_log { @@ -169,6 +185,10 @@ arguments. $log = Catalyst::Log->new; $log = Catalyst::Log->new( 'warn', 'error' ); +=head2 level + +Contains a bitmask of the currently set log levels. + =head2 levels Set log levels @@ -217,6 +237,8 @@ This protected method is what actually sends the log information to STDERR. You may subclass this module and override this method to get finer control over the log output. +=head2 meta + =head1 SEE ALSO L.