line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
#=============================================================================== |
2
|
|
|
|
|
|
|
# |
3
|
|
|
|
|
|
|
# FILE: Logger.pm |
4
|
|
|
|
|
|
|
# |
5
|
|
|
|
|
|
|
# DESCRIPTION: Syslog wrapper for Net SDS |
6
|
|
|
|
|
|
|
# |
7
|
|
|
|
|
|
|
# AUTHOR: Michael Bochkaryov (Rattler), |
8
|
|
|
|
|
|
|
# COMPANY: Net.Style |
9
|
|
|
|
|
|
|
# CREATED: 25.04.2008 17:32:37 EEST |
10
|
|
|
|
|
|
|
#=============================================================================== |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
=head1 NAME |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
NetSDS::Logger - syslog wrapper for applications and classes |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
=head1 SYNOPSIS |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
use NetSDS::Logger; |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
my $logger = NetSDS::Logger->new( |
21
|
|
|
|
|
|
|
name => 'NetSDS-SuperDaemon', |
22
|
|
|
|
|
|
|
); |
23
|
|
|
|
|
|
|
$logger->log("info", "Syslog message here"); |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
=head1 DESCRIPTION |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
This module contains implementation of logging functionality for NetSDS components. |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
By default, messages are logged with C facility and C options. |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
B: C module is for internal use mostly from application |
32
|
|
|
|
|
|
|
frameworks like C, C, etc. |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
=cut |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
package NetSDS::Logger; |
37
|
|
|
|
|
|
|
|
38
|
2
|
|
|
2
|
|
7014
|
use 5.8.0; |
|
2
|
|
|
|
|
9
|
|
|
2
|
|
|
|
|
164
|
|
39
|
2
|
|
|
2
|
|
11
|
use warnings; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
77
|
|
40
|
|
|
|
|
|
|
|
41
|
2
|
|
|
2
|
|
1708
|
use Unix::Syslog qw(:macros :subs); |
|
2
|
|
|
|
|
2235
|
|
|
2
|
|
|
|
|
859
|
|
42
|
|
|
|
|
|
|
|
43
|
2
|
|
|
2
|
|
16
|
use version; our $VERSION = '1.301'; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
16
|
|
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
#=============================================================================== |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
=head1 CLASS API |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
=over |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
=item B - constructor |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
Constructor B creates new logger object and opens socket with default |
54
|
|
|
|
|
|
|
NetSDS logging parameters. |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
Arguments allowed (as hash): |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
B - application name for identification |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
Use only ASCII characters in "name" to avoid possible errors. |
61
|
|
|
|
|
|
|
Default value is "NetSDS". |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
B - logging facility |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
Available facility values: |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
* local0..local7 |
68
|
|
|
|
|
|
|
* user |
69
|
|
|
|
|
|
|
* daemon |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
If not set 'local0' is used as default value |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
=cut |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
#----------------------------------------------------------------------- |
76
|
|
|
|
|
|
|
sub new { |
77
|
|
|
|
|
|
|
|
78
|
0
|
|
|
0
|
1
|
|
my ( $class, %params ) = @_; |
79
|
|
|
|
|
|
|
|
80
|
0
|
|
|
|
|
|
my $self = {}; |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
# Set application identification name |
83
|
0
|
|
|
|
|
|
my $name = 'NetSDS'; |
84
|
0
|
0
|
|
|
|
|
if ( $params{name} ) { |
85
|
0
|
|
|
|
|
|
$name = $params{name}; |
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
# Set logging facility |
89
|
0
|
|
|
|
|
|
my %facility_map = ( |
90
|
|
|
|
|
|
|
'local0' => LOG_LOCAL0, |
91
|
|
|
|
|
|
|
'local1' => LOG_LOCAL1, |
92
|
|
|
|
|
|
|
'local2' => LOG_LOCAL2, |
93
|
|
|
|
|
|
|
'local3' => LOG_LOCAL3, |
94
|
|
|
|
|
|
|
'local4' => LOG_LOCAL4, |
95
|
|
|
|
|
|
|
'local5' => LOG_LOCAL5, |
96
|
|
|
|
|
|
|
'local6' => LOG_LOCAL6, |
97
|
|
|
|
|
|
|
'local7' => LOG_LOCAL7, |
98
|
|
|
|
|
|
|
'user' => LOG_USER, |
99
|
|
|
|
|
|
|
'daemon' => LOG_DAEMON, |
100
|
|
|
|
|
|
|
); |
101
|
|
|
|
|
|
|
|
102
|
0
|
|
|
|
|
|
my $facility = LOG_LOCAL0; # default is local0 |
103
|
0
|
0
|
|
|
|
|
if ( $params{facility} ) { |
104
|
0
|
|
0
|
|
|
|
$facility = $facility_map{ $params{facility} } || LOG_LOCAL0; |
105
|
|
|
|
|
|
|
} |
106
|
|
|
|
|
|
|
|
107
|
0
|
|
|
|
|
|
openlog( $name, LOG_PID | LOG_CONS | LOG_NDELAY, $facility ); |
108
|
|
|
|
|
|
|
|
109
|
0
|
|
|
|
|
|
return bless $self, $class; |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
} ## end sub new |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
#*********************************************************************** |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
=item B - write record to log |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
Wrapper to C method of L module. |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
Level is passed as string and may be one of the following: |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
alert - LOG_ALERT |
122
|
|
|
|
|
|
|
crit - LOG_CRIT |
123
|
|
|
|
|
|
|
debug - LOG_DEBUG |
124
|
|
|
|
|
|
|
emerg - LOG_EMERG |
125
|
|
|
|
|
|
|
error - LOG_ERR |
126
|
|
|
|
|
|
|
info - LOG_INFO |
127
|
|
|
|
|
|
|
notice - LOG_NOTICE |
128
|
|
|
|
|
|
|
warning - LOG_WARNING |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
=cut |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
#----------------------------------------------------------------------- |
133
|
|
|
|
|
|
|
sub log { |
134
|
|
|
|
|
|
|
|
135
|
0
|
|
|
0
|
1
|
|
my ( $self, $level, $message ) = @_; |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
# Level aliases |
138
|
0
|
|
|
|
|
|
my %LEVFIX = ( |
139
|
|
|
|
|
|
|
alert => LOG_ALERT, |
140
|
|
|
|
|
|
|
crit => LOG_CRIT, |
141
|
|
|
|
|
|
|
critical => LOG_CRIT, |
142
|
|
|
|
|
|
|
deb => LOG_DEBUG, |
143
|
|
|
|
|
|
|
debug => LOG_DEBUG, |
144
|
|
|
|
|
|
|
emerg => LOG_EMERG, |
145
|
|
|
|
|
|
|
emergency => LOG_EMERG, |
146
|
|
|
|
|
|
|
panic => LOG_EMERG, |
147
|
|
|
|
|
|
|
err => LOG_ERR, |
148
|
|
|
|
|
|
|
error => LOG_ERR, |
149
|
|
|
|
|
|
|
inf => LOG_INFO, |
150
|
|
|
|
|
|
|
info => LOG_INFO, |
151
|
|
|
|
|
|
|
inform => LOG_INFO, |
152
|
|
|
|
|
|
|
note => LOG_NOTICE, |
153
|
|
|
|
|
|
|
notice => LOG_NOTICE, |
154
|
|
|
|
|
|
|
warning => LOG_WARNING, |
155
|
|
|
|
|
|
|
warn => LOG_WARNING, |
156
|
|
|
|
|
|
|
); |
157
|
|
|
|
|
|
|
|
158
|
0
|
|
|
|
|
|
my $LEV = $LEVFIX{$level}; |
159
|
|
|
|
|
|
|
|
160
|
0
|
0
|
|
|
|
|
if ( !$LEV ) { |
161
|
0
|
|
|
|
|
|
$LEV = LOG_INFO; |
162
|
|
|
|
|
|
|
} |
163
|
|
|
|
|
|
|
|
164
|
0
|
0
|
|
|
|
|
if ( !$message ) { |
165
|
0
|
|
|
|
|
|
$message = ""; |
166
|
|
|
|
|
|
|
} |
167
|
|
|
|
|
|
|
|
168
|
0
|
|
|
|
|
|
syslog( $LEV, "[$level] $message" ); |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
} ## end sub log |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
#*********************************************************************** |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
=item B - class destructor |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
Destructor (DESTROY method) calls C function. That's all. |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
=cut |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
#----------------------------------------------------------------------- |
181
|
|
|
|
|
|
|
sub DESTROY { |
182
|
|
|
|
|
|
|
|
183
|
0
|
|
|
0
|
|
|
closelog(); |
184
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
} |
186
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
1; |
188
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
__END__ |