Interface/Logger.pm untested

filter: Statementstmt 0.0% 0 / 89 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 21 Podpod 0.0% 0 / 12 totaltotal 0.0% 0 / 122 CC 1 SCAR 6.9
CC
1
Cov
0%
_build_logger
2.0
open_log
2.0
_set_log_mask
2.0
CRAP 2.0
1 package LinkLocal::IPv4::Interface::Logger;
2  
3 our $VERSION = '0.19_01';
4  
5 require 5.010_000;
6  
7 # Copyright (C) 2010 Raymond Mroz, Tony Li Xu
8 #
9 # This program is free software: you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation, either version 3 of the License, or
12 # (at your option) any later version.
13 #
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License
20 # along with this program. If not, see <http://www.gnu.org/licenses/>.
21  
22 use LinkLocal::IPv4::Interface::Types;
23 use Moose;
24 use MooseX::Params::Validate;
25 use Try::Tiny;
26 use Sys::Syslog qw/ :standard :macros /;
27  
28 use feature ':5.10';
29  
30 # Log level constants
31 use constant {
32 EMERG => 0,
33 ALERT => 1,
34 CRIT => 2,
35 ERROR => 3,
36 WARN => 4,
37 NOTICE => 5,
38 INFO => 6,
39 DEBUG => 7,
40 };
41  
42 # Attributes
43 has '_logger' => (
44 isa => 'Sys::Syslog',
45 is => 'bare',
46 lazy => 1,
47 builder => '_build_logger',
48 );
49  
50 # Builder
51 sub _build_logger {
52 my $class = shift;
53 my $this = ref Sys::Syslog();
54  
55 bless $this, $class;
56 return $this->_logger;
57 }
58  
59 # Modifier
60 after '_build_logger' => sub {
61 my $this = shift;
62  
63 $this->_set_log_mask();
64 };
65  
66 # Methods
67 sub open_log {
68 my ( $self, %params ) = validated_hash(
69 \@_,
70 indent => { isa => 'Str', optional => 1, default => 'LinkLocal--IPv4' },
71 options => { isa => 'Str', optional => 1, default => 'pid,cons' },
72 facility => { isa => 'Str', optional => 1, default => 'user' }
73 );
74  
75 try {
76 openlog( $params{indent}, $params{options}, $params{facility} );
77 }
78 catch {
79 die "Unable to open syslog instance for writing: $!";
80 };
81 }
82  
83 sub _set_log_mask {
84 my $this = shift;
85  
86 try {
87 setlogmask( ~( &Sys::Syslog::LOG_MASK(NOTICE) ) );
88 }
89 catch {
90 die "Error while setting log mask: $!";
91 };
92 }
93  
94 sub emerg {
95 my $this = shift;
96 my ($message) = pos_validated_list( \@_, { isa => 'Str' } );
97  
98 try {
99 syslog( 'emerg', "%8s: %s", "Emerg", $message );
100 }
101 catch {
102 die "Error while writing to syslog: $!";
103 };
104 }
105  
106 sub alert {
107 my $this = shift;
108 my ($message) = pos_validated_list( \@_, { isa => 'Str' } );
109  
110 try {
111 syslog( 'alert', "%8s: %s", "Alert", $message );
112 }
113 catch {
114 die "Error while writing to syslog: $!";
115 };
116 }
117  
118 sub critical {
119 my $this = shift;
120 my ($message) = pos_validated_list( \@_, { isa => 'Str' } );
121  
122 try {
123 syslog( 'crit', "%8s: %s", "Critical", $message );
124 }
125 catch {
126 die "Error while writing to syslog: $!";
127 };
128 }
129  
130 sub error {
131 my $this = shift;
132 my ($message) = pos_validated_list( \@_, { isa => 'Str' } );
133  
134 try {
135 syslog( 'err', "%8s: %s", "Error", $message );
136 }
137 catch {
138 die "Error while writing to syslog: $!";
139 };
140 }
141  
142 sub warning {
143 my $this = shift;
144 my ($message) = pos_validated_list( \@_, { isa => 'Str' } );
145  
146 try {
147 syslog( 'warning', "%8s: %s", "Warning", $message );
148 }
149 catch {
150 die "Error while writing to syslog: $!";
151 };
152 }
153  
154 sub notice {
155 my $this = shift;
156 my ($message) = pos_validated_list( \@_, { isa => 'Str' } );
157  
158 try {
159 syslog( 'notice', "%8s: %s", "Notice", $message );
160 }
161 catch {
162 die "Error while writing to syslog: $!";
163 };
164 }
165  
166 sub info {
167 my $this = shift;
168 my ($message) = pos_validated_list( \@_, { isa => 'Str' } );
169  
170 try {
171 syslog( 'info', "%8s: %s", "Info", $message );
172 }
173 catch {
174 die "Error while writing to syslog: $!";
175 };
176 }
177  
178 sub debug {
179 my $this = shift;
180 my ($message) = pos_validated_list( \@_, { isa => 'Str' } );
181  
182 try {
183 syslog( 'debug', "%8s: %s", "Debug", $message );
184 }
185 catch {
186 die "Error while writing to syslog: $!";
187 };
188 }
189  
190 sub close_log {
191 my $this = shift;
192  
193 try {
194 closelog();
195 }
196 catch {
197 die "Error while closing syslog: $!";
198 };
199 }
200  
201 no Moose;
202  
203 __PACKAGE__->meta->make_immutable;
204  
205 =head1 NAME
206  
207 LinkLocal::IPv4::Interface::Logger - A Moose-wrapped syslog interface.
208  
209 =head1 SYNOPSIS
210  
211 my $logger = LinkLocal::IPv4::Interface::Logger->new();
212  
213 $logger->open_log( indent => 'IPv4' );
214 $logger->error("This is an error test");
215 $logger->emerg("This is an emerg test");
216 $logger->close_log();
217
218 $logger->open_log( indent => 'IPv4--Test', facility => 'local3' );
219 $logger->debug("This is a debug test");
220 $logger->notice("This is a notice test");
221 $logger->close_log();
222  
223 =head1 DESCRIPTION
224  
225 This package provides for a Moose'ish encapsulation of basic syslog functionality. It is in most
226 ways a simple delegation or mapping to those calls and macros present via L<Sys::Syslog>, but does
227 so in an OOPish and Moose'ish way.
228  
229 =head2 ATTRIBUTES
230  
231 =over 4
232  
233 =head3 Public Attributes
234  
235 =item C<N/A>
236  
237 =head3 Private Attributes
238  
239 =item C<_logger>
240  
241 The wrapped logger object instance.
242  
243 =back
244  
245 =head2 METHODS
246  
247 =over4
248  
249 =head3 Constructors
250  
251 =item C<new>
252  
253 The default constructor takes no arguments and returns an instance of the Logger
254 object.
255  
256 =head3 Public Methods
257  
258 =item C<open_log>
259  
260 Opens up the syslog instance for writing. Can take optional indent, options and
261 facility arguments although sane defaults are provided by L<MooseX::Params::Validate>.
262  
263 =item C<emerg>
264  
265 =item C<alert>
266  
267 =item C<critical>
268  
269 =item C<error>
270  
271 =item C<warning>
272  
273 =item C<notice>
274  
275 =item C<info>
276  
277 =item C<debug>
278  
279 Each of the above public methods take a message argument and logs it at the appropriate
280 level.
281  
282 =item C<close_log>
283  
284 Close the syslog instance for writing.
285  
286 =head3 Private Methods
287  
288 =item C<_set_log_mask>
289  
290 This private method is used to set a default log level mask. Called by the logger's builder
291 method modifier.
292  
293 =item C<_build_logger>
294  
295 Builds a logger object instance from the L<Sys::Syslog> interface and wraps it as a L<Moose>
296 class type.
297  
298 =back
299  
300 =head1 SEE ALSO
301  
302 Refer to RFC-3927, I<Dynamic Configuration of IPv4 Link-Local Adresses>, the complete
303 text of which can be found in the top level of the package archive.
304  
305 L<perl>, L<Moose>, L<MooseX::Params::Validate>, L<Sys::Syslog>, L<Try::Tiny>
306  
307 This project is also hosted on github at:
308 git@github.com:raymroz/LinkLocal--IPv4.git
309  
310 =head1 BUGS
311  
312 What's a bug???
313  
314 =head1 AUTHOR
315  
316 Tony Li Xu, E<lt>tonylixu@gmail.comE<gt>, Ray Mroz, E<lt>mroz@cpan.orgE<gt>
317  
318 =head1 COPYRIGHT AND LICENSE
319  
320 Copyright (C) 2010 Ray Mroz
321  
322 This program is free software: you can redistribute it and/or modify
323 it under the terms of the GNU General Public License as published by
324 the Free Software Foundation, either version 3 of the License, or
325 (at your option) any later version.
326  
327 This program is distributed in the hope that it will be useful,
328 but WITHOUT ANY WARRANTY; without even the implied warranty of
329 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
330 GNU General Public License for more details.
331  
332 You should have received a copy of the GNU General Public License
333 along with this program. If not, see <http://www.gnu.org/licenses/>.
334  
335  
336 =cut
337