line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
5
|
|
|
5
|
|
119227
|
use strict; |
|
5
|
|
|
|
|
11
|
|
|
5
|
|
|
|
|
178
|
|
2
|
5
|
|
|
5
|
|
28
|
use warnings; |
|
5
|
|
|
|
|
9
|
|
|
5
|
|
|
|
|
134
|
|
3
|
5
|
|
|
5
|
|
111
|
use 5.006; # Found with Perl::MinimumVersion |
|
5
|
|
|
|
|
32
|
|
|
5
|
|
|
|
|
332
|
|
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
package Log::UDP::Client; |
6
|
|
|
|
|
|
|
BEGIN { |
7
|
5
|
|
|
5
|
|
111
|
$Log::UDP::Client::VERSION = '0.20.0'; |
8
|
|
|
|
|
|
|
} |
9
|
5
|
|
|
5
|
|
7240
|
use Moose; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
with 'Data::Serializable' => { -version => '0.40.0' }; |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
# ABSTRACT: A simple way to send structured log messages via UDP |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
use IO::Socket::INET (); |
15
|
|
|
|
|
|
|
use Carp qw(carp croak); |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
has "server_address" => ( |
19
|
|
|
|
|
|
|
is => 'rw', |
20
|
|
|
|
|
|
|
isa => 'Str', |
21
|
|
|
|
|
|
|
default => sub { "127.0.0.1"; }, |
22
|
|
|
|
|
|
|
); |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
has "server_port" => ( |
26
|
|
|
|
|
|
|
is => 'rw', |
27
|
|
|
|
|
|
|
isa => 'Int', |
28
|
|
|
|
|
|
|
default => sub { 9999; } |
29
|
|
|
|
|
|
|
); |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
has "throws_exception" => ( |
33
|
|
|
|
|
|
|
is => 'rw', |
34
|
|
|
|
|
|
|
isa => 'Bool', |
35
|
|
|
|
|
|
|
default => 0, |
36
|
|
|
|
|
|
|
); |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
has "socket" => ( |
40
|
|
|
|
|
|
|
is => 'ro', |
41
|
|
|
|
|
|
|
isa => 'IO::Socket::INET', |
42
|
|
|
|
|
|
|
lazy => 1, |
43
|
|
|
|
|
|
|
default => sub { IO::Socket::INET->new( Proto => 'udp' ); } |
44
|
|
|
|
|
|
|
); |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
# Perl::Critic bug: Subroutines::RequireArgUnpacking shouldn't be needed here |
48
|
|
|
|
|
|
|
## no critic qw(Subroutines::ProhibitBuiltinHomonyms Subroutines::RequireArgUnpacking) |
49
|
|
|
|
|
|
|
sub send { |
50
|
|
|
|
|
|
|
my ($self, $message) = @_; |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
# Make sure message was specified |
53
|
|
|
|
|
|
|
if ( @_ < 2 ) { |
54
|
|
|
|
|
|
|
croak("Please specify message") if $self->throws_exception; |
55
|
|
|
|
|
|
|
return; # FAIL |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
# Use the specified serializer to encode the message in a binary format |
59
|
|
|
|
|
|
|
my $serialized_message = $self->serialize( $message ); |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
# Trap failure in serialization when not emitting exceptions |
62
|
|
|
|
|
|
|
if ( not $self->throws_exception and not defined($serialized_message) ) { |
63
|
|
|
|
|
|
|
return; # FAIL |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
# Send UDP message |
67
|
|
|
|
|
|
|
my $length = CORE::send( |
68
|
|
|
|
|
|
|
$self->socket, |
69
|
|
|
|
|
|
|
$serialized_message, |
70
|
|
|
|
|
|
|
0, |
71
|
|
|
|
|
|
|
IO::Socket::INET::pack_sockaddr_in( |
72
|
|
|
|
|
|
|
$self->server_port, |
73
|
|
|
|
|
|
|
IO::Socket::INET::inet_aton( $self->server_address ) |
74
|
|
|
|
|
|
|
) |
75
|
|
|
|
|
|
|
); |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
# Check for transmission error |
78
|
|
|
|
|
|
|
if ( $length != length($serialized_message) ) { |
79
|
|
|
|
|
|
|
my $error = "Couldn't send message: $!\n"; |
80
|
|
|
|
|
|
|
croak($error) if $self->throws_exception; |
81
|
|
|
|
|
|
|
carp($error); |
82
|
|
|
|
|
|
|
return 0; |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
# Everything OK |
86
|
|
|
|
|
|
|
return 1; |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
} |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
1; |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
=pod |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
=encoding utf-8 |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
=head1 NAME |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
Log::UDP::Client - A simple way to send structured log messages via UDP |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
=head1 VERSION |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
version 0.20.0 |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
=head1 SYNOPSIS |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
use Log::UDP::Client; |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
# Send the simple scalar to the server |
111
|
|
|
|
|
|
|
Log::UDP::Client->new->send("Hi"); |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
# Log lots of messages |
114
|
|
|
|
|
|
|
my $logger = Log::UDP::Client->new(server_port => 15000); |
115
|
|
|
|
|
|
|
my $counter=0; |
116
|
|
|
|
|
|
|
while(++$counter) { |
117
|
|
|
|
|
|
|
$logger->send($counter); |
118
|
|
|
|
|
|
|
last if $counter >= 1000; |
119
|
|
|
|
|
|
|
} |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
# Send some debugging info |
122
|
|
|
|
|
|
|
$logger->send({ |
123
|
|
|
|
|
|
|
pid => $$, |
124
|
|
|
|
|
|
|
program => $0, |
125
|
|
|
|
|
|
|
args => \@ARGV, |
126
|
|
|
|
|
|
|
}); |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
# Use of JSON serializer |
129
|
|
|
|
|
|
|
my $logger = Log::UDP::Client->new( serializer_module => 'JSON' ); |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
# Will emit { "message" => "Hi" } because JSON want to wrap stuff into a hashref |
132
|
|
|
|
|
|
|
$logger->send("Hi"); |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
# Use of custom serializer |
135
|
|
|
|
|
|
|
use Storable qw(freeze); |
136
|
|
|
|
|
|
|
my $logger = Log::UDP::Client->new ( |
137
|
|
|
|
|
|
|
serializer => sub { |
138
|
|
|
|
|
|
|
return nfreeze( \( $_[0] ) ); |
139
|
|
|
|
|
|
|
}, |
140
|
|
|
|
|
|
|
); |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
=head1 DESCRIPTION |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
This module enables you to send a message (simple string or complicated object) |
145
|
|
|
|
|
|
|
over an UDP socket to a listening server. The message will be encoded with a |
146
|
|
|
|
|
|
|
serializer module (default is L<Storable>). |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
=head1 ATTRIBUTES |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
=head2 server_address : Str |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
IP address or hostname for the server you want to send the messages to. |
153
|
|
|
|
|
|
|
This field can be changed after instantiation. Default is 127.0.0.1. |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
=head2 server_port : Int |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
Port for the server you plan to send the messages to. |
158
|
|
|
|
|
|
|
This field can be changed after instantiation. Default is port 9999. |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
=head2 throws_exception : Bool |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
If errors are encountered, should we throw exception or just return? |
163
|
|
|
|
|
|
|
Default is return. Set to true for exceptions. You can change this flag |
164
|
|
|
|
|
|
|
after instantiation. |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
=head2 socket : IO::Socket::INET |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
Read-only field that contains the socket used to send the messages. |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
=head1 METHODS |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
=head2 send($message) |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
Instance method that actually encodes and transmits the specified message |
175
|
|
|
|
|
|
|
over UDP to the listening server. Will die if throw_exception is set to true |
176
|
|
|
|
|
|
|
and some kind of transmission error occurs. The message will be serialized by |
177
|
|
|
|
|
|
|
the instance-defined serializer. Returns true on success. |
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
=head1 INHERITED METHODS |
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
=over 4 |
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
=item * |
184
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
deserialize |
186
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
=item * |
188
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
deserializer |
190
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
=item * |
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
serialize |
194
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
=item * |
196
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
serializer |
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
=item * |
200
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
serializer_module |
202
|
|
|
|
|
|
|
|
203
|
|
|
|
|
|
|
=back |
204
|
|
|
|
|
|
|
|
205
|
|
|
|
|
|
|
All of these methods are inherited from L<Data::Serializable>. Read more about them there. |
206
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
=head1 SEE ALSO |
208
|
|
|
|
|
|
|
|
209
|
|
|
|
|
|
|
=over 4 |
210
|
|
|
|
|
|
|
|
211
|
|
|
|
|
|
|
=item * |
212
|
|
|
|
|
|
|
|
213
|
|
|
|
|
|
|
L<Moose> |
214
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
=item * |
216
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
L<Storable> |
218
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
=item * |
220
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
L<JSON::XS> |
222
|
|
|
|
|
|
|
|
223
|
|
|
|
|
|
|
=item * |
224
|
|
|
|
|
|
|
|
225
|
|
|
|
|
|
|
L<IO::Socket::INET> |
226
|
|
|
|
|
|
|
|
227
|
|
|
|
|
|
|
=back |
228
|
|
|
|
|
|
|
|
229
|
|
|
|
|
|
|
=for :stopwords CPAN AnnoCPAN RT CPANTS Kwalitee diff |
230
|
|
|
|
|
|
|
|
231
|
|
|
|
|
|
|
=head1 SUPPORT |
232
|
|
|
|
|
|
|
|
233
|
|
|
|
|
|
|
You can find documentation for this module with the perldoc command. |
234
|
|
|
|
|
|
|
|
235
|
|
|
|
|
|
|
perldoc Log::UDP::Client |
236
|
|
|
|
|
|
|
|
237
|
|
|
|
|
|
|
=head2 Websites |
238
|
|
|
|
|
|
|
|
239
|
|
|
|
|
|
|
=over 4 |
240
|
|
|
|
|
|
|
|
241
|
|
|
|
|
|
|
=item * |
242
|
|
|
|
|
|
|
|
243
|
|
|
|
|
|
|
Search CPAN |
244
|
|
|
|
|
|
|
|
245
|
|
|
|
|
|
|
L<http://search.cpan.org/dist/Log-UDP-Client> |
246
|
|
|
|
|
|
|
|
247
|
|
|
|
|
|
|
=item * |
248
|
|
|
|
|
|
|
|
249
|
|
|
|
|
|
|
AnnoCPAN: Annotated CPAN documentation |
250
|
|
|
|
|
|
|
|
251
|
|
|
|
|
|
|
L<http://annocpan.org/dist/Log-UDP-Client> |
252
|
|
|
|
|
|
|
|
253
|
|
|
|
|
|
|
=item * |
254
|
|
|
|
|
|
|
|
255
|
|
|
|
|
|
|
CPAN Ratings |
256
|
|
|
|
|
|
|
|
257
|
|
|
|
|
|
|
L<http://cpanratings.perl.org/d/Log-UDP-Client> |
258
|
|
|
|
|
|
|
|
259
|
|
|
|
|
|
|
=item * |
260
|
|
|
|
|
|
|
|
261
|
|
|
|
|
|
|
CPAN Forum |
262
|
|
|
|
|
|
|
|
263
|
|
|
|
|
|
|
L<http://cpanforum.com/dist/Log-UDP-Client> |
264
|
|
|
|
|
|
|
|
265
|
|
|
|
|
|
|
=item * |
266
|
|
|
|
|
|
|
|
267
|
|
|
|
|
|
|
RT: CPAN's Bug Tracker |
268
|
|
|
|
|
|
|
|
269
|
|
|
|
|
|
|
L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Log-UDP-Client> |
270
|
|
|
|
|
|
|
|
271
|
|
|
|
|
|
|
=item * |
272
|
|
|
|
|
|
|
|
273
|
|
|
|
|
|
|
CPANTS Kwalitee |
274
|
|
|
|
|
|
|
|
275
|
|
|
|
|
|
|
L<http://cpants.perl.org/dist/overview/Log-UDP-Client> |
276
|
|
|
|
|
|
|
|
277
|
|
|
|
|
|
|
=item * |
278
|
|
|
|
|
|
|
|
279
|
|
|
|
|
|
|
CPAN Testers Results |
280
|
|
|
|
|
|
|
|
281
|
|
|
|
|
|
|
L<http://cpantesters.org/distro/L/Log-UDP-Client.html> |
282
|
|
|
|
|
|
|
|
283
|
|
|
|
|
|
|
=item * |
284
|
|
|
|
|
|
|
|
285
|
|
|
|
|
|
|
CPAN Testers Matrix |
286
|
|
|
|
|
|
|
|
287
|
|
|
|
|
|
|
L<http://matrix.cpantesters.org/?dist=Log-UDP-Client> |
288
|
|
|
|
|
|
|
|
289
|
|
|
|
|
|
|
=item * |
290
|
|
|
|
|
|
|
|
291
|
|
|
|
|
|
|
Source Code Repository |
292
|
|
|
|
|
|
|
|
293
|
|
|
|
|
|
|
The code is open to the world, and available for you to hack on. Please feel free to browse it and play |
294
|
|
|
|
|
|
|
with it, or whatever. If you want to contribute patches, please send me a diff or prod me to pull |
295
|
|
|
|
|
|
|
from your repository :) |
296
|
|
|
|
|
|
|
|
297
|
|
|
|
|
|
|
L<git://github.com/robinsmidsrod/Log-UDP-Client.git> |
298
|
|
|
|
|
|
|
|
299
|
|
|
|
|
|
|
=back |
300
|
|
|
|
|
|
|
|
301
|
|
|
|
|
|
|
=head2 Bugs |
302
|
|
|
|
|
|
|
|
303
|
|
|
|
|
|
|
Please report any bugs or feature requests to C<bug-log-udp-client at rt.cpan.org>, or through |
304
|
|
|
|
|
|
|
the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Log-UDP-Client>. I will be |
305
|
|
|
|
|
|
|
notified, and then you'll automatically be notified of progress on your bug as I make changes. |
306
|
|
|
|
|
|
|
|
307
|
|
|
|
|
|
|
=head1 AUTHOR |
308
|
|
|
|
|
|
|
|
309
|
|
|
|
|
|
|
Robin Smidsrød <robin@smidsrod.no> |
310
|
|
|
|
|
|
|
|
311
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
312
|
|
|
|
|
|
|
|
313
|
|
|
|
|
|
|
This software is copyright (c) 2010 by Robin Smidsrød. |
314
|
|
|
|
|
|
|
|
315
|
|
|
|
|
|
|
This is free software; you can redistribute it and/or modify it under |
316
|
|
|
|
|
|
|
the same terms as the Perl 5 programming language system itself. |
317
|
|
|
|
|
|
|
|
318
|
|
|
|
|
|
|
=cut |
319
|
|
|
|
|
|
|
|
320
|
|
|
|
|
|
|
|
321
|
|
|
|
|
|
|
__END__ |
322
|
|
|
|
|
|
|
|