Filter/Logger/IOHandle.pm untested

filter: Statementstmt 0.0% 0 / 39 Branchbran 0.0% 0 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 59 CC 4 SCAR 13.5
CC
4
Cov
0%
new
6.0
log
6.0
log_rejected_message
6.0
CRAP 3.8
1 #
2 # Courier::Filter::Logger::IOHandle class
3 #
4 # (C) 2004-2008 Julian Mehnle <julian@mehnle.net>
5 # $Id: IOHandle.pm 210 2008-03-21 19:30:31Z julian $
6 #
7 ###############################################################################
8  
9 =head1 NAME
10  
11 Courier::Filter::Logger::IOHandle - I/O handle logger for the Courier::Filter
12 framework
13  
14 =cut
15  
16 package Courier::Filter::Logger::IOHandle;
17  
18 use warnings;
19 use strict;
20  
21 use base 'Courier::Filter::Logger';
22  
23 use IO::Handle;
24  
25 use constant TRUE => (0 == 0);
26 use constant FALSE => not TRUE;
27  
28 =head1 SYNOPSIS
29  
30 use Courier::Filter::Logger::IOHandle;
31
32 my $logger = Courier::Filter::Logger::IOHandle->new(
33 handle => $handle
34 );
35
36 # For use in an individual filter module:
37 my $module = Courier::Filter::Module::My->new(
38 ...
39 logger => $logger,
40 ...
41 );
42
43 # For use as a global Courier::Filter logger object:
44 my $filter = Courier::Filter->new(
45 ...
46 logger => $logger,
47 ...
48 );
49  
50 =head1 DESCRIPTION
51  
52 This class is an I/O handle logger class for use with Courier::Filter and its
53 filter modules.
54  
55 =cut
56  
57 # Implementation:
58 ###############################################################################
59  
60 =head2 Constructor
61  
62 The following constructor is provided:
63  
64 =over
65  
66 =item B<new(%options)>: returns I<Courier::Filter::Logger::IOHandle>
67  
68 Creates a new logger that logs messages as lines to an I/O handle.
69  
70 %options is a list of key/value pairs representing any of the following
71 options:
72  
73 =over
74  
75 =item B<handle>
76  
77 I<Required>. The I/O handle or B<IO::Handle> object to which log messages
78 should be written.
79  
80 =item B<timestamp>
81  
82 A boolean value controlling whether every log message line should be prefixed
83 with a timestamp (in local time, in ISO format). Defaults to B<false>.
84  
85 =back
86  
87 =cut
88  
89 sub new {
90 my ($class, %options) = @_;
91
92 my $self = $class->SUPER::new(%options);
93
94 $self->{autoflush} = TRUE
95 if not defined($self->{autoflush});
96 $self->{handle}->autoflush($self->{autoflush});
97
98 return $self;
99 }
100  
101 =back
102  
103 =head2 Instance methods
104  
105 The following instance methods are provided:
106  
107 =over
108  
109 =begin comment
110  
111 =item B<log($text)>: throws Perl exceptions
112  
113 Logs the text given as C<$text> (a string which may contain newlines).
114 Prefixes each line with a timestamp if the C<timestamp> option has been set
115 through the constructor.
116  
117 =end comment
118  
119 =cut
120  
121 sub log {
122 my ($self, $text) = @_;
123
124 my $timestamp = '';
125 if ($self->{timestamp}) {
126 my ($y, $m, $d, $h, $n, $s) = (localtime)[5,4,3,2,1,0];
127 $timestamp = sprintf(
128 "%04d-%02d-%02d %02d:%02d:%02d ",
129 $y+1900, $m+1, $d, $h, $n, $s
130 );
131 }
132
133 my @lines = split(/\n/, $text);
134 $self->{handle}->print("$timestamp$_\n")
135 foreach @lines;
136
137 return;
138 }
139  
140 =item B<log_error($text)>: throws Perl exceptions
141  
142 Logs the error message given as C<$text> (a string which may contain newlines).
143 Prefixes each line with a timestamp if the C<timestamp> option has been set
144 through the constructor.
145  
146 =cut
147  
148 sub log_error {
149 my ($self, $text) = @_;
150 return $self->log($text);
151 }
152  
153 =item B<log_rejected_message($message, $reason)>: throws Perl exceptions
154  
155 Logs the B<Courier::Message> given as C<$message> as having been rejected due
156 to C<$reason> (a string which may contain newlines).
157  
158 =cut
159  
160 sub log_rejected_message {
161 my ($self, $message, $reason) = @_;
162
163 $reason =~ s/^/Reason: /gm;
164
165 my $text = sprintf(
166 "Rejected message sent from %s to %s through %s\n%s\n",
167 '<' . $message->sender . '>',
168 join(', ', map("<$_>", $message->recipients)),
169 $message->remote_host . (
170 $message->remote_host_name ?
171 ' (' . $message->remote_host_name . ')'
172 : ''
173 ),
174 $reason
175 );
176 return $self->log($text);
177 }
178  
179 =back
180  
181 =head1 SEE ALSO
182  
183 L<Courier::Filter::Logger>, L<Courier::Filter::Overview>.
184  
185 For AVAILABILITY, SUPPORT, and LICENSE information, see
186 L<Courier::Filter::Overview>.
187  
188 =head1 AUTHOR
189  
190 Julian Mehnle <julian@mehnle.net>
191  
192 =cut
193  
194 TRUE;