… | |
… | |
1237 | |
1237 | |
1238 | use Carp (); |
1238 | use Carp (); |
1239 | |
1239 | |
1240 | our $VERSION = '6.02'; |
1240 | our $VERSION = '6.02'; |
1241 | our $MODEL; |
1241 | our $MODEL; |
1242 | |
|
|
1243 | our @ISA; |
1242 | our @ISA; |
1244 | |
|
|
1245 | our @REGISTRY; |
1243 | our @REGISTRY; |
1246 | |
|
|
1247 | our $VERBOSE; |
1244 | our $VERBOSE; |
|
|
1245 | our $MAX_SIGNAL_LATENCY = 10; |
|
|
1246 | our %PROTOCOL; # (ipv4|ipv6) => (1|2), higher numbers are preferred |
1248 | |
1247 | |
1249 | BEGIN { |
1248 | BEGIN { |
1250 | require "AnyEvent/constants.pl"; |
1249 | require "AnyEvent/constants.pl"; |
1251 | |
1250 | |
1252 | eval "sub TAINT (){" . (${^TAINT}*1) . "}"; |
1251 | eval "sub TAINT (){" . (${^TAINT}*1) . "}"; |
… | |
… | |
1260 | @ENV{grep /^PERL_ANYEVENT_/, keys %ENV} = () |
1259 | @ENV{grep /^PERL_ANYEVENT_/, keys %ENV} = () |
1261 | if ${^TAINT}; |
1260 | if ${^TAINT}; |
1262 | |
1261 | |
1263 | # $ENV{PERL_ANYEVENT_xxx} now valid |
1262 | # $ENV{PERL_ANYEVENT_xxx} now valid |
1264 | |
1263 | |
1265 | $VERBOSE = length $ENV{PERL_ANYEVENT_VERBOSE} ? $ENV{PERL_ANYEVENT_VERBOSE}*1 : 3; |
1264 | $VERBOSE = length $ENV{PERL_ANYEVENT_VERBOSE} ? $ENV{PERL_ANYEVENT_VERBOSE}*1 : 4; |
1266 | } |
|
|
1267 | |
1265 | |
1268 | our $MAX_SIGNAL_LATENCY = 10; |
|
|
1269 | |
|
|
1270 | our %PROTOCOL; # (ipv4|ipv6) => (1|2), higher numbers are preferred |
|
|
1271 | |
|
|
1272 | { |
|
|
1273 | my $idx; |
1266 | my $idx; |
1274 | $PROTOCOL{$_} = ++$idx |
1267 | $PROTOCOL{$_} = ++$idx |
1275 | for reverse split /\s*,\s*/, |
1268 | for reverse split /\s*,\s*/, |
1276 | $ENV{PERL_ANYEVENT_PROTOCOLS} || "ipv4,ipv6"; |
1269 | $ENV{PERL_ANYEVENT_PROTOCOLS} || "ipv4,ipv6"; |
1277 | } |
1270 | } |
… | |
… | |
1310 | () |
1303 | () |
1311 | } |
1304 | } |
1312 | |
1305 | |
1313 | sub log($$;@) { |
1306 | sub log($$;@) { |
1314 | # 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 |
1315 | if ($_[0] <= $VERBOSE) { # also catches non-numeric levels(!) |
1308 | if ($_[0] <= ($VERBOSE || 1)) { # also catches non-numeric levels(!) and fatal |
1316 | require AnyEvent::Log; |
1309 | require AnyEvent::Log; # among other things, sets $VERBOSE to 9 |
1317 | # AnyEvent::Log overwrites this function |
1310 | # AnyEvent::Log overwrites this function |
1318 | goto &log; |
1311 | goto &log; |
1319 | } |
1312 | } |
1320 | |
1313 | |
1321 | 0 # not logged |
1314 | 0 # not logged |
1322 | } |
1315 | } |
1323 | |
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 | |
1324 | if (length $ENV{PERL_ANYEVENT_LOG}) { |
1347 | if (length $ENV{PERL_ANYEVENT_LOG}) { |
1325 | require AnyEvent::Log; # AnyEvent::Log does the thing for us |
1348 | require AnyEvent::Log; # AnyEvent::Log does the thing for us |
1326 | } |
1349 | } |
1327 | |
1350 | |
1328 | our @models = ( |
1351 | our @models = ( |
1329 | [EV:: => AnyEvent::Impl::EV:: , 1], |
1352 | [EV:: => AnyEvent::Impl::EV::], |
1330 | [AnyEvent::Loop:: => AnyEvent::Impl::Perl:: , 1], |
1353 | [AnyEvent::Loop:: => AnyEvent::Impl::Perl::], |
1331 | # everything below here will not (normally) be autoprobed |
1354 | # everything below here will not (normally) be autoprobed |
1332 | # as the pure perl backend should work everywhere |
1355 | # as the pure perl backend should work everywhere |
1333 | # 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 |
1334 | [Event:: => AnyEvent::Impl::Event::, 1], |
1358 | [Event:: => AnyEvent::Impl::Event::], # slow, stable |
1335 | [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 |
1336 | [Event::Lib:: => AnyEvent::Impl::EventLib::], # too buggy |
1361 | [Event::Lib:: => AnyEvent::Impl::EventLib::], # too buggy |
1337 | [Irssi:: => AnyEvent::Impl::Irssi::], # Irssi has a bogus "Event" package |
|
|
1338 | [Tk:: => AnyEvent::Impl::Tk::], # crashes with many handles |
1362 | [Tk:: => AnyEvent::Impl::Tk::], # crashes with many handles |
1339 | [Qt:: => AnyEvent::Impl::Qt::], # requires special main program |
1363 | [Qt:: => AnyEvent::Impl::Qt::], # requires special main program |
1340 | [POE::Kernel:: => AnyEvent::Impl::POE::], # lasciate ogni speranza |
1364 | [POE::Kernel:: => AnyEvent::Impl::POE::], # lasciate ogni speranza |
1341 | [Wx:: => AnyEvent::Impl::POE::], |
1365 | [Wx:: => AnyEvent::Impl::POE::], |
1342 | [Prima:: => AnyEvent::Impl::POE::], |
1366 | [Prima:: => AnyEvent::Impl::POE::], |
… | |
… | |
1391 | $model = "AnyEvent::Impl::$model" unless $model =~ s/::$//; |
1415 | $model = "AnyEvent::Impl::$model" unless $model =~ s/::$//; |
1392 | if (eval "require $model") { |
1416 | if (eval "require $model") { |
1393 | AnyEvent::log 7 => "loaded model '$model' (forced by \$ENV{PERL_ANYEVENT_MODEL}), using it."; |
1417 | AnyEvent::log 7 => "loaded model '$model' (forced by \$ENV{PERL_ANYEVENT_MODEL}), using it."; |
1394 | $MODEL = $model; |
1418 | $MODEL = $model; |
1395 | } else { |
1419 | } else { |
1396 | AnyEvent::log 5 => "unable to load model '$model' (from \$ENV{PERL_ANYEVENT_MODEL}):\n$@"; |
1420 | AnyEvent::log 4 => "unable to load model '$model' (from \$ENV{PERL_ANYEVENT_MODEL}):\n$@"; |
1397 | } |
1421 | } |
1398 | } |
1422 | } |
1399 | |
1423 | |
1400 | # check for already loaded models |
1424 | # check for already loaded models |
1401 | unless ($MODEL) { |
1425 | unless ($MODEL) { |
… | |
… | |
1411 | } |
1435 | } |
1412 | |
1436 | |
1413 | unless ($MODEL) { |
1437 | unless ($MODEL) { |
1414 | # try to autoload a model |
1438 | # try to autoload a model |
1415 | for (@REGISTRY, @models) { |
1439 | for (@REGISTRY, @models) { |
1416 | my ($package, $model, $autoload) = @$_; |
1440 | my ($package, $model) = @$_; |
1417 | if ( |
1441 | if ( |
1418 | $autoload |
|
|
1419 | and eval "require $package" |
1442 | eval "require $package" |
1420 | and ${"$package\::VERSION"} > 0 |
1443 | and ${"$package\::VERSION"} > 0 |
1421 | and eval "require $model" |
1444 | and eval "require $model" |
1422 | ) { |
1445 | ) { |
1423 | AnyEvent::log 7 => "autoloaded model '$model', using it."; |
1446 | AnyEvent::log 7 => "autoloaded model '$model', using it."; |
1424 | $MODEL = $model; |
1447 | $MODEL = $model; |
1425 | last; |
1448 | last; |
1426 | } |
1449 | } |
1427 | } |
1450 | } |
1428 | |
1451 | |
1429 | $MODEL |
1452 | $MODEL |
1430 | 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?"; |
1431 | } |
1454 | } |
1432 | } |
1455 | } |
1433 | |
1456 | |
1434 | # free memory only needed for probing |
1457 | # free memory only needed for probing |
1435 | undef @models; |
1458 | undef @models; |
… | |
… | |
2102 | C<PERL_ANYEVENT_STRICT=1> in your environment while developing programs |
2125 | C<PERL_ANYEVENT_STRICT=1> in your environment while developing programs |
2103 | can be very useful, however. |
2126 | can be very useful, however. |
2104 | |
2127 | |
2105 | =item C<PERL_ANYEVENT_DEBUG_SHELL> |
2128 | =item C<PERL_ANYEVENT_DEBUG_SHELL> |
2106 | |
2129 | |
2107 | If this env variable is set, then its contents will be interpreted by |
2130 | If this env variable is nonempty, then its contents will be interpreted by |
2108 | C<AnyEvent::Socket::parse_hostport> (after replacing every occurance of |
2131 | C<AnyEvent::Socket::parse_hostport> and C<AnyEvent::Debug::shell> (after |
2109 | C<$$> by the process pid) and an C<AnyEvent::Debug::shell> is bound on |
2132 | replacing every occurance of C<$$> by the process pid). The shell object |
2110 | that port. The shell object is saved in C<$AnyEvent::Debug::SHELL>. |
2133 | is saved in C<$AnyEvent::Debug::SHELL>. |
2111 | |
2134 | |
2112 | This happens when the first watcher is created. |
2135 | This happens when the first watcher is created. |
2113 | |
2136 | |
2114 | For example, to bind a debug shell on a unix domain socket in |
2137 | For example, to bind a debug shell on a unix domain socket in |
2115 | F<< /tmp/debug<pid>.sock >>, you could use this: |
2138 | F<< /tmp/debug<pid>.sock >>, you could use this: |
2116 | |
2139 | |
2117 | 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 |
2118 | |
2142 | |
|
|
2143 | Or 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 | |
2119 | Note that creating sockets in F</tmp> is very unsafe on multiuser |
2148 | Note that creating sockets in F</tmp> or on localhost is very unsafe on |
2120 | systems. |
2149 | multiuser systems. |
2121 | |
2150 | |
2122 | =item C<PERL_ANYEVENT_DEBUG_WRAP> |
2151 | =item C<PERL_ANYEVENT_DEBUG_WRAP> |
2123 | |
2152 | |
2124 | Can be set to C<0>, C<1> or C<2> and enables wrapping of all watchers for |
2153 | Can be set to C<0>, C<1> or C<2> and enables wrapping of all watchers for |
2125 | debugging purposes. See C<AnyEvent::Debug::wrap> for details. |
2154 | debugging purposes. See C<AnyEvent::Debug::wrap> for details. |