Adding wfLogWarning for production warnings.
authordaniel <daniel.kinzler@wikimedia.de>
Mon, 18 Mar 2013 09:32:14 +0000 (10:32 +0100)
committerDaniel Kinzler <daniel.kinzler@wikimedia.de>
Tue, 19 Mar 2013 15:24:00 +0000 (15:24 +0000)
Currently, trigger_error() is used to report non-fatal problems in
production. It seems more consistent to have our own function to do
that, with behavior consistent with wfWarn().

Change-Id: I531b7ceec089978c2832721486f277fdfca65270

includes/GlobalFunctions.php
includes/debug/Debug.php

index 016736f..004284e 100644 (file)
@@ -1093,16 +1093,29 @@ function wfDeprecated( $function, $version = false, $component = false, $callerO
 
 /**
  * Send a warning either to the debug log or in a PHP error depending on
- * $wgDevelopmentWarnings
+ * $wgDevelopmentWarnings. To log warnings in production, use wfLogWarning() instead.
  *
  * @param string $msg message to send
  * @param $callerOffset Integer: number of items to go back in the backtrace to
  *        find the correct caller (1 = function calling wfWarn, ...)
- * @param $level Integer: PHP error level; only used when $wgDevelopmentWarnings
- *        is true
+ * @param $level Integer: PHP error level; defaults to E_USER_NOTICE;
+ *        only used when $wgDevelopmentWarnings is true
  */
 function wfWarn( $msg, $callerOffset = 1, $level = E_USER_NOTICE ) {
-       MWDebug::warning( $msg, $callerOffset + 1, $level );
+       MWDebug::warning( $msg, $callerOffset + 1, $level, 'auto' );
+}
+
+/**
+ * Send a warning as a PHP error and the debug log. This is intended for logging
+ * warnings in production. For logging development warnings, use WfWarn instead.
+ *
+ * @param $msg String: message to send
+ * @param $callerOffset Integer: number of items to go back in the backtrace to
+ *        find the correct caller (1 = function calling wfLogWarning, ...)
+ * @param $level Integer: PHP error level; defaults to E_USER_WARNING
+ */
+function wfLogWarning( $msg, $callerOffset = 1, $level = E_USER_WARNING ) {
+       MWDebug::warning( $msg, $callerOffset + 1, $level, 'production' );
 }
 
 /**
@@ -2578,8 +2591,7 @@ function wfMkdirParents( $dir, $mode = null, $caller = null ) {
 
        if( !$ok ) {
                // PHP doesn't report the path in its warning message, so add our own to aid in diagnosis.
-               trigger_error( sprintf( "%s: failed to mkdir \"%s\" mode 0%o", __FUNCTION__, $dir, $mode ),
-                       E_USER_WARNING );
+               wfLogWarning( sprintf( "failed to mkdir \"%s\" mode 0%o", $dir, $mode ) );
        }
        return $ok;
 }
index 8c39e1a..582b3ac 100644 (file)
@@ -135,10 +135,24 @@ class MWDebug {
         * @since 1.19
         * @param $msg string
         * @param $callerOffset int
-        * @param int $level A PHP error level. See sendWarning()
+        * @param $level int A PHP error level. See sendWarning()
+        * @param $log string: 'production' will always trigger a php error, 'auto'
+        *        will trigger an error if $wgDevelopmentWarnings is true, and 'debug'
+        *        will only write to the debug log(s).
+        *
         * @return mixed
         */
-       public static function warning( $msg, $callerOffset = 1, $level = E_USER_NOTICE ) {
+       public static function warning( $msg, $callerOffset = 1, $level = E_USER_NOTICE, $log = 'auto' ) {
+               global $wgDevelopmentWarnings;
+
+               if ( $log === 'auto' && !$wgDevelopmentWarnings ) {
+                       $log = 'debug';
+               }
+
+               if ( $log === 'debug' ) {
+                       $level = false;
+               }
+
                $callerDescription = self::getCallerDescription( $callerOffset );
 
                self::sendWarning( $msg, $callerDescription, $level );
@@ -212,7 +226,8 @@ class MWDebug {
                }
 
                if ( $sendToLog ) {
-                       self::sendWarning( $msg, $callerDescription, E_USER_DEPRECATED );
+                       global $wgDevelopmentWarnings; // we could have a more specific $wgDeprecationWarnings setting.
+                       self::sendWarning( $msg, $callerDescription, $wgDevelopmentWarnings ? E_USER_DEPRECATED : false );
                }
 
                if ( self::$enabled ) {
@@ -267,23 +282,21 @@ class MWDebug {
        }
 
        /**
-        * Send a warning either to the debug log or by triggering an user PHP
-        * error depending on $wgDevelopmentWarnings.
+        * Send a warning to the debug log and optionally also trigger a PHP
+        * error, depending on the $level argument.
         *
-        * @param string $msg Message to send
-        * @param array $caller caller description get from getCallerDescription()
-        * @param $level error level to use if $wgDevelopmentWarnings is true
+        * @param $msg string Message to send
+        * @param $caller array caller description get from getCallerDescription()
+        * @param $level int|bool error level to use; set to false to not trigger an error
         */
        private static function sendWarning( $msg, $caller, $level ) {
-               global $wgDevelopmentWarnings;
-
                $msg .= ' [Called from ' . $caller['func'] . ' in ' . $caller['file'] . ']';
 
-               if ( $wgDevelopmentWarnings ) {
+               if ( $level !== false ) {
                        trigger_error( $msg, $level );
-               } else {
-                       wfDebug( "$msg\n" );
                }
+
+               wfDebug( "$msg\n" );
        }
 
        /**