--- AnyEvent/lib/AnyEvent/Log.pm 2011/08/25 03:08:48 1.31 +++ AnyEvent/lib/AnyEvent/Log.pm 2011/08/26 00:32:45 1.38 @@ -51,15 +51,18 @@ something like: use AnyEvent::Log; - AnyEvent::Log::FILTER->level ("info"); + $AnyEvent::Log::FILTER->level ("info"); The design goal behind this module was to keep it simple (and small), but make it powerful enough to be potentially useful for any module, and extensive enough for the most common tasks, such as logging to multiple targets, or being able to log into a database. -The amount of documentation might indicate otherwise, but the module is -still just below 300 lines of code. +The module is also usable before AnyEvent itself is initialised, in which +case some of the functionality might be reduced. + +The amount of documentation might indicate otherwise, but the runtime part +of the module is still just below 300 lines of code. =head1 LOGGING LEVELS @@ -110,7 +113,7 @@ use POSIX (); use AnyEvent (); BEGIN { AnyEvent::common_sense } -use AnyEvent::Util (); +#use AnyEvent::Util (); need to load this in a delayed fashion, as it uses AE::log our $VERSION = $AnyEvent::VERSION; @@ -205,10 +208,21 @@ trace => 9, ); -sub now () { time } +our $TIME_EXACT; + +sub exact_time($) { + $TIME_EXACT = shift; + *_ts = $AnyEvent::MODEL + ? $TIME_EXACT ? \&AE::now : \&AE::time + : sub () { $TIME_EXACT ? do { require Time::HiRes; Time::HiRes::time () } : time }; +} + +BEGIN { + exact_time 0; +} AnyEvent::post_detect { - *now = \&AE::now; + exact_time $TIME_EXACT; }; our @LEVEL2STR = qw(0 fatal alert crit error warn note info debug trace); @@ -251,7 +265,7 @@ $format = $format->() if ref $format; $format = sprintf $format, @args if @args; $format =~ s/\n$//; - $now = now; + $now = _ts; }; # format msg @@ -356,10 +370,11 @@ _reassess $logger+0; - my $guard = AnyEvent::Util::guard { + require AnyEvent::Util; + my $guard = AnyEvent::Util::guard (sub { # "clean up" delete $LOGGER{$logger+0}; - }; + }); sub { $guard if 0; # keep guard alive, but don't cause runtime overhead @@ -375,6 +390,19 @@ @_ } +=item AnyEvent::Log::exact_time $on + +By default, C will use C, i.e. the cached +eventloop time, for the log timestamps. After calling this function with a +true value it will instead resort to C, i.e. fetch the current +time on each log message. This only makes a difference for event loops +that actually cache the time (such as L or L). + +Since C has to work even before the L has been +initialised, this switch will also decide whether to use C or +C when logging a message before L becomes +available. + =back =head1 LOGGING CONTEXTS @@ -515,7 +543,7 @@ @$_ = ($_->[0], (1 << 10) - 1 - 1) for $LOG, $FILTER, $COLLECT; - $LOG->slaves; + #$LOG->slaves; $LOG->title ('$AnyEvent::Log::LOG'); $LOG->log_to_warn; @@ -1014,7 +1042,7 @@ =item C or CI -Configured the context to log to syslog. If I is given, then it is +Configures the context to log to syslog. If I is given, then it is evaluated in the L package, so you could use: log=syslog=LOG_LOCAL0 @@ -1066,7 +1094,7 @@ # or, more verbose filter=only,level,warn -=item C<1>..C<9>, a logging level name (C, C etc.) +=item C<1>..C<9> or a logging level name (C, C etc.) A numeric loglevel or the name of a loglevel will be interpreted according to the most recent C, C or C directive. By default, @@ -1074,13 +1102,13 @@ =item C<+>I -Adds/attaches the named context as slave to the context. +Attaches the named context as slave to the context. =item C<+> -A line C<+> clears the slave list form the context. Anonymous (C<%name>) -contexts have no slaves by default, but package contexts have the parent -context as slave by default. +A line C<+> detaches all contexts, i.e. clears the slave list from the +context. Anonymous (C<%name>) contexts have no attached slaves by default, +but package contexts have the parent context as slave by default. Example: log messages from My::Module to a file, do not send them to the default log collector. @@ -1090,8 +1118,8 @@ =back Any character can be escaped by prefixing it with a C<\> (backslash), as -usual, so to log to a file containing a comma, colon, backslash and space in the -filename, you would do this: +usual, so to log to a file containing a comma, colon, backslash and some +spaces in the filename, you would do this: PERL_ANYEVENT_LOG='log=file=/some\ \:file\ with\,\ \\-escapes'