2 * Logger for MediaWiki javascript.
3 * Implements the stub left by the main 'mediawiki' module.
5 * @author Michael Dale <mdale@wikimedia.org>
6 * @author Trevor Parscal <tparscal@wikimedia.org>
17 * Logs a message to the console.
19 * In the case the browser does not have a console API, a console is created on-the-fly by appending
20 * a `<div id="mw-log-console">` element to the bottom of the body and then appending this and future
21 * messages to that, instead of the console.
23 * @param {string...} msg Messages to output to console.
25 mw
.log = function () {
26 // Turn arguments into an array
27 var args
= Array
.prototype.slice
.call( arguments
),
28 // Allow log messages to use a configured prefix to identify the source window (ie. frame)
29 prefix
= mw
.config
.exists( 'mw.log.prefix' ) ? mw
.config
.get( 'mw.log.prefix' ) + '> ' : '';
31 // Try to use an existing console
32 if ( window
.console
!== undefined && $.isFunction( window
.console
.log
) ) {
33 args
.unshift( prefix
);
34 window
.console
.log
.apply( window
.console
, args
);
38 // If there is no console, use our own log box
39 mw
.loader
.using( 'jquery.footHovzer', function () {
43 // Create HH:MM:SS.MIL timestamp
44 time
= ( d
.getHours() < 10 ? '0' + d
.getHours() : d
.getHours() ) +
45 ':' + ( d
.getMinutes() < 10 ? '0' + d
.getMinutes() : d
.getMinutes() ) +
46 ':' + ( d
.getSeconds() < 10 ? '0' + d
.getSeconds() : d
.getSeconds() ) +
47 '.' + ( d
.getMilliseconds() < 10 ? '00' + d
.getMilliseconds() : ( d
.getMilliseconds() < 100 ? '0' + d
.getMilliseconds() : d
.getMilliseconds() ) ),
48 $log
= $( '#mw-log-console' );
51 $log
= $( '<div id="mw-log-console"></div>' ).css( {
54 backgroundColor
: 'white',
55 borderTop
: 'solid 2px #ADADAD'
57 hovzer
= $.getFootHovzer();
58 hovzer
.$.append( $log
);
64 borderBottom
: 'solid 1px #DDDDDD',
66 fontFamily
: 'monospace',
67 whiteSpace
: 'pre-wrap',
68 padding
: '0.125em 0.25em'
70 .text( prefix
+ args
.join( ', ' ) )
71 .prepend( '<span style="float: right;">[' + time
+ ']</span>' )
77 * Write a message the console's warning channel.
78 * Also logs a stacktrace for easier debugging.
79 * Each action is silently ignored if the browser doesn't support it.
81 * @param {string...} msg Messages to output to console
83 mw
.log
.warn = function () {
84 var console
= window
.console
;
85 if ( console
&& console
.warn
) {
86 console
.warn
.apply( console
, arguments
);
87 if ( console
.trace
) {
94 * Create a property in a host object that, when accessed, will produce
95 * a deprecation warning in the console with backtrace.
97 * @param {Object} obj Host object of deprecated property
98 * @param {string} key Name of property to create in `obj`
99 * @param {Mixed} val The value this property should return when accessed
100 * @param {string} [msg] Optional text to include in the deprecation message.
102 mw
.log
.deprecate
= !Object
.defineProperty
? function ( obj
, key
, val
) {
104 } : function ( obj
, key
, val
, msg
) {
105 msg
= 'MWDeprecationWarning: Use of "' + key
+ '" property is deprecated.' +
106 ( msg
? ( ' ' + msg
) : '' );
108 Object
.defineProperty( obj
, key
, {
115 set: function ( newVal
) {
121 // IE8 can throw on Object.defineProperty
126 }( mediaWiki
, jQuery
) );