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.21 by root, Sat Aug 20 22:33:08 2011 UTC vs.
Revision 1.23 by root, Sun Aug 21 03:20:52 2011 UTC

8 use AnyEvent; 8 use AnyEvent;
9 9
10 AE::log debug => "hit my knee"; 10 AE::log debug => "hit my knee";
11 AE::log warn => "it's a bit too hot"; 11 AE::log warn => "it's a bit too hot";
12 AE::log error => "the flag was false!"; 12 AE::log error => "the flag was false!";
13 AE::log fatal => "the bit toggled! run!"; 13 AE::log fatal => "the bit toggled! run!"; # never returns
14 14
15 # "complex" use 15 # "complex" use (for speed sensitive code)
16 use AnyEvent::Log; 16 use AnyEvent::Log;
17 17
18 my $tracer = AnyEvent::Log::logger trace => \$my $trace; 18 my $tracer = AnyEvent::Log::logger trace => \$my $trace;
19 19
20 $tracer->("i am here") if $trace; 20 $tracer->("i am here") if $trace;
23 # configuration 23 # configuration
24 24
25 # set logging for the current package to errors and higher only 25 # set logging for the current package to errors and higher only
26 AnyEvent::Log::ctx->level ("error"); 26 AnyEvent::Log::ctx->level ("error");
27 27
28 # set logging globally to anything below debug 28 # set logging level to suppress anything below "notice"
29 $AnyEvent::Log::FILTER->level ("notice"); 29 $AnyEvent::Log::FILTER->level ("notice");
30
31 # send all critical and higher priority messages to syslog,
32 # regardless of (most) other settings
33 $AnyEvent::Log::COLLECT->attach (new AnyEvent::Log::Ctx
34 level => "critical",
35 log_to_syslog => 0,
36 );
30 37
31 # see also EXAMPLES, below 38 # see also EXAMPLES, below
32 39
33=head1 DESCRIPTION 40=head1 DESCRIPTION
34 41
138 $ctx 145 $ctx
139} 146}
140 147
141=item AnyEvent::Log::log $level, $msg[, @args] 148=item AnyEvent::Log::log $level, $msg[, @args]
142 149
143Requests logging of the given C<$msg> with the given log level. 150Requests logging of the given C<$msg> with the given log level, and
151returns true if the message was logged I<somewhere>.
144 152
145For C<fatal> log levels, the program will abort. 153For C<fatal> log levels, the program will abort.
146 154
147If only a C<$msg> is given, it is logged as-is. With extra C<@args>, the 155If only a C<$msg> is given, it is logged as-is. With extra C<@args>, the
148C<$msg> is interpreted as an sprintf format string. 156C<$msg> is interpreted as an sprintf format string.
154supposed to return the message. It will be called only then the message 162supposed to return the message. It will be called only then the message
155actually gets logged, which is useful if it is costly to create the 163actually gets logged, which is useful if it is costly to create the
156message in the first place. 164message in the first place.
157 165
158Whether the given message will be logged depends on the maximum log level 166Whether the given message will be logged depends on the maximum log level
159and the caller's package. 167and the caller's package. The return value can be used to ensure that
168messages or not "lost" - for example, when L<AnyEvent::Debug> detects a
169runtime error it tries to log it at C<die> level, but if that message is
170lost it simply uses warn.
160 171
161Note that you can (and should) call this function as C<AnyEvent::log> or 172Note that you can (and should) call this function as C<AnyEvent::log> or
162C<AE::log>, without C<use>-ing this module if possible (i.e. you don't 173C<AE::log>, without C<use>-ing this module if possible (i.e. you don't
163need any additional functionality), as those functions will load the 174need any additional functionality), as those functions will load the
164logging module on demand only. They are also much shorter to write. 175logging module on demand only. They are also much shorter to write.
224 ? $level+0 235 ? $level+0
225 : $STR2LEVEL{$level} || Carp::croak "$level: not a valid logging level, caught"; 236 : $STR2LEVEL{$level} || Carp::croak "$level: not a valid logging level, caught";
226 237
227 my $mask = 1 << $level; 238 my $mask = 1 << $level;
228 239
229 my (%seen, @ctx, $now, $fmt); 240 my ($success, %seen, @ctx, $now, $fmt);
230 241
231 do 242 do
232 { 243 {
233 # skip if masked 244 # skip if masked
234 if ($ctx->[1] & $mask && !$seen{$ctx+0}++) { 245 if ($ctx->[1] & $mask && !$seen{$ctx+0}++) {
245 256
246 # format msg 257 # format msg
247 my $str = $ctx->[4] 258 my $str = $ctx->[4]
248 ? $ctx->[4]($now, $_[0], $level, $format) 259 ? $ctx->[4]($now, $_[0], $level, $format)
249 : ($fmt ||= _format $now, $_[0], $level, $format); 260 : ($fmt ||= _format $now, $_[0], $level, $format);
261
262 $success = 1;
250 263
251 $ctx->[3]($str) 264 $ctx->[3]($str)
252 or push @ctx, values %{ $ctx->[2] }; # not consumed - propagate 265 or push @ctx, values %{ $ctx->[2] }; # not consumed - propagate
253 } else { 266 } else {
254 push @ctx, values %{ $ctx->[2] }; # not masked - propagate 267 push @ctx, values %{ $ctx->[2] }; # not masked - propagate
256 } 269 }
257 } 270 }
258 while $ctx = pop @ctx; 271 while $ctx = pop @ctx;
259 272
260 exit 1 if $level <= 1; 273 exit 1 if $level <= 1;
274
275 $success
261} 276}
262 277
263sub log($$;@) { 278sub log($$;@) {
264 _log 279 _log
265 $CTX{ (caller)[0] } ||= _pkg_ctx +(caller)[0], 280 $CTX{ (caller)[0] } ||= _pkg_ctx +(caller)[0],
269*AnyEvent::log = *AE::log = \&log; 284*AnyEvent::log = *AE::log = \&log;
270 285
271=item $logger = AnyEvent::Log::logger $level[, \$enabled] 286=item $logger = AnyEvent::Log::logger $level[, \$enabled]
272 287
273Creates a code reference that, when called, acts as if the 288Creates a code reference that, when called, acts as if the
274C<AnyEvent::Log::log> function was called at this point with the givne 289C<AnyEvent::Log::log> function was called at this point with the given
275level. C<$logger> is passed a C<$msg> and optional C<@args>, just as with 290level. C<$logger> is passed a C<$msg> and optional C<@args>, just as with
276the C<AnyEvent::Log::log> function: 291the C<AnyEvent::Log::log> function:
277 292
278 my $debug_log = AnyEvent::Log::logger "debug"; 293 my $debug_log = AnyEvent::Log::logger "debug";
279 294

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines