line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Logger::Simple;
|
2
|
|
|
|
|
|
|
{
|
3
|
3
|
|
|
3
|
|
74768
|
use strict;
|
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
156
|
|
4
|
3
|
|
|
3
|
|
18
|
use Carp;
|
|
3
|
|
|
|
|
5
|
|
|
3
|
|
|
|
|
279
|
|
5
|
3
|
|
|
3
|
|
3374
|
use FileHandle;
|
|
3
|
|
|
|
|
40171
|
|
|
3
|
|
|
|
|
18
|
|
6
|
3
|
|
|
3
|
|
1294
|
use Fcntl qw(:flock);
|
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
328
|
|
7
|
3
|
|
|
3
|
|
4253
|
use Time::HiRes qw/usleep/;
|
|
3
|
|
|
|
|
6801
|
|
|
3
|
|
|
|
|
16
|
|
8
|
3
|
|
|
3
|
|
660
|
use vars qw /$VERSION $SEM $ms $SEMAPHORE $FILEHANDLE @HISTORY/;
|
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
402
|
|
9
|
3
|
|
|
3
|
|
4702
|
use Object::InsideOut;
|
|
3
|
|
|
|
|
209457
|
|
|
3
|
|
|
|
|
21
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
$ms=750_000;
|
12
|
|
|
|
|
|
|
$VERSION='2.0';
|
13
|
|
|
|
|
|
|
$SEM = ".LS.lock";
|
14
|
|
|
|
|
|
|
$SEMAPHORE=new FileHandle;
|
15
|
|
|
|
|
|
|
$FILEHANDLE=new FileHandle;
|
16
|
|
|
|
|
|
|
@HISTORY=();
|
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
my @Log :Field('Standard'=>'Log','Type'=>'LIST');
|
19
|
|
|
|
|
|
|
my @FileHandle :Field('Standard'=>'FileHandle','Type'=>'SCALAR');
|
20
|
|
|
|
|
|
|
my @Semaphore :Field('Standard'=>'Semaphore','Type'=>'SCALAR');
|
21
|
|
|
|
|
|
|
my @Error :Field('Standard'=>'Error','Type'=>'LIST');
|
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
my %init_args :InitArgs=(
|
24
|
|
|
|
|
|
|
'Log'=>{
|
25
|
|
|
|
|
|
|
'Regex' => qr/^Log$/i,
|
26
|
|
|
|
|
|
|
'Mandatory' => 1,
|
27
|
|
|
|
|
|
|
},
|
28
|
|
|
|
|
|
|
);
|
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
sub _init :Init{
|
31
|
2
|
|
|
|
|
1253
|
my($self,$args)= @_;
|
32
|
2
|
50
|
|
|
|
10
|
if(exists($args->{'Log'})){
|
33
|
2
|
|
|
|
|
15
|
$self->set(\@Log,$args->{'Log'});
|
34
|
|
|
|
|
|
|
}
|
35
|
2
|
|
|
|
|
83
|
$self->set(\@FileHandle,$FILEHANDLE);
|
36
|
2
|
|
|
|
|
46
|
$self->set(\@Semaphore,$SEMAPHORE);
|
37
|
2
|
|
|
|
|
50
|
$self->open_log;
|
38
|
3
|
|
|
3
|
|
957
|
}
|
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
16
|
|
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
sub open_log{
|
41
|
2
|
|
|
2
|
0
|
5
|
my $self=shift;
|
42
|
2
|
|
|
|
|
66
|
my $FH=$self->get_FileHandle;
|
43
|
2
|
|
|
|
|
62
|
my $Log=$self->get_Log;
|
44
|
2
|
50
|
|
|
|
327
|
if(! open($FH,">>$Log")){
|
45
|
0
|
|
|
|
|
0
|
$self->write_error("Unable to open logfile\n");
|
46
|
0
|
|
|
|
|
0
|
return 0;
|
47
|
|
|
|
|
|
|
}
|
48
|
2
|
|
|
|
|
17
|
$FH->autoflush(1);
|
49
|
2
|
|
|
|
|
122
|
return 1;
|
50
|
|
|
|
|
|
|
}
|
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
sub write{
|
53
|
3
|
|
|
3
|
1
|
401
|
my($self,$msg)=@_;
|
54
|
3
|
|
|
|
|
81
|
my $FH=$self->get_FileHandle;
|
55
|
3
|
|
|
|
|
245
|
my $format="$0 : [".scalar (localtime)."] $msg";
|
56
|
|
|
|
|
|
|
## Fix to ignore locking on Win32
|
57
|
3
|
50
|
|
|
|
13
|
if($^O eq "MSWin32"){}else{
|
58
|
3
|
|
|
|
|
9
|
$self->lock();
|
59
|
|
|
|
|
|
|
}
|
60
|
3
|
50
|
|
|
|
91
|
if(! print $FH "$format\n"){
|
61
|
0
|
|
|
|
|
0
|
croak "Unable to write to log file: $!\n";
|
62
|
|
|
|
|
|
|
}
|
63
|
3
|
50
|
|
|
|
10
|
if($^O eq "MSWin32"){}else{
|
64
|
3
|
|
|
|
|
8
|
$self->unlock();
|
65
|
|
|
|
|
|
|
}
|
66
|
3
|
|
|
|
|
9
|
$self->update_history($msg);
|
67
|
|
|
|
|
|
|
}
|
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
sub update_history{
|
70
|
3
|
|
|
3
|
0
|
4
|
my($self,$msg)=@_;
|
71
|
3
|
|
|
|
|
9
|
push @HISTORY,$msg;
|
72
|
|
|
|
|
|
|
}
|
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
sub retrieve_history{
|
75
|
3
|
|
|
3
|
1
|
750
|
my $self=shift;
|
76
|
3
|
100
|
|
|
|
18
|
if(wantarray){
|
77
|
2
|
|
|
|
|
6
|
return @HISTORY;
|
78
|
|
|
|
|
|
|
}else{
|
79
|
1
|
|
|
|
|
3
|
my $message=$HISTORY[$#HISTORY];
|
80
|
1
|
|
|
|
|
6
|
return $message;
|
81
|
|
|
|
|
|
|
}
|
82
|
|
|
|
|
|
|
}
|
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
sub lock{
|
85
|
4
|
|
|
4
|
0
|
28
|
my $self=shift;
|
86
|
4
|
50
|
|
|
|
20
|
if($^O eq "MSWin32"){ return 1; }
|
|
0
|
|
|
|
|
0
|
|
87
|
4
|
|
|
|
|
108
|
my $SM=$self->get_Semaphore;
|
88
|
4
|
|
50
|
|
|
298
|
open $SM,">$SEM"||die"Can't create lock file: $!\n";
|
89
|
4
|
50
|
|
|
|
65
|
flock($SM,LOCK_EX) or die"Can't obtain file lock: $!\n";
|
90
|
|
|
|
|
|
|
}
|
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
sub unlock{
|
93
|
4
|
|
|
4
|
0
|
571
|
my $self=shift;
|
94
|
4
|
|
|
|
|
98
|
my $SM=$self->get_Semaphore;
|
95
|
4
|
50
|
|
|
|
63
|
if(-e $SEM){
|
96
|
4
|
|
|
|
|
23
|
flock($SM,LOCK_UN);
|
97
|
4
|
|
|
|
|
65
|
close $SM;
|
98
|
4
|
|
|
|
|
15
|
$SM->autoflush(1);
|
99
|
4
|
50
|
|
|
|
128
|
if($^O eq "MSWin32"){
|
100
|
0
|
|
|
|
|
0
|
system "C:\\Windows\\System32\\cmd.exe \/c del $SEM";
|
101
|
|
|
|
|
|
|
}else{
|
102
|
4
|
|
|
|
|
225
|
unlink $SEM;
|
103
|
|
|
|
|
|
|
}
|
104
|
|
|
|
|
|
|
}
|
105
|
|
|
|
|
|
|
}
|
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
sub wait{
|
108
|
0
|
|
|
0
|
0
|
0
|
while(-e $SEM){
|
109
|
0
|
|
|
|
|
0
|
usleep $ms;
|
110
|
|
|
|
|
|
|
}
|
111
|
|
|
|
|
|
|
}
|
112
|
|
|
|
|
|
|
sub clear_history{
|
113
|
1
|
|
|
1
|
1
|
369
|
my $self=shift;
|
114
|
1
|
|
|
|
|
4
|
@HISTORY=();
|
115
|
|
|
|
|
|
|
}
|
116
|
|
|
|
|
|
|
}
|
117
|
|
|
|
|
|
|
1;
|
118
|
|
|
|
|
|
|
__END__
|