ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent/lib/AnyEvent/Log.pm
(Generate patch)

Comparing AnyEvent/lib/AnyEvent/Log.pm (file contents):
Revision 1.36 by root, Thu Aug 25 05:39:47 2011 UTC vs.
Revision 1.39 by root, Fri Aug 26 00:37:26 2011 UTC

111 111
112use Carp (); 112use Carp ();
113use POSIX (); 113use POSIX ();
114 114
115use AnyEvent (); BEGIN { AnyEvent::common_sense } 115use AnyEvent (); BEGIN { AnyEvent::common_sense }
116use AnyEvent::Util (); 116#use AnyEvent::Util (); need to load this in a delayed fashion, as it uses AE::log
117 117
118our $VERSION = $AnyEvent::VERSION; 118our $VERSION = $AnyEvent::VERSION;
119 119
120our ($COLLECT, $FILTER, $LOG); 120our ($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
211sub now () { time } 211our $TIME_EXACT;
212
213sub 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
220BEGIN {
221 exact_time 0;
222}
212 223
213AnyEvent::post_detect { 224AnyEvent::post_detect {
214 *now = \&AE::now; 225 exact_time $TIME_EXACT;
215}; 226};
216 227
217our @LEVEL2STR = qw(0 fatal alert crit error warn note info debug trace); 228our @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, @_
375sub logger($;$) { 387sub 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
395By default, C<AnyEvent::Log> will use C<AE::now>, i.e. the cached
396eventloop time, for the log timestamps. After calling this function with a
397true value it will instead resort to C<AE::time>, i.e. fetch the current
398time on each log message. This only makes a difference for event loops
399that actually cache the time (such as L<EV> or L<AnyEvent::Loop>).
400
401This setting can be changed at any time by calling this function.
402
403Since C<AnyEvent::Log> has to work even before the L<AnyEvent> has been
404initialised, this switch will also decide whether to use C<CORE::time> or
405C<Time::HiRes::time> when logging a message before L<AnyEvent> becomes
406available.
380 407
381=back 408=back
382 409
383=head1 LOGGING CONTEXTS 410=head1 LOGGING CONTEXTS
384 411

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines