File Coverage

lib/Mail/SpamAssassin/Logger/Stderr.pm
Criterion Covered Total %
statement 26 41 63.4
branch 0 10 0.0
condition 1 3 33.3
subroutine 8 9 88.8
pod 0 3 0.0
total 35 66 53.0


line stmt bran cond sub pod time code
1             # <@LICENSE>
2             # Licensed to the Apache Software Foundation (ASF) under one or more
3             # contributor license agreements. See the NOTICE file distributed with
4             # this work for additional information regarding copyright ownership.
5             # The ASF licenses this file to you under the Apache License, Version 2.0
6             # (the "License"); you may not use this file except in compliance with
7             # the License. You may obtain a copy of the License at:
8             #
9             # http://www.apache.org/licenses/LICENSE-2.0
10             #
11             # Unless required by applicable law or agreed to in writing, software
12             # distributed under the License is distributed on an "AS IS" BASIS,
13             # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14             # See the License for the specific language governing permissions and
15             # limitations under the License.
16             # </@LICENSE>
17              
18             =head1 NAME
19              
20             Mail::SpamAssassin::Logger::Stderr - log to standard error
21              
22             =head1 SYNOPSIS
23              
24             loadplugin Mail::SpamAssassin::Logger::Stderr
25              
26             =head1 DESCRIPTION
27              
28             =cut
29              
30             package Mail::SpamAssassin::Logger::Stderr;
31              
32 43     43   283 use strict;
  43         284  
  43         1623  
33 43     43   1975 use warnings;
  43         91  
  43         1414  
34             # use bytes;
35 43     43   251 use re 'taint';
  43         116  
  43         1515  
36              
37 43     43   1253 use POSIX ();
  43         14339  
  43         774  
38 43     43   23060 use Time::HiRes ();
  43         59138  
  43         3897  
39              
40             our @ISA = ();
41              
42             # ADDING OS-DEPENDENT LINE TERMINATOR - BUG 6456
43              
44             # Using Mail::SpamAssassin::Util::am_running_on_windows() leads to circular
45             # dependencies. So, we are duplicating the code instead.
46 43     43   314 use constant RUNNING_ON_WINDOWS => ($^O =~ /^(?:mswin|dos|os2)/oi);
  43         89  
  43         18895  
47              
48             my $eol = "\n";
49             if (RUNNING_ON_WINDOWS) {
50             $eol = "\r\n";
51             }
52              
53             sub new {
54 43     43 0 107 my $class = shift;
55              
56 43   33     359 $class = ref($class) || $class;
57 43         93 my $self = { };
58 43         98 bless ($self, $class);
59              
60 43         91 my %params = @_;
61 43         212 $self->{timestamp_fmt} = $params{timestamp_fmt};
62              
63 43         184 return($self);
64             }
65              
66             sub log_message {
67 0     0 0 0 my ($self, $level, $msg) = @_;
68              
69 0         0 my $timestamp;
70 0         0 my $fmt = $self->{timestamp_fmt};
71 0 0       0 if (!defined $fmt) {
    0          
72             # default since 3.3.0
73 0         0 my $now = Time::HiRes::time;
74 0         0 my $datetime = POSIX::strftime("%b %d %H:%M", localtime($now));
75 0 0       0 utf8::encode($datetime) if utf8::is_utf8($datetime); # Bug 7305
76 0         0 $timestamp = sprintf("%s:%06.3f", $datetime, $now-int($now/60)*60);
77             # Bug 6329: %e is not in a POSIX standard, use %d instead and edit
78 0         0 local $1; $timestamp =~ s/^(\S+\s+)0/$1 /;
  0         0  
79             } elsif ($fmt eq '') {
80 0         0 $timestamp = '';
81             } else {
82 0         0 $timestamp = POSIX::strftime($fmt, localtime(Time::HiRes::time));
83             }
84 0 0       0 $timestamp .= ' ' if $timestamp ne '';
85              
86 0         0 my($nwrite) = syswrite(STDERR, sprintf("%s[%d] %s: %s%s",
87             $timestamp, $$, $level, $msg, $eol));
88 0 0       0 defined $nwrite or warn "error writing to log file: $!";
89             }
90              
91             sub close_log {
92 43     43 0 466 my ($self) = @_;
93             }
94              
95             1;