line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Haineko::Log; |
2
|
2
|
|
|
2
|
|
2470
|
use strict; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
77
|
|
3
|
2
|
|
|
2
|
|
10
|
use warnings; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
71
|
|
4
|
|
|
|
|
|
|
|
5
|
2
|
|
|
2
|
|
3384
|
use Sys::Syslog qw(:DEFAULT setlogsock); |
|
2
|
|
|
|
|
70109
|
|
|
2
|
|
|
|
|
458
|
|
6
|
2
|
|
|
2
|
|
997
|
use Class::Accessor::Lite; |
|
2
|
|
|
|
|
1191
|
|
|
2
|
|
|
|
|
20
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
my $rwaccessors = [ |
9
|
|
|
|
|
|
|
'facility', # (String) syslog facility |
10
|
|
|
|
|
|
|
'loglevel', # (String) default log level |
11
|
|
|
|
|
|
|
'disabled', # (Integer) syslog disabled |
12
|
|
|
|
|
|
|
'option', # (HashRef) Logging options |
13
|
|
|
|
|
|
|
]; |
14
|
|
|
|
|
|
|
my $roaccessors = [ |
15
|
|
|
|
|
|
|
'identity', # (String) Log identiy string |
16
|
|
|
|
|
|
|
'queueid', # (String) Queue ID |
17
|
|
|
|
|
|
|
'useragent', # (String) User agent name |
18
|
|
|
|
|
|
|
'remoteaddr', # (String) Client IP address |
19
|
|
|
|
|
|
|
'remoteport', # (String) Client port number |
20
|
|
|
|
|
|
|
]; |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
Class::Accessor::Lite->mk_accessors( @$rwaccessors ); |
23
|
|
|
|
|
|
|
Class::Accessor::Lite->mk_ro_accessors( @$roaccessors ); |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
# Set prefix or suffix into the log message |
26
|
|
|
|
|
|
|
# Emergency (level 0) |
27
|
|
|
|
|
|
|
# Alert (level 1) |
28
|
|
|
|
|
|
|
# Critical (level 2) |
29
|
|
|
|
|
|
|
# Error (level 3) |
30
|
|
|
|
|
|
|
# Warning (level 4) |
31
|
|
|
|
|
|
|
# Notice (level 5) |
32
|
|
|
|
|
|
|
# Info (level 6) |
33
|
|
|
|
|
|
|
# Debug (level 7) |
34
|
|
|
|
|
|
|
my $LogLevels = [ |
35
|
|
|
|
|
|
|
'emerg', 'alert', 'crit', 'err' , |
36
|
|
|
|
|
|
|
'warning', 'notice', 'info', 'debug', |
37
|
|
|
|
|
|
|
]; |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
sub new { |
40
|
14
|
|
|
14
|
1
|
1857
|
my $class = shift; |
41
|
14
|
|
|
|
|
60
|
my $argvs = { @_ }; |
42
|
|
|
|
|
|
|
|
43
|
14
|
|
|
|
|
173
|
my $logoptions = { |
44
|
|
|
|
|
|
|
'cons' => 0, |
45
|
|
|
|
|
|
|
'ndelay' => 1, |
46
|
|
|
|
|
|
|
'noeol' => 0, |
47
|
|
|
|
|
|
|
'nofatal' => 1, |
48
|
|
|
|
|
|
|
'nonul' => 0, |
49
|
|
|
|
|
|
|
'nowait' => 0, |
50
|
|
|
|
|
|
|
'perror' => 0, |
51
|
|
|
|
|
|
|
'pid' => 1, |
52
|
|
|
|
|
|
|
}; |
53
|
|
|
|
|
|
|
|
54
|
14
|
|
100
|
|
|
55
|
$argvs->{'facility'} ||= 'local2'; |
55
|
14
|
|
50
|
|
|
86
|
$argvs->{'loglevel'} ||= 'info'; |
56
|
14
|
50
|
|
|
|
33
|
$argvs->{'loglevel'} = 'info' unless grep { $argvs->{'loglevel'} eq $_ } @$LogLevels; |
|
112
|
|
|
|
|
209
|
|
57
|
14
|
|
100
|
|
|
49
|
$argvs->{'disabled'} //= 0; |
58
|
14
|
|
|
|
|
47
|
$argvs->{'identity'} = 'haineko'; |
59
|
14
|
|
100
|
|
|
47
|
$argvs->{'queueid'} //= q(); |
60
|
14
|
|
50
|
|
|
68
|
$argvs->{'useragent'} ||= q(); |
61
|
14
|
|
100
|
|
|
53
|
$argvs->{'remoteaddr'} ||= q(); |
62
|
14
|
|
100
|
|
|
45
|
$argvs->{'remoteport'} //= q(); |
63
|
|
|
|
|
|
|
|
64
|
14
|
50
|
33
|
|
|
55
|
if( defined $argvs->{'option'} && ref $argvs->{'option'} eq 'HASH' ) { |
65
|
|
|
|
|
|
|
# Set logging options |
66
|
0
|
|
|
|
|
0
|
for my $e ( keys %$logoptions ) { |
67
|
0
|
|
0
|
|
|
0
|
$argvs->{'option'}->{ $e } //= $logoptions->{ $e }; |
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
} else { |
71
|
14
|
|
|
|
|
32
|
$argvs->{'option'} = $logoptions; |
72
|
|
|
|
|
|
|
} |
73
|
|
|
|
|
|
|
|
74
|
14
|
|
|
|
|
55
|
return bless $argvs, __PACKAGE__; |
75
|
|
|
|
|
|
|
} |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
sub o { |
78
|
|
|
|
|
|
|
# Return syslog option as a string |
79
|
2
|
|
|
2
|
0
|
3173
|
my $self = shift; |
80
|
2
|
0
|
|
|
|
12
|
my $opts = [ grep { $_ if $self->{'options'}->{ $_ } } keys %{ $self->{'options'} } ]; |
|
0
|
|
|
|
|
0
|
|
|
2
|
|
|
|
|
6
|
|
81
|
2
|
|
|
|
|
12
|
return join( ',', @$opts ); |
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
sub h { |
85
|
|
|
|
|
|
|
# Return syslog header string |
86
|
2
|
|
|
2
|
0
|
3
|
my $self = shift; |
87
|
2
|
|
|
|
|
4
|
my $head = []; |
88
|
2
|
|
|
|
|
4
|
my $host = q(); |
89
|
|
|
|
|
|
|
|
90
|
2
|
50
|
|
|
|
12
|
push @$head, sprintf( "queueid=%s", $self->{'queueid'} ) if $self->{'queueid'}; |
91
|
2
|
50
|
|
|
|
8
|
$host = sprintf( "client=%s", $self->{'remoteaddr'} ) if $self->{'remoteaddr'}; |
92
|
2
|
50
|
33
|
|
|
13
|
$host .= sprintf( ":%d", $self->{'remoteport'} ) if $host && $self->{'remoteport'}; |
93
|
2
|
50
|
|
|
|
6
|
push @$head, $host if length $host; |
94
|
2
|
50
|
|
|
|
5
|
push @$head, sprintf( "ua='%s'", $self->{'useragent'} ) if $self->{'useragent'}; |
95
|
|
|
|
|
|
|
|
96
|
2
|
50
|
|
|
|
7
|
return q() unless scalar @$head; |
97
|
2
|
|
|
|
|
15
|
return join( ', ', @$head ); |
98
|
|
|
|
|
|
|
} |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
sub w { |
101
|
|
|
|
|
|
|
# write messages |
102
|
14
|
|
|
14
|
1
|
24
|
my $self = shift; |
103
|
14
|
|
33
|
|
|
41
|
my $sllv = shift || $self->{'loglevel'}; |
104
|
14
|
|
|
|
|
17
|
my $mesg = shift; |
105
|
14
|
|
|
|
|
25
|
my $text = q(); |
106
|
14
|
|
|
|
|
27
|
my $logs = []; |
107
|
|
|
|
|
|
|
|
108
|
14
|
50
|
|
|
|
62
|
return 0 if $self->{'disabled'}; |
109
|
0
|
0
|
|
|
|
|
return 0 unless ref $mesg eq 'HASH'; |
110
|
|
|
|
|
|
|
|
111
|
0
|
0
|
|
|
|
|
$sllv = 'info' unless grep { $sllv eq $_ } @$LogLevels; |
|
0
|
|
|
|
|
|
|
112
|
0
|
|
|
|
|
|
push @$logs, $self->h; |
113
|
|
|
|
|
|
|
|
114
|
0
|
|
|
|
|
|
for my $e ( keys %$mesg ) { |
115
|
|
|
|
|
|
|
|
116
|
0
|
0
|
|
|
|
|
next if ref $mesg->{ $e }; |
117
|
0
|
0
|
|
|
|
|
next unless $mesg->{ $e }; |
118
|
0
|
|
|
|
|
|
$text = $mesg->{ $e }; |
119
|
0
|
0
|
|
|
|
|
$text = sprintf( "'%s'", $text ) if $text =~ m/\s/; |
120
|
0
|
|
|
|
|
|
push @$logs, sprintf( "%s=%s", $e, $text ); |
121
|
|
|
|
|
|
|
} |
122
|
|
|
|
|
|
|
|
123
|
0
|
0
|
|
|
|
|
if( defined $mesg->{'message'} ) { |
124
|
|
|
|
|
|
|
|
125
|
0
|
0
|
|
|
|
|
if( ref $mesg->{'message'} eq 'ARRAY' ) { |
126
|
0
|
|
|
|
|
|
$text = sprintf( "message='%s'", join( ' | ', @{ $mesg->{'message'} } ) ); |
|
0
|
|
|
|
|
|
|
127
|
0
|
|
|
|
|
|
push @$logs, $text; |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
} else { |
130
|
0
|
|
|
|
|
|
push @$logs, sprintf( "message=%s", $mesg->{'message'} ); |
131
|
|
|
|
|
|
|
} |
132
|
|
|
|
|
|
|
} |
133
|
|
|
|
|
|
|
|
134
|
0
|
0
|
|
|
|
|
openlog( $self->{'identify'}, $self->o, $self->{'facility'} ) || return 0; |
135
|
0
|
0
|
|
|
|
|
syslog( $sllv, join( ', ', @$logs ) ) || return 0; |
136
|
0
|
0
|
|
|
|
|
closelog || return 0; |
137
|
0
|
|
|
|
|
|
return 1; |
138
|
|
|
|
|
|
|
} |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
1; |
141
|
|
|
|
|
|
|
__END__ |