mediawiki.js: Refactor definition of mw.log singleton
authorTimo Tijhof <krinklemail@gmail.com>
Fri, 23 Sep 2016 19:40:10 +0000 (20:40 +0100)
committerKrinkle <krinklemail@gmail.com>
Fri, 23 Sep 2016 20:35:07 +0000 (20:35 +0000)
Move this out of the large plain object that defines 'mw' itself
because right now none of the mw object initialisation is able
to call mw.log.

In a future commit, a call to mw.log is introduced in the mw.Map()
constructor, which is also used to set the mw.messages, mw.templates
and mw.user.options fields.

* Rename the other log() function to avoid clashing.
* Update outdated comment about not using mw.log() which was
  there about confusion between log() and mw.log(). However,
  this code was updated long ago to use mw.track() instead of
  log() directly, and log() has now been renamed.

Change-Id: I7d6bd4e3c9753e30278752c59d6cbd4d2fd10d96

resources/src/mediawiki/mediawiki.js
resources/src/mediawiki/mediawiki.log.js

index 2c7343f..f34ecdb 100644 (file)
@@ -11,7 +11,7 @@
 ( function ( $ ) {
        'use strict';
 
-       var mw, StringSet,
+       var mw, StringSet, log,
                hasOwn = Object.prototype.hasOwnProperty,
                slice = Array.prototype.slice,
                trackCallbacks = $.Callbacks( 'memory' ),
                }
        };
 
+       log = ( function () {
+               // Also update the restoration of methods in mediawiki.log.js
+               // when adding or removing methods here.
+               var log = function () {},
+                       console = window.console;
+
+               /**
+                * @class mw.log
+                * @singleton
+                */
+
+               /**
+                * Write a message to the console's warning channel.
+                * Actions not supported by the browser console are silently ignored.
+                *
+                * @param {...string} msg Messages to output to console
+                */
+               log.warn = console && console.warn && Function.prototype.bind ?
+                       Function.prototype.bind.call( console.warn, console ) :
+                       $.noop;
+
+               /**
+                * Write a message to the console's error channel.
+                *
+                * Most browsers provide a stacktrace by default if the argument
+                * is a caught Error object.
+                *
+                * @since 1.26
+                * @param {Error|...string} msg Messages to output to console
+                */
+               log.error = console && console.error && Function.prototype.bind ?
+                       Function.prototype.bind.call( console.error, console ) :
+                       $.noop;
+
+               /**
+                * Create a property in a host object that, when accessed, will produce
+                * a deprecation warning in the console.
+                *
+                * @param {Object} obj Host object of deprecated property
+                * @param {string} key Name of property to create in `obj`
+                * @param {Mixed} val The value this property should return when accessed
+                * @param {string} [msg] Optional text to include in the deprecation message
+                */
+               log.deprecate = !Object.defineProperty ? function ( obj, key, val ) {
+                       obj[ key ] = val;
+               } : function ( obj, key, val, msg ) {
+                       msg = 'Use of "' + key + '" is deprecated.' + ( msg ? ( ' ' + msg ) : '' );
+                       var logged = new StringSet();
+                       function uniqueTrace() {
+                               var trace = new Error().stack;
+                               if ( logged.has( trace ) ) {
+                                       return false;
+                               }
+                               logged.add( trace );
+                               return true;
+                       }
+                       Object.defineProperty( obj, key, {
+                               configurable: true,
+                               enumerable: true,
+                               get: function () {
+                                       if ( uniqueTrace() ) {
+                                               mw.track( 'mw.deprecate', key );
+                                               mw.log.warn( msg );
+                                       }
+                                       return val;
+                               },
+                               set: function ( newVal ) {
+                                       if ( uniqueTrace() ) {
+                                               mw.track( 'mw.deprecate', key );
+                                               mw.log.warn( msg );
+                                       }
+                                       val = newVal;
+                               }
+                       } );
+
+               };
+
+               return log;
+       }() );
+
        /**
         * @class mw
         */
                },
 
                /**
-                * Dummy placeholder for {@link mw.log}
+                * No-op dummy placeholder for {@link mw.log} in debug mode.
                 *
                 * @method
                 */
