Source: lib/debug/log.js

  1. /**
  2. * @license
  3. * Copyright 2016 Google Inc.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. goog.provide('shaka.log');
  18. /**
  19. * @namespace shaka.log
  20. * @summary
  21. * A console logging framework which is compiled out for deployment. This is
  22. * only available when using the uncompiled version.
  23. * @exportDoc
  24. */
  25. /**
  26. * Log levels.
  27. * @enum {number}
  28. * @exportDoc
  29. */
  30. shaka.log.Level = {
  31. NONE: 0,
  32. ERROR: 1,
  33. WARNING: 2,
  34. INFO: 3,
  35. DEBUG: 4,
  36. V1: 5,
  37. V2: 6
  38. };
  39. /**
  40. * @define {number} the maximum log level.
  41. */
  42. goog.define('shaka.log.MAX_LOG_LEVEL', 3);
  43. /**
  44. * This always logs to the console, even in Release mode. This should only be
  45. * used for deprecation messages and things the app should never ignore.
  46. *
  47. * @type {function(*, ...*)}
  48. */
  49. shaka.log.alwaysError = function() {};
  50. /**
  51. * This always logs to the console, even in Release mode. This should only be
  52. * used for deprecation messages and things the app should never ignore.
  53. *
  54. * @type {function(*, ...*)}
  55. */
  56. shaka.log.alwaysWarn = function() {};
  57. /**
  58. * This log is for when an error occurs. This should always be accompanied with
  59. * an error event, thrown exception, or rejected Promise. Logs are disabled in
  60. * Release mode, so there should be other methods of detecting the error.
  61. *
  62. * @type {function(*, ...*)}
  63. */
  64. shaka.log.error = function() {};
  65. /**
  66. * This log is for possible errors or things that may be surprising to a user.
  67. * For example, if we work around unusual or bad content, we should warn that
  68. * they should fix their content. Deprecation messages and messages the app
  69. * shouldn't ignore should use alwaysWarn instead.
  70. *
  71. * @type {function(*, ...*)}
  72. */
  73. shaka.log.warning = function() {};
  74. /**
  75. * This log is for messages to the user about what is happening. For example,
  76. * when we update a manifest or install a polyfill.
  77. *
  78. * @type {function(*, ...*)}
  79. */
  80. shaka.log.info = function() {};
  81. /**
  82. * This log is to aid *users* in debugging their content. This should be for
  83. * logs about the content and what we do with it. For example, when we change
  84. * streams or what we are choosing.
  85. *
  86. * @type {function(*, ...*)}
  87. */
  88. shaka.log.debug = function() {};
  89. /**
  90. * This log is for debugging Shaka Player itself. This may be logs about
  91. * internal states or events. This may also be for more verbose logs about
  92. * content, such as for segment appends.
  93. *
  94. * @type {function(*, ...*)}
  95. */
  96. shaka.log.v1 = function() {};
  97. /**
  98. * This log is for tracing and debugging Shaka Player. These logs will happen
  99. * a lot, for example, logging every segment append or every update check.
  100. * These are mostly used for tracking which calls happen through the code.
  101. *
  102. * @type {function(*, ...*)}
  103. */
  104. shaka.log.v2 = function() {};
  105. // IE8 has no console unless it is opened in advance.
  106. // IE9 console methods are not Functions and have no bind.
  107. if (window.console && window.console.log.bind) {
  108. shaka.log.alwaysWarn = console.warn.bind(console);
  109. shaka.log.alwaysError = console.error.bind(console);
  110. if (goog.DEBUG) {
  111. /** @type {number} */
  112. shaka.log.currentLevel;
  113. /**
  114. * Change the log level. Useful for debugging in uncompiled mode.
  115. *
  116. * @param {number} level
  117. * @exportDoc
  118. */
  119. shaka.log.setLevel = function(level) {
  120. let nop = function() {};
  121. let log = shaka.log;
  122. const Level = shaka.log.Level;
  123. shaka.log.currentLevel = level;
  124. log.error = (level >= Level.ERROR) ? console.error.bind(console) : nop;
  125. log.warning = (level >= Level.WARNING) ? console.warn.bind(console) : nop;
  126. log.info = (level >= Level.INFO) ? console.info.bind(console) : nop;
  127. log.debug = (level >= Level.DEBUG) ? console.log.bind(console) : nop;
  128. log.v1 = (level >= Level.V1) ? console.debug.bind(console) : nop;
  129. log.v2 = (level >= Level.V2) ? console.debug.bind(console) : nop;
  130. };
  131. shaka.log.setLevel(shaka.log.MAX_LOG_LEVEL);
  132. } else {
  133. if (shaka.log.MAX_LOG_LEVEL >= shaka.log.Level.ERROR) {
  134. shaka.log.error = console.error.bind(console);
  135. }
  136. if (shaka.log.MAX_LOG_LEVEL >= shaka.log.Level.WARNING) {
  137. shaka.log.warning = console.warn.bind(console);
  138. }
  139. if (shaka.log.MAX_LOG_LEVEL >= shaka.log.Level.INFO) {
  140. shaka.log.info = console.info.bind(console);
  141. }
  142. if (shaka.log.MAX_LOG_LEVEL >= shaka.log.Level.DEBUG) {
  143. shaka.log.debug = console.log.bind(console);
  144. }
  145. if (shaka.log.MAX_LOG_LEVEL >= shaka.log.Level.V1) {
  146. shaka.log.v1 = console.debug.bind(console);
  147. }
  148. if (shaka.log.MAX_LOG_LEVEL >= shaka.log.Level.V2) {
  149. shaka.log.v2 = console.debug.bind(console);
  150. }
  151. }
  152. }