logger.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * Copyright (C) 2016 Bilibili. All Rights Reserved.
  3. *
  4. * @author zheng qian <xqq@xqq.im>
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. import EventEmitter from 'events';
  19. class Log {
  20. static e(tag, msg) {
  21. if (!tag || Log.FORCE_GLOBAL_TAG)
  22. tag = Log.GLOBAL_TAG;
  23. let str = `[${tag}] > ${msg}`;
  24. if (Log.ENABLE_CALLBACK) {
  25. Log.emitter.emit('log', 'error', str);
  26. }
  27. if (!Log.ENABLE_ERROR) {
  28. return;
  29. }
  30. if (console.error) {
  31. console.error(str);
  32. } else if (console.warn) {
  33. console.warn(str);
  34. } else {
  35. console.log(str);
  36. }
  37. }
  38. static i(tag, msg) {
  39. if (!tag || Log.FORCE_GLOBAL_TAG)
  40. tag = Log.GLOBAL_TAG;
  41. let str = `[${tag}] > ${msg}`;
  42. if (Log.ENABLE_CALLBACK) {
  43. Log.emitter.emit('log', 'info', str);
  44. }
  45. if (!Log.ENABLE_INFO) {
  46. return;
  47. }
  48. if (console.info) {
  49. console.info(str);
  50. } else {
  51. console.log(str);
  52. }
  53. }
  54. static w(tag, msg) {
  55. if (!tag || Log.FORCE_GLOBAL_TAG)
  56. tag = Log.GLOBAL_TAG;
  57. let str = `[${tag}] > ${msg}`;
  58. if (Log.ENABLE_CALLBACK) {
  59. Log.emitter.emit('log', 'warn', str);
  60. }
  61. if (!Log.ENABLE_WARN) {
  62. return;
  63. }
  64. if (console.warn) {
  65. console.warn(str);
  66. } else {
  67. console.log(str);
  68. }
  69. }
  70. static d(tag, msg) {
  71. if (!tag || Log.FORCE_GLOBAL_TAG)
  72. tag = Log.GLOBAL_TAG;
  73. let str = `[${tag}] > ${msg}`;
  74. if (Log.ENABLE_CALLBACK) {
  75. Log.emitter.emit('log', 'debug', str);
  76. }
  77. if (!Log.ENABLE_DEBUG) {
  78. return;
  79. }
  80. if (console.debug) {
  81. console.debug(str);
  82. } else {
  83. console.log(str);
  84. }
  85. }
  86. static v(tag, msg) {
  87. if (!tag || Log.FORCE_GLOBAL_TAG)
  88. tag = Log.GLOBAL_TAG;
  89. let str = `[${tag}] > ${msg}`;
  90. if (Log.ENABLE_CALLBACK) {
  91. Log.emitter.emit('log', 'verbose', str);
  92. }
  93. if (!Log.ENABLE_VERBOSE) {
  94. return;
  95. }
  96. console.log(str);
  97. }
  98. }
  99. Log.GLOBAL_TAG = 'flv.js';
  100. Log.FORCE_GLOBAL_TAG = false;
  101. Log.ENABLE_ERROR = true;
  102. Log.ENABLE_INFO = true;
  103. Log.ENABLE_WARN = true;
  104. Log.ENABLE_DEBUG = true;
  105. Log.ENABLE_VERBOSE = true;
  106. Log.ENABLE_CALLBACK = false;
  107. Log.emitter = new EventEmitter();
  108. export default Log;