9c3d89034a0ef12feae982bad7e265920d02f6d1
[lhc/web/wiklou.git] / resources / mediawiki / mediawiki.log.js
1 /**
2 * Logger for MediaWiki javascript.
3 * Implements the stub left by the main 'mediawiki' module.
4 *
5 * @author Michael Dale <mdale@wikimedia.org>
6 * @author Trevor Parscal <tparscal@wikimedia.org>
7 */
8
9 (function( $ ) {
10
11 /**
12 * Logs a message to the console.
13 *
14 * In the case the browser does not have a console API, a console is created on-the-fly by appending
15 * a <div id="mw-log-console"> element to the bottom of the body and then appending this and future
16 * messages to that, instead of the console.
17 *
18 * @param logmsg {String} Messages to output to the console.
19 */
20 mw.log = function( logmsg ) {
21 // Allow log messages to use a configured prefix to identify the source window (ie. frame)
22 if ( mw.config.exists( 'mw.log.prefix' ) ) {
23 logmsg = mw.config.get( 'mw.log.prefix' ) + '> ' + logmsg;
24 }
25
26 // Try to use an existing console
27 if ( window.console !== undefined && $.isFunction( window.console.log ) ) {
28 window.console.log( logmsg );
29 return;
30 }
31
32 // If there is no console, use our own log box
33
34 var d = new Date(),
35 // Create HH:MM:SS.MIL timestamp
36 time = ( d.getHours() < 10 ? '0' + d.getHours() : d.getHours() ) +
37 ':' + ( d.getMinutes() < 10 ? '0' + d.getMinutes() : d.getMinutes() ) +
38 ':' + ( d.getSeconds() < 10 ? '0' + d.getSeconds() : d.getSeconds() ) +
39 '.' + ( d.getMilliseconds() < 10 ? '00' + d.getMilliseconds() : ( d.getMilliseconds() < 100 ? '0' + d.getMilliseconds() : d.getMilliseconds() ) ),
40 $log = $( '#mw-log-console' );
41
42 if ( !$log.length ) {
43 $log = $( '<div id="mw-log-console"></div>' ).css( {
44 position: 'fixed',
45 overflow: 'auto',
46 zIndex: 500,
47 bottom: '0px',
48 left: '0px',
49 right: '0px',
50 height: '150px',
51 backgroundColor: 'white',
52 borderTop: 'solid 2px #ADADAD'
53 } );
54 $( 'body' )
55 // Since the position is fixed, make sure we don't hide any actual content.
56 // Increase padding to account for #mw-log-console.
57 .css( 'paddingBottom', '+=150px' )
58 .append( $log );
59 }
60 $log.append(
61 $( '<div></div>' )
62 .css( {
63 borderBottom: 'solid 1px #DDDDDD',
64 fontSize: 'small',
65 fontFamily: 'monospace',
66 whiteSpace: 'pre-wrap',
67 padding: '0.125em 0.25em'
68 } )
69 .text( logmsg )
70 .prepend( '<span style="float: right;">[' + time + ']</span>' )
71 );
72 };
73
74 })( jQuery );