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.14 by root, Sat Aug 20 01:49:27 2011 UTC vs.
Revision 1.15 by root, Sat Aug 20 02:16:59 2011 UTC

308 308
309 $$renabled = 1; # TODO 309 $$renabled = 1; # TODO
310 } 310 }
311} 311}
312 312
313sub _logger($;$) { 313sub _logger {
314 my ($ctx, $level, $renabled) = @_; 314 my ($ctx, $level, $renabled) = @_;
315
316 $renabled ||= \my $enabled;
317 315
318 $$renabled = 1; 316 $$renabled = 1;
319 317
320 my $logger = [$ctx, $level, $renabled]; 318 my $logger = [$ctx, $level, $renabled];
321 319
393All other (anonymous) contexts have no parents and an empty title by 391All other (anonymous) contexts have no parents and an empty title by
394default. 392default.
395 393
396When the module is loaded it creates the default context called 394When the module is loaded it creates the default context called
397C<AnyEvent::Log::Default> (also stored in C<$AnyEvent::Log::Default>), 395C<AnyEvent::Log::Default> (also stored in C<$AnyEvent::Log::Default>),
398which simply logs everything to STDERR and doesn't propagate anything 396which simply logs everything via C<warn> and doesn't propagate anything
399anywhere by default. The purpose of the default context is to provide 397anywhere by default. The purpose of the default context is to provide
400a convenient place to override the global logging target or to attach 398a convenient place to override the global logging target or to attach
401additional log targets. It's not meant for filtering. 399additional log targets. It's not meant for filtering.
402 400
403It then creates the root context called C<AnyEvent::Log::Root> (also 401It then creates the root context called C<AnyEvent::Log::Root> (also
417C<AE::Log::Top>. 415C<AE::Log::Top>.
418 416
419The effect of all this is that log messages, by default, wander up 417The effect of all this is that log messages, by default, wander up
420to the root context where log messages with lower priority then 418to the root context where log messages with lower priority then
421C<$ENV{PERL_ANYEVENT_VERBOSE}> will be filtered away and then to the 419C<$ENV{PERL_ANYEVENT_VERBOSE}> will be filtered away and then to the
422AnyEvent::Log::Default context to be logged to STDERR. 420AnyEvent::Log::Default context to be passed to C<warn>.
423 421
424Splitting the top level context into three contexts makes it easy to set 422Splitting the top level context into three contexts makes it easy to set
425a global logging level (by modifying the root context), but still allow 423a global logging level (by modifying the root context), but still allow
426other contexts to log, for example, their debug and trace messages to the 424other contexts to log, for example, their debug and trace messages to the
427default target despite the global logging level, or to attach additional 425default target despite the global logging level, or to attach additional
428log targets that log messages, regardless of the global logging level. 426log targets that log messages, regardless of the global logging level.
429 427
430It also makes it easy to replace the default STDERR-logger by something 428It also makes it easy to replace the default warn-logger by something that
431that logs to a file, or to attach additional logging targets. 429logs to a file, or to attach additional logging targets.
432 430
433=head2 CREATING/FINDING/DESTROYING CONTEXTS 431=head2 CREATING/FINDING/DESTROYING CONTEXTS
434 432
435=over 4 433=over 4
436 434
458 : bless [undef, (1 << 10) - 1 - 1], "AnyEvent::Log::Ctx" 456 : bless [undef, (1 << 10) - 1 - 1], "AnyEvent::Log::Ctx"
459} 457}
460 458
461=item AnyEvent::Log::reset 459=item AnyEvent::Log::reset
462 460
463Deletes all contexts and recreates the default hierarchy, i.e. resets the 461Resets all package contexts contexts and recreates the default hierarchy
464logging subsystem to defaults. 462if necessary, i.e. resets the logging subsystem to defaults.
465 463
466This can be used to implement config-file (re-)loading: before loading a 464This can be used to implement config-file (re-)loading: before loading a
467configuration, reset all contexts. 465configuration, reset all contexts.
468 466
467Note that this currently destroys all logger callbacks - bug me if you
468need this fixed :)
469
469=cut 470=cut
470 471
471sub reset { 472sub reset {
472 @$_ = () for values %CTX; # just to be sure - to kill circular logging dependencies 473 # hard to kill complex data structures
473 %CTX = (); 474 # we recreate all package loggers and reset the hierarchy
475 while (my ($k, $v) = each %CTX) {
476 @$v = ($k, (1 << 10) - 1 - 1, { });
474 477
475 my $default = ctx undef; 478 my $pkg = $k =~ /^(.+)::/ ? $1 : "AE::Log::Top";
479 $v->attach ($CTX{$pkg});
480 }
481
482 $AnyEvent::Log::Default->parents;
476 $default->title ("AnyEvent::Log::Default"); 483 $AnyEvent::Log::Default->title ("AnyEvent::Log::Default");
477 $default->log_cb (sub { 484 $AnyEvent::Log::Default->log_cb (sub {
478 print STDERR shift; 485 warn shift;
479 0 486 0
480 }); 487 });
481 $AnyEvent::Log::Default = $CTX{"AnyEvent::Log::Default"} = $CTX{"AE::Log::Default"} = $default; 488 $CTX{"AnyEvent::Log::Default"} = $CTX{"AE::Log::Default"} = $AnyEvent::Log::Default;
482 489
483 my $root = ctx undef; 490 $AnyEvent::Log::Root->parents ($AnyEvent::Log::Default);
484 $root->title ("AnyEvent::Log::Root"); 491 $AnyEvent::Log::Root->title ("AnyEvent::Log::Root");
485 $root->level ($AnyEvent::VERBOSE); 492 $AnyEvent::Log::Root->level ($AnyEvent::VERBOSE);
486 $root->attach ($default);
487 $AnyEvent::Log::Root = $CTX{"AnyEvent::Log::Root"} = $CTX{"AE::Log::Root"} = $root; 493 $CTX{"AnyEvent::Log::Root"} = $CTX{"AE::Log::Root"} = $AnyEvent::Log::Root;
488 494
489 my $top = ctx undef; 495 $AnyEvent::Log::Top->parents ($AnyEvent::Log::Root);
490 $top->title ("AnyEvent::Log::Top"); 496 $AnyEvent::Log::Top->title ("AnyEvent::Log::Top");
491 $top->attach ($root);
492 $AnyEvent::Log::Top = $CTX{"AnyEvent::Log::Top"} = $CTX{"AE::Log::Top"} = $top; 497 $CTX{"AnyEvent::Log::Top"} = $CTX{"AE::Log::Top"} = $AnyEvent::Log::Top;
498
499 _reassess;
493} 500}
501
502# create the default logger contexts
503$AnyEvent::Log::Default = ctx undef;
504$AnyEvent::Log::Root = ctx undef;
505$AnyEvent::Log::Top = ctx undef;
494 506
495AnyEvent::Log::reset; 507AnyEvent::Log::reset;
496 508
497# hello, CPAN, please catch me 509# hello, CPAN, please catch me
498package AnyEvent::Log::Default; 510package AnyEvent::Log::Default;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines