MLite/Log.pm

filter: Statementstmt 18.8% 20 / 106 Branchbran 0.0% 0 / 46 Conditioncond 0.0% 0 / 21 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 22.2% 6 / 27 Podpod 100% 18 / 18 totaltotal 19.2% 44 / 229 CC 32 SCAR 38.8
CC
32
Cov
12%
log
156.0
_level
56.0
debug
42.0
CRAP 48.3
« MLite/Helper.pm ↑ Summary MLite/Helper/Install.pm »
1 package WWW::MLite::Log; # $Id: Log.pm 10 2014-05-22 14:05:32Z minus $
2 1 ▶use strict; # use Data::Dumper; $Data::Dumper::Deparse = 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
3
4 =head1 NAME
5
6 WWW::MLite::Log - CTK Logging methods
7
8 =head1 VERSION
9
10 Version 1.00
11
12 =head1 SYNOPSIS
13
14 $config->set(debug => 0); # 0 - off / 1 - on
15 $config->set(loglevel => 'emerg'); # or '7'
16 $config->set(logfile => 'test.log'); # File name. Optional. Default - undef
17 $config->set(syslog => 0); 0 - off / 1 - on (Apache log file)
18
19 $mlite->debug( " ... Blah-Blah-Blah ... ", $verbose, $file );
20 $mlite->log( " ... Blah-Blah-Blah ... ", "info", $file, $separator );
21 $mlite->syslog( " ... Blah-Blah-Blah ... ", $level );
22 $mlite->exception( " ... Blah-Blah-Blah ... " );
23
24 $mlite->log_except(); # 9 exception
25 $mlite->log_fatal(); # 8 fatal
26 $mlite->log_emerg(); # 7 system is unusable
27 $mlite->log_alert(); # 6 action must be taken immediately
28 $mlite->log_crit(); # 5 critical conditions
29 $mlite->log_error(); # 4 error conditions
30 $mlite->log_warning(); # 3 warning conditions
31 $mlite->log_notice(); # 2 normal but significant condition
32 $mlite->log_info(); # 1 informational
33 $mlite->log_debug(); # 0 debug-level messages (default)
34
35 =head1 DESCRIPTION
36
37 All of methods are returned by log-records
38
39 =over 8
40
41 =item B<exception>
42
43 my $excstat = $mlite->exception( $message );
44
45 Write exception information to file
46
47 =item B<debug>
48
49 my $debugstat = $mlite->debug( $message, $verbose, $file );
50
51 Write debugging information to logfile
52
53 $message - Log (debug) message
54
55 $verbose - System information flag. 1 - verbose mode, on / 0 - regular mode, off
56
57 $file - Log file (absolute). Default - STDOUT (Apache logging).
58 If the flag syslog the value is ignored - the message is written to the Apache logfile.
59
60 It should be noted that if the flag is omitted then the output information debug be ignored.
61
62 =item B<log>
63
64 my $logstat = $mlite->log( $message, $level, $file, $separator );
65
66 Main logging method
67
68 $message - Log message
69
70 $level - logging level. It may be either a numeric or string value of the form:
71
72 debug -- 0 (default)
73 info -- 1
74 notice -- 2
75 warning -- 3
76 error -- 4
77 crit -- 5
78 alert -- 6
79 emerg -- 7
80 fatal -- 8
81 except -- 9
82
83 $file - Log File (absolute). Default - STDOUT (Apache logging).
84 If the flag syslog the value is ignored - the message is written to the Apache logfile
85
86 $separator - Log-record separator char's string. Default as char(32): ' '
87
88 =item B<log_debug>
89
90 Alias for call: $mlite->log( $message, 'debug' )
91
92 =item B<log_info>
93
94 Alias for call: $mlite->log( $message, 'info' )
95
96 =item B<log_notice>
97
98 Alias for call: $mlite->log( $message, 'notice' )
99
100 =item B<log_warning>
101
102 Alias for call: $mlite->log( $message, 'warning' )
103
104 =item B<log_warn>
105
106 Alias for call: $mlite->log( $message, 'warning' )
107
108 =item B<log_error>
109
110 Alias for call: $mlite->log( $message, 'error' )
111
112 =item B<log_err>
113
114 Alias for call: $mlite->log( $message, 'error' )
115
116 =item B<log_crit>
117
118 Alias for call: $mlite->log( $message, 'crit' )
119
120 =item B<log_alert>
121
122 Alias for call: $mlite->log( $message, 'alert' )
123
124 =item B<log_emerg>
125
126 Alias for call: $mlite->log( $message, 'emerg' )
127
128 =item B<log_fatal>
129
130 Alias for call: $mlite->log( $message, 'fatal' )
131
132 =item B<log_except>
133
134 Alias for call: $mlite->log( $message, 'except' )
135
136 =item B<log_exception>
137
138 Alias for call: $mlite->log( $message, 'except' )
139
140 =item B<syslog, logsys>
141
142 my $logstat = $mlite->syslog( $message, $level );
143
144 Apache logging to the Apache logfile (ErrorLog of your virtualhost)
145
146 $level can take the following values:
147
148 debug, info, notice, warning, error, crit, alert, emerg, fatal, except
149
150 The function returns work status
151
152 =back
153
154 =head1 HISTORY
155
156 See C<CHANGES> file
157
158 =head1 AUTHOR
159
160 Serz Minus (Lepenkov Sergey) L<http://www.serzik.com> E<lt>minus@mail333.comE<gt>
161
162 =head1 COPYRIGHT
163
164 Copyright (C) 1998-2014 D&D Corporation. All Rights Reserved
165
166 =head1 LICENSE
167
168 This program is free software: you can redistribute it and/or modify
169 it under the terms of the GNU General Public License as published by
170 the Free Software Foundation, either version 3 of the License, or
171 (at your option) any later version.
172
173 This program is distributed in the hope that it will be useful,
174 but WITHOUT ANY WARRANTY; without even the implied warranty of
175 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
176 GNU General Public License for more details.
177
178 See C<LICENSE> file
179
180 =cut
181
182 1 ▶use vars qw/$VERSION/;
Subroutine BEGIN: called CC=1 SCAR=0.0
183 1 $VERSION = '1.00';
184
185 use constant {
186 1 LOGLEVELS => {
187 'debug' => 0,
188 'info' => 1,
189 'notice' => 2,
190 'warning' => 3,
191 'error' => 4,
192 'crit' => 5,
193 'alert' => 6,
194 'emerg' => 7,
195 'fatal' => 8,
196 'except' => 9,
197 },
198 1 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
199
200 1 ▶use FileHandle;
Subroutine BEGIN: called CC=1 SCAR=0.0
201 1 ▶use CTK::Util qw/ :BASE /; # Óòèëèòàðèé
Subroutine BEGIN: called CC=1 SCAR=0.0
202 1 ▶use WWW::MLite::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
203
204 sub log {
205 #
206 # Ïðîöåäóðà ëîãèðîâàíèÿ äàííûõ.
207 #
208 # IN:
209 # message - ñîîáùåíèå.
210 # level - Óðîâåíü çàïèñè ëîãà (ñì. ïðîöåäóðó syslog())
211 # file - ÀÁÑÎËÞÒÍÛÉ ïóòü è èìÿ ôàéëà êóäà ïèñàòü. Ïî óìîë÷àíèþ èñïîëüçóåòñÿ ôàéë default.log TEMP-äèðåêòîðèè
212 # sep - Ðàçäåëèòåëü çíà÷åíèé. Ïî óìîë÷àíèþ ïðîáåë
213 #
214 0 ▶ my $self = shift;
Subroutine log: not called CC=12 SCAR=50.5
Pod documented
215 0 ▶ my $message = shift; $message = '' unless defined $message;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $message 0 0 0
216 0 ▶ my $level = shift || 'debug';
Condition0%0/2
shift() || 'debug'
l!l
00
MC/DC0%0/1
shift() || 'debug'
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
217 0 ▶ my $file = shift || $self->conf->logfile;
Condition0%0/3
shift() || $self->conf->logfile
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() || $self->conf->logfile
shift() $self->conf->logfile
Truth table0%0/3
A || B
A= shift()B= $self->conf->logfile
ABresult
1X1
011
000
218 0 ▶ my $sep = shift; $sep = ' ' unless defined $sep;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $sep 0 0 0
219
220 # Îïðåäåëÿåì óðîâåíü ïåðåäàííûé â ïðîöåäóðó
221 0 my $ll = _level($level);
222 0 my $llc = _level2index($ll); # ÷èñëîâîå çíà÷åíè ïåðåäàííîãî â ïðîöåäóðó óðîâíÿ
223
224 # Ñìîòðèì íà óðîâåíü ëîãà f($ll), åñëè îí óñòàíîâëåí < ÷åì LogLevl çàäàííûé êîíôèãóðàöèåé òî ïðîñòî âûõîäèì
225 0 ▶ my $llsys = _level2index(_level(lc( defined($self->conf->loglevel) ? $self->conf->loglevel : 'emerg' )) );
Branch: 1 branch
BranchTrueFalseTotal
defined $self->conf->loglevel ? : 0 0 0
226
227 #unless (($llc == 0) && $self->conf->debug) { # åñëè ïåðåäàí level=debug è óñòàíîâëåí ôëàã äåáàãà - ïðîïñêàåì ïðîâåðêó!
228 # return 0 if $llc < $llsys;
229 #}
230 0 ▶ return 0 if $llc < $llsys;
Branch: 1 branch
BranchTrueFalseTotal
if $llc < $llsys 0 0 0
231
232 # Ôîðìèðóåì âûõîäíóþ ñòðîêó
233 0 ▶ my $usesyslog = $self->conf->syslog ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->conf->syslog ? : 0 0 0
234 0 ▶ $usesyslog = 1 unless defined($file); # Èñïîëüçîâàòü ñèñòåìíîå ëîãèðîâàíèå, åñëè ôàéë íå çàäàí
Branch: 1 branch
BranchTrueFalseTotal
unless defined $file 0 0 0
235
236 0 my @sl;
237 0 ▶ unless ($usesyslog) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($usesyslog) 0 0 0
238 0 @sl = (
239 sprintf('[%s]',dtf("%w %MON %DD %hh:%mm:%ss %YYYY")), # Tue Feb 02 16:15:18 2013
240 sprintf('[%s]',$ll),
241 sprintf('[client %s]',$self->conf->remote_addr),
242 );
243 }
244 0 push @sl, sprintf('[sid %s]',$self->conf->sid);
245 0 ▶ push @sl, sprintf('[user %s]',$self->conf->remote_user) if $self->conf->remote_user;
Branch: 1 branch
BranchTrueFalseTotal
if $self->conf->remote_user 0 0 0
246 0 push @sl, sprintf('[uri %s]',$self->conf->request_uri);
247 0 push @sl, $message;
248 0 my $logstring = join($sep, @sl);
249
250 # Çàïèñü!
251 0 ▶ return syslog($self,$logstring,$level) if $usesyslog; # Â ñèñòåìíûé ëîã
Branch: 1 branch
BranchTrueFalseTotal
if $usesyslog 0 0 0
252 0 return _log_flush($file, $logstring); #  ñâîé ëîã. Òóò ñëîæíåå, èäåò çàìîðî÷êà ñ ôàéëàìè
253 }
254 0 ▶sub log_debug { shift->log(shift,'debug') };
Subroutine log_debug: not called CC=1 SCAR=6.9
Pod documented
255 0 ▶sub log_info { shift->log(shift,'info') };
Subroutine log_info: not called CC=1 SCAR=6.9
Pod documented
256 0 ▶sub log_notice { shift->log(shift,'notice') };
Subroutine log_notice: not called CC=1 SCAR=6.9
Pod documented
257 0 ▶sub log_warning { shift->log(shift,'warning') };
Subroutine log_warning: not called CC=1 SCAR=6.9
Pod documented
258 0 ▶sub log_warn { goto &log_warning };
Subroutine log_warn: not called CC=1 SCAR=6.9
Pod documented
259 0 ▶sub log_error { shift->log(shift,'error') };
Subroutine log_error: not called CC=1 SCAR=6.9
Pod documented
260 0 ▶sub log_err { goto &log_error };
Subroutine log_err: not called CC=1 SCAR=6.9
Pod documented
261 0 ▶sub log_crit { shift->log(shift,'crit') };
Subroutine log_crit: not called CC=1 SCAR=6.9
Pod documented
262 0 ▶sub log_alert { shift->log(shift,'alert') };
Subroutine log_alert: not called CC=1 SCAR=6.9
Pod documented
263 0 ▶sub log_emerg { shift->log(shift,'emerg') };
Subroutine log_emerg: not called CC=1 SCAR=6.9
Pod documented
264 0 ▶sub log_fatal { shift->log(shift,'fatal') };
Subroutine log_fatal: not called CC=1 SCAR=6.9
Pod documented
265 0 ▶sub log_except { shift->log(shift,'except') };
Subroutine log_except: not called CC=1 SCAR=6.9
Pod documented
266 0 ▶sub log_exception { goto &log_except };
Subroutine log_exception: not called CC=1 SCAR=6.9
Pod documented
267
268 sub syslog {
269 #
270 # Ïðîöåäóðà èñïîëüçóåò ôóíêöèþ àïà÷à äëÿ âñòàâêè çàïèñåé â ëîã
271 #
272 # IN:
273 # message - ñîîáùåíèå.
274 # level - Óðîâåíü çàïèñè ëîãà (ñì. ïðîöåäóðó syslog())
275 #
276 0 ▶ my $self = shift;
Subroutine syslog: not called CC=4 SCAR=30.0
Pod documented
277 0 my $message = shift;
278 0 ▶ my $level = shift || ''; # emerg(), alert(), crit(), error(), warn(), notice(), info(), debug()
Condition0%0/2
shift() || ''
l!l
00
MC/DC0%0/1
shift() || ''
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
279 0 ▶ my $msg = translate(defined($message) ? $message : '');
Branch: 1 branch
BranchTrueFalseTotal
defined $message ? : 0 0 0
280
281 0 ▶ if ($level) {
Branch: 1 branch
BranchTrueFalseTotal
if ($level) { } 0 0 0
282 0 printf STDERR "[%s] %s\n",uc(_level($level)), $msg;
283 } else {
284 0 printf STDERR "%s\n", $msg;
285 }
286 0 return 1;
287
288 }
289 0 ▶sub logsys { goto &syslog };
Subroutine logsys: not called CC=1 SCAR=6.9
Pod documented
290 sub debug {
291 #
292 # Ïðîöåäóðà îòëàäêè. Çàïèñûâàåò â îòëàäî÷íûé ôàéë èíôîðìàöèþ.
293 # !!! Èñïîëüçóåòñÿ äëÿ ÏÐÈÊËÀÄÍÛÕ à íå ñèñòåìíûõ íóæä
294 #
295 # IN:
296 # $message - ñîîáùåíèå
297 # $verbose - ôëàã ñèñòåìíîé èíôîðìàöèè. 1 - âêëþ÷èòü äîáàâëåíèå ñèñòåìíîé èíôîðìàöèè / 0 - âûêë
298 # $file - ÈÌß ôàéëà äëÿ îòëàäêè. Ïî óìîë÷àíèþ èñïîëüçóåòñÿ ôàéë ëîãà
299 #
300 0 ▶ my $self = shift;
Subroutine debug: not called CC=6 SCAR=37.4
Pod documented
301 0 ▶ my $message = shift; $message = '' unless defined $message;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $message 0 0 0
302 0 ▶ my $verbose = shift || 0;
Condition0%0/2
shift() || 0
l!l
00
MC/DC0%0/1
shift() || 0
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
303 0 my $file = shift;
304
305 0 ▶ return 0 unless $self->conf->debug;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->conf->debug 0 0 0
306
307 # Áåðåì çíà÷åíèå ïî óìîë÷àíèþ åñëè îíî íå çàäàíî
308 0 my $buff = '';
309 0 ▶ if ($verbose) {
Branch: 1 branch
BranchTrueFalseTotal
if ($verbose) { } 0 0 0
310 0 my ($pkg, $fn, $ln) = caller;
311 0 my $tm = sprintf "%+.*f",4, (getHiTime() - $self->conf->hitime);
312 0 ▶ $buff = "[time $tm] [package $pkg] [file $fn] [line $ln]".($message ? ' ' : '').$message;
Branch: 1 branch
BranchTrueFalseTotal
$message ? : 0 0 0
313 } else {
314 0 $buff = $message;
315 }
316 0 $self->log($buff,'debug',$file);
317 }
318 sub exception {
319 # Ïðîöåäóðà ðåàãèðîâàíèÿ íà exception
320 0 ▶ my $self = shift;
Subroutine exception: not called CC=2 SCAR=17.9
Pod documented
321 0 my $message = shift;
322 0 ▶ $message = '' unless defined $message;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $message 0 0 0
323 0 $self->log_except($message);
324 }
325
326
327 sub _log_flush {
328 # ñáðàñûâàåì áóôåð â ôàéë, âîçâðàùàÿ ñòàòóñ îïåðàöèè
329 0 ▶ my $fn = shift;
Subroutine _log_flush: not called CC=5 SCAR=34.0
330 0 my $buffer = shift;
331 0 ▶ return 0 unless defined $fn;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $fn 0 0 0
332
333 0 my $fh = FileHandle->new($fn,'a');
334 0 ▶ unless ($fh) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fh) 0 0 0
335 0 ▶ carp(defined($!) ? $! : "Can't open file \"$fn\"");
Branch: 1 branch
BranchTrueFalseTotal
defined $! ? : 0 0 0
336 0 return 0;
337 }
338
339 0 ▶ $fh->print(defined($buffer) ? $buffer : '',"\n");
Branch: 1 branch
BranchTrueFalseTotal
defined $buffer ? : 0 0 0
340 0 $fh->close();
341
342 0 return 1;
343 }
344 sub _level {
345 # Îïðåäåëÿåì óðîâåíü ïåðåäàííûé â ïðîöåäóðó
346 0 ▶ my $level = shift;
Subroutine _level: not called CC=7 SCAR=40.3
347 0 my $loglevels = LOGLEVELS;
348 0 my %levels = %$loglevels;
349 0 my %rlevels = reverse %$loglevels;
350 0 my $ll;
351 0 ▶ if (defined($level) && ($level =~ /^[0-9]+$/) && defined $rlevels{$level}) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $level and $level =~ /^[0-9]+$/ and defined $rlevels{$level}) { } 0 0 0
elsif (defined $level and $level =~ /^[a-z0-9]+$/i and defined $levels{lc $level}) { } 0 0 0
Condition0%0/6
defined $level and $level =~ /^[0-9]+$/ and defined $rlevels{$level}
!ll&&!rl&&r
000
defined $level and $level =~ /^[0-9]+$/
!ll&&!rl&&r
000
MC/DC0%0/3
defined $level and $level =~ /^[0-9]+$/ and defined $rlevels{$level}
defined $level $level =~ /^[0-9]+$/ defined $rlevels{$level}
Truth table0%0/4
A and B and C
A= defined $levelB= $level =~ /^[0-9]+$/C= defined $rlevels{$level}
ABCresult
0XX0
10X0
1100
1111
352 0 $ll = $rlevels{$level};
353 ▶ } elsif (defined($level) && ($level =~ /^[a-z0-9]+$/i) && defined $levels{lc($level)}) {
Condition0%0/6
defined $level and $level =~ /^[a-z0-9]+$/i and defined $levels{lc $level}
!ll&&!rl&&r
000
defined $level and $level =~ /^[a-z0-9]+$/i
!ll&&!rl&&r
000
MC/DC0%0/3
defined $level and $level =~ /^[a-z0-9]+$/i and defined $levels{lc $level}
defined $level $level =~ /^[a-z0-9]+$/i defined $levels{lc $level}
Truth table0%0/4
A and B and C
A= defined $levelB= $level =~ /^[a-z0-9]+$/iC= defined $levels{lc $level}
ABCresult
0XX0
10X0
1100
1111
354 0 $ll = lc($level);
355 } else {
356 0 $ll = 'debug'; # Îáðàáîò÷èê ïî óìîë÷àíèþ
357 }
358 0 return $ll;
359 }
360 sub _level2index {
361 0 ▶ my $level = shift;
Subroutine _level2index: not called CC=2 SCAR=17.9
362 0 my $loglevels = LOGLEVELS;
363 0 ▶ return 0 unless $level;
Branch: 1 branch
BranchTrueFalseTotal
unless $level 0 0 0
364 0 return $loglevels->{$level};
365 }
366 1 1;
367 __END__
« MLite/Helper.pm ↑ Summary MLite/Helper/Install.pm »