Localisation updates from https://translatewiki.net.
[mediawiki.git] / resources / mediawiki / mediawiki.log.js
blob75e4c9610f92fe64f22af14cdf3f4256a18d829e
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          * @class mw.log
13          * @singleton
14          */
16         /**
17          * Logs a message to the console.
18          *
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.
22          *
23          * @param {string...} msg Messages to output to console.
24          */
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 );
35                         return;
36                 }
38                 // If there is no console, use our own log box
39                 mw.loader.using( 'jquery.footHovzer', function () {
41                         var     hovzer,
42                                 d = new Date(),
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' );
50                         if ( !$log.length ) {
51                                 $log = $( '<div id="mw-log-console"></div>' ).css( {
52                                                 overflow: 'auto',
53                                                 height: '150px',
54                                                 backgroundColor: 'white',
55                                                 borderTop: 'solid 2px #ADADAD'
56                                         } );
57                                 hovzer = $.getFootHovzer();
58                                 hovzer.$.append( $log );
59                                 hovzer.update();
60                         }
61                         $log.append(
62                                 $( '<div>' )
63                                         .css( {
64                                                 borderBottom: 'solid 1px #DDDDDD',
65                                                 fontSize: 'small',
66                                                 fontFamily: 'monospace',
67                                                 whiteSpace: 'pre-wrap',
68                                                 padding: '0.125em 0.25em'
69                                         } )
70                                         .text( prefix + args.join( ', ' ) )
71                                         .prepend( '<span style="float: right;">[' + time + ']</span>' )
72                         );
73                 } );
74         };
76         /**
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.
80          *
81          * @param {string...} msg Messages to output to console
82          */
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 ) {
88                                 console.trace();
89                         }
90                 }
91         };
93         /**
94          * Create a property in a host object that, when accessed, will produce
95          * a deprecation warning in the console with backtrace.
96          *
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.
101          */
102         mw.log.deprecate = !Object.defineProperty ? function ( obj, key, val ) {
103                 obj[key] = val;
104         } : function ( obj, key, val, msg ) {
105                 msg = 'MWDeprecationWarning: Use of "' + key + '" property is deprecated.' +
106                         ( msg ? ( ' ' + msg ) : '' );
107                 try {
108                         Object.defineProperty( obj, key, {
109                                 configurable: true,
110                                 enumerable: true,
111                                 get: function () {
112                                         mw.log.warn( msg );
113                                         return val;
114                                 },
115                                 set: function ( newVal ) {
116                                         mw.log.warn( msg );
117                                         val = newVal;
118                                 }
119                         } );
120                 } catch ( err ) {
121                         // IE8 can throw on Object.defineProperty
122                         obj[key] = val;
123                 }
124         };
126 }( mediaWiki, jQuery ) );