-               log: ( function () {
-                       // Also update the restoration of methods in mediawiki.log.js
-                       // when adding or removing methods here.
-                       var log = function () {},
-                               console = window.console;
-
-                       /**
-                        * @class mw.log
-                        * @singleton
-                        */
-
-                       /**
-                        * Write a message to the console's warning channel.
-                        * Actions not supported by the browser console are silently ignored.
-                        *
-                        * @param {...string} msg Messages to output to console
-                        */
-                       log.warn = console && console.warn && Function.prototype.bind ?
-                               Function.prototype.bind.call( console.warn, console ) :
-                               $.noop;
-
-                       /**
-                        * Write a message to the console's error channel.
-                        *
-                        * Most browsers provide a stacktrace by default if the argument
-                        * is a caught Error object.
-                        *
-                        * @since 1.26
-                        * @param {Error|...string} msg Messages to output to console
-                        */
-                       log.error = console && console.error && Function.prototype.bind ?
-                               Function.prototype.bind.call( console.error, console ) :
-                               $.noop;
-
-                       /**
-                        * Create a property in a host object that, when accessed, will produce
-                        * a deprecation warning in the console with backtrace.
-                        *
-                        * @param {Object} obj Host object of deprecated property
-                        * @param {string} key Name of property to create in `obj`
-                        * @param {Mixed} val The value this property should return when accessed
-                        * @param {string} [msg] Optional text to include in the deprecation message
-                        */
-                       log.deprecate = !Object.defineProperty ? function ( obj, key, val ) {
-                               obj[ key ] = val;
-                       } : function ( obj, key, val, msg ) {
-                               msg = 'Use of "' + key + '" is deprecated.' + ( msg ? ( ' ' + msg ) : '' );
-                               var logged = new StringSet();
-                               function uniqueTrace() {
-                                       var trace = new Error().stack;
-                                       if ( logged.has( trace ) ) {
-                                               return false;
-                                       }
-                                       logged.add( trace );
-                                       return true;
-                               }
-                               Object.defineProperty( obj, key, {
-                                       configurable: true,
-                                       enumerable: true,
-                                       get: function () {
-                                               if ( uniqueTrace() ) {
-                                                       mw.track( 'mw.deprecate', key );
-                                                       mw.log.warn( msg );
-                                               }
-                                               return val;
-                                       },
-                                       set: function ( newVal ) {
-                                               if ( uniqueTrace() ) {
-                                                       mw.track( 'mw.deprecate', key );
-                                                       mw.log.warn( msg );
-                                               }
-                                               val = newVal;
-                                       }
-                               } );
-
-                       };
-
-                       return log;
-               }() ),
+               log: log,
 
                /**
                 * Client for ResourceLoader server end point.
                                                                markModuleReady();
                                                        }
                                                } catch ( e ) {
-                                                       // This needs to NOT use mw.log because these errors are common in production mode
-                                                       // and not in debug mode, such as when a symbol that should be global isn't exported
+                                                       // Use mw.track instead of mw.log because these errors are common in production mode
+                                                       // (e.g. undefined variable), and mw.log is only enabled in debug mode.
                                                        registry[ module ].state = 'error';
                                                        mw.track( 'resourceloader.exception', { exception: e, module: module, source: 'module-execute' } );
                                                        handlePending( module );
         * reference, so that debugging tools loaded later are supported (e.g. Firebug Lite in IE).
         *
         * @private
-        * @method log_
         * @param {string} topic Stream name passed by mw.track
         * @param {Object} data Data passed by mw.track
         * @param {Error} [data.exception]
         * @param {string} data.source Error source
         * @param {string} [data.module] Name of module which caused the error
         */
-       function log( topic, data ) {
+       function logError( topic, data ) {
                var msg,
                        e = data.exception,
                        source = data.source,
        }
 
        // Subscribe to error streams
-       mw.trackSubscribe( 'resourceloader.exception', log );
-       mw.trackSubscribe( 'resourceloader.assert', log );
+       mw.trackSubscribe( 'resourceloader.exception', logError );
+       mw.trackSubscribe( 'resourceloader.assert', logError );
 
        /**
         * Fired when all modules associated with the page have finished loading.
index 93fb470..c886817 100644 (file)
@@ -8,9 +8,8 @@
 
 ( function ( mw, $ ) {
 
-       // Reference to dummy
-       // We don't need the dummy, but it has other methods on it
-       // that we need to restore afterwards.
+       // Keep reference to the dummy placeholder from mediawiki.js
+       // The root is replaced below, but it has other methods that we need to restore.
        var original = mw.log,
                slice = Array.prototype.slice;