summaryrefslogtreecommitdiff
path: root/resources/mediawiki/mediawiki.log.js
blob: ee08b12bc667078f981fc16ed5c99d4cb85c0c63 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/**
 * Logger for MediaWiki javascript.
 * Implements the stub left by the main 'mediawiki' module.
 *
 * @author Michael Dale <mdale@wikimedia.org>
 * @author Trevor Parscal <tparscal@wikimedia.org>
 */

( function ( mw, $ ) {

	/**
	 * Logs a message to the console.
	 *
	 * In the case the browser does not have a console API, a console is created on-the-fly by appending
	 * a <div id="mw-log-console"> element to the bottom of the body and then appending this and future
	 * messages to that, instead of the console.
	 *
	 * @param {String} First in list of variadic messages to output to console.
	 */
	mw.log = function ( /* logmsg, logmsg, */ ) {
		// Turn arguments into an array
		var	args = Array.prototype.slice.call( arguments ),
			// Allow log messages to use a configured prefix to identify the source window (ie. frame)
			prefix = mw.config.exists( 'mw.log.prefix' ) ? mw.config.get( 'mw.log.prefix' ) + '> ' : '';

		// Try to use an existing console
		if ( window.console !== undefined && $.isFunction( window.console.log ) ) {
			args.unshift( prefix );
			window.console.log.apply( window.console, args );
			return;
		}

		// If there is no console, use our own log box
		mw.loader.using( 'jquery.footHovzer', function () {

			var	hovzer,
				d = new Date(),
				// Create HH:MM:SS.MIL timestamp
				time = ( d.getHours() < 10 ? '0' + d.getHours() : d.getHours() ) +
				 ':' + ( d.getMinutes() < 10 ? '0' + d.getMinutes() : d.getMinutes() ) +
				 ':' + ( d.getSeconds() < 10 ? '0' + d.getSeconds() : d.getSeconds() ) +
				 '.' + ( d.getMilliseconds() < 10 ? '00' + d.getMilliseconds() : ( d.getMilliseconds() < 100 ? '0' + d.getMilliseconds() : d.getMilliseconds() ) ),
				 $log = $( '#mw-log-console' );

			if ( !$log.length ) {
				$log = $( '<div id="mw-log-console"></div>' ).css( {
						overflow: 'auto',
						height: '150px',
						backgroundColor: 'white',
						borderTop: 'solid 2px #ADADAD'
					} );
				hovzer = $.getFootHovzer();
				hovzer.$.append( $log );
				hovzer.update();
			}
			$log.append(
				$( '<div></div>' )
					.css( {
						borderBottom: 'solid 1px #DDDDDD',
						fontSize: 'small',
						fontFamily: 'monospace',
						whiteSpace: 'pre-wrap',
						padding: '0.125em 0.25em'
					} )
					.text( prefix + args.join( ', ' ) )
					.prepend( '<span style="float: right;">[' + time + ']</span>' )
			);
		} );
	};

}( mediaWiki, jQuery ) );