line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package FusionInventory::Agent::Logger::File; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
19664534
|
use strict; |
|
1
|
|
|
|
|
9
|
|
|
1
|
|
|
|
|
203
|
|
4
|
1
|
|
|
1
|
|
10
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
129
|
|
5
|
1
|
|
|
1
|
|
15
|
use base 'FusionInventory::Agent::Logger::Backend'; |
|
1
|
|
|
|
|
53
|
|
|
1
|
|
|
|
|
783
|
|
6
|
|
|
|
|
|
|
|
7
|
1
|
|
|
1
|
|
8
|
use English qw(-no_match_vars); |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
30
|
|
8
|
1
|
|
|
1
|
|
1303
|
use Fcntl qw(:flock); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
276
|
|
9
|
1
|
|
|
1
|
|
919
|
use File::stat; |
|
1
|
|
|
|
|
15644
|
|
|
1
|
|
|
|
|
8
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
sub new { |
12
|
0
|
|
|
0
|
1
|
|
my ($class, %params) = @_; |
13
|
|
|
|
|
|
|
|
14
|
0
|
0
|
|
|
|
|
my $self = { |
15
|
|
|
|
|
|
|
logfile => $params{config}->{logfile}, |
16
|
|
|
|
|
|
|
logfile_maxsize => $params{config}->{'logfile-maxsize'} ? |
17
|
|
|
|
|
|
|
$params{config}->{'logfile-maxsize'} * 1024 * 1024 : 0 |
18
|
|
|
|
|
|
|
}; |
19
|
0
|
|
|
|
|
|
bless $self, $class; |
20
|
|
|
|
|
|
|
|
21
|
0
|
|
|
|
|
|
return $self; |
22
|
|
|
|
|
|
|
} |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
sub addMessage { |
25
|
0
|
|
|
0
|
1
|
|
my ($self, %params) = @_; |
26
|
|
|
|
|
|
|
|
27
|
0
|
|
|
|
|
|
my $level = $params{level}; |
28
|
0
|
|
|
|
|
|
my $message = $params{message}; |
29
|
|
|
|
|
|
|
|
30
|
0
|
|
|
|
|
|
my $handle; |
31
|
0
|
0
|
|
|
|
|
if ($self->{logfile_maxsize}) { |
32
|
0
|
|
|
|
|
|
my $stat = stat($self->{logfile}); |
33
|
0
|
0
|
0
|
|
|
|
if ($stat && $stat->size() > $self->{logfile_maxsize}) { |
34
|
0
|
0
|
|
|
|
|
if (!open $handle, '>', $self->{logfile}) { |
35
|
0
|
|
|
|
|
|
warn "Can't open $self->{logfile}: $ERRNO"; |
36
|
0
|
|
|
|
|
|
return; |
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
} |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
|
41
|
0
|
0
|
0
|
|
|
|
if (!$handle && !open $handle, '>>', $self->{logfile}) { |
42
|
0
|
|
|
|
|
|
warn "can't open $self->{logfile}: $ERRNO"; |
43
|
0
|
|
|
|
|
|
return; |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
|
46
|
0
|
|
|
|
|
|
my $locked; |
47
|
0
|
|
|
|
|
|
my $retryTill = time + 60; |
48
|
|
|
|
|
|
|
|
49
|
0
|
|
0
|
|
|
|
while ($retryTill > time && !$locked) { |
50
|
|
|
|
|
|
|
## no critic (ProhibitBitwise) |
51
|
|
|
|
|
|
|
# get an exclusive lock on log file |
52
|
0
|
0
|
|
|
|
|
$locked = 1 if flock($handle, LOCK_EX|LOCK_NB); |
53
|
|
|
|
|
|
|
} |
54
|
|
|
|
|
|
|
|
55
|
0
|
0
|
|
|
|
|
if (!$locked) { |
56
|
0
|
|
|
|
|
|
die "can't get an exclusive lock on $self->{logfile}: $ERRNO"; |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
|
59
|
0
|
|
|
|
|
|
print {$handle} |
|
0
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
"[". localtime() ."]" . |
61
|
|
|
|
|
|
|
"[$level]" . |
62
|
|
|
|
|
|
|
" $message\n"; |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
# closing handle release the lock automatically |
65
|
0
|
|
|
|
|
|
close $handle; |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
1; |
70
|
|
|
|
|
|
|
__END__ |