Merge "Cleaned up FileJournal documentation."
[mediawiki.git] / resources / mediawiki / mediawiki.log.js
blob9ce83054a14f26c7cde0f1dfca62d93fb5dd874e
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  */
9 ( function ( mw, $ ) {
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 {String} First in list of variadic messages to output to console.
19          */
20         mw.log = function ( /* logmsg, logmsg, */ ) {
21                 // Turn arguments into an array
22                 var     args = Array.prototype.slice.call( arguments ),
23                         // Allow log messages to use a configured prefix to identify the source window (ie. frame)
24                         prefix = mw.config.exists( 'mw.log.prefix' ) ? mw.config.get( 'mw.log.prefix' ) + '> ' : '';
26                 // Try to use an existing console
27                 if ( window.console !== undefined && $.isFunction( window.console.log ) ) {
28                         args.unshift( prefix );
29                         window.console.log.apply( window.console, args );
30                         return;
31                 }
33                 // If there is no console, use our own log box
34                 mw.loader.using( 'jquery.footHovzer', function () {
36                         var     d = new Date(),
37                                 // Create HH:MM:SS.MIL timestamp
38                                 time = ( d.getHours() < 10 ? '0' + d.getHours() : d.getHours() ) +
39                                  ':' + ( d.getMinutes() < 10 ? '0' + d.getMinutes() : d.getMinutes() ) +
40                                  ':' + ( d.getSeconds() < 10 ? '0' + d.getSeconds() : d.getSeconds() ) +
41                                  '.' + ( d.getMilliseconds() < 10 ? '00' + d.getMilliseconds() : ( d.getMilliseconds() < 100 ? '0' + d.getMilliseconds() : d.getMilliseconds() ) ),
42                                  $log = $( '#mw-log-console' );
43         
44                         if ( !$log.length ) {
45                                 $log = $( '<div id="mw-log-console"></div>' ).css( {
46                                                 overflow: 'auto',
47                                                 height: '150px',
48                                                 backgroundColor: 'white',
49                                                 borderTop: 'solid 2px #ADADAD'
50                                         } );
51                                 var hovzer = $.getFootHovzer();
52                                 hovzer.$.append( $log );
53                                 hovzer.update();
54                         }
55                         $log.append(
56                                 $( '<div></div>' )
57                                         .css( {
58                                                 borderBottom: 'solid 1px #DDDDDD',
59                                                 fontSize: 'small',
60                                                 fontFamily: 'monospace',
61                                                 whiteSpace: 'pre-wrap',
62                                                 padding: '0.125em 0.25em'
63                                         } )
64                                         .text( prefix + args.join( ', ' ) )
65                                         .prepend( '<span style="float: right;">[' + time + ']</span>' )
66                         );
67                 } );
68         };
70 }( mediaWiki, jQuery ) );