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.373 by root, Thu Aug 25 03:08:48 2011 UTC vs.
Revision 1.377 by root, Thu Aug 25 23:20:17 2011 UTC

1053 1053
1054=item AnyEvent::log $level, $msg[, @args] 1054=item AnyEvent::log $level, $msg[, @args]
1055 1055
1056Log the given C<$msg> at the given C<$level>. 1056Log the given C<$msg> at the given C<$level>.
1057 1057
1058Loads AnyEvent::Log on first use and calls C<AnyEvent::Log::log> - 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
1060load AnyEvent::Log and call C<AnyEvent::Log::log> - consequently, look at
1059consequently, look at the L<AnyEvent::Log> documentation for details. 1061the L<AnyEvent::Log> documentation for details.
1062
1063If the test fails it will simply return.
1060 1064
1061If you want to sprinkle loads of logging calls around your code, consider 1065If you want to sprinkle loads of logging calls around your code, consider
1062creating a logger callback with the C<AnyEvent::Log::logger> function. 1066creating a logger callback with the C<AnyEvent::Log::logger> function,
1067which can reduce typing, codesize and can reduce the logging overhead
1068enourmously.
1063 1069
1064=back 1070=back
1065 1071
1066=head1 WHAT TO DO IN A MODULE 1072=head1 WHAT TO DO IN A MODULE
1067 1073
1160 1166
1161Implement event-based interfaces to the protocols of the same name (for 1167Implement event-based interfaces to the protocols of the same name (for
1162the curious, IGS is the International Go Server and FCP is the Freenet 1168the curious, IGS is the International Go Server and FCP is the Freenet
1163Client Protocol). 1169Client Protocol).
1164 1170
1165=item L<AnyEvent::Handle::UDP>
1166
1167Here be danger!
1168
1169As Pauli would put it, "Not only is it not right, it's not even wrong!" -
1170there are so many things wrong with AnyEvent::Handle::UDP, most notably
1171its use of a stream-based API with a protocol that isn't streamable, that
1172the only way to improve it is to delete it.
1173
1174It features data corruption (but typically only under load) and general
1175confusion. On top, the author is not only clueless about UDP but also
1176fact-resistant - some gems of his understanding: "connect doesn't work
1177with UDP", "UDP packets are not IP packets", "UDP only has datagrams, not
1178packets", "I don't need to implement proper error checking as UDP doesn't
1179support error checking" and so on - he doesn't even understand what's
1180wrong with his module when it is explained to him.
1181
1182=item L<AnyEvent::DBI>
1183
1184Executes L<DBI> requests asynchronously in a proxy process for you,
1185notifying you in an event-based way when the operation is finished.
1186
1187=item L<AnyEvent::AIO> 1171=item L<AnyEvent::AIO>
1188 1172
1189Truly asynchronous (as opposed to non-blocking) I/O, should be in the 1173Truly asynchronous (as opposed to non-blocking) I/O, should be in the
1190toolbox of every event programmer. AnyEvent::AIO transparently fuses 1174toolbox of every event programmer. AnyEvent::AIO transparently fuses
1191L<IO::AIO> and AnyEvent together, giving AnyEvent access to event-based 1175L<IO::AIO> and AnyEvent together, giving AnyEvent access to event-based
1192file I/O, and much more. 1176file I/O, and much more.
1193 1177
1178=item L<AnyEvent::Filesys::Notify>
1179
1180AnyEvent is good for non-blocking stuff, but it can't detect file or
1181path changes (e.g. "watch this directory for new files", "watch this
1182file for changes"). The L<AnyEvent::Filesys::Notify> module promises to
1183do just that in a portbale fashion, supporting inotify on GNU/Linux and
1184some weird, without doubt broken, stuff on OS X to monitor files. It can
1185fall back to blocking scans at regular intervals transparently on other
1186platforms, so it's about as portable as it gets.
1187
1188(I haven't used it myself, but I haven't heard anybody complaining about
1189it yet).
1190
1191=item L<AnyEvent::DBI>
1192
1193Executes L<DBI> requests asynchronously in a proxy process for you,
1194notifying you in an event-based way when the operation is finished.
1195
1194=item L<AnyEvent::HTTPD> 1196=item L<AnyEvent::HTTPD>
1195 1197
1196A simple embedded webserver. 1198A simple embedded webserver.
1197 1199
1198=item L<AnyEvent::FastPing> 1200=item L<AnyEvent::FastPing>
1199 1201
1200The fastest ping in the west. 1202The fastest ping in the west.
1201 1203
1202=item L<Coro> 1204=item L<Coro>
1203 1205
1204Has special support for AnyEvent via L<Coro::AnyEvent>. 1206Has special support for AnyEvent via L<Coro::AnyEvent>, which allows you
1207to simply invert the flow control - don't call us, we will call you:
1208
1209 async {
1210 Coro::AnyEvent::sleep 5; # creates a 5s timer and waits for it
1211 print "5 seconds later!\n";
1212
1213 Coro::AnyEvent::readable *STDIN; # uses an I/O watcher
1214 my $line = <STDIN>; # works for ttys
1215
1216 AnyEvent::HTTP::http_get "url", Coro::rouse_cb;
1217 my ($body, $hdr) = Coro::rouse_wait;
1218 };
1205 1219
1206=back 1220=back
1207 1221
1208=cut 1222=cut
1209 1223
1291 1305
1292 () 1306 ()
1293} 1307}
1294 1308
1295sub log($$;@) { 1309sub log($$;@) {
1296 # only load the bug bloated module when we actually are about to log something 1310 # only load the big bloated module when we actually are about to log something
1297 if ($_[0] <= $VERBOSE) { 1311 if ($_[0] <= $VERBOSE) { # also catches non-numeric levels(!)
1298 require AnyEvent::Log; 1312 require AnyEvent::Log;
1299 # AnyEvent::Log overwrites this function 1313 # AnyEvent::Log overwrites this function
1300 goto &log; 1314 goto &log;
1301 } 1315 }
1316
1317 0 # not logged
1302} 1318}
1303 1319
1304if (length $ENV{PERL_ANYEVENT_LOG}) { 1320if (length $ENV{PERL_ANYEVENT_LOG}) {
1305 require AnyEvent::Log; # AnyEvent::Log does the thing for us 1321 require AnyEvent::Log; # AnyEvent::Log does the thing for us
1306} 1322}
1371 $model = "AnyEvent::Impl::$model" unless $model =~ s/::$//; 1387 $model = "AnyEvent::Impl::$model" unless $model =~ s/::$//;
1372 if (eval "require $model") { 1388 if (eval "require $model") {
1373 AnyEvent::log 7 => "loaded model '$model' (forced by \$ENV{PERL_ANYEVENT_MODEL}), using it."; 1389 AnyEvent::log 7 => "loaded model '$model' (forced by \$ENV{PERL_ANYEVENT_MODEL}), using it.";
1374 $MODEL = $model; 1390 $MODEL = $model;
1375 } else { 1391 } else {
1376 AnyEvent::log warn => "unable to load model '$model' (from \$ENV{PERL_ANYEVENT_MODEL}):\n$@"; 1392 AnyEvent::log 5 => "unable to load model '$model' (from \$ENV{PERL_ANYEVENT_MODEL}):\n$@";
1377 } 1393 }
1378 } 1394 }
1379 1395
1380 # check for already loaded models 1396 # check for already loaded models
1381 unless ($MODEL) { 1397 unless ($MODEL) {
1559 1575
1560sub time { 1576sub time {
1561 eval q{ # poor man's autoloading {} 1577 eval q{ # poor man's autoloading {}
1562 # probe for availability of Time::HiRes 1578 # probe for availability of Time::HiRes
1563 if (eval "use Time::HiRes (); Time::HiRes::time (); 1") { 1579 if (eval "use Time::HiRes (); Time::HiRes::time (); 1") {
1564 AnyEvent::log 8 => "AnyEvent: using Time::HiRes for sub-second timing accuracy."
1565 if $AnyEvent::VERBOSE >= 8;
1566 *time = sub { Time::HiRes::time () }; 1580 *time = sub { Time::HiRes::time () };
1567 *AE::time = \& Time::HiRes::time ; 1581 *AE::time = \& Time::HiRes::time ;
1582 *now = \&time;
1583 AnyEvent::log 8 => "AnyEvent: using Time::HiRes for sub-second timing accuracy.";
1568 # if (eval "use POSIX (); (POSIX::times())... 1584 # if (eval "use POSIX (); (POSIX::times())...
1569 } else { 1585 } else {
1570 AnyEvent::log critical => "using built-in time(), WARNING, no sub-second resolution!";
1571 *time = sub { CORE::time }; 1586 *time = sub { CORE::time };
1572 *AE::time = sub (){ CORE::time }; 1587 *AE::time = sub (){ CORE::time };
1588 *now = \&time;
1589 AnyEvent::log 3 => "using built-in time(), WARNING, no sub-second resolution!";
1573 } 1590 }
1574
1575 *now = \&time;
1576 }; 1591 };
1577 die if $@; 1592 die if $@;
1578 1593
1579 &time 1594 &time
1580} 1595}
1673 1688
1674sub signal { 1689sub signal {
1675 eval q{ # poor man's autoloading {} 1690 eval q{ # poor man's autoloading {}
1676 # probe for availability of Async::Interrupt 1691 # probe for availability of Async::Interrupt
1677 if (_have_async_interrupt) { 1692 if (_have_async_interrupt) {
1678 AnyEvent::log 8 => "using Async::Interrupt for race-free signal handling." 1693 AnyEvent::log 8 => "using Async::Interrupt for race-free signal handling.";
1679 if $AnyEvent::VERBOSE >= 8;
1680 1694
1681 $SIGPIPE_R = new Async::Interrupt::EventPipe; 1695 $SIGPIPE_R = new Async::Interrupt::EventPipe;
1682 $SIG_IO = AE::io $SIGPIPE_R->fileno, 0, \&_signal_exec; 1696 $SIG_IO = AE::io $SIGPIPE_R->fileno, 0, \&_signal_exec;
1683 1697
1684 } else { 1698 } else {
1685 AnyEvent::log 8 => "using emulated perl signal handling with latency timer." 1699 AnyEvent::log 8 => "using emulated perl signal handling with latency timer.";
1686 if $AnyEvent::VERBOSE >= 8;
1687 1700
1688 if (AnyEvent::WIN32) { 1701 if (AnyEvent::WIN32) {
1689 require AnyEvent::Util; 1702 require AnyEvent::Util;
1690 1703
1691 ($SIGPIPE_R, $SIGPIPE_W) = AnyEvent::Util::portable_pipe (); 1704 ($SIGPIPE_R, $SIGPIPE_W) = AnyEvent::Util::portable_pipe ();
2033conditions. You can set this environment variable to make AnyEvent more 2046conditions. You can set this environment variable to make AnyEvent more
2034talkative. If you want to do more than just set the global logging level 2047talkative. If you want to do more than just set the global logging level
2035you should have a look at C<PERL_ANYEVENT_LOG>, which allows much more 2048you should have a look at C<PERL_ANYEVENT_LOG>, which allows much more
2036complex specifications. 2049complex specifications.
2037 2050
2038When set to C<5> or higher, causes AnyEvent to warn about unexpected 2051When set to C<5> or higher (warn), causes AnyEvent to warn about unexpected
2039conditions, such as not being able to load the event model specified by 2052conditions, such as not being able to load the event model specified by
2040C<PERL_ANYEVENT_MODEL>. 2053C<PERL_ANYEVENT_MODEL>, or a guard callback throwing an exception - this
2054is the minimum recommended level.
2041 2055
2042When set to C<7> or higher, cause AnyEvent to report to STDERR which event 2056When set to C<7> or higher (info), cause AnyEvent to report which event model it
2043model it chooses. 2057chooses.
2044 2058
2045When set to C<8> or higher, then AnyEvent will report extra information on 2059When set to C<8> or higher (debug), then AnyEvent will report extra information on
2046which optional modules it loads and how it implements certain features. 2060which optional modules it loads and how it implements certain features.
2047 2061
2048=item C<PERL_ANYEVENT_LOG> 2062=item C<PERL_ANYEVENT_LOG>
2049 2063
2050Accepts rather complex logging specifications. For example, you could log 2064Accepts rather complex logging specifications. For example, you could log
2052stderr, and errors and above to syslog, with: 2066stderr, and errors and above to syslog, with:
2053 2067
2054 PERL_ANYEVENT_LOG=Some::Module=debug,+log:filter=warn,+%syslog:%syslog=error,syslog 2068 PERL_ANYEVENT_LOG=Some::Module=debug,+log:filter=warn,+%syslog:%syslog=error,syslog
2055 2069
2056For the rather extensive details, see L<AnyEvent::Log>. 2070For the rather extensive details, see L<AnyEvent::Log>.
2071
2072This variable is evaluated when AnyEvent (or L<AnyEvent::Log>) is loaded,
2073so will take effect even before AnyEvent has initialised itself.
2057 2074
2058Note that specifying this environment variable causes the L<AnyEvent::Log> 2075Note that specifying this environment variable causes the L<AnyEvent::Log>
2059module to be loaded, while C<PERL_ANYEVENT_VERBOSE> does not, so only 2076module to be loaded, while C<PERL_ANYEVENT_VERBOSE> does not, so only
2060using the latter saves a few hundred kB of memory until the first message 2077using the latter saves a few hundred kB of memory until the first message
2061is being logged. 2078is being logged.
2080If this env variable is set, then its contents will be interpreted by 2097If this env variable is set, then its contents will be interpreted by
2081C<AnyEvent::Socket::parse_hostport> (after replacing every occurance of 2098C<AnyEvent::Socket::parse_hostport> (after replacing every occurance of
2082C<$$> by the process pid) and an C<AnyEvent::Debug::shell> is bound on 2099C<$$> by the process pid) and an C<AnyEvent::Debug::shell> is bound on
2083that port. The shell object is saved in C<$AnyEvent::Debug::SHELL>. 2100that port. The shell object is saved in C<$AnyEvent::Debug::SHELL>.
2084 2101
2085This takes place when the first watcher is created. 2102This happens when the first watcher is created.
2086 2103
2087For example, to bind a debug shell on a unix domain socket in 2104For example, to bind a debug shell on a unix domain socket in
2088F<< /tmp/debug<pid>.sock >>, you could use this: 2105F<< /tmp/debug<pid>.sock >>, you could use this:
2089 2106
2090 PERL_ANYEVENT_DEBUG_SHELL=/tmp/debug\$\$.sock perlprog 2107 PERL_ANYEVENT_DEBUG_SHELL=/tmp/debug\$\$.sock perlprog

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines