blib/lib/Log.pm untested

filter: Statementstmt 0.0% 0 / 122 Branchbran 0.0% 0 / 32 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 165 CC 17 SCAR 52.1
CC
17
Cov
0%
start_log
272.0
end_log
6.0
new
2.0
CRAP 182.3
1 package CGI::AppBuilder::Log;
2  
3 use warnings;
4 use Carp;
5 use IO::File;
6 use POSIX qw(strftime);
7  
8 use CGI::AppBuilder;
9  
10 # require Exporter;
11 @ISA = qw(Exporter CGI::AppBuilder);
12 our @EXPORT = qw();
13 our @EXPORT_OK = qw(start_log end_log
14 );
15 our %EXPORT_TAGS = (
16 all => [@EXPORT_OK],
17 log => [qw(start_log end_log)],
18 );
19 $CGI::AppBuilder::Log::VERSION = 0.12;
20  
21 =head1 NAME
22  
23 CGI::AppBuilder::Log - Gather and write log entries for CGI applications
24  
25 =head1 SYNOPSIS
26  
27 my $self = bless {}, "main";
28 use CGI::AppBuilder::Log /:log/;
29  
30 =head1 DESCRIPTION
31  
32 The package contains the methods for gathering and creating log file.
33  
34 =head3 new (ifn => 'file.cfg', opt => 'hvS:')
35  
36 This is a inherited method from CGI::AppBuilder. See the same method
37 in CGI::AppBuilder for more details.
38  
39 =cut
40  
41 sub new {
42 my ($s, %args) = @_;
43 return $s->SUPER::new(%args);
44 }
45  
46 =head2 start_log($dtl, $brf, $cns, $arg, $lvl)
47  
48 Input variables:
49  
50 $dtl - file name for detailed log
51 $brf - file name for brief log
52 $cns - a list of fields which are stored in brief log
53 $arg - command line arguments
54 $lvl - log levle
55 1 - default:start_time,end_time,elapsed_time,user,args,result
56 2 - 1 plus: remote_addr,http_user_agent,http_accept_language,
57 http_accept_charset
58  
59 Variables used or routines called:
60  
61 echoMSG - print debug messages
62  
63 How to use:
64  
65 use CGI::AppBuilder::Log qw(:log);
66 my $self= bless {}, "main";
67 my $ar = $self->start_log('details.log','brief.log',
68 'start_time,end_time,elapsed_time,file_tranferred,status');
69  
70 Return: a hash array containing the fields in $cns.
71  
72 This method creates log files if they do not exist and prepare a
73 hash array to store needed fields for end_log. The hash array has
74 the following elements:
75  
76 cns - a list of field names separated by commas
77 fld - a hash array containing the field defined in cns.
78 fn_brf - file name for brief log
79 fh_brf - file handler for brief log
80 fn_dtl - file name for detail log
81 fh_dtl - file handler for detail log
82  
83 If the I<cns> is not specifed, then it defaults to
84 start_time,end_time,elapsed_time,user,args,result.
85  
86 =cut
87  
88 sub start_log {
89 my $s = shift;
90 my ($dtl,$brf,$cns,$arg,$lvl) = @_;
91 my $ar = bless {}, ref($s);
92 return $ar if ! $dtl;
93  
94 $lvl = 1 if !$lvl;
95 $s->echoMSG(" -- start logging in $dtl...",1);
96 my ($cn1,$cn2) = ("","");
97 if (!$cns) {
98 $cn1='start_time,end_time,elapsed_time,user,args,result';
99 $cns = $cn1;
100 if ($lvl>1) {
101 $cn2 = "REMOTE_ADDR,HTTP_USER_AGENT,HTTP_ACCEPT_LANGUAGE,";
102 $cn2 .= "HTTP_ACCEPT_CHARSET";
103 }
104 }
105 foreach my $k (split /,/, $cns) {
106 $k = lc $k; ${$ar}{fld}{$k} = "";
107 }
108 if ($cn2) {
109 foreach my $k (split /,/, $cn2) {
110 my $i = lc $k;
111 ${$ar}{fld}{$i} = $ENV{$k} if exists $ENV{$k};
112 ${$ar}{fld}{$i} = "" if !exists $ENV{$k};
113 }
114 $cns .= lc ",$cn2";
115 }
116 $ar->{user} = ($^O =~ /(linux|solaris)/i) ? `/usr/ucb/whoami` : "";
117 $ar->{args} = (exists $ENV{QUERY_STRING})?
118 $ENV{QUERY_STRING}:$arg;
119 my ($tx1, $txt);
120 my $fh_dtl = new IO::File ">> $dtl";
121 croak "ERR: could not write to $dtl: $!\n" if !defined($fh_dtl);
122 ${$ar}{fld}{start_time} = time;
123 $ENV{FH_DEBUG_LOG} = $fh_dtl;
124 ${$ar}{cns} = $cns;
125 ${$ar}{fn_dtl} = $dtl;
126 ${$ar}{fh_dtl} = $fh_dtl;
127 my $stm = strftime "%a %b %e %H:%M:%S %Y",
128 localtime(${$ar}{fld}{start_time});
129 $tx1 = "# File Name: $dtl\n# Start at $stm\n";
130 print $fh_dtl $tx1;
131 return $ar if ! $brf;
132  
133 my ($pkg, $fn, $line, $subroutine, $hasargs, $wantarray,
134 $evaltext, $is_require, $hints, $bitmask) = caller(3);
135 $subroutine = 'start_log' if ! $subroutine;
136 $tx1 = "# File Name: $brf\n# Generated By: $subroutine\n";
137 $tx1 .= "# Fields: (elapsed times are in seconds)\n";
138 $cn1 = $cns; $cn1 =~ s/,/\|/g;
139 $tx1 .= "# $cn1\n";
140 $txt = $tx1 if ! -f $brf;
141 my $dbg = $s->debug;
142 $s->debug(1) if !$dbg; # we at least log message at level 1
143 my $fh_brf = new IO::File ">> $brf";
144 print $fh_brf "$txt" if $txt;
145 $ar->{fn_brf} = $brf;
146 $ar->{fh_brf} = $fh_brf;
147 return $ar;
148 }
149  
150 =head2 end_log($ar)
151  
152 Input variables:
153  
154 $ar - array ref returned from start_log. The elements can
155 be populated in before end_log.
156  
157 Variables used or routines called:
158  
159 strftime - time formater from POSIX
160 disp_param - display parameters
161  
162 How to use:
163  
164 use CGI::AppBuilder::Log qw(:log);
165 my $self= bless {}, "main";
166 my $ar = $self->start_log('details.log','brief.log');
167 $self->end_log($ar);
168  
169 Return: none.
170  
171 =cut
172  
173 sub end_log {
174 my $s = shift;
175 my ($ar) = @_;
176 $s->echo_msg(" -- end logging ...",1);
177 my %b = %{${$ar}{fld}};
178 # my $f = "%a %b %e %H:%M:%S %Y";
179 my $f = "%Y%m%d.%H%M%S";
180 my $fh1 = ${$ar}{fh_brf};
181 my $fh2 = ${$ar}{fh_dtl};
182 my $fn1 = $ar->{fn_brf};
183 my $fn2 = $ar->{fn_dtl};
184 my $cns = ${$ar}{cns};
185 $s->{hf_brf} = $fh1;
186 $s->{hf_dtl} = $fh2;
187 $b{end_time} = time;
188 $b{elapsed_time} = $b{end_time} - $b{start_time};
189 $b{start_time} = strftime $f, localtime($b{start_time});
190 $b{end_time} = strftime $f, localtime($b{end_time});
191 $b{result} = 'OK';
192
193 my ($txt) = ("");
194 foreach my $k (split /,/, $cns) { $txt .= "$b{$k}|"; }
195 my $fmt = "%s=%s\n";
196 $txt =~ s/\|$//;
197 print $fh1 "$txt\n";
198 for my $k (sort keys %b) {
199 my $t = sprintf $fmt, $k, ((defined $b{$k}) ? $b{$k} : "");
200 print $fh2 $t;
201 }
202 for my $k (sort keys %$ar) {
203 my $t = sprintf $fmt, $k, $ar->{$k};
204 print $fh2 $t;
205 }
206 print $fh2 "# End at $b{end_time} $b{result}\n\n";
207 undef $fh1; # close breif file hanlder
208 undef $fh2; # close detail file handler
209 chmod 0777, $fn1, $fn2;
210 }
211  
212 1;
213  
214 =head1 CODING HISTORY
215  
216 =over 4
217  
218 =item * Version 0.10
219  
220 Extract start_log and end_log from Debug::EchoMessage.
221  
222 =item * Version 0.11
223  
224 =back
225  
226 =head1 FUTURE IMPLEMENTATION
227  
228 =over 4
229  
230 =item * no plan yet
231  
232 =back
233  
234 =head1 AUTHOR
235  
236 Copyright (c) 2004 Hanming Tu. All rights reserved.
237  
238 This package is free software and is provided "as is" without express
239 or implied warranty. It may be used, redistributed and/or modified
240 under the terms of the Perl Artistic License (see
241 http://www.perl.com/perl/misc/Artistic.html)
242  
243 =cut
244