1 // This file extends the mw.log skeleton defined in startup/mediawiki.js.
2 // Code that is not needed by mw.loader is placed here.
4 /* eslint-disable no-console */
7 * Log debug messages and developer warnings to the browser console.
9 * See [mw.log()]{@link mw.log(2)} for verbose debug logging.
15 * Create a function that returns true for the first call from any particular call stack.
19 * @return {boolean|undefined} return.return True if the caller was not seen before.
22 // Optimisation: Don't create or compute anything for the common case
23 // where deprecations are not triggered.
26 return function isFirst() {
30 const stack = new Error().stack;
31 if ( !stacks.has( stack ) ) {
39 * Write a message to the browser console's error channel.
41 * Most browsers also print a stacktrace when calling this method if the
42 * argument is an Error object.
46 * @param {...Mixed} msg Messages to output to console
48 mw.log.error = Function.prototype.bind.call( console.error, console );
51 * Create a function that logs a deprecation warning when called.
54 * var deprecatedNoB = mw.log.makeDeprecated( 'hello_without_b', 'Use of hello without b is deprecated.' );
56 * function hello( a, b ) {
57 * if ( b === undefined ) {
67 * @param {string|null} key Name of the feature for deprecation tracker,
68 * or null for a console-only deprecation.
69 * @param {string} msg Deprecation warning.
72 mw.log.makeDeprecated = function ( key, msg ) {
73 const isFirst = stackSet();
74 return function maybeLog() {
77 mw.track( 'mw.deprecate', key );
85 * Create a property on a host object that, when accessed, will log
86 * a deprecation warning to the console.
89 * mw.log.deprecate( window, 'myGlobalFn', myGlobalFn );
92 * mw.log.deprecate( Thing, 'old', old, 'Use Other.thing instead', 'Thing.old' );
94 * @param {Object} obj Host object of deprecated property
95 * @param {string} key Name of property to create in `obj`
96 * @param {any} val The value this property should return when accessed
97 * @param {string} [msg] Optional extra text to add to the deprecation warning
98 * @param {string} [logName] Name of the feature for deprecation tracker.
99 * Tracking is disabled by default, except for global variables on `window`.
101 mw.log.deprecate = function ( obj, key, val, msg, logName ) {
102 const maybeLog = mw.log.makeDeprecated(
103 logName || ( obj === window ? key : null ),
104 'Use of "' + ( logName || key ) + '" is deprecated.' + ( msg ? ' ' + msg : '' )
106 Object.defineProperty( obj, key, {
113 set: function ( newVal ) {