File Coverage

blib/lib/Log/Cabin.pm
Criterion Covered Total %
statement 18 80 22.5
branch 0 28 0.0
condition 0 12 0.0
subroutine 6 18 33.3
pod 0 9 0.0
total 24 147 16.3


line stmt bran cond sub pod time code
1             package Log::Cabin;
2              
3 1     1   29996 use strict;
  1         2  
  1         232  
4 1     1   7 use warnings;
  1         2  
  1         43  
5              
6             require Exporter;
7 1     1   988 use AutoLoader qw(AUTOLOAD);
  1         7728  
  1         6  
8              
9             our @ISA = qw(Exporter);
10              
11             # Items to export into callers namespace by default. Note: do not export
12             # names by default without a very good reason. Use EXPORT_OK instead.
13             # Do not simply export all your public functions/methods/constants.
14              
15             # This allows declaration use Log::Cabin ':all';
16             # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
17             # will save memory.
18             our %EXPORT_TAGS = ( 'all' => [ qw(
19            
20             ) ] );
21              
22             our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
23              
24             our @EXPORT = qw(
25            
26             );
27              
28             our $VERSION = '0.06';
29              
30              
31             # Preloaded methods go here.
32              
33 1     1   126 use strict;
  1         2  
  1         32  
34 1     1   992 use Sys::Hostname;
  1         1476  
  1         61  
35 1     1   543 use Log::Cabin::Foundation;
  1         3  
  1         873  
36              
37             my $_DEFAULT_LOG_CATEGORY = "Log::Cabin";
38             my $_IS_INIT=0;
39             my $OFF=0;
40             my $FATAL=1;
41             my $ERROR=2;
42             my $WARN=3;
43             my $INFO=4;
44             my $DEBUG=5;
45             my $ALL=6;
46              
47             my $LEVELTEXT = {0=>'OFF',
48             1=>'FATAL',
49             2=>'ERROR',
50             3=>'WARN',
51             4=>'INFO',
52             5=>'DEBUG',
53             6=>'ALL'};
54              
55             my $SINGLETON_INSTANCE=undef;
56              
57             ## logger collection is a hash to ensure that calls for the same package
58             # name return the same logger. This was previously an array, which
59             # was Very Bad.
60             my %ALL_LOGGERS;
61              
62             sub new {
63 0     0 0   my ($class) = shift;
64 0 0         if ($_IS_INIT == 1) {
65 0           return $SINGLETON_INSTANCE;
66             } else {
67 0   0       my $self = bless {}, ref($class) || $class;
68 0           $self->_init(@_);
69              
70 0 0         unless ($self->{_SKIP_INIT}) {
71 0           $_IS_INIT=1;
72 0           $SINGLETON_INSTANCE = $self;
73            
74 0           for (values %ALL_LOGGERS) {
75 0           $_->set_logger_instance($SINGLETON_INSTANCE);
76             }
77             }
78            
79 0           return $self;
80             }
81             }
82              
83             sub DESTROY{
84 0     0     my $self = shift;
85 0 0         if(defined $self->{_OUTPUT_HANDLE}){
86 0           close $self->{_OUTPUT_HANDLE};
87             }
88              
89             }
90              
91             sub _init {
92 0     0     my $self = shift;
93 0 0         die if($_IS_INIT==1);
94 0           $self->{_DEFAULT_LOG_LEVEL} = $WARN;
95 0           $self->{_LOG_LEVEL} = $self->{_DEFAULT_LOG_LEVEL};
96 0           $self->{_LOG_FILE} = undef;
97 0           $self->{_HOSTNAME} = hostname;
98 0           $self->{_PID} = $$;
99 0           $self->{_CLOBBER} = 1;
100 0           my %arg = @_;
101 0           foreach my $key (keys %arg) {
102 0           $self->{"_$key"} = $arg{$key};
103             }
104             }
105              
106             sub initialized{
107 0     0 0   return $_IS_INIT;
108             }
109              
110             sub get_instance{
111 0     0 0   return $SINGLETON_INSTANCE;
112             }
113              
114             sub set_file_output{
115 0     0 0   my($self,$filename) = @_;
116 0           $self->{_LOG_FILE} = $filename;
117 0           my $filehandle;
118 0 0         if($self->{_CLOBBER}){
119 0 0         open($filehandle,"+>",$self->{_LOG_FILE})
120             or die "Can't open log file for writing $self->{_LOG_FILE}";
121             }
122             else{
123 0 0         open($filehandle,">",$self->{_LOG_FILE})
124             or die "Can't open log file for writing $self->{_LOG_FILE}";
125             }
126 0           $self->{_OUTPUT_HANDLE} = $filehandle;
127             }
128              
129             sub set_output{
130 0     0 0   my($self,$handle) = @_;
131 0           $self->{_OUTPUT_HANDLE} = $handle;
132             }
133              
134             sub level{
135 0     0 0   my ($self, $level) = @_;
136 0 0 0       if (defined $level && $level =~ /^\-*\d+$/) {
137 0           $self->{_LOG_LEVEL} = $level;
138             }
139 0           return $self->{_LOG_LEVEL};
140             }
141              
142             sub more_logging{
143 0     0 0   my($self,$level) = @_;
144 0 0 0       if(defined $level && $level =~ /^\-*\d+$/){
145 0           $self->{_LOG_LEVEL} += $level;
146             }
147             }
148              
149             sub less_logging{
150 0     0 0   my($self,$level) = @_;
151 0 0 0       if(defined $level && $level =~ /^\-*\d+$/){
152 0           $self->{_LOG_LEVEL} -= $level;
153             }
154             }
155              
156             sub _output{
157 0     0     my($self,$msg,$loggername,$level,$package,$filename,$line,$subroutine) = @_;
158 0           my $datestamp = localtime(time());
159 0 0         if (defined $self->{_OUTPUT_HANDLE}) {
160 0           print {$self->{_OUTPUT_HANDLE}} "$loggername $LEVELTEXT->{$level} $datestamp $self->{_HOSTNAME}:$self->{_PID} $filename:$package:$subroutine:$line || $msg\n";
  0            
161             }
162             }
163              
164             sub get_logger {
165 0     0 0   my($class, @args) = @_;
166              
167 0           my $singleton;
168 0 0         if (defined $SINGLETON_INSTANCE) {
    0          
169 0           $singleton = $SINGLETON_INSTANCE;
170             } elsif (ref $class) {
171 0           $singleton = $class;
172             } else {
173 0           $singleton = new Log::Cabin('SKIP_INIT'=>1);
174             }
175            
176 0 0         if (!ref $class) {
177 0           @args = ($class,@args);
178             }
179            
180 0           my $loggerinst = new Log::Cabin::Foundation($singleton, @args);
181 0           $ALL_LOGGERS{$loggerinst->name} = $loggerinst;
182 0           return $loggerinst;
183             }
184              
185             # Autoload methods go after =cut, and are processed by the autosplit program.
186              
187             1;
188             __END__