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. if (window.console) {
  118. /** @private {!Object.<shaka.log.Level, function(...*)>} */
  119. shaka.log.logMap_ = {
  120. [shaka.log.Level.ERROR]: (...args) => console.error(...args),
  121. [shaka.log.Level.WARNING]: (...args) => console.warn(...args),
  122. [shaka.log.Level.INFO]: (...args) => console.info(...args),
  123. [shaka.log.Level.DEBUG]: (...args) => console.log(...args),
  124. [shaka.log.Level.V1]: (...args) => console.debug(...args),
  125. [shaka.log.Level.V2]: (...args) => console.debug(...args),
  126. };
  127. shaka.log.alwaysWarn = (...args) => console.warn(...args);
  128. shaka.log.alwaysError = (...args) => console.error(...args);
  129. if (goog.DEBUG) {
  130. // Since we don't want to export shaka.log in production builds, we don't
  131. // use the @export annotation. But the module wrapper (used in debug builds
  132. // since v2.5.11) hides anything non-exported. This is a debug-only,
  133. // API-based export to make sure logging is available in debug builds.
  134. goog.exportSymbol('shaka.log', shaka.log);
  135. /** @type {number} */
  136. shaka.log.currentLevel;
  137. /**
  138. * Change the log level. Useful for debugging in uncompiled mode.
  139. *
  140. * @param {number} level
  141. * @exportDoc
  142. */
  143. shaka.log.setLevel = (level) => {
  144. const getLog = (curLevel) => {
  145. if (curLevel <= level) {
  146. goog.asserts.assert(
  147. shaka.log.logMap_[curLevel], 'Unexpected log level');
  148. return shaka.log.logMap_[curLevel];
  149. } else {
  150. return () => {};
  151. }
  152. };
  153. shaka.log.currentLevel = level;
  154. shaka.log.error = getLog(shaka.log.Level.ERROR);
  155. shaka.log.warning = getLog(shaka.log.Level.WARNING);
  156. shaka.log.info = getLog(shaka.log.Level.INFO);
  157. shaka.log.debug = getLog(shaka.log.Level.DEBUG);
  158. shaka.log.v1 = getLog(shaka.log.Level.V1);
  159. shaka.log.v2 = getLog(shaka.log.Level.V2);
  160. };
  161. shaka.log.setLevel(shaka.log.MAX_LOG_LEVEL);
  162. } else {
  163. if (shaka.log.MAX_LOG_LEVEL >= shaka.log.Level.ERROR) {
  164. shaka.log.error = shaka.log.logMap_[shaka.log.Level.ERROR];
  165. }
  166. if (shaka.log.MAX_LOG_LEVEL >= shaka.log.Level.WARNING) {
  167. shaka.log.warning = shaka.log.logMap_[shaka.log.Level.WARNING];
  168. }
  169. if (shaka.log.MAX_LOG_LEVEL >= shaka.log.Level.INFO) {
  170. shaka.log.info = shaka.log.logMap_[shaka.log.Level.INFO];
  171. }
  172. if (shaka.log.MAX_LOG_LEVEL >= shaka.log.Level.DEBUG) {
  173. shaka.log.debug = shaka.log.logMap_[shaka.log.Level.DEBUG];
  174. }
  175. if (shaka.log.MAX_LOG_LEVEL >= shaka.log.Level.V1) {
  176. shaka.log.v1 = shaka.log.logMap_[shaka.log.Level.V1];
  177. }
  178. if (shaka.log.MAX_LOG_LEVEL >= shaka.log.Level.V2) {
  179. shaka.log.v2 = shaka.log.logMap_[shaka.log.Level.V2];
  180. }
  181. }
  182. }