blib/lib/Log/Common.pm untested

filter: Statementstmt 0.0% 0 / 72 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 50 MC/DCmcdc 0.0% 0 / 29 Subroutinesub 0.0% 0 / 6 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 184 CC 13 SCAR 34.8
CC
13
Cov
0%
error
56.0
new
30.0
access
12.0
CRAP 32.4
1 # Log::Common - write log file entries in the httpd common log formats
2 # $Id: Common.pm,v 1.2 1998/10/16 17:37:04 martin Exp $
3  
4 # Copyright (c) 1998 Martin Hamilton and Jon Knight. All rights reserved.
5  
6 # This program is free software; you can redistribute it and/or modify it
7 # under the same terms as Perl itself.
8  
9 package Log::Common;
10  
11 use strict;
12 use vars qw($VERSION);
13 use Carp;
14  
15 $VERSION = "1.00";
16  
17  
18 sub new {
19 my ($this, %args) = @_;
20 my $class = ref($this) || $this;
21 my ($a, $v, $access_fd, $error_fd);
22 my ($self) = {};
23  
24 while(($a, $v) = each %args) { $self->{$a} = $v; }
25 bless $self, $class;
26  
27 if ($args{access}) {
28 open(ACCESS_FD, ">>$args{access}");
29 $access_fd = *ACCESS_FD;
30 flock($access_fd, 2);
31 }
32  
33 if ($args{error}) {
34 open(ERROR_FD, ">>$args{error}");
35 $error_fd = *ERROR_FD;
36 flock($error_fd, 2);
37 }
38  
39 $self->{access_fd} = $access_fd;
40 $self->{error_fd} = $error_fd;
41 $self->{class} = $args{class} if $args{class};
42 $self->{no_stderr} = $args{no_stderr} if $args{no_stderr};
43 return $self;
44 }
45  
46  
47 # write "error" log file entry in httpd server common log format
48 sub error {
49 my ($self, %args) = @_;
50 my ($class) = ($args{class} || $self->{class}) || undef;
51 my ($no_stderr) = ($args{no_stderr} || $self->{no_stderr}) || 0;
52 my ($message) = ($args{message} || $self->{message}) || "nada";
53 my ($info) = ($args{info} || $self->{info}) || undef;
54 my ($LOG);
55  
56 # don't send the error message to STDERR if we're running as a
57 # CGI program, or in harness to one
58 if (!defined($ENV{"GATEWAY_INTERFACE"}) || !$no_stderr) {
59 my $msg = "";
60 $msg = "$class: " if defined($class);
61 $msg .= $message;
62 $msg .= " ($info)" if defined($info);
63  
64 warn "$msg\n";
65 }
66  
67 $LOG = $self->{error_fd};
68 flock($LOG, 2); # lock prior to writing
69  
70 # what we came here for in the first place
71 print $LOG "[" . gmtime(time) . "] ";
72 print $LOG "$class: " if defined($class);
73 print $LOG $message;
74 # supplementary info if available
75 print $LOG "($info)" if defined($info);
76 print $LOG "\n";
77  
78 flock(LOG, 8); # unlock after writing
79 croak($args{fatal}) if defined($args{fatal});
80 }
81  
82  
83 # write "access" log file entry in httpd common log format
84 sub access {
85 my ($self, %args) = @_;
86 my $host = (($args{host} || $ENV{REMOTE_HOST}) || $ENV{REMOTE_ADDR})
87 || "0.0.0.0";
88 my $ident = ($args{ident} || $ENV{REMOTE_IDENT}) || "-";
89 my $user = ($args{user} || $ENV{REMOTE_USER}) || "-";
90 my $message = $args{message} || "nada";
91 my $code = $args{code} || 0,
92 my $count = $args{count} || 0;
93  
94 my(@MON) = ('Dummy', 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul',
95 'Aug', 'Sep', 'Oct', 'Nov', 'Dec');
96 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime;
97 my ($gsec,$gmin,$ghour,$gmday,$gmon,$gyear,$gwday,$gyday,$gisdst)
98 = gmtime;
99 my ($LOG);
100  
101 my $offset = $hour - $ghour;
102 $year += 1900; # enough of a Y2K fix ?
103  
104 my $datestr = sprintf("%02d/%s/%4d:%02d:%02d:%02d %s%02d00", $mday,
105 $MON[$mon + 1], $year, $hour, $min, $sec,
106 $offset >= 0 ? '+' : '-', $offset);
107  
108 $LOG = $self->{access_fd};
109 flock($LOG, 2); # lock prior to writing
110  
111 # what we came here for
112 printf $LOG "%s %s %s [%s] \"%s\" %d %d\n",
113 $host, $ident, $user, $datestr, $message, $code, $count;
114  
115 flock($LOG, 8); # unlock after writing
116 croak($args{fatal}) if defined($args{fatal});
117 }
118  
119 1;
120 __END__