… | |
… | |
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 | |
143 | Requests logging of the given C<$msg> with the given log level. |
143 | Requests logging of the given C<$msg> with the given log level, and |
|
|
144 | returns true if the message was logged I<somewhere>. |
144 | |
145 | |
145 | For C<fatal> log levels, the program will abort. |
146 | For C<fatal> log levels, the program will abort. |
146 | |
147 | |
147 | If only a C<$msg> is given, it is logged as-is. With extra C<@args>, the |
148 | If only a C<$msg> is given, it is logged as-is. With extra C<@args>, the |
148 | C<$msg> is interpreted as an sprintf format string. |
149 | C<$msg> is interpreted as an sprintf format string. |
… | |
… | |
154 | supposed to return the message. It will be called only then the message |
155 | supposed to return the message. It will be called only then the message |
155 | actually gets logged, which is useful if it is costly to create the |
156 | actually gets logged, which is useful if it is costly to create the |
156 | message in the first place. |
157 | message in the first place. |
157 | |
158 | |
158 | Whether the given message will be logged depends on the maximum log level |
159 | Whether the given message will be logged depends on the maximum log level |
159 | and the caller's package. |
160 | and the caller's package. The return value can be used to ensure that |
|
|
161 | messages or not "lost" - for example, when L<AnyEvent::Debug> detects a |
|
|
162 | runtime error it tries to log it at C<die> level, but if that message is |
|
|
163 | lost it simply uses warn. |
160 | |
164 | |
161 | Note that you can (and should) call this function as C<AnyEvent::log> or |
165 | Note that you can (and should) call this function as C<AnyEvent::log> or |
162 | C<AE::log>, without C<use>-ing this module if possible (i.e. you don't |
166 | C<AE::log>, without C<use>-ing this module if possible (i.e. you don't |
163 | need any additional functionality), as those functions will load the |
167 | need any additional functionality), as those functions will load the |
164 | logging module on demand only. They are also much shorter to write. |
168 | logging 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 | |
263 | sub log($$;@) { |
271 | sub 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 | |
273 | Creates a code reference that, when called, acts as if the |
281 | Creates a code reference that, when called, acts as if the |
274 | C<AnyEvent::Log::log> function was called at this point with the givne |
282 | C<AnyEvent::Log::log> function was called at this point with the given |
275 | level. C<$logger> is passed a C<$msg> and optional C<@args>, just as with |
283 | level. C<$logger> is passed a C<$msg> and optional C<@args>, just as with |
276 | the C<AnyEvent::Log::log> function: |
284 | the 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 | |