… | |
… | |
1058 | If L<AnyEvent::Log> is not loaded then this function makes a simple test |
1058 | If L<AnyEvent::Log> is not loaded then this function makes a simple test |
1059 | to see whether the message will be logged. If the test succeeds it will |
1059 | to see whether the message will be logged. If the test succeeds it will |
1060 | load AnyEvent::Log and call C<AnyEvent::Log::log> - consequently, look at |
1060 | load AnyEvent::Log and call C<AnyEvent::Log::log> - consequently, look at |
1061 | the L<AnyEvent::Log> documentation for details. |
1061 | the L<AnyEvent::Log> documentation for details. |
1062 | |
1062 | |
1063 | If the test fails it will simply return. |
1063 | If the test fails it will simply return. Right now this happens when a |
|
|
1064 | numerical loglevel is used and it is larger than the level specified via |
|
|
1065 | C<$ENV{PERL_ANYEVENT_VERBOSE}>. |
1064 | |
1066 | |
1065 | If you want to sprinkle loads of logging calls around your code, consider |
1067 | If you want to sprinkle loads of logging calls around your code, consider |
1066 | creating a logger callback with the C<AnyEvent::Log::logger> function, |
1068 | creating a logger callback with the C<AnyEvent::Log::logger> function, |
1067 | which can reduce typing, codesize and can reduce the logging overhead |
1069 | which can reduce typing, codesize and can reduce the logging overhead |
1068 | enourmously. |
1070 | enourmously. |
… | |
… | |
1235 | |
1237 | |
1236 | use Carp (); |
1238 | use Carp (); |
1237 | |
1239 | |
1238 | our $VERSION = '6.02'; |
1240 | our $VERSION = '6.02'; |
1239 | our $MODEL; |
1241 | our $MODEL; |
1240 | |
|
|
1241 | our @ISA; |
1242 | our @ISA; |
1242 | |
|
|
1243 | our @REGISTRY; |
1243 | our @REGISTRY; |
1244 | |
|
|
1245 | our $VERBOSE; |
1244 | our $VERBOSE; |
|
|
1245 | our $MAX_SIGNAL_LATENCY = 10; |
|
|
1246 | our %PROTOCOL; # (ipv4|ipv6) => (1|2), higher numbers are preferred |
1246 | |
1247 | |
1247 | BEGIN { |
1248 | BEGIN { |
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 | |
1264 | our $MAX_SIGNAL_LATENCY = 10; |
1264 | $VERBOSE = length $ENV{PERL_ANYEVENT_VERBOSE} ? $ENV{PERL_ANYEVENT_VERBOSE}*1 : 4; |
1265 | |
1265 | |
1266 | our %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 | |
1309 | sub log($$;@) { |
1306 | sub 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 |
1318 | } |
1315 | } |
1319 | |
1316 | |
|
|
1317 | sub 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 | } |
|
|
1345 | } |
|
|
1346 | |
1320 | if (length $ENV{PERL_ANYEVENT_LOG}) { |
1347 | if (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 | } |
1323 | |
1350 | |
1324 | our @models = ( |
1351 | our @models = ( |
1325 | [EV:: => AnyEvent::Impl::EV:: , 1], |
1352 | [EV:: => AnyEvent::Impl::EV::], |
1326 | [AnyEvent::Loop:: => AnyEvent::Impl::Perl:: , 1], |
1353 | [AnyEvent::Loop:: => AnyEvent::Impl::Perl::], |
1327 | # everything below here will not (normally) be autoprobed |
1354 | # everything below here will not (normally) be autoprobed |
1328 | # as the pure perl backend should work everywhere |
1355 | # as the pure perl backend should work everywhere |
1329 | # and is usually faster |
1356 | # and is usually faster |
|
|
1357 | [Irssi:: => AnyEvent::Impl::Irssi::], # Irssi has a bogus "Event" package, so msut be near the top |
1330 | [Event:: => AnyEvent::Impl::Event::, 1], |
1358 | [Event:: => AnyEvent::Impl::Event::], # slow, stable |
1331 | [Glib:: => AnyEvent::Impl::Glib:: , 1], # becomes extremely slow with many watchers |
1359 | [Glib:: => AnyEvent::Impl::Glib::], # becomes extremely slow with many watchers |
|
|
1360 | # everything below here should not be autoloaded |
1332 | [Event::Lib:: => AnyEvent::Impl::EventLib::], # too buggy |
1361 | [Event::Lib:: => AnyEvent::Impl::EventLib::], # too buggy |
1333 | [Irssi:: => AnyEvent::Impl::Irssi::], # Irssi has a bogus "Event" package |
|
|
1334 | [Tk:: => AnyEvent::Impl::Tk::], # crashes with many handles |
1362 | [Tk:: => AnyEvent::Impl::Tk::], # crashes with many handles |
1335 | [Qt:: => AnyEvent::Impl::Qt::], # requires special main program |
1363 | [Qt:: => AnyEvent::Impl::Qt::], # requires special main program |
1336 | [POE::Kernel:: => AnyEvent::Impl::POE::], # lasciate ogni speranza |
1364 | [POE::Kernel:: => AnyEvent::Impl::POE::], # lasciate ogni speranza |
1337 | [Wx:: => AnyEvent::Impl::POE::], |
1365 | [Wx:: => AnyEvent::Impl::POE::], |
1338 | [Prima:: => AnyEvent::Impl::POE::], |
1366 | [Prima:: => AnyEvent::Impl::POE::], |
… | |
… | |
1367 | our @methods = qw(io timer time now now_update signal child idle condvar); |
1395 | our @methods = qw(io timer time now now_update signal child idle condvar); |
1368 | |
1396 | |
1369 | sub detect() { |
1397 | sub detect() { |
1370 | return $MODEL if $MODEL; # some programs keep references to detect |
1398 | return $MODEL if $MODEL; # some programs keep references to detect |
1371 | |
1399 | |
|
|
1400 | # IO::Async::Loop::AnyEvent is extremely evil, refuse to work with it |
|
|
1401 | # the author knows about the problems and what it does to AnyEvent as a whole |
|
|
1402 | # (and the ability of others to use AnyEvent), but simply wants to abuse AnyEvent |
|
|
1403 | # anyway. |
|
|
1404 | AnyEvent::log fatal => "AnyEvent: IO::Async::Loop::AnyEvent detected - this module is broken by design,\n" |
|
|
1405 | . "abuses internals and breaks AnyEvent, will not continue." |
|
|
1406 | if exists $INC{"IO/Async/Loop/AnyEvent.pm"}; |
|
|
1407 | |
1372 | local $!; # for good measure |
1408 | local $!; # for good measure |
1373 | local $SIG{__DIE__}; # we use eval |
1409 | local $SIG{__DIE__}; # we use eval |
1374 | |
1410 | |
1375 | # free some memory |
1411 | # free some memory |
1376 | *detect = sub () { $MODEL }; |
1412 | *detect = sub () { $MODEL }; |
… | |
… | |
1387 | $model = "AnyEvent::Impl::$model" unless $model =~ s/::$//; |
1423 | $model = "AnyEvent::Impl::$model" unless $model =~ s/::$//; |
1388 | if (eval "require $model") { |
1424 | if (eval "require $model") { |
1389 | AnyEvent::log 7 => "loaded model '$model' (forced by \$ENV{PERL_ANYEVENT_MODEL}), using it."; |
1425 | AnyEvent::log 7 => "loaded model '$model' (forced by \$ENV{PERL_ANYEVENT_MODEL}), using it."; |
1390 | $MODEL = $model; |
1426 | $MODEL = $model; |
1391 | } else { |
1427 | } else { |
1392 | AnyEvent::log 5 => "unable to load model '$model' (from \$ENV{PERL_ANYEVENT_MODEL}):\n$@"; |
1428 | AnyEvent::log 4 => "unable to load model '$model' (from \$ENV{PERL_ANYEVENT_MODEL}):\n$@"; |
1393 | } |
1429 | } |
1394 | } |
1430 | } |
1395 | |
1431 | |
1396 | # check for already loaded models |
1432 | # check for already loaded models |
1397 | unless ($MODEL) { |
1433 | unless ($MODEL) { |
… | |
… | |
1407 | } |
1443 | } |
1408 | |
1444 | |
1409 | unless ($MODEL) { |
1445 | unless ($MODEL) { |
1410 | # try to autoload a model |
1446 | # try to autoload a model |
1411 | for (@REGISTRY, @models) { |
1447 | for (@REGISTRY, @models) { |
1412 | my ($package, $model, $autoload) = @$_; |
1448 | my ($package, $model) = @$_; |
1413 | if ( |
1449 | if ( |
1414 | $autoload |
|
|
1415 | and eval "require $package" |
1450 | eval "require $package" |
1416 | and ${"$package\::VERSION"} > 0 |
1451 | and ${"$package\::VERSION"} > 0 |
1417 | and eval "require $model" |
1452 | and eval "require $model" |
1418 | ) { |
1453 | ) { |
1419 | AnyEvent::log 7 => "autoloaded model '$model', using it."; |
1454 | AnyEvent::log 7 => "autoloaded model '$model', using it."; |
1420 | $MODEL = $model; |
1455 | $MODEL = $model; |
1421 | last; |
1456 | last; |
1422 | } |
1457 | } |
1423 | } |
1458 | } |
1424 | |
1459 | |
1425 | $MODEL |
1460 | $MODEL |
1426 | or die "AnyEvent: backend autodetection failed - did you properly install AnyEvent?"; |
1461 | or AnyEvent::log fatal => "AnyEvent: backend autodetection failed - did you properly install AnyEvent?"; |
1427 | } |
1462 | } |
1428 | } |
1463 | } |
1429 | |
1464 | |
1430 | # free memory only needed for probing |
1465 | # free memory only needed for probing |
1431 | undef @models; |
1466 | undef @models; |
… | |
… | |
2040 | |
2075 | |
2041 | =over 4 |
2076 | =over 4 |
2042 | |
2077 | |
2043 | =item C<PERL_ANYEVENT_VERBOSE> |
2078 | =item C<PERL_ANYEVENT_VERBOSE> |
2044 | |
2079 | |
2045 | By default, AnyEvent will be completely silent except in fatal |
2080 | By default, AnyEvent will only log messages with loglevel C<3> |
2046 | conditions. You can set this environment variable to make AnyEvent more |
2081 | (C<critical>) or higher (see L<AnyEvent::Log>). You can set this |
|
|
2082 | environment variable to a numerical loglevel to make AnyEvent more (or |
|
|
2083 | less) talkative. |
|
|
2084 | |
2047 | talkative. If you want to do more than just set the global logging level |
2085 | If you want to do more than just set the global logging level |
2048 | you should have a look at C<PERL_ANYEVENT_LOG>, which allows much more |
2086 | you should have a look at C<PERL_ANYEVENT_LOG>, which allows much more |
2049 | complex specifications. |
2087 | complex specifications. |
2050 | |
2088 | |
|
|
2089 | When set to C<0> (C<off>), then no messages whatsoever will be logged with |
|
|
2090 | the default logging settings. |
|
|
2091 | |
2051 | When set to C<5> or higher (warn), causes AnyEvent to warn about unexpected |
2092 | When set to C<5> or higher (C<warn>), causes AnyEvent to warn about |
2052 | conditions, such as not being able to load the event model specified by |
2093 | unexpected conditions, such as not being able to load the event model |
2053 | C<PERL_ANYEVENT_MODEL>, or a guard callback throwing an exception - this |
2094 | specified by C<PERL_ANYEVENT_MODEL>, or a guard callback throwing an |
2054 | is the minimum recommended level. |
2095 | exception - this is the minimum recommended level. |
2055 | |
2096 | |
2056 | When set to C<7> or higher (info), cause AnyEvent to report which event model it |
2097 | When set to C<7> or higher (info), cause AnyEvent to report which event model it |
2057 | chooses. |
2098 | chooses. |
2058 | |
2099 | |
2059 | When set to C<8> or higher (debug), then AnyEvent will report extra information on |
2100 | When set to C<8> or higher (debug), then AnyEvent will report extra information on |
… | |
… | |
2092 | C<PERL_ANYEVENT_STRICT=1> in your environment while developing programs |
2133 | C<PERL_ANYEVENT_STRICT=1> in your environment while developing programs |
2093 | can be very useful, however. |
2134 | can be very useful, however. |
2094 | |
2135 | |
2095 | =item C<PERL_ANYEVENT_DEBUG_SHELL> |
2136 | =item C<PERL_ANYEVENT_DEBUG_SHELL> |
2096 | |
2137 | |
2097 | If this env variable is set, then its contents will be interpreted by |
2138 | If this env variable is nonempty, then its contents will be interpreted by |
2098 | C<AnyEvent::Socket::parse_hostport> (after replacing every occurance of |
2139 | C<AnyEvent::Socket::parse_hostport> and C<AnyEvent::Debug::shell> (after |
2099 | C<$$> by the process pid) and an C<AnyEvent::Debug::shell> is bound on |
2140 | replacing every occurance of C<$$> by the process pid). The shell object |
2100 | that port. The shell object is saved in C<$AnyEvent::Debug::SHELL>. |
2141 | is saved in C<$AnyEvent::Debug::SHELL>. |
2101 | |
2142 | |
2102 | This happens when the first watcher is created. |
2143 | This happens when the first watcher is created. |
2103 | |
2144 | |
2104 | For example, to bind a debug shell on a unix domain socket in |
2145 | For example, to bind a debug shell on a unix domain socket in |
2105 | F<< /tmp/debug<pid>.sock >>, you could use this: |
2146 | F<< /tmp/debug<pid>.sock >>, you could use this: |
2106 | |
2147 | |
2107 | PERL_ANYEVENT_DEBUG_SHELL=/tmp/debug\$\$.sock perlprog |
2148 | PERL_ANYEVENT_DEBUG_SHELL=/tmp/debug\$\$.sock perlprog |
|
|
2149 | # connect with e.g.: socat readline /tmp/debug123.sock |
2108 | |
2150 | |
|
|
2151 | Or to bind to tcp port 4545 on localhost: |
|
|
2152 | |
|
|
2153 | PERL_ANYEVENT_DEBUG_SHELL=127.0.0.1:4545 perlprog |
|
|
2154 | # connect with e.g.: telnet localhost 4545 |
|
|
2155 | |
2109 | Note that creating sockets in F</tmp> is very unsafe on multiuser |
2156 | Note that creating sockets in F</tmp> or on localhost is very unsafe on |
2110 | systems. |
2157 | multiuser systems. |
2111 | |
2158 | |
2112 | =item C<PERL_ANYEVENT_DEBUG_WRAP> |
2159 | =item C<PERL_ANYEVENT_DEBUG_WRAP> |
2113 | |
2160 | |
2114 | Can be set to C<0>, C<1> or C<2> and enables wrapping of all watchers for |
2161 | Can be set to C<0>, C<1> or C<2> and enables wrapping of all watchers for |
2115 | debugging purposes. See C<AnyEvent::Debug::wrap> for details. |
2162 | debugging purposes. See C<AnyEvent::Debug::wrap> for details. |