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 / Level.pm < prev    next >
Encoding:
Perl POD Document  |  2003-02-27  |  7.4 KB  |  296 lines

  1. ###############r###################################
  2. package Log::Log4perl::Level;
  3. ##################################################
  4.  
  5. use 5.006;
  6. use strict;
  7. use warnings;
  8. use Carp;
  9.  
  10. # log4j, for whatever reason, puts 0 as all and MAXINT as OFF.
  11. # this seems less optimal, as more logging would imply a higher
  12. # level. But oh well. Probably some brokenness that has persisted. :)
  13. use constant ALL_INT   => 0;
  14. use constant DEBUG_INT => 10000;
  15. use constant INFO_INT  => 20000;
  16. use constant WARN_INT  => 30000;
  17. use constant ERROR_INT => 40000;
  18. use constant FATAL_INT => 50000;
  19. use constant OFF_INT   => (2 ** 31) - 1;
  20.  
  21. no strict qw(refs);
  22. use vars qw(%PRIORITY %LEVELS);
  23.  
  24. our %PRIORITY = (); # unless (%PRIORITY);
  25. our %LEVELS = () unless (%LEVELS);
  26. our %SYSLOG = () unless (%SYSLOG);
  27. our %L4P_TO_LD = () unless (%L4P_TO_LD);
  28.  
  29. sub add_priority {
  30.   my ($prio, $intval, $syslog, $log_dispatch_level) = @_;
  31.   $prio = uc($prio); # just in case;
  32.  
  33.   $PRIORITY{$prio}    = $intval;
  34.   $LEVELS{$intval}    = $prio;
  35.  
  36.   # Set up the mapping between Log4perl integer levels and 
  37.   # Log::Dispatch levels
  38.   # Note: Log::Dispatch uses the following levels:
  39.   # 0 debug
  40.   # 1 info
  41.   # 2 notice
  42.   # 3 warning
  43.   # 4 error
  44.   # 5 critical
  45.   # 6 alert
  46.   # 7 emergency
  47.  
  48.       # The equivalent Log::Dispatch level is optional, set it to 
  49.       # the highest value (7=emerg) if it's not provided.
  50.   $log_dispatch_level = 7 unless defined $log_dispatch_level;
  51.   
  52.   $L4P_TO_LD{$prio}  = $log_dispatch_level;
  53.  
  54.   $SYSLOG{$prio}      = $syslog if defined($syslog);
  55. }
  56.  
  57. # create the basic priorities
  58. add_priority("OFF",   OFF_INT,   -1, 7);
  59. add_priority("FATAL", FATAL_INT,  0, 7);
  60. add_priority("ERROR", ERROR_INT,  3, 4);
  61. add_priority("WARN",  WARN_INT,   4, 3);
  62. add_priority("INFO",  INFO_INT,   6, 1);
  63. add_priority("DEBUG", DEBUG_INT,  7, 0);
  64. add_priority("ALL",   ALL_INT,    7, 0);
  65.  
  66. # we often sort numerically, so a helper func for readability
  67. sub numerically {$a <=> $b}
  68.  
  69. ###########################################
  70. sub import {
  71. ###########################################
  72.     my($class, $namespace) = @_;
  73.            
  74.     if(defined $namespace) {
  75.         # Export $OFF, $FATAL, $ERROR etc. to
  76.         # the given namespace
  77.         $namespace .= "::" unless $namespace =~ /::$/;
  78.     } else {
  79.         # Export $OFF, $FATAL, $ERROR etc. to
  80.         # the caller's namespace
  81.         $namespace = caller(0) . "::";
  82.     }
  83.  
  84.     for my $key (keys %PRIORITY) {
  85.         my $name  = "$namespace$key";
  86.         my $value = $PRIORITY{$key};
  87.         *{"$name"} = \$value;
  88.     my $nameint = "$namespace${key}_INT";
  89.     my $func = uc($key) . "_INT";
  90.     *{"$nameint"} = \&$func;
  91.     }
  92. }
  93.  
  94. ##################################################
  95. sub new { 
  96. ##################################################
  97.     # We don't need any of this class nonsense
  98.     # in Perl, because we won't allow subclassing
  99.     # from this. We're optimizing for raw speed.
  100. }
  101.  
  102. ##################################################
  103. sub to_priority {
  104. # changes a level name string to a priority numeric
  105. ##################################################
  106.     my($string) = @_;
  107.  
  108.     if(exists $PRIORITY{$string}) {
  109.         return $PRIORITY{$string};
  110.     }else{
  111.         croak "level '$string' is not a valid error level (".join ('|', keys %PRIORITY),')';
  112.     }
  113. }
  114.  
  115. ##################################################
  116. sub to_level {
  117. # changes a priority numeric constant to a level name string 
  118. ##################################################
  119.     my ($priority) = @_;
  120.     if (exists $LEVELS{$priority}) {
  121.         return $LEVELS{$priority}
  122.     }else {
  123.       croak("priority '$priority' is not a valid error level number (",
  124.       join("|", sort numerically keys %LEVELS), "
  125.           )");
  126.     }
  127.  
  128. }
  129.  
  130. ##################################################
  131. sub to_LogDispatch_string {
  132. # translates into strings that Log::Dispatch recognizes
  133. ##################################################
  134.     my($priority) = @_;
  135.  
  136.     confess "do what? no priority?" unless defined $priority;
  137.  
  138.     my $string;
  139.  
  140.     if(exists $LEVELS{$priority}) {
  141.         $string = $LEVELS{$priority};
  142.     }
  143.  
  144.         # Log::Dispatch idiosyncrasies
  145.     if($priority == $PRIORITY{WARN}) {
  146.         $string = "WARNING";
  147.     }
  148.          
  149.     if($priority == $PRIORITY{FATAL}) {
  150.         $string = "EMERGENCY";
  151.     }
  152.          
  153.     return $string;
  154. }
  155.  
  156. ###################################################
  157. sub is_valid {
  158. ###################################################
  159.     my $q = shift;
  160.  
  161.     if ($q =~ /[A-Z]/) {
  162.         return exists $PRIORITY{$q};
  163.     }else{
  164.         return $LEVELS{$q};
  165.     }
  166.     
  167. }
  168.  
  169. sub get_higher_level {
  170.     my ($old_priority, $delta) = @_;
  171.  
  172.     $delta ||= 1;
  173.  
  174.     my $new_priority = 0;
  175.  
  176.     foreach (1..$delta){
  177.         #so the list is DEBUG, INFO, WARN, ERROR, FATAL
  178.       # but remember, the numbers go in reverse order!
  179.         foreach my $p (sort numerically keys %LEVELS){
  180.             if ($p > $old_priority) {
  181.                 $new_priority = $p;
  182.                 last;
  183.             }
  184.         }
  185.         $old_priority = $new_priority;
  186.     }
  187.     return $new_priority;
  188. }
  189.  
  190. sub get_lower_level {
  191.     my ($old_priority, $delta) = @_;
  192.  
  193.     $delta ||= 1;
  194.  
  195.     my $new_priority = 0;
  196.  
  197.     foreach (1..$delta){
  198.         #so the list is FATAL, ERROR, WARN, INFO, DEBUG
  199.       # but remember, the numbers go in reverse order!
  200.         foreach my $p (reverse sort numerically keys %LEVELS){
  201.             if ($p < $old_priority) {
  202.                 $new_priority = $p;
  203.                 last;
  204.             }
  205.         }
  206.         $old_priority = $new_priority;
  207.     }
  208.     return $new_priority;
  209. }
  210.  
  211. sub isGreaterOrEqual {
  212.   my $lval = shift;
  213.   my $rval = shift;
  214.   
  215.   # in theory, we should check if the above really ARE valid levels.
  216.   # but we just use numeric comparison, since they aren't really classes.
  217.  
  218.   # oh, yeah, and 'cuz level ints go from 0 .. N with 0 being highest,
  219.   # these are reversed.
  220.   return $lval <= $rval;
  221. }
  222.  
  223. ######################################################################
  224. # since the integer representation of levels is reversed from what
  225. # we normally want, we don't want to use < and >... instead, we
  226. # want to use this comparison function
  227.  
  228.  
  229. 1;
  230.  
  231. __END__
  232.  
  233. =head1 NAME
  234.  
  235. Log::Log4perl::Level - Predefined log levels
  236.  
  237. =head1 SYNOPSIS
  238.  
  239.   use Log::Log4perl::Level;
  240.   print $ERROR, "\n";
  241.  
  242.   # -- or --
  243.  
  244.   use Log::Log4perl qw(:levels);
  245.   print $ERROR, "\n";
  246.  
  247. =head1 DESCRIPTION
  248.  
  249. C<Log::Log4perl::Level> simply exports a predefined set of I<Log4perl> log
  250. levels into the caller's name space. It is used internally by 
  251. C<Log::Log4perl>. The following scalars are defined:
  252.  
  253.     $OFF
  254.     $FATAL
  255.     $ERROR
  256.     $WARN
  257.     $INFO
  258.     $DEBUG
  259.     $ALL
  260.  
  261. C<Log::Log4perl> also exports these constants into the caller's namespace
  262. if you pull it in providing the C<:levels> tag:
  263.  
  264.     use Log::Log4perl qw(:levels);
  265.  
  266. This is the preferred way, there's usually no need to call 
  267. C<Log::Log4perl::Level> explicitely.
  268.  
  269. The numerical values assigned to these constants are purely virtual,
  270. only used by Log::Log4perl internally and can change at any time,
  271. so please don't make any assumptions.
  272.  
  273. If the caller wants to import these constants into a different namespace,
  274. it can be provided with the C<use> command:
  275.  
  276.     use Log::Log4perl::Level qw(MyNameSpace);
  277.  
  278. After this C<$MyNameSpace::ERROR>, C<$MyNameSpace::INFO> etc. 
  279. will be defined accordingly.
  280.  
  281. =head1 SEE ALSO
  282.  
  283. =head1 AUTHOR
  284.  
  285. Mike Schilli, E<lt>m@perlmeister.comE<gt>
  286.  
  287. =head1 COPYRIGHT AND LICENSE
  288.  
  289. Copyright 2002 by Mike Schilli
  290.  
  291. This library is free software; you can redistribute it and/or modify
  292. it under the same terms as Perl itself. 
  293.  
  294. =cut
  295.