r41215 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r41214‎ | r41215 | r41216 >
Date:07:11, 24 September 2008
Author:tstarling
Status:old
Tags:
Comment:
Implemented UDP logging.
Modified paths:
  • /trunk/phase3/includes/GlobalFunctions.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/GlobalFunctions.php
@@ -285,16 +285,46 @@
286286 }
287287
288288 /**
289 - * Log to a file without getting "file size exceeded" signals
 289+ * Log to a file without getting "file size exceeded" signals.
 290+ *
 291+ * Can also log to TCP or UDP with the syntax udp://host:port/prefix. This will
 292+ * send lines to the specified port, prefixed by the specified prefix and a space.
290293 */
291294 function wfErrorLog( $text, $file ) {
292 - wfSuppressWarnings();
293 - $exists = file_exists( $file );
294 - $size = $exists ? filesize( $file ) : false;
295 - if ( !$exists || ( $size !== false && $size + strlen( $text ) < 0x7fffffff ) ) {
296 - error_log( $text, 3, $file );
 295+ if ( substr( $file, 0, 4 ) == 'udp:' ) {
 296+ if ( preg_match( '!^(tcp|udp):(?://)?\[([0-9a-fA-F:]+)\]:(\d+)(?:/(.*))?$!', $file, $m ) ) {
 297+ // IPv6 bracketed host
 298+ $protocol = $m[1];
 299+ $host = $m[2];
 300+ $port = $m[3];
 301+ $prefix = isset( $m[4] ) ? $m[4] : '';
 302+ } elseif ( preg_match( '!^(tcp|udp):(?://)?([a-zA-Z0-9-]+):(\d+)(?:/(.*))?$!', $file, $m ) ) {
 303+ $protocol = $m[1];
 304+ $host = $m[2];
 305+ $port = $m[3];
 306+ $prefix = isset( $m[4] ) ? $m[4] : '';
 307+ } else {
 308+ throw new MWException( __METHOD__.": Invalid UDP specification" );
 309+ }
 310+ $prefix = strval( $prefix );
 311+ if ( $prefix != '' ) {
 312+ $prefix .= ' ';
 313+ }
 314+ $sock = fsockopen( "$protocol://$host", $port );
 315+ if ( !$sock ) {
 316+ return;
 317+ }
 318+ fwrite( $sock, $prefix . $text );
 319+ fclose( $sock );
 320+ } else {
 321+ wfSuppressWarnings();
 322+ $exists = file_exists( $file );
 323+ $size = $exists ? filesize( $file ) : false;
 324+ if ( !$exists || ( $size !== false && $size + strlen( $text ) < 0x7fffffff ) ) {
 325+ error_log( $text, 3, $file );
 326+ }
 327+ wfRestoreWarnings();
297328 }
298 - wfRestoreWarnings();
299329 }
300330
301331 /**