… | |
… | |
308 | |
308 | |
309 | $$renabled = 1; # TODO |
309 | $$renabled = 1; # TODO |
310 | } |
310 | } |
311 | } |
311 | } |
312 | |
312 | |
313 | sub _logger($;$) { |
313 | sub _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 | |
… | |
… | |
393 | All other (anonymous) contexts have no parents and an empty title by |
391 | All other (anonymous) contexts have no parents and an empty title by |
394 | default. |
392 | default. |
395 | |
393 | |
396 | When the module is loaded it creates the default context called |
394 | When the module is loaded it creates the default context called |
397 | C<AnyEvent::Log::Default> (also stored in C<$AnyEvent::Log::Default>), |
395 | C<AnyEvent::Log::Default> (also stored in C<$AnyEvent::Log::Default>), |
398 | which simply logs everything to STDERR and doesn't propagate anything |
396 | which simply logs everything via C<warn> and doesn't propagate anything |
399 | anywhere by default. The purpose of the default context is to provide |
397 | anywhere by default. The purpose of the default context is to provide |
400 | a convenient place to override the global logging target or to attach |
398 | a convenient place to override the global logging target or to attach |
401 | additional log targets. It's not meant for filtering. |
399 | additional log targets. It's not meant for filtering. |
402 | |
400 | |
403 | It then creates the root context called C<AnyEvent::Log::Root> (also |
401 | It then creates the root context called C<AnyEvent::Log::Root> (also |
… | |
… | |
417 | C<AE::Log::Top>. |
415 | C<AE::Log::Top>. |
418 | |
416 | |
419 | The effect of all this is that log messages, by default, wander up |
417 | The effect of all this is that log messages, by default, wander up |
420 | to the root context where log messages with lower priority then |
418 | to the root context where log messages with lower priority then |
421 | C<$ENV{PERL_ANYEVENT_VERBOSE}> will be filtered away and then to the |
419 | C<$ENV{PERL_ANYEVENT_VERBOSE}> will be filtered away and then to the |
422 | AnyEvent::Log::Default context to be logged to STDERR. |
420 | AnyEvent::Log::Default context to be passed to C<warn>. |
423 | |
421 | |
424 | Splitting the top level context into three contexts makes it easy to set |
422 | Splitting the top level context into three contexts makes it easy to set |
425 | a global logging level (by modifying the root context), but still allow |
423 | a global logging level (by modifying the root context), but still allow |
426 | other contexts to log, for example, their debug and trace messages to the |
424 | other contexts to log, for example, their debug and trace messages to the |
427 | default target despite the global logging level, or to attach additional |
425 | default target despite the global logging level, or to attach additional |
428 | log targets that log messages, regardless of the global logging level. |
426 | log targets that log messages, regardless of the global logging level. |
429 | |
427 | |
430 | It also makes it easy to replace the default STDERR-logger by something |
428 | It also makes it easy to replace the default warn-logger by something that |
431 | that logs to a file, or to attach additional logging targets. |
429 | logs 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 | |
463 | Deletes all contexts and recreates the default hierarchy, i.e. resets the |
461 | Resets all package contexts contexts and recreates the default hierarchy |
464 | logging subsystem to defaults. |
462 | if necessary, i.e. resets the logging subsystem to defaults. |
465 | |
463 | |
466 | This can be used to implement config-file (re-)loading: before loading a |
464 | This can be used to implement config-file (re-)loading: before loading a |
467 | configuration, reset all contexts. |
465 | configuration, reset all contexts. |
468 | |
466 | |
|
|
467 | Note that this currently destroys all logger callbacks - bug me if you |
|
|
468 | need this fixed :) |
|
|
469 | |
469 | =cut |
470 | =cut |
470 | |
471 | |
471 | sub reset { |
472 | sub 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 | |
495 | AnyEvent::Log::reset; |
507 | AnyEvent::Log::reset; |
496 | |
508 | |
497 | # hello, CPAN, please catch me |
509 | # hello, CPAN, please catch me |
498 | package AnyEvent::Log::Default; |
510 | package AnyEvent::Log::Default; |