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>
11 // Keep reference to the dummy placeholder from mediawiki.js
12 // The root is replaced below, but it has other methods that we need to restore.
13 var original = mw.log,
14 slice = Array.prototype.slice;
17 * Logs a message to the console in debug mode.
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.
24 * @param {...string} msg Messages to output to console.
26 mw.log = function () {
27 // Turn arguments into an array
28 var args = slice.call( arguments ),
29 // Allow log messages to use a configured prefix to identify the source window (ie. frame)
30 prefix = mw.config.exists( 'mw.log.prefix' ) ? mw.config.get( 'mw.log.prefix' ) + '> ' : '';
32 // Try to use an existing console
33 // Generally we can cache this, but in this case we want to re-evaluate this as a
34 // global property live so that things like Firebug Lite can take precedence.
35 if ( window.console && window.console.log && window.console.log.apply ) {
36 args.unshift( prefix );
37 window.console.log.apply( window.console, args );
41 // If there is no console, use our own log box
42 mw.loader.using( 'jquery.footHovzer', function () {
46 // Create HH:MM:SS.MIL timestamp
47 time = ( d.getHours() < 10 ? '0' + d.getHours() : d.getHours() ) +
48 ':' + ( d.getMinutes() < 10 ? '0' + d.getMinutes() : d.getMinutes() ) +
49 ':' + ( d.getSeconds() < 10 ? '0' + d.getSeconds() : d.getSeconds() ) +
50 '.' + ( d.getMilliseconds() < 10 ? '00' + d.getMilliseconds() : ( d.getMilliseconds() < 100 ? '0' + d.getMilliseconds() : d.getMilliseconds() ) ),
51 $log = $( '#mw-log-console' );
54 $log = $( '<div id="mw-log-console"></div>' ).css( {
57 backgroundColor: 'white',
58 borderTop: 'solid 2px #ADADAD'
60 hovzer = $.getFootHovzer();
61 hovzer.$.append( $log );
67 borderBottom: 'solid 1px #DDDDDD',
69 fontFamily: 'monospace',
70 whiteSpace: 'pre-wrap',
71 padding: '0.125em 0.25em'
73 .text( prefix + args.join( ', ' ) )
74 .prepend( '<span style="float: right;">[' + time + ']</span>' )
79 // Restore original methods
80 mw.log.warn = original.warn;
81 mw.log.error = original.error;
82 mw.log.deprecate = original.deprecate;
84 }( mediaWiki, jQuery ) );