… | |
… | |
111 | |
111 | |
112 | use Carp (); |
112 | use Carp (); |
113 | use POSIX (); |
113 | use POSIX (); |
114 | |
114 | |
115 | use AnyEvent (); BEGIN { AnyEvent::common_sense } |
115 | use AnyEvent (); BEGIN { AnyEvent::common_sense } |
116 | use AnyEvent::Util (); |
116 | #use AnyEvent::Util (); need to load this in a delayed fashion, as it uses AE::log |
117 | |
117 | |
118 | our $VERSION = $AnyEvent::VERSION; |
118 | our $VERSION = $AnyEvent::VERSION; |
119 | |
119 | |
120 | our ($COLLECT, $FILTER, $LOG); |
120 | our ($COLLECT, $FILTER, $LOG); |
121 | |
121 | |
… | |
… | |
206 | info => 7, |
206 | info => 7, |
207 | debug => 8, |
207 | debug => 8, |
208 | trace => 9, |
208 | trace => 9, |
209 | ); |
209 | ); |
210 | |
210 | |
211 | sub now () { time } |
211 | our $TIME_EXACT; |
|
|
212 | |
|
|
213 | sub exact_time($) { |
|
|
214 | $TIME_EXACT = shift; |
|
|
215 | *_ts = $AnyEvent::MODEL |
|
|
216 | ? $TIME_EXACT ? \&AE::now : \&AE::time |
|
|
217 | : sub () { $TIME_EXACT ? do { require Time::HiRes; Time::HiRes::time () } : time }; |
|
|
218 | } |
|
|
219 | |
|
|
220 | BEGIN { |
|
|
221 | exact_time 0; |
|
|
222 | } |
212 | |
223 | |
213 | AnyEvent::post_detect { |
224 | AnyEvent::post_detect { |
214 | *now = \&AE::now; |
225 | exact_time $TIME_EXACT; |
215 | }; |
226 | }; |
216 | |
227 | |
217 | our @LEVEL2STR = qw(0 fatal alert crit error warn note info debug trace); |
228 | our @LEVEL2STR = qw(0 fatal alert crit error warn note info debug trace); |
218 | |
229 | |
219 | # time, ctx, level, msg |
230 | # time, ctx, level, msg |
… | |
… | |
252 | # now get raw message, unless we have it already |
263 | # now get raw message, unless we have it already |
253 | unless ($now) { |
264 | unless ($now) { |
254 | $format = $format->() if ref $format; |
265 | $format = $format->() if ref $format; |
255 | $format = sprintf $format, @args if @args; |
266 | $format = sprintf $format, @args if @args; |
256 | $format =~ s/\n$//; |
267 | $format =~ s/\n$//; |
257 | $now = now; |
268 | $now = _ts; |
258 | }; |
269 | }; |
259 | |
270 | |
260 | # format msg |
271 | # format msg |
261 | my $str = $ctx->[4] |
272 | my $str = $ctx->[4] |
262 | ? $ctx->[4]($now, $_[0], $level, $format) |
273 | ? $ctx->[4]($now, $_[0], $level, $format) |
… | |
… | |
357 | |
368 | |
358 | $LOGGER{$logger+0} = $logger; |
369 | $LOGGER{$logger+0} = $logger; |
359 | |
370 | |
360 | _reassess $logger+0; |
371 | _reassess $logger+0; |
361 | |
372 | |
|
|
373 | require AnyEvent::Util; |
362 | my $guard = AnyEvent::Util::guard { |
374 | my $guard = AnyEvent::Util::guard (sub { |
363 | # "clean up" |
375 | # "clean up" |
364 | delete $LOGGER{$logger+0}; |
376 | delete $LOGGER{$logger+0}; |
365 | }; |
377 | }); |
366 | |
378 | |
367 | sub { |
379 | sub { |
368 | $guard if 0; # keep guard alive, but don't cause runtime overhead |
380 | $guard if 0; # keep guard alive, but don't cause runtime overhead |
369 | |
381 | |
370 | _log $ctx, $level, @_ |
382 | _log $ctx, $level, @_ |
… | |
… | |
375 | sub logger($;$) { |
387 | sub logger($;$) { |
376 | _logger |
388 | _logger |
377 | $CTX{ (caller)[0] } ||= _pkg_ctx +(caller)[0], |
389 | $CTX{ (caller)[0] } ||= _pkg_ctx +(caller)[0], |
378 | @_ |
390 | @_ |
379 | } |
391 | } |
|
|
392 | |
|
|
393 | =item AnyEvent::Log::exact_time $on |
|
|
394 | |
|
|
395 | By default, C<AnyEvent::Log> will use C<AE::now>, i.e. the cached |
|
|
396 | eventloop time, for the log timestamps. After calling this function with a |
|
|
397 | true value it will instead resort to C<AE::time>, i.e. fetch the current |
|
|
398 | time on each log message. This only makes a difference for event loops |
|
|
399 | that actually cache the time (such as L<EV> or L<AnyEvent::Loop>). |
|
|
400 | |
|
|
401 | This setting can be changed at any time by calling this function. |
|
|
402 | |
|
|
403 | Since C<AnyEvent::Log> has to work even before the L<AnyEvent> has been |
|
|
404 | initialised, this switch will also decide whether to use C<CORE::time> or |
|
|
405 | C<Time::HiRes::time> when logging a message before L<AnyEvent> becomes |
|
|
406 | available. |
380 | |
407 | |
381 | =back |
408 | =back |
382 | |
409 | |
383 | =head1 LOGGING CONTEXTS |
410 | =head1 LOGGING CONTEXTS |
384 | |
411 | |