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.22 by root, Sun Aug 21 02:19:30 2011 UTC

138 $ctx 138 $ctx
139} 139}
140 140
141=item AnyEvent::Log::log $level, $msg[, @args] 141=item AnyEvent::Log::log $level, $msg[, @args]
142 142
143Requests logging of the given C<$msg> with the given log level. 143Requests logging of the given C<$msg> with the given log level, and
144returns true if the message was logged I<somewhere>.
144 145
145For C<fatal> log levels, the program will abort. 146For C<fatal> log levels, the program will abort.
146 147
147If only a C<$msg> is given, it is logged as-is. With extra C<@args>, the 148If 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. 149C<$msg> is interpreted as an sprintf format string.
154supposed to return the message. It will be called only then the message 155supposed 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 156actually gets logged, which is useful if it is costly to create the
156message in the first place. 157message in the first place.
157 158
158Whether the given message will be logged depends on the maximum log level 159Whether the given message will be logged depends on the maximum log level
159and the caller's package. 160and the caller's package. The return value can be used to ensure that
161messages or not "lost" - for example, when L<AnyEvent::Debug> detects a
162runtime error it tries to log it at C<die> level, but if that message is
163lost it simply uses warn.
160 164
161Note that you can (and should) call this function as C<AnyEvent::log> or 165Note 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 166C<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 167need any additional functionality), as those functions will load the
164logging module on demand only. They are also much shorter to write. 168logging module on demand only. They are also much shorter to write.
224 ? $level+0 228 ? $level+0
225 : $STR2LEVEL{$level} || Carp::croak "$level: not a valid logging level, caught"; 229 : $STR2LEVEL{$level} || Carp::croak "$level: not a valid logging level, caught";
226 230
227 my $mask = 1 << $level; 231 my $mask = 1 << $level;
228 232
229 my (%seen, @ctx, $now, $fmt); 233 my ($success, %seen, @ctx, $now, $fmt);
230 234
231 do 235 do
232 { 236 {
233 # skip if masked 237 # skip if masked
234 if ($ctx->[1] & $mask && !$seen{$ctx+0}++) { 238 if ($ctx->[1] & $mask && !$seen{$ctx+0}++) {
245 249
246 # format msg 250 # format msg
247 my $str = $ctx->[4] 251 my $str = $ctx->[4]
248 ? $ctx->[4]($now, $_[0], $level, $format) 252 ? $ctx->[4]($now, $_[0], $level, $format)
249 : ($fmt ||= _format $now, $_[0], $level, $format); 253 : ($fmt ||= _format $now, $_[0], $level, $format);
254
255 $success = 1;
250 256
251 $ctx->[3]($str) 257 $ctx->[3]($str)
252 or push @ctx, values %{ $ctx->[2] }; # not consumed - propagate 258 or push @ctx, values %{ $ctx->[2] }; # not consumed - propagate
253 } else { 259 } else {
254 push @ctx, values %{ $ctx->[2] }; # not masked - propagate 260 push @ctx, values %{ $ctx->[2] }; # not masked - propagate
256 } 262 }
257 } 263 }
258 while $ctx = pop @ctx; 264 while $ctx = pop @ctx;
259 265
260 exit 1 if $level <= 1; 266 exit 1 if $level <= 1;
267
268 $success
261} 269}
262 270
263sub log($$;@) { 271sub log($$;@) {
264 _log 272 _log
265 $CTX{ (caller)[0] } ||= _pkg_ctx +(caller)[0], 273 $CTX{ (caller)[0] } ||= _pkg_ctx +(caller)[0],
269*AnyEvent::log = *AE::log = \&log; 277*AnyEvent::log = *AE::log = \&log;
270 278
271=item $logger = AnyEvent::Log::logger $level[, \$enabled] 279=item $logger = AnyEvent::Log::logger $level[, \$enabled]
272 280
273Creates a code reference that, when called, acts as if the 281Creates a code reference that, when called, acts as if the
274C<AnyEvent::Log::log> function was called at this point with the givne 282C<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 283level. C<$logger> is passed a C<$msg> and optional C<@args>, just as with
276the C<AnyEvent::Log::log> function: 284the C<AnyEvent::Log::log> function:
277 285
278 my $debug_log = AnyEvent::Log::logger "debug"; 286 my $debug_log = AnyEvent::Log::logger "debug";
279 287

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines