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. /** @type {function(*, ...*)} */
  44. shaka.log.error = function() {};
  45. /** @type {function(*, ...*)} */
  46. shaka.log.warning = function() {};
  47. /** @type {function(*, ...*)} */
  48. shaka.log.info = function() {};
  49. /** @type {function(*, ...*)} */
  50. shaka.log.debug = function() {};
  51. /** @type {function(*, ...*)} */
  52. shaka.log.v1 = function() {};
  53. /** @type {function(*, ...*)} */
  54. shaka.log.v2 = function() {};
  55. // IE8 has no console unless it is opened in advance.
  56. // IE9 console methods are not Functions and have no bind.
  57. if (window.console && window.console.log.bind) {
  58. if (!COMPILED) {
  59. /** @type {number} */
  60. shaka.log.currentLevel;
  61. /**
  62. * Change the log level. Useful for debugging in uncompiled mode.
  63. *
  64. * @param {number} level
  65. * @exportDoc
  66. */
  67. shaka.log.setLevel = function(level) {
  68. var nop = function() {};
  69. var log = shaka.log;
  70. var Level = shaka.log.Level;
  71. shaka.log.currentLevel = level;
  72. log.error = (level >= Level.ERROR) ? console.error.bind(console) : nop;
  73. log.warning = (level >= Level.WARNING) ? console.warn.bind(console) : nop;
  74. log.info = (level >= Level.INFO) ? console.info.bind(console) : nop;
  75. log.debug = (level >= Level.DEBUG) ? console.log.bind(console) : nop;
  76. log.v1 = (level >= Level.V1) ? console.debug.bind(console) : nop;
  77. log.v2 = (level >= Level.V2) ? console.debug.bind(console) : nop;
  78. };
  79. shaka.log.setLevel(shaka.log.MAX_LOG_LEVEL);
  80. } else {
  81. if (shaka.log.MAX_LOG_LEVEL >= shaka.log.Level.ERROR) {
  82. shaka.log.error = console.error.bind(console);
  83. }
  84. if (shaka.log.MAX_LOG_LEVEL >= shaka.log.Level.WARNING) {
  85. shaka.log.warning = console.warn.bind(console);
  86. }
  87. if (shaka.log.MAX_LOG_LEVEL >= shaka.log.Level.INFO) {
  88. shaka.log.info = console.info.bind(console);
  89. }
  90. if (shaka.log.MAX_LOG_LEVEL >= shaka.log.Level.DEBUG) {
  91. shaka.log.debug = console.log.bind(console);
  92. }
  93. if (shaka.log.MAX_LOG_LEVEL >= shaka.log.Level.V1) {
  94. shaka.log.v1 = console.debug.bind(console);
  95. }
  96. if (shaka.log.MAX_LOG_LEVEL >= shaka.log.Level.V2) {
  97. shaka.log.v2 = console.debug.bind(console);
  98. }
  99. }
  100. }