064f704b81bae42afb6557e7e72bae77e64ba2df
[yaffs-website] / node_modules / video.js / es5 / utils / log.js
1 'use strict';
2
3 exports.__esModule = true;
4 exports.logByType = undefined;
5
6 var _window = require('global/window');
7
8 var _window2 = _interopRequireDefault(_window);
9
10 var _browser = require('./browser');
11
12 var _obj = require('./obj');
13
14 function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
15
16 var log = void 0;
17
18 /**
19  * Log messages to the console and history based on the type of message
20  *
21  * @param  {string} type
22  *         The name of the console method to use.
23  *
24  * @param  {Array} args
25  *         The arguments to be passed to the matching console method.
26  *
27  * @param  {boolean} [stringify]
28  *         By default, only old IEs should get console argument stringification,
29  *         but this is exposed as a parameter to facilitate testing.
30  */
31 /**
32  * @file log.js
33  * @module log
34  */
35 var logByType = exports.logByType = function logByType(type, args) {
36   var stringify = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : !!_browser.IE_VERSION && _browser.IE_VERSION < 11;
37
38
39   if (type !== 'log') {
40
41     // add the type to the front of the message when it's not "log"
42     args.unshift(type.toUpperCase() + ':');
43   }
44
45   // add to history
46   log.history.push(args);
47
48   // add console prefix after adding to history
49   args.unshift('VIDEOJS:');
50
51   // If there's no console then don't try to output messages, but they will
52   // still be stored in `log.history`.
53   //
54   // Was setting these once outside of this function, but containing them
55   // in the function makes it easier to test cases where console doesn't exist
56   // when the module is executed.
57   var fn = _window2['default'].console && _window2['default'].console[type];
58
59   // Bail out if there's no console.
60   if (!fn) {
61     return;
62   }
63
64   // IEs previous to 11 log objects uselessly as "[object Object]"; so, JSONify
65   // objects and arrays for those less-capable browsers.
66   if (stringify) {
67     args = args.map(function (a) {
68       if ((0, _obj.isObject)(a) || Array.isArray(a)) {
69         try {
70           return JSON.stringify(a);
71         } catch (x) {
72           return String(a);
73         }
74       }
75
76       // Cast to string before joining, so we get null and undefined explicitly
77       // included in output (as we would in a modern console).
78       return String(a);
79     }).join(' ');
80   }
81
82   // Old IE versions do not allow .apply() for console methods (they are
83   // reported as objects rather than functions).
84   if (!fn.apply) {
85     fn(args);
86   } else {
87     fn[Array.isArray(args) ? 'apply' : 'call'](_window2['default'].console, args);
88   }
89 };
90
91 /**
92  * Log plain debug messages
93  *
94  * @param {Mixed[]} args
95  *        One or more messages or objects that should be logged.
96  */
97 log = function log() {
98   for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
99     args[_key] = arguments[_key];
100   }
101
102   logByType('log', args);
103 };
104
105 /**
106  * Keep a history of log messages
107  *
108  * @type {Array}
109  */
110 log.history = [];
111
112 /**
113  * Log error messages
114  *
115  * @param {Mixed[]} args
116  *        One or more messages or objects that should be logged as an error
117  */
118 log.error = function () {
119   for (var _len2 = arguments.length, args = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
120     args[_key2] = arguments[_key2];
121   }
122
123   return logByType('error', args);
124 };
125
126 /**
127  * Log warning messages
128  *
129  * @param {Mixed[]} args
130  *        One or more messages or objects that should be logged as a warning.
131  */
132 log.warn = function () {
133   for (var _len3 = arguments.length, args = Array(_len3), _key3 = 0; _key3 < _len3; _key3++) {
134     args[_key3] = arguments[_key3];
135   }
136
137   return logByType('warn', args);
138 };
139
140 exports['default'] = log;