PageRenderTime 23ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/platforms/ios/www/plugins/cordova-plugin-console/www/console-via-logger.js

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