Wombat/Logger/FileLogger.pm untested

filter: Statementstmt 0.0% 0 / 51 Branchbran 0.0% 0 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 77 CC 5 SCAR 13.9
CC
5
Cov
0%
new
6.0
setFilename
6.0
start
6.0
CRAP 4.0
1 # -*- Mode: Perl; indent-tabs-mode: nil; -*-
2  
3 package Wombat::Logger::FileLogger;
4  
5 =pod
6  
7 =head1 NAME
8  
9 Wombat::Logger::FileLogger - file logger class
10  
11 =head1 SYNOPSIS
12  
13 my $logger = Wombat::Logger::FileLogger->new();
14 $logger->setFileName("/var/log/wombat/wombat.log");
15 $logger->log("this will show up in the log file");
16  
17 =head1 DESCRIPTION
18  
19 Convenience base class for logger implementations. The only method
20 that must be implemented is C<write()>, plus any accessor methods
21 required for configuration.
22  
23 =cut
24  
25 use base qw(Wombat::Logger::LoggerBase);
26 use fields qw(fh filename);
27 use strict;
28 use warnings;
29  
30 use Symbol ();
31 use Wombat::Exception ();
32  
33 =pod
34  
35 =head1 CONSTRUCTOR
36  
37 =over
38  
39 =item new()
40  
41 Construct and return a B<Wombat::Logger::FileLogger> instance,
42 initializing fields appropriately. If subclasses override the
43 constructor, they must be sure to call
44  
45 $self->SUPER::new();
46  
47 =back
48  
49 =cut
50  
51 sub new {
52 my $self = shift;
53  
54 $self = fields::new($self) unless ref $self;
55 $self->SUPER::new();
56  
57 $self->{fh} = undef;
58 $self->{filename} = undef;
59  
60 return $self;
61 }
62  
63 =pod
64  
65 =head1 ACCESSOR METHODS
66  
67 =over
68  
69 =item getFilename()
70  
71 Return the name of the file that is the log destination.
72  
73 =cut
74  
75 sub getFilename {
76 my $self = shift;
77  
78 return $self->{filename};
79 }
80  
81 =pod
82  
83 =item setFilename($filename)
84  
85 Set the name of the file that is the log destination.
86  
87 B<Parameters:>
88  
89 =over
90  
91 =item $filename
92  
93 the name of the file
94  
95 =back
96  
97 =cut
98  
99 sub setFilename {
100 my $self = shift;
101 my $filename = shift;
102  
103 $filename = File::Spec->rel2abs($filename, $ENV{WOMBAT_HOME}) unless
104 File::Spec->file_name_is_absolute($filename);
105 $self->{filename} = $filename;
106  
107 return 1;
108 }
109  
110 =pod
111  
112 =back
113  
114 =head1 PUBLIC METHODS
115  
116 =over
117  
118 =item write($string)
119  
120 Write the specified string to the log destination. The default
121 implementation does nothing. Subclasses must override this method.
122  
123 B<Parameters:>
124  
125 =over
126  
127 =item $string
128  
129 the string to write to the log destination
130  
131 =back
132  
133 =cut
134  
135 sub write {
136 my $self = shift;
137  
138 $self->{fh}->print(@_);
139  
140 return 1;
141 }
142  
143 =pod
144  
145 =back
146  
147 =head1 LIFECYCLE METHODS
148  
149 =over
150  
151 =item start()
152  
153 Prepare for the beginning of active use of this Logger by opening the
154 file.
155  
156 B<Throws:>
157  
158 =over
159  
160 =item B<Wombat::LifecycleException>
161  
162 if the file cannot be opened
163  
164 =back
165  
166 =cut
167  
168 sub start {
169 my $self = shift;
170  
171 $self->SUPER::start();
172  
173 # ungh dum
174 undef $self->{started};
175  
176 $self->{fh} = Symbol::gensym();
177 unless (open $self->{fh}, ">> $self->{filename}") {
178 my $msg = "can't open $self->{filename}: $!";
179 Wombat::LifecycleException->throw($msg);
180 };
181 $self->{fh}->autoflush();
182  
183 $self->{started} = 1;
184  
185 return 1;
186 }
187  
188 =pod
189  
190 =item stop()
191  
192 Gracefully terminate the active use of this Logger by closing the
193 file.
194  
195 B<Throws:>
196  
197 =over
198  
199 =item B<Wombat::LifecycleException>
200  
201 if the file cannot be closed
202  
203 =back
204  
205 =cut
206  
207 sub stop {
208 my $self = shift;
209  
210 $self->SUPER::stop();
211  
212 unless (close $self->{fh}) {
213 my $msg = "cannot close $self->{filename}: $!";
214 Wombat::LifecycleException->throw($msg);
215 };
216  
217 return 1;
218 }
219  
220 1;
221 __END__