ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent/lib/AnyEvent.pm
(Generate patch)

Comparing AnyEvent/lib/AnyEvent.pm (file contents):
Revision 1.379 by root, Fri Aug 26 18:09:04 2011 UTC vs.
Revision 1.384 by root, Mon Sep 5 07:21:54 2011 UTC

1058If L<AnyEvent::Log> is not loaded then this function makes a simple test 1058If L<AnyEvent::Log> is not loaded then this function makes a simple test
1059to see whether the message will be logged. If the test succeeds it will 1059to see whether the message will be logged. If the test succeeds it will
1060load AnyEvent::Log and call C<AnyEvent::Log::log> - consequently, look at 1060load AnyEvent::Log and call C<AnyEvent::Log::log> - consequently, look at
1061the L<AnyEvent::Log> documentation for details. 1061the L<AnyEvent::Log> documentation for details.
1062 1062
1063If the test fails it will simply return. 1063If the test fails it will simply return. Right now this happens when a
1064numerical loglevel is used and it is larger than the level specified via
1065C<$ENV{PERL_ANYEVENT_VERBOSE}>.
1064 1066
1065If you want to sprinkle loads of logging calls around your code, consider 1067If you want to sprinkle loads of logging calls around your code, consider
1066creating a logger callback with the C<AnyEvent::Log::logger> function, 1068creating a logger callback with the C<AnyEvent::Log::logger> function,
1067which can reduce typing, codesize and can reduce the logging overhead 1069which can reduce typing, codesize and can reduce the logging overhead
1068enourmously. 1070enourmously.
1235 1237
1236use Carp (); 1238use Carp ();
1237 1239
1238our $VERSION = '6.02'; 1240our $VERSION = '6.02';
1239our $MODEL; 1241our $MODEL;
1240
1241our @ISA; 1242our @ISA;
1242
1243our @REGISTRY; 1243our @REGISTRY;
1244
1245our $VERBOSE; 1244our $VERBOSE;
1245our $MAX_SIGNAL_LATENCY = 10;
1246our %PROTOCOL; # (ipv4|ipv6) => (1|2), higher numbers are preferred
1246 1247
1247BEGIN { 1248BEGIN {
1248 require "AnyEvent/constants.pl"; 1249 require "AnyEvent/constants.pl";
1249 1250
1250 eval "sub TAINT (){" . (${^TAINT}*1) . "}"; 1251 eval "sub TAINT (){" . (${^TAINT}*1) . "}";
1256 for grep s/^AE_// && !exists $ENV{"PERL_ANYEVENT_$_"}, keys %ENV; 1257 for grep s/^AE_// && !exists $ENV{"PERL_ANYEVENT_$_"}, keys %ENV;
1257 1258
1258 @ENV{grep /^PERL_ANYEVENT_/, keys %ENV} = () 1259 @ENV{grep /^PERL_ANYEVENT_/, keys %ENV} = ()
1259 if ${^TAINT}; 1260 if ${^TAINT};
1260 1261
1261 $VERBOSE = $ENV{PERL_ANYEVENT_VERBOSE}*1; 1262 # $ENV{PERL_ANYEVENT_xxx} now valid
1262}
1263 1263
1264our $MAX_SIGNAL_LATENCY = 10; 1264 $VERBOSE = length $ENV{PERL_ANYEVENT_VERBOSE} ? $ENV{PERL_ANYEVENT_VERBOSE}*1 : 4;
1265 1265
1266our %PROTOCOL; # (ipv4|ipv6) => (1|2), higher numbers are preferred
1267
1268{
1269 my $idx; 1266 my $idx;
1270 $PROTOCOL{$_} = ++$idx 1267 $PROTOCOL{$_} = ++$idx
1271 for reverse split /\s*,\s*/, 1268 for reverse split /\s*,\s*/,
1272 $ENV{PERL_ANYEVENT_PROTOCOLS} || "ipv4,ipv6"; 1269 $ENV{PERL_ANYEVENT_PROTOCOLS} || "ipv4,ipv6";
1273} 1270}
1306 () 1303 ()
1307} 1304}
1308 1305
1309sub log($$;@) { 1306sub log($$;@) {
1310 # only load the big bloated module when we actually are about to log something 1307 # only load the big bloated module when we actually are about to log something
1311 if ($_[0] <= $VERBOSE) { # also catches non-numeric levels(!) 1308 if ($_[0] <= ($VERBOSE || 1)) { # also catches non-numeric levels(!) and fatal
1312 require AnyEvent::Log; 1309 require AnyEvent::Log; # among other things, sets $VERBOSE to 9
1313 # AnyEvent::Log overwrites this function 1310 # AnyEvent::Log overwrites this function
1314 goto &log; 1311 goto &log;
1315 } 1312 }
1316 1313
1317 0 # not logged 1314 0 # not logged
1315}
1316
1317sub logger($;$) {
1318 package AnyEvent::Log;
1319
1320 my ($level, $renabled) = @_;
1321
1322 $$renabled = $level <= $VERBOSE;
1323
1324 my $pkg = (caller)[0];
1325
1326 my $logger = [$pkg, $level, $renabled];
1327
1328 our %LOGGER;
1329 $LOGGER{$logger+0} = $logger;
1330
1331 require AnyEvent::Util;
1332 my $guard = AnyEvent::Util::guard (sub {
1333 # "clean up"
1334 delete $LOGGER{$logger+0};
1335 });
1336
1337 sub {
1338 return 0 unless $$renabled;
1339
1340 $guard if 0; # keep guard alive, but don't cause runtime overhead
1341 require AnyEvent::Log unless $AnyEvent::Log::VERSION;
1342 package AnyEvent::Log;
1343 _log ($logger->[0], $level, @_) # logger->[0] has been converted at load time
1344 }
1318} 1345}
1319 1346
1320if (length $ENV{PERL_ANYEVENT_LOG}) { 1347if (length $ENV{PERL_ANYEVENT_LOG}) {
1321 require AnyEvent::Log; # AnyEvent::Log does the thing for us 1348 require AnyEvent::Log; # AnyEvent::Log does the thing for us
1322} 1349}
1387 $model = "AnyEvent::Impl::$model" unless $model =~ s/::$//; 1414 $model = "AnyEvent::Impl::$model" unless $model =~ s/::$//;
1388 if (eval "require $model") { 1415 if (eval "require $model") {
1389 AnyEvent::log 7 => "loaded model '$model' (forced by \$ENV{PERL_ANYEVENT_MODEL}), using it."; 1416 AnyEvent::log 7 => "loaded model '$model' (forced by \$ENV{PERL_ANYEVENT_MODEL}), using it.";
1390 $MODEL = $model; 1417 $MODEL = $model;
1391 } else { 1418 } else {
1392 AnyEvent::log 5 => "unable to load model '$model' (from \$ENV{PERL_ANYEVENT_MODEL}):\n$@"; 1419 AnyEvent::log 4 => "unable to load model '$model' (from \$ENV{PERL_ANYEVENT_MODEL}):\n$@";
1393 } 1420 }
1394 } 1421 }
1395 1422
1396 # check for already loaded models 1423 # check for already loaded models
1397 unless ($MODEL) { 1424 unless ($MODEL) {
1421 last; 1448 last;
1422 } 1449 }
1423 } 1450 }
1424 1451
1425 $MODEL 1452 $MODEL
1426 or die "AnyEvent: backend autodetection failed - did you properly install AnyEvent?"; 1453 or AnyEvent::log fatal => "AnyEvent: backend autodetection failed - did you properly install AnyEvent?";
1427 } 1454 }
1428 } 1455 }
1429 1456
1430 # free memory only needed for probing 1457 # free memory only needed for probing
1431 undef @models; 1458 undef @models;
2040 2067
2041=over 4 2068=over 4
2042 2069
2043=item C<PERL_ANYEVENT_VERBOSE> 2070=item C<PERL_ANYEVENT_VERBOSE>
2044 2071
2045By default, AnyEvent will be completely silent except in fatal 2072By default, AnyEvent will only log messages with loglevel C<3>
2046conditions. You can set this environment variable to make AnyEvent more 2073(C<critical>) or higher (see L<AnyEvent::Log>). You can set this
2074environment variable to a numerical loglevel to make AnyEvent more (or
2075less) talkative.
2076
2047talkative. If you want to do more than just set the global logging level 2077If you want to do more than just set the global logging level
2048you should have a look at C<PERL_ANYEVENT_LOG>, which allows much more 2078you should have a look at C<PERL_ANYEVENT_LOG>, which allows much more
2049complex specifications. 2079complex specifications.
2050 2080
2081When set to C<0> (C<off>), then no messages whatsoever will be logged with
2082the default logging settings.
2083
2051When set to C<5> or higher (warn), causes AnyEvent to warn about unexpected 2084When set to C<5> or higher (C<warn>), causes AnyEvent to warn about
2052conditions, such as not being able to load the event model specified by 2085unexpected conditions, such as not being able to load the event model
2053C<PERL_ANYEVENT_MODEL>, or a guard callback throwing an exception - this 2086specified by C<PERL_ANYEVENT_MODEL>, or a guard callback throwing an
2054is the minimum recommended level. 2087exception - this is the minimum recommended level.
2055 2088
2056When set to C<7> or higher (info), cause AnyEvent to report which event model it 2089When set to C<7> or higher (info), cause AnyEvent to report which event model it
2057chooses. 2090chooses.
2058 2091
2059When set to C<8> or higher (debug), then AnyEvent will report extra information on 2092When set to C<8> or higher (debug), then AnyEvent will report extra information on
2092C<PERL_ANYEVENT_STRICT=1> in your environment while developing programs 2125C<PERL_ANYEVENT_STRICT=1> in your environment while developing programs
2093can be very useful, however. 2126can be very useful, however.
2094 2127
2095=item C<PERL_ANYEVENT_DEBUG_SHELL> 2128=item C<PERL_ANYEVENT_DEBUG_SHELL>
2096 2129
2097If this env variable is set, then its contents will be interpreted by 2130If this env variable is nonempty, then its contents will be interpreted by
2098C<AnyEvent::Socket::parse_hostport> (after replacing every occurance of 2131C<AnyEvent::Socket::parse_hostport> and C<AnyEvent::Debug::shell> (after
2099C<$$> by the process pid) and an C<AnyEvent::Debug::shell> is bound on 2132replacing every occurance of C<$$> by the process pid). The shell object
2100that port. The shell object is saved in C<$AnyEvent::Debug::SHELL>. 2133is saved in C<$AnyEvent::Debug::SHELL>.
2101 2134
2102This happens when the first watcher is created. 2135This happens when the first watcher is created.
2103 2136
2104For example, to bind a debug shell on a unix domain socket in 2137For example, to bind a debug shell on a unix domain socket in
2105F<< /tmp/debug<pid>.sock >>, you could use this: 2138F<< /tmp/debug<pid>.sock >>, you could use this:
2106 2139
2107 PERL_ANYEVENT_DEBUG_SHELL=/tmp/debug\$\$.sock perlprog 2140 PERL_ANYEVENT_DEBUG_SHELL=/tmp/debug\$\$.sock perlprog
2141 # connect with e.g.: socat readline /tmp/debug123.sock
2108 2142
2143Or to bind to tcp port 4545 on localhost:
2144
2145 PERL_ANYEVENT_DEBUG_SHELL=127.0.0.1:4545 perlprog
2146 # connect with e.g.: telnet localhost 4545
2147
2109Note that creating sockets in F</tmp> is very unsafe on multiuser 2148Note that creating sockets in F</tmp> or on localhost is very unsafe on
2110systems. 2149multiuser systems.
2111 2150
2112=item C<PERL_ANYEVENT_DEBUG_WRAP> 2151=item C<PERL_ANYEVENT_DEBUG_WRAP>
2113 2152
2114Can be set to C<0>, C<1> or C<2> and enables wrapping of all watchers for 2153Can be set to C<0>, C<1> or C<2> and enables wrapping of all watchers for
2115debugging purposes. See C<AnyEvent::Debug::wrap> for details. 2154debugging purposes. See C<AnyEvent::Debug::wrap> for details.

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines