File: source/skylink-debug.js

  1. /**
  2. * The list of the SDK <code>console</code> API log levels.
  3. * @attribute LOG_LEVEL
  4. * @param {Number} DEBUG <small>Value <code>4</code></small>
  5. * The value of the log level that displays <code>console</code> <code>debug</code>,
  6. * <code>log</code>, <code>info</code>, <code>warn</code> and <code>error</code> logs.
  7. * @param {Number} LOG <small>Value <code>3</code></small>
  8. * The value of the log level that displays only <code>console</code> <code>log</code>,
  9. * <code>info</code>, <code>warn</code> and <code>error</code> logs.
  10. * @param {Number} INFO <small>Value <code>2</code></small>
  11. * The value of the log level that displays only <code>console</code> <code>info</code>,
  12. * <code>warn</code> and <code>error</code> logs.
  13. * @param {Number} WARN <small>Value <code>1</code></small>
  14. * The value of the log level that displays only <code>console</code> <code>warn</code>
  15. * and <code>error</code> logs.
  16. * @param {Number} ERROR <small>Value <code>0</code></small>
  17. * The value of the log level that displays only <code>console</code> <code>error</code> logs.
  18. * @type JSON
  19. * @readOnly
  20. * @for Skylink
  21. * @since 0.5.4
  22. */
  23. Skylink.prototype.LOG_LEVEL = {
  24. DEBUG: 4,
  25. LOG: 3,
  26. INFO: 2,
  27. WARN: 1,
  28. ERROR: 0
  29. };
  30.  
  31. /**
  32. * Stores the log message starting header string.
  33. * E.g. "<header> - <the log message>".
  34. * @attribute _LOG_KEY
  35. * @type String
  36. * @private
  37. * @scoped true
  38. * @for Skylink
  39. * @since 0.5.4
  40. */
  41. var _LOG_KEY = 'SkylinkJS';
  42.  
  43.  
  44. /**
  45. * Stores the list of available SDK log levels.
  46. * @attribute _LOG_LEVELS
  47. * @type Array
  48. * @private
  49. * @scoped true
  50. * @for Skylink
  51. * @since 0.5.5
  52. */
  53. var _LOG_LEVELS = ['error', 'warn', 'info', 'log', 'debug'];
  54.  
  55. /**
  56. * Stores the current SDK log level.
  57. * Default is ERROR (<code>0</code>).
  58. * @attribute _logLevel
  59. * @type String
  60. * @default 0
  61. * @private
  62. * @scoped true
  63. * @for Skylink
  64. * @since 0.5.4
  65. */
  66. var _logLevel = 0;
  67.  
  68. /**
  69. * Stores the flag if debugging mode is enabled.
  70. * This manipulates the SkylinkLogs interface.
  71. * @attribute _enableDebugMode
  72. * @type Boolean
  73. * @default false
  74. * @private
  75. * @scoped true
  76. * @for Skylink
  77. * @since 0.5.4
  78. */
  79. var _enableDebugMode = false;
  80.  
  81. /**
  82. * Stores the flag if logs should be stored in SkylinkLogs interface.
  83. * @attribute _enableDebugStack
  84. * @type Boolean
  85. * @default false
  86. * @private
  87. * @scoped true
  88. * @for Skylink
  89. * @since 0.5.5
  90. */
  91. var _enableDebugStack = false;
  92.  
  93. /**
  94. * Stores the flag if logs should trace if available.
  95. * This uses the <code>console.trace</code> API.
  96. * @attribute _enableDebugTrace
  97. * @type Boolean
  98. * @default false
  99. * @private
  100. * @scoped true
  101. * @for Skylink
  102. * @since 0.5.5
  103. */
  104. var _enableDebugTrace = false;
  105.  
  106. /**
  107. * Stores the logs used for SkylinkLogs object.
  108. * @attribute _storedLogs
  109. * @type Array
  110. * @private
  111. * @scoped true
  112. * @for Skylink
  113. * @since 0.5.5
  114. */
  115. var _storedLogs = [];
  116.  
  117. /**
  118. * Function that gets the stored logs.
  119. * @method _getStoredLogsFn
  120. * @private
  121. * @scoped true
  122. * @for Skylink
  123. * @since 0.5.5
  124. */
  125. var _getStoredLogsFn = function (logLevel) {
  126. if (typeof logLevel === 'undefined') {
  127. return _storedLogs;
  128. }
  129. var returnLogs = [];
  130. for (var i = 0; i < _storedLogs.length; i++) {
  131. if (_storedLogs[i][1] === _LOG_LEVELS[logLevel]) {
  132. returnLogs.push(_storedLogs[i]);
  133. }
  134. }
  135. return returnLogs;
  136. };
  137.  
  138. /**
  139. * Function that clears the stored logs.
  140. * @method _clearAllStoredLogsFn
  141. * @private
  142. * @scoped true
  143. * @for Skylink
  144. * @since 0.5.5
  145. */
  146. var _clearAllStoredLogsFn = function () {
  147. _storedLogs = [];
  148. };
  149.  
  150. /**
  151. * Function that prints in the Web Console interface the stored logs.
  152. * @method _printAllStoredLogsFn
  153. * @private
  154. * @scoped true
  155. * @for Skylink
  156. * @since 0.5.5
  157. */
  158. var _printAllStoredLogsFn = function () {
  159. for (var i = 0; i < _storedLogs.length; i++) {
  160. var timestamp = _storedLogs[i][0];
  161. var log = (console[_storedLogs[i][1]] !== 'undefined') ?
  162. _storedLogs[i][1] : 'log';
  163. var message = _storedLogs[i][2];
  164. var debugObject = _storedLogs[i][3];
  165.  
  166. if (typeof debugObject !== 'undefined') {
  167. console[log](message, debugObject, timestamp);
  168. } else {
  169. console[log](message, timestamp);
  170. }
  171. }
  172. };
  173.  
  174. /**
  175. * <blockquote class="info">
  176. * To utilise and enable the <code>SkylinkLogs</code> API functionalities, the
  177. * <a href="#method_setDebugMode"><code>setDebugMode()</code> method</a>
  178. * <code>options.storeLogs</code> parameter has to be enabled.
  179. * </blockquote>
  180. * The object interface to manage the SDK <a href="https://developer.mozilla.org/en/docs/Web/API/console">
  181. * Javascript Web Console</a> logs.
  182. * @property SkylinkLogs
  183. * @type JSON
  184. * @global true
  185. * @for Skylink
  186. * @since 0.5.5
  187. */
  188. window.SkylinkLogs = {
  189. /**
  190. * Function that gets the current stored SDK <code>console</code> logs.
  191. * @property SkylinkLogs.getLogs
  192. * @param {Number} [logLevel] The specific log level of logs to return.
  193. * - When not provided or that the level does not exists, it will return all logs of all levels.
  194. * [Rel: Skylink.LOG_LEVEL]
  195. * @return {Array} The array of stored logs.<ul>
  196. * <li><code><#index></code><var><b>{</b>Array<b>}</b></var><p>The stored log item.</p><ul>
  197. * <li><code>0</code><var><b>{</b>Date<b>}</b></var><p>The DateTime of when the log was stored.</p></li>
  198. * <li><code>1</code><var><b>{</b>String<b>}</b></var><p>The log level. [Rel: Skylink.LOG_LEVEL]</p></li>
  199. * <li><code>2</code><var><b>{</b>String<b>}</b></var><p>The log message.</p></li>
  200. * <li><code>3</code><var><b>{</b>Any<b>}</b></var><span class="label">Optional</span><p>The log message object.
  201. * </p></li></ul></li></ul>
  202. * @example
  203. * // Example 1: Get logs of specific level
  204. * var debugLogs = SkylinkLogs.getLogs(skylinkDemo.LOG_LEVEL.DEBUG);
  205. *
  206. * // Example 2: Get all the logs
  207. * var allLogs = SkylinkLogs.getLogs();
  208. * @type Function
  209. * @global true
  210. * @triggerForPropHackNone true
  211. * @for Skylink
  212. * @since 0.5.5
  213. */
  214. getLogs: _getStoredLogsFn,
  215.  
  216. /**
  217. * Function that clears all the current stored SDK <code>console</code> logs.
  218. * @property SkylinkLogs.clearAllLogs
  219. * @type Function
  220. * @example
  221. * // Example 1: Clear all the logs
  222. * SkylinkLogs.clearAllLogs();
  223. * @global true
  224. * @triggerForPropHackNone true
  225. * @for Skylink
  226. * @since 0.5.5
  227. */
  228. clearAllLogs: _clearAllStoredLogsFn,
  229.  
  230. /**
  231. * Function that prints all the current stored SDK <code>console</code> logs into the
  232. * <a href="https://developer.mozilla.org/en/docs/Web/API/console">Javascript Web Console</a>.
  233. * @property SkylinkLogs.printAllLogs
  234. * @type Function
  235. * @example
  236. * // Example 1: Print all the logs
  237. * SkylinkLogs.printAllLogs();
  238. * @global true
  239. * @triggerForPropHackNone true
  240. * @for Skylink
  241. * @since 0.5.5
  242. */
  243. printAllLogs: _printAllStoredLogsFn
  244. };
  245.  
  246. /**
  247. * Function that handles the logs received and prints in the Web Console interface according to the log level set.
  248. * @method _logFn
  249. * @private
  250. * @required
  251. * @scoped true
  252. * @for Skylink
  253. * @since 0.5.5
  254. */
  255. var _logFn = function(logLevel, message, debugObject) {
  256. var outputLog = _LOG_KEY;
  257.  
  258. if (typeof message === 'object') {
  259. outputLog += (message[0]) ? ' [' + message[0] + '] -' : ' -';
  260. outputLog += (message[1]) ? ' <<' + message[1] + '>>' : '';
  261. if (message[2]) {
  262. outputLog += ' ';
  263. if (typeof message[2] === 'object') {
  264. for (var i = 0; i < message[2].length; i++) {
  265. outputLog += '(' + message[2][i] + ')';
  266. }
  267. } else {
  268. outputLog += '(' + message[2] + ')';
  269. }
  270. }
  271. outputLog += ' ' + message[3];
  272. } else {
  273. outputLog += ' - ' + message;
  274. }
  275.  
  276. if (_enableDebugMode && _enableDebugStack) {
  277. // store the logs
  278. var logItem = [(new Date()), _LOG_LEVELS[logLevel], outputLog];
  279.  
  280. if (typeof debugObject !== 'undefined') {
  281. logItem.push(debugObject);
  282. }
  283. _storedLogs.push(logItem);
  284. }
  285.  
  286. if (_logLevel >= logLevel) {
  287. // Fallback to log if failure
  288. logLevel = (typeof console[_LOG_LEVELS[logLevel]] === 'undefined') ? 3 : logLevel;
  289.  
  290. if (_enableDebugMode && _enableDebugTrace) {
  291. var logConsole = (typeof console.trace === 'undefined') ? logLevel[3] : 'trace';
  292. if (typeof debugObject !== 'undefined') {
  293. console[_LOG_LEVELS[logLevel]](outputLog, debugObject);
  294. // output if supported
  295. if (typeof console.trace !== 'undefined') {
  296. console.trace('');
  297. }
  298. } else {
  299. console[_LOG_LEVELS[logLevel]](outputLog);
  300. // output if supported
  301. if (typeof console.trace !== 'undefined') {
  302. console.trace('');
  303. }
  304. }
  305. } else {
  306. if (typeof debugObject !== 'undefined') {
  307. console[_LOG_LEVELS[logLevel]](outputLog, debugObject);
  308. } else {
  309. console[_LOG_LEVELS[logLevel]](outputLog);
  310. }
  311. }
  312. }
  313. };
  314.  
  315. /**
  316. * Stores the logging functions.
  317. * @attribute log
  318. * @param {Function} debug The function that handles the DEBUG level logs.
  319. * @param {Function} log The function that handles the LOG level logs.
  320. * @param {Function} info The function that handles the INFO level logs.
  321. * @param {Function} warn The function that handles the WARN level logs.
  322. * @param {Function} error The function that handles the ERROR level logs.
  323. * @type JSON
  324. * @private
  325. * @scoped true
  326. * @for Skylink
  327. * @since 0.5.4
  328. */
  329. var log = {
  330. debug: function (message, object) {
  331. _logFn(4, message, object);
  332. },
  333.  
  334. log: function (message, object) {
  335. _logFn(3, message, object);
  336. },
  337.  
  338. info: function (message, object) {
  339. _logFn(2, message, object);
  340. },
  341.  
  342. warn: function (message, object) {
  343. _logFn(1, message, object);
  344. },
  345.  
  346. error: function (message, object) {
  347. _logFn(0, message, object);
  348. }
  349. };
  350.  
  351. /**
  352. * Function that configures the level of <code>console</code> API logs to be printed in the
  353. * <a href="https://developer.mozilla.org/en/docs/Web/API/console">Javascript Web Console</a>.
  354. * @method setLogLevel
  355. * @param {Number} [logLevel] The specific log level of logs to return.
  356. * - When not provided or that the level does not exists, it will not overwrite the current log level.
  357. * <small>By default, the initial log level is <code>ERROR</code>.</small>
  358. * [Rel: Skylink.LOG_LEVEL]
  359. * @example
  360. * // Example 1: Print all of the console.debug, console.log, console.info, console.warn and console.error logs.
  361. * skylinkDemo.setLogLevel(skylinkDemo.LOG_LEVEL.DEBUG);
  362. *
  363. * // Example 2: Print only the console.log, console.info, console.warn and console.error logs.
  364. * skylinkDemo.setLogLevel(skylinkDemo.LOG_LEVEL.LOG);
  365. *
  366. * // Example 3: Print only the console.info, console.warn and console.error logs.
  367. * skylinkDemo.setLogLevel(skylinkDemo.LOG_LEVEL.INFO);
  368. *
  369. * // Example 4: Print only the console.warn and console.error logs.
  370. * skylinkDemo.setLogLevel(skylinkDemo.LOG_LEVEL.WARN);
  371. *
  372. * // Example 5: Print only the console.error logs. This is done by default.
  373. * skylinkDemo.setLogLevel(skylinkDemo.LOG_LEVEL.ERROR);
  374. * @for Skylink
  375. * @since 0.5.5
  376. */
  377. Skylink.prototype.setLogLevel = function(logLevel) {
  378. if(logLevel === undefined) {
  379. logLevel = Skylink.LOG_LEVEL.WARN;
  380. }
  381. for (var level in this.LOG_LEVEL) {
  382. if (this.LOG_LEVEL[level] === logLevel) {
  383. _logLevel = logLevel;
  384. log.log([null, 'Log', level, 'Log level exists. Level is set']);
  385. return;
  386. }
  387. }
  388. log.error([null, 'Log', level, 'Log level does not exist. Level is not set']);
  389. };
  390.  
  391. /**
  392. * Function that configures the debugging mode of the SDK.
  393. * @method setDebugMode
  394. * @param {Boolean|JSON} [options=false] The debugging options.
  395. * - When provided as a boolean, this sets both <code>options.trace</code>
  396. * and <code>options.storeLogs</code> to its boolean value.
  397. * @param {Boolean} [options.trace=false] The flag if SDK <code>console</code> logs
  398. * should output as <code>console.trace()</code> logs for tracing the <code>Function</code> call stack.
  399. * <small>Note that the <code>console.trace()</code> output logs is determined by the log level set
  400. * <a href="#method_setLogLevel"><code>setLogLevel()</code> method</a>.</small>
  401. * <small>If <code>console.trace()</code> API is not supported, <code>setDebugMode()</code>
  402. * will fallback to use <code>console.log()</code> API.</small>
  403. * @param {Boolean} [options.storeLogs=false] The flag if SDK should store the <code>console</code> logs.
  404. * <small>This is required to be enabled for <a href="#prop_SkylinkLogs"><code>SkylinkLogs</code> API</a>.</small>
  405. * @example
  406. * // Example 1: Enable both options.storeLogs and options.trace
  407. * skylinkDemo.setDebugMode(true);
  408. *
  409. * // Example 2: Enable only options.storeLogs
  410. * skylinkDemo.setDebugMode({ storeLogs: true });
  411. *
  412. * // Example 3: Disable debugging mode
  413. * skylinkDemo.setDebugMode();
  414. * @for Skylink
  415. * @since 0.5.2
  416. */
  417. Skylink.prototype.setDebugMode = function(isDebugMode) {
  418. if (typeof isDebugMode === 'object') {
  419. if (Object.keys(isDebugMode).length > 0) {
  420. _enableDebugTrace = !!isDebugMode.trace;
  421. _enableDebugStack = !!isDebugMode.storeLogs;
  422. } else {
  423. _enableDebugMode = false;
  424. _enableDebugTrace = false;
  425. _enableDebugStack = false;
  426. }
  427. }
  428. if (isDebugMode === false) {
  429. _enableDebugMode = false;
  430. _enableDebugTrace = false;
  431. _enableDebugStack = false;
  432.  
  433. return;
  434. }
  435. _enableDebugMode = true;
  436. _enableDebugTrace = true;
  437. _enableDebugStack = true;
  438. };