Source: lib/debug/log.js

  1. /*! @license
  2. * Shaka Player
  3. * Copyright 2016 Google LLC
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. goog.provide('shaka.log');
  7. goog.require('goog.asserts');
  8. /**
  9. * @summary
  10. * A console logging framework which is compiled out for deployment. This is
  11. * only available when using the uncompiled version.
  12. * @exportDoc
  13. */
  14. shaka.log = class {
  15. /**
  16. * This always logs to the console, even in Release mode. This should only be
  17. * used for deprecation messages and things the app should never ignore.
  18. *
  19. * @param {...*} args
  20. */
  21. static alwaysError(...args) {}
  22. /**
  23. * This always logs to the console, even in Release mode. This should only be
  24. * used for deprecation messages and things the app should never ignore.
  25. *
  26. * @param {...*} args
  27. */
  28. static alwaysWarn(...args) {}
  29. /**
  30. * This always logs to the console, even in Release mode. This should only be
  31. * used for deprecation messages and things the app should never ignore.
  32. *
  33. * @param {string} id
  34. * @param {...*} args
  35. */
  36. static warnOnce(id, ...args) {
  37. if (shaka.log.oneTimeWarningIssued_.has(id)) {
  38. return;
  39. }
  40. shaka.log.oneTimeWarningIssued_.add(id);
  41. shaka.log.alwaysWarn(...args);
  42. }
  43. /**
  44. * This log is for when an error occurs. This should always be accompanied
  45. * with an error event, thrown exception, or rejected Promise. Logs are
  46. * disabled in Release mode, so there should be other methods of detecting the
  47. * error.
  48. *
  49. * @param {...*} args
  50. */
  51. static error(...args) {}
  52. /**
  53. * This log is for possible errors or things that may be surprising to a user.
  54. * For example, if we work around unusual or bad content, we should warn that
  55. * they should fix their content. Deprecation messages and messages the app
  56. * shouldn't ignore should use alwaysWarn instead.
  57. *
  58. * @param {...*} args
  59. */
  60. static warning(...args) {}
  61. /**
  62. * This log is for messages to the user about what is happening. For example,
  63. * when we update a manifest or install a polyfill.
  64. *
  65. * @param {...*} args
  66. */
  67. static info(...args) {}
  68. /**
  69. * This log is to aid *users* in debugging their content. This should be for
  70. * logs about the content and what we do with it. For example, when we change
  71. * streams or what we are choosing.
  72. *
  73. * @param {...*} args
  74. */
  75. static debug(...args) {}
  76. /**
  77. * This log is for debugging Shaka Player itself. This may be logs about
  78. * internal states or events. This may also be for more verbose logs about
  79. * content, such as for segment appends.
  80. *
  81. * @param {...*} args
  82. */
  83. static v1(...args) {}
  84. /**
  85. * This log is for tracing and debugging Shaka Player. These logs will happen
  86. * a lot, for example, logging every segment append or every update check.
  87. * These are mostly used for tracking which calls happen through the code.
  88. *
  89. * @param {...*} args
  90. */
  91. static v2(...args) {}
  92. };
  93. /**
  94. * Log levels.
  95. * @enum {number}
  96. * @exportDoc
  97. */
  98. shaka.log.Level = {
  99. NONE: 0,
  100. ERROR: 1,
  101. WARNING: 2,
  102. INFO: 3,
  103. DEBUG: 4,
  104. V1: 5,
  105. V2: 6,
  106. };
  107. /**
  108. * @define {number} the maximum log level.
  109. */
  110. shaka.log.MAX_LOG_LEVEL = 3;
  111. /**
  112. * A Set to indicate which one-time warnings have been issued.
  113. *
  114. * @private {!Set.<string>}
  115. */
  116. shaka.log.oneTimeWarningIssued_ = new Set();
  117. // IE8 has no console unless it is opened in advance.
  118. // IE9 console methods are not Functions and have no bind.
  119. if (window.console && window.console.log.bind) {
  120. /** @private {!Object.<shaka.log.Level, function(...*)>} */
  121. shaka.log.logMap_ = {
  122. /* eslint-disable no-restricted-syntax */
  123. [shaka.log.Level.ERROR]: console.error.bind(console),
  124. [shaka.log.Level.WARNING]: console.warn.bind(console),
  125. [shaka.log.Level.INFO]: console.info.bind(console),
  126. [shaka.log.Level.DEBUG]: console.log.bind(console),
  127. [shaka.log.Level.V1]: console.debug.bind(console),
  128. [shaka.log.Level.V2]: console.debug.bind(console),
  129. /* eslint-enable no-restricted-syntax */
  130. };
  131. shaka.log.alwaysWarn = shaka.log.logMap_[shaka.log.Level.WARNING];
  132. shaka.log.alwaysError = shaka.log.logMap_[shaka.log.Level.ERROR];
  133. if (goog.DEBUG) {
  134. // Since we don't want to export shaka.log in production builds, we don't
  135. // use the @export annotation. But the module wrapper (used in debug builds
  136. // since v2.5.11) hides anything non-exported. This is a debug-only,
  137. // API-based export to make sure logging is available in debug builds.
  138. goog.exportSymbol('shaka.log', shaka.log);
  139. /** @type {number} */
  140. shaka.log.currentLevel;
  141. /**
  142. * Change the log level. Useful for debugging in uncompiled mode.
  143. *
  144. * @param {number} level
  145. * @exportDoc
  146. */
  147. shaka.log.setLevel = (level) => {
  148. const getLog = (curLevel) => {
  149. if (curLevel <= level) {
  150. goog.asserts.assert(
  151. shaka.log.logMap_[curLevel], 'Unexpected log level');
  152. return shaka.log.logMap_[curLevel];
  153. } else {
  154. return () => {};
  155. }
  156. };
  157. shaka.log.currentLevel = level;
  158. shaka.log.error = getLog(shaka.log.Level.ERROR);
  159. shaka.log.warning = getLog(shaka.log.Level.WARNING);
  160. shaka.log.info = getLog(shaka.log.Level.INFO);
  161. shaka.log.debug = getLog(shaka.log.Level.DEBUG);
  162. shaka.log.v1 = getLog(shaka.log.Level.V1);
  163. shaka.log.v2 = getLog(shaka.log.Level.V2);
  164. };
  165. shaka.log.setLevel(shaka.log.MAX_LOG_LEVEL);
  166. } else {
  167. if (shaka.log.MAX_LOG_LEVEL >= shaka.log.Level.ERROR) {
  168. shaka.log.error = shaka.log.logMap_[shaka.log.Level.ERROR];
  169. }
  170. if (shaka.log.MAX_LOG_LEVEL >= shaka.log.Level.WARNING) {
  171. shaka.log.warning = shaka.log.logMap_[shaka.log.Level.WARNING];
  172. }
  173. if (shaka.log.MAX_LOG_LEVEL >= shaka.log.Level.INFO) {
  174. shaka.log.info = shaka.log.logMap_[shaka.log.Level.INFO];
  175. }
  176. if (shaka.log.MAX_LOG_LEVEL >= shaka.log.Level.DEBUG) {
  177. shaka.log.debug = shaka.log.logMap_[shaka.log.Level.DEBUG];
  178. }
  179. if (shaka.log.MAX_LOG_LEVEL >= shaka.log.Level.V1) {
  180. shaka.log.v1 = shaka.log.logMap_[shaka.log.Level.V1];
  181. }
  182. if (shaka.log.MAX_LOG_LEVEL >= shaka.log.Level.V2) {
  183. shaka.log.v2 = shaka.log.logMap_[shaka.log.Level.V2];
  184. }
  185. }
  186. }