File Coverage

blib/lib/SVK/Logger.pm
Criterion Covered Total %
statement 41 55 74.5
branch 4 8 50.0
condition 2 5 40.0
subroutine 7 14 50.0
pod n/a
total 54 82 65.8


line stmt bran cond sub pod time code
1             # BEGIN BPS TAGGED BLOCK {{{
2             # COPYRIGHT:
3             #
4             # This software is Copyright (c) 2003-2008 Best Practical Solutions, LLC
5             #
6             #
7             # (Except where explicitly superseded by other copyright notices)
8             #
9             #
10             # LICENSE:
11             #
12             #
13             # This program is free software; you can redistribute it and/or
14             # modify it under the terms of either:
15             #
16             # a) Version 2 of the GNU General Public License. You should have
17             # received a copy of the GNU General Public License along with this
18             # program. If not, write to the Free Software Foundation, Inc., 51
19             # Franklin Street, Fifth Floor, Boston, MA 02110-1301 or visit
20             # their web page on the internet at
21             # http://www.gnu.org/copyleft/gpl.html.
22             #
23             # b) Version 1 of Perl's "Artistic License". You should have received
24             # a copy of the Artistic License with this package, in the file
25             # named "ARTISTIC". The license is also available at
26             # http://opensource.org/licenses/artistic-license.php.
27             #
28             # This work is distributed in the hope that it will be useful, but
29             # WITHOUT ANY WARRANTY; without even the implied warranty of
30             # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
31             # General Public License for more details.
32             #
33             # CONTRIBUTION SUBMISSION POLICY:
34             #
35             # (The following paragraph is not intended to limit the rights granted
36             # to you to modify and distribute this software under the terms of the
37             # GNU General Public License and is only of importance to you if you
38             # choose to contribute your changes and enhancements to the community
39             # by submitting them to Best Practical Solutions, LLC.)
40             #
41             # By intentionally submitting any modifications, corrections or
42             # derivatives to this work, or any other work intended for use with SVK,
43             # to Best Practical Solutions, LLC, you confirm that you are the
44             # copyright holder for those contributions and you grant Best Practical
45             # Solutions, LLC a nonexclusive, worldwide, irrevocable, royalty-free,
46             # perpetual, license to use, copy, create derivative works based on
47             # those contributions, and sublicense and distribute those contributions
48             # and any derivatives thereof.
49             #
50             # END BPS TAGGED BLOCK }}}
51             package SVK::Logger;
52 188     188   2194 use strict;
  188         363  
  188         8659  
53 188     188   1204 use warnings;
  188         383  
  188         18376  
54              
55 188     188   1081 use SVK::Version; our $VERSION = $SVK::VERSION;
  188         337  
  188         89985  
56              
57             if (eval {
58             require Log::Log4perl;
59             Log::Log4perl->import(':levels');
60             1;
61             } ) {
62             my $level = lc($ENV{SVKLOGLEVEL} || "info");
63             $level = { map { $_ => uc $_ } qw( debug info warn error fatal ) }
64             ->{ $level } || 'INFO';
65              
66             my $conf_file = $ENV{SVKLOGCONFFILE};
67             my $conf;
68             if ( defined($conf_file) and -e $conf_file ) {
69             my $fh;
70             open $fh, $conf_file or die $!;
71             local $/;
72             $conf = <$fh>;
73             close $fh;
74             }
75             #warn $conf unless $Log::Log4perl::Logger::INITIALIZED;
76             $conf ||= qq{
77             log4perl.rootLogger=$level, Screen
78             log4perl.appender.Screen = Log::Log4perl::Appender::Screen
79             log4perl.appender.Screen.stderr = 0
80             log4perl.appender.Screen.layout = PatternLayout
81             log4perl.appender.Screen.layout.ConversionPattern = %m%n
82             };
83              
84             # ... passed as a reference to init()
85             Log::Log4perl::init( \$conf ) unless Log::Log4perl->initialized;
86             *get_logger = sub { Log::Log4perl->get_logger(@_) };
87             }
88             else {
89 188     188   457 *get_logger = sub { 'SVK::Logger::Compat' };
90             }
91              
92             sub import {
93 188     188   440 my $class = shift;
94 188   50     1610 my $var = shift || 'logger';
95            
96             # it's ok if people add a sigil; we can get rid of that.
97 188         1246 $var =~ s/^\$*//;
98            
99             # Find out which package we'll export into.
100 188         720 my $caller = caller() . '';
101              
102 188         745 (my $name = $caller) =~ s/::/./g;
103 188         865 my $logger = get_logger(lc($name));
104             {
105             # As long as we don't use a package variable, each module we export
106             # into will get their own object. Also, this allows us to decide on
107             # the exported variable name. Hope it isn't too bad form...
108 188     188   1398 no strict 'refs';
  188         630  
  188         117318  
  188         685  
109 188         378 *{ $caller . "::$var" } = \$logger;
  188         31397  
110             }
111             }
112              
113             package SVK::Logger::Compat;
114             require Carp;
115              
116             my $current_level;
117             my $level;
118              
119             BEGIN {
120 188     188   4149 my $i;
121 188         499 $level = { map { $_ => ++$i } reverse qw( debug info warn error fatal ) };
  940         3132  
122 188   33     3377 $current_level = $level->{lc($ENV{SVKLOGLEVEL} || "info")} || $level->{info};
123              
124 188     0   1078 my $ignore = sub { return };
  0         0  
125             my $warn = sub {
126 0     0   0 shift;
127 0         0 my $s = join "", @_;
128 0         0 chomp $s;
129 0         0 print "$s\n";
130 188         1108 };
131 188     0   879 my $die = sub { shift; die $_[0]."\n"; };
  0         0  
  0         0  
132 188     0   711 my $carp = sub { shift; goto \&Carp::carp };
  0         0  
  0         0  
133 188     0   1468 my $confess = sub { shift; goto \&Carp::confess };
  0         0  
  0         0  
134 188     0   875 my $croak = sub { shift; goto \&Carp::croak };
  0         0  
  0         0  
135              
136 188 50       1259 *debug = $current_level >= $level->{debug} ? $warn : $ignore;
137 188 50       783 *info = $current_level >= $level->{info} ? $warn : $ignore;
138 188 50       2084 *warn = $current_level >= $level->{warn} ? $warn : $ignore;
139 188 50       833 *error = $current_level >= $level->{warn} ? $warn : $ignore;
140 188         464 *fatal = $die;
141 188         375 *logconfess = $confess;
142 188         418 *logdie = $die;
143 188         368 *logcarp = $carp;
144 188         20584 *logcroak = $croak;
145              
146             }
147              
148 0     0     sub is_debug { $current_level >= $level->{debug} }
149              
150             1;
151              
152             __END__