File Coverage

blib/lib/Log/Fine/Handle.pm
Criterion Covered Total %
statement 44 44 100.0
branch 12 18 66.6
condition 10 18 55.5
subroutine 11 11 100.0
pod 4 4 100.0
total 81 95 85.2


line stmt bran cond sub pod time code
1              
2             =head1 NAME
3              
4             Log::Fine::Handle - Controls where to send logging output
5              
6             =head1 SYNOPSIS
7              
8             Sets up an output handle for log messages
9              
10             use Log::Fine;
11             use Log::Fine::Handle;
12              
13             # Instantiate the handle (default values shown)
14             my $handle = Log::Fine::Handle::Foo
15             ->new( name => "foo0",
16             mask => Log::Fine::Handle->DEFAULT_LOGMASK,
17             formatter => Log::Fine::Formatter:Basic->new() );
18              
19             # See if a handle is loggable at a given level
20             my $rc = $handle->isLoggable(INFO);
21              
22             # Write a message
23             $handle->msgWrite(INFO, "Informational message", 1);
24              
25             =head1 DESCRIPTION
26              
27             A Log::Fine::Handle object controls I to send formatted log
28             messages. The destination can be a file, syslog, a database table, or
29             simply to output. Message formatting is then handled by a
30             L object.
31              
32             =cut
33              
34 15     15   1706 use strict;
  15         19  
  15         330  
35 15     15   41 use warnings;
  15         17  
  15         386  
36              
37             package Log::Fine::Handle;
38              
39 15     15   44 use base qw( Log::Fine );
  15         13  
  15         854  
40              
41 15     15   53 use Log::Fine;
  15         10  
  15         220  
42 15     15   4744 use Log::Fine::Formatter::Basic;
  15         20  
  15         276  
43 15     15   53 use Log::Fine::Levels;
  15         14  
  15         4908  
