home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / tsw / TSW_3.4.0.exe / Apache2 / perl / Output.pm < prev    next >
Encoding:
Perl POD Document  |  2003-09-27  |  6.8 KB  |  302 lines

  1. package Log::Dispatch::Output;
  2.  
  3. use strict;
  4.  
  5. use Log::Dispatch;
  6.  
  7. use base qw( Log::Dispatch::Base );
  8.  
  9. use Params::Validate qw(validate SCALAR ARRAYREF CODEREF);
  10. Params::Validate::validation_options( allow_extra => 1 );
  11.  
  12. use vars qw[ $VERSION ];
  13.  
  14. use Carp ();
  15.  
  16. $VERSION = sprintf "%d.%02d", q$Revision: 1.26 $ =~ /: (\d+)\.(\d+)/;
  17.  
  18. 1;
  19.  
  20. sub new
  21. {
  22.     my $proto = shift;
  23.     my $class = ref $proto || $proto;
  24.  
  25.     die "The new method must be overridden in the $class subclass";
  26. }
  27.  
  28. sub log
  29. {
  30.     my $self = shift;
  31.  
  32.     my %p = validate( @_, { level => { type => SCALAR },
  33.                 message => { type => SCALAR },
  34.               } );
  35.  
  36.     return unless $self->_should_log($p{level});
  37.  
  38.     $p{message} = $self->_apply_callbacks(%p)
  39.     if $self->{callbacks};
  40.  
  41.     $self->log_message(%p);
  42. }
  43.  
  44. sub _basic_init
  45. {
  46.     my $self = shift;
  47.  
  48.     my %p = validate( @_, { name => { type => SCALAR },
  49.                 min_level => { type => SCALAR },
  50.                 max_level => { type => SCALAR,
  51.                        optional => 1 },
  52.                 callbacks => { type => ARRAYREF | CODEREF,
  53.                        optional => 1 },
  54.               } );
  55.  
  56.     # Map the names to numbers so they can be compared.
  57.     $self->{level_names} = [ qw( debug info notice warning error critical alert emergency ) ];
  58.  
  59.     my $x = 0;
  60.     $self->{level_numbers} = { ( map { $_ => $x++ } @{ $self->{level_names} } ),
  61.                    err   => 4,
  62.                    crit  => 5,
  63.                    emerg => 7 };
  64.  
  65.     $self->{name} = $p{name};
  66.  
  67.     $self->{min_level} = $self->_level_as_number($p{min_level});
  68.     die "Invalid level specified for min_level" unless defined $self->{min_level};
  69.  
  70.     # Either use the parameter supplies or just the highest possible
  71.     # level.
  72.     $self->{max_level} =
  73.     ( exists $p{max_level} ?
  74.           $self->_level_as_number($p{max_level}) :
  75.           $#{ $self->{level_names} }
  76.         );
  77.  
  78.     die "Invalid level specified for max_level" unless defined $self->{max_level};
  79.  
  80.     my @cb = $self->_get_callbacks(%p);
  81.     $self->{callbacks} = \@cb if @cb;
  82. }
  83.  
  84. sub name
  85. {
  86.     my $self = shift;
  87.  
  88.     return $self->{name};
  89. }
  90.  
  91. sub min_level
  92. {
  93.     my $self = shift;
  94.  
  95.     return $self->{level_names}[ $self->{min_level} ];
  96. }
  97.  
  98. sub max_level
  99. {
  100.     my $self = shift;
  101.  
  102.     return $self->{level_names}[ $self->{max_level} ];
  103. }
  104.  
  105. sub accepted_levels
  106. {
  107.     my $self = shift;
  108.  
  109.     return @{ $self->{level_names} }[ $self->{min_level} .. $self->{max_level} ] ;
  110. }
  111.  
  112. sub _should_log
  113. {
  114.     my $self = shift;
  115.  
  116.     my $msg_level = $self->_level_as_number(shift);
  117.     return ( ( $msg_level >= $self->{min_level} ) &&
  118.          ( $msg_level <= $self->{max_level} ) );
  119. }
  120.  
  121. sub _level_as_number
  122. {
  123.     my $self = shift;
  124.     my $level = shift;
  125.  
  126.     unless ( defined $level )
  127.     {
  128.     Carp::croak "undefined value provided for log level";
  129.     }
  130.  
  131.     return $level if $level =~ /^\d$/;
  132.  
  133.     unless ( Log::Dispatch->level_is_valid($level) )
  134.     {
  135.     Carp::croak "$level is not a valid Log::Dispatch log level";
  136.     }
  137.  
  138.     return $self->{level_numbers}{$level};
  139. }
  140.  
  141. sub _level_as_name
  142. {
  143.     my $self = shift;
  144.     my $level = shift;
  145.  
  146.     unless ( defined $level )
  147.     {
  148.     Carp::croak "undefined value provided for log level";
  149.     }
  150.  
  151.     return $level unless $level =~ /^\d$/;
  152.  
  153.     return $self->{level_names}[$level];
  154. }
  155.  
  156.  
  157. __END__
  158.  
  159. =head1 NAME
  160.  
  161. Log::Dispatch::Output - Base class for all Log::Dispatch::* object
  162.  
  163. =head1 SYNOPSIS
  164.  
  165.   package Log::Dispatch::MySubclass;
  166.  
  167.   use Log::Dispatch::Output;
  168.   use base qw( Log::Dispatch::Output );
  169.  
  170.   sub new
  171.   {
  172.       my $proto = shift;
  173.       my $class = ref $proto || $proto;
  174.  
  175.       my %p = @_;
  176.  
  177.       my $self = bless {}, $class
  178.  
  179.       $self->_basic_init(%p);
  180.  
  181.       # Do more if you like
  182.   }
  183.  
  184.   sub log_message
  185.   {
  186.       my $self = shift;
  187.       my %p = @_;
  188.  
  189.       # Do something with message in $p{message}
  190.   }
  191.  
  192. =head1 DESCRIPTION
  193.  
  194. This module is the base class from which all Log::Dispatch::* objects
  195. should be derived.
  196.  
  197. =head1 METHODS
  198.  
  199. =over 4
  200.  
  201. =item * new(%p)
  202.  
  203. This must be overridden in a subclass.  Takes the following
  204. parameters:
  205.  
  206. =over 4
  207.  
  208. =item * name ($)
  209.  
  210. The name of the object (not the filename!).  Required.
  211.  
  212. =item * min_level ($)
  213.  
  214. The minimum logging level this object will accept.  See the
  215. Log::Dispatch documentation for more information.  Required.
  216.  
  217. =item * max_level ($)
  218.  
  219. The maximum logging level this obejct will accept.  See the
  220. Log::Dispatch documentation for more information.  This is not
  221. required.  By default the maximum is the highest possible level (which
  222. means functionally that the object has no maximum).
  223.  
  224. =item * callbacks( \& or [ \&, \&, ... ] )
  225.  
  226. This parameter may be a single subroutine reference or an array
  227. reference of subroutine references.  These callbacks will be called in
  228. the order they are given and passed a hash containing the following keys:
  229.  
  230.  ( message => $log_message, level => $log_level )
  231.  
  232. The callbacks are expected to modify the message and then return a
  233. single scalar containing that modified message.  These callbacks will
  234. be called when either the C<log> or C<log_to> methods are called and
  235. will only be applied to a given message once.  If they do not return
  236. the message then you will get no output.  Make sure to return the
  237. message!
  238.  
  239. =back
  240.  
  241. =item * _basic_init(%p)
  242.  
  243. This should be called from a subclass's constructor.  Make sure to
  244. pass the arguments in @_ to it.  It sets the object's name and minimum
  245. level.  It also sets up two other attributes which are used by other
  246. Log::Dispatch::Output methods, level_names and level_numbers.
  247.  
  248. =item * name
  249.  
  250. Returns the object's name.
  251.  
  252. =item * min_level
  253.  
  254. Returns the object's minimum log level.
  255.  
  256. =item * max_level
  257.  
  258. Returns the object's maximum log level.
  259.  
  260. =item * accepted_levels
  261.  
  262. Returns a list of the object's accepted levels (by name) from minimum
  263. to maximum.
  264.  
  265. =item * log( level => $, message => $ )
  266.  
  267. Sends a message if the level is greater than or equal to the object's
  268. minimum level.  This method applies any message formatting callbacks
  269. that the object may have.
  270.  
  271. =item * _should_log ($)
  272.  
  273. This method is called from the C<log()> method with the log level of
  274. the message to be logged as an argument.  It returns a boolean value
  275. indicating whether or not the message should be logged by this
  276. particular object.  The C<log()> method will not process the message
  277. if the return value is false.
  278.  
  279. =item * _level_as_number ($)
  280.  
  281. This method will take a log level as a string (or a number) and return
  282. the number of that log level.  If not given an argument, it returns
  283. the calling object's log level instead.  If it cannot determine the
  284. level then it will issue a warning and return undef.
  285.  
  286. =back
  287.  
  288. =head2 Subclassing
  289.  
  290. This class should be used as the base class for all logging objects
  291. you create that you would like to work under the Log::Dispatch
  292. architecture.  Subclassing is fairly trivial.  For most subclasses, if
  293. you simply copy the code in the SYNOPSIS and then put some
  294. functionality into the C<log_message> method then you should be all
  295. set.  Please make sure to use the C<_basic_init> method as directed.
  296.  
  297. =head1 AUTHOR
  298.  
  299. Dave Rolsky, <autarch@urth.org>
  300.  
  301. =cut
  302.