console-via-logger.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*
  2. *
  3. * Licensed to the Apache Software Foundation (ASF) under one
  4. * or more contributor license agreements. See the NOTICE file
  5. * distributed with this work for additional information
  6. * regarding copyright ownership. The ASF licenses this file
  7. * to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance
  9. * with the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing,
  14. * software distributed under the License is distributed on an
  15. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16. * KIND, either express or implied. See the License for the
  17. * specific language governing permissions and limitations
  18. * under the License.
  19. *
  20. */
  21. //------------------------------------------------------------------------------
  22. var logger = require("./logger");
  23. //------------------------------------------------------------------------------
  24. // object that we're exporting
  25. //------------------------------------------------------------------------------
  26. var console = module.exports;
  27. //------------------------------------------------------------------------------
  28. // copy of the original console object
  29. //------------------------------------------------------------------------------
  30. var WinConsole = window.console;
  31. //------------------------------------------------------------------------------
  32. // whether to use the logger
  33. //------------------------------------------------------------------------------
  34. var UseLogger = false;
  35. //------------------------------------------------------------------------------
  36. // Timers
  37. //------------------------------------------------------------------------------
  38. var Timers = {};
  39. //------------------------------------------------------------------------------
  40. // used for unimplemented methods
  41. //------------------------------------------------------------------------------
  42. function noop() {}
  43. //------------------------------------------------------------------------------
  44. // used for unimplemented methods
  45. //------------------------------------------------------------------------------
  46. console.useLogger = function (value) {
  47. if (arguments.length) UseLogger = !!value;
  48. if (UseLogger) {
  49. if (logger.useConsole()) {
  50. throw new Error("console and logger are too intertwingly");
  51. }
  52. }
  53. return UseLogger;
  54. };
  55. //------------------------------------------------------------------------------
  56. console.log = function() {
  57. if (logger.useConsole()) return;
  58. logger.log.apply(logger, [].slice.call(arguments));
  59. };
  60. //------------------------------------------------------------------------------
  61. console.error = function() {
  62. if (logger.useConsole()) return;
  63. logger.error.apply(logger, [].slice.call(arguments));
  64. };
  65. //------------------------------------------------------------------------------
  66. console.warn = function() {
  67. if (logger.useConsole()) return;
  68. logger.warn.apply(logger, [].slice.call(arguments));
  69. };
  70. //------------------------------------------------------------------------------
  71. console.info = function() {
  72. if (logger.useConsole()) return;
  73. logger.info.apply(logger, [].slice.call(arguments));
  74. };
  75. //------------------------------------------------------------------------------
  76. console.debug = function() {
  77. if (logger.useConsole()) return;
  78. logger.debug.apply(logger, [].slice.call(arguments));
  79. };
  80. //------------------------------------------------------------------------------
  81. console.assert = function(expression) {
  82. if (expression) return;
  83. var message = logger.format.apply(logger.format, [].slice.call(arguments, 1));
  84. console.log("ASSERT: " + message);
  85. };
  86. //------------------------------------------------------------------------------
  87. console.clear = function() {};
  88. //------------------------------------------------------------------------------
  89. console.dir = function(object) {
  90. console.log("%o", object);
  91. };
  92. //------------------------------------------------------------------------------
  93. console.dirxml = function(node) {
  94. console.log(node.innerHTML);
  95. };
  96. //------------------------------------------------------------------------------
  97. console.trace = noop;
  98. //------------------------------------------------------------------------------
  99. console.group = console.log;
  100. //------------------------------------------------------------------------------
  101. console.groupCollapsed = console.log;
  102. //------------------------------------------------------------------------------
  103. console.groupEnd = noop;
  104. //------------------------------------------------------------------------------
  105. console.time = function(name) {
  106. Timers[name] = new Date().valueOf();
  107. };
  108. //------------------------------------------------------------------------------
  109. console.timeEnd = function(name) {
  110. var timeStart = Timers[name];
  111. if (!timeStart) {
  112. console.warn("unknown timer: " + name);
  113. return;
  114. }
  115. var timeElapsed = new Date().valueOf() - timeStart;
  116. console.log(name + ": " + timeElapsed + "ms");
  117. };
  118. //------------------------------------------------------------------------------
  119. console.timeStamp = noop;
  120. //------------------------------------------------------------------------------
  121. console.profile = noop;
  122. //------------------------------------------------------------------------------
  123. console.profileEnd = noop;
  124. //------------------------------------------------------------------------------
  125. console.count = noop;
  126. //------------------------------------------------------------------------------
  127. console.exception = console.log;
  128. //------------------------------------------------------------------------------
  129. console.table = function(data, columns) {
  130. console.log("%o", data);
  131. };
  132. //------------------------------------------------------------------------------
  133. // return a new function that calls both functions passed as args
  134. //------------------------------------------------------------------------------
  135. function wrappedOrigCall(orgFunc, newFunc) {
  136. return function() {
  137. var args = [].slice.call(arguments);
  138. try { orgFunc.apply(WinConsole, args); } catch (e) {}
  139. try { newFunc.apply(console, args); } catch (e) {}
  140. };
  141. }
  142. //------------------------------------------------------------------------------
  143. // For every function that exists in the original console object, that
  144. // also exists in the new console object, wrap the new console method
  145. // with one that calls both
  146. //------------------------------------------------------------------------------
  147. for (var key in console) {
  148. if (typeof WinConsole[key] == "function") {
  149. console[key] = wrappedOrigCall(WinConsole[key], console[key]);
  150. }
  151. }