44              
45             our $VERSION = $Log::Fine::VERSION;
46              
47             =head1 METHODS
48              
49             =head2 bitmaskListEnabled
50              
51             Gets a list of enabled bit masks
52              
53             =head3 Returns
54              
55             An array containing a list of strings representing bitmasks
56             enabled for this handle
57              
58             =cut
59              
60             sub bitmaskListEnabled
61             {
62              
63 1     1 1 5 my $self = shift;
64 1         3 my $map = $self->levelMap();
65 1         1 my @bitmasks = ();
66              
67             # Reminder: log() here is the perl logarithmic function (see
68             # perlfunc(3)) and is not to be confused with the name of this
69             # module ;)
70 1         3 foreach my $maskname ($map->logMasks()) {
71 8 100       16 push @bitmasks, $maskname
72             if $self->isLoggable(log($map->maskToValue($maskname)) / log(2) - 1);
73             }
74              
75 1         4 return @bitmasks;
76              
77             } # bitmaskListEnabled()
78              
79             =head2 formatter
80              
81             Getter/Setter for the objects formatter attribute
82              
83             =head3 Parameters
84              
85             =over
86              
87             =item * formatter
88              
89             B<[optional]> A valid L object
90              
91             =back
92              
93             =head3 Returns
94              
95             A L object
96              
97             =cut
98              
99             sub formatter
100             {
101              
102 2     2 1 3 my $self = shift;
103 2         2 my $formatter = shift;
104              
105             # Should the first argument is a valid formatter, then set the
106             # objects formatter attribute appropriately
107 2 50 66     22 $self->{formatter} = $formatter
      66        
      33        
108             if ( defined $formatter
109             and ref $formatter
110             and UNIVERSAL::can($formatter, 'isa')
111             and $formatter->isa("Log::Fine::Formatter"));
112              
113             # return the objects formatter attribute
114 2         11 return $self->{formatter};
115              
116             } # formatter()
117              
118             =head2 isLoggable
119              
120             Specifies whether the handle is loggable at the given level.
121              
122             =head3 Parameters
123              
124             =over
125              
126             =item * level
127              
128             Name of level or numeric value representing level
129              
130             =back
131              
132             =head3 Returns
133              
134             1 if this level is loggable, undef otherwise
135              
136             =cut
137              
138             sub isLoggable
139             {
140              
141 791     791 1 931 my $self = shift;
142 791         698 my $lvl = shift;
143              
144             # Return undef if level is not defined
145 791 50       1522 return unless defined $lvl;
146              
147             # Convert level to value if we are given a string, otherwise
148             # use value as is.
149 791 50       2687 my $val =
150             ($lvl =~ /^\d+$/) ? $lvl : $self->levelMap()->levelToValue($lvl);
151              
152             # Make sure we have a valid value
153 791 50       1561 return unless defined($val);
154              
155 791         926 my $shifted = 2 << $val;
156              
157             # Bitand the level and the mask to see if we're loggable
158 791 100       3029 return (($self->{mask} & $shifted) == $shifted) ? 1 : undef;
159              
160             } # isLoggable()
161              
162             =head2 msgWrite
163              
164             Tells the handle to output the given log message.
165              
166             B msgWrite() is an I method to the Log::Fine
167             framework, meant to be sub-classed. Use
168             L for actual logging.
169              
170             =head3 Parameters
171              
172             =over
173              
174             =item * level
175              
176             Level at which to log
177              
178             =item * message
179              
180             Message to log
181              
182             =item * skip
183              
184             Passed to L for accurate method logging
185              
186             =back
187              
188             =head3 Returns
189              
190             None
191              
192             =cut
193              
194             sub msgWrite
195             {
196              
197 1     1 1 1 my $self = shift;
198 1         2 my $class = ref $self;
199              
200 1 50       3 my $msg =
201             ($class eq 'Log::Fine::Handle')
202             ? "direct call to abstract method msgWrite()!\n See Log::Fine::Handle documentation"
203             : "call to abstract method ${class}::msgWrite()";
204              
205 1         6 $self->_fatal($msg);
206              
207             } # msgWrite()
208              
209             # --------------------------------------------------------------------
210              
211             ##
212             # Initializes our object
213              
214             sub _init
215             {
216              
217 402     402   373 my $self = shift;
218              
219             # Perform any necessary upper class initializations
220 402         914 $self->SUPER::_init();
221              
222             # Set default bitmask
223             $self->{mask} = $self->levelMap()->bitmaskAll()
224 402 100       840 unless defined $self->{mask};
225              
226             # Set the default formatter
227             $self->{formatter} = Log::Fine::Formatter::Basic->new()
228             unless ( defined $self->{formatter}
229             and ref $self->{formatter}
230             and UNIVERSAL::can($self->{formatter}, 'isa')
231 402 50 66     1511 and $self->{formatter}->isa("Log::Fine::Formatter"));
      66        
      33        
232              
233 402         633 return $self;
234              
235             } # _init()
236              
237             =head1 BUGS
238              
239             Please report any bugs or feature requests to
240             C, or through the web interface at
241             L.
242             I will be notified, and then you'll automatically be notified of progress on
243             your bug as I make changes.
244              
245             =head1 SUPPORT
246              
247             You can find documentation for this module with the perldoc command.
248              
249             perldoc Log::Fine
250              
251             You can also look for information at:
252              
253             =over 4
254              
255             =item * AnnoCPAN: Annotated CPAN documentation
256              
257             L
258              
259             =item * CPAN Ratings
260              
261             L
262              
263             =item * RT: CPAN's request tracker
264              
265             L
266              
267             =item * Search CPAN
268              
269             L
270              
271             =back
272              
273             =head1 AUTHOR
274              
275             Christopher M. Fuhrman, C<< >>
276              
277             =head1 SEE ALSO
278              
279             L, L, L
280              
281             =head1 COPYRIGHT & LICENSE
282              
283             Copyright (c) 2008, 2010-2011, 2013 Christopher M. Fuhrman,
284             All rights reserved.
285              
286             This program is free software licensed under the...
287              
288             The BSD License
289              
290             The full text of the license can be found in the
291             LICENSE file included with this module.
292              
293             =cut
294              
295             1; # End of Log::Fine::Handle