1 cordova.define("cordova-plugin-console.console", function(require, exports, module) { /*
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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
22 //------------------------------------------------------------------------------
24 var logger = require("./logger");
25 var utils = require("cordova/utils");
27 //------------------------------------------------------------------------------
28 // object that we're exporting
29 //------------------------------------------------------------------------------
30 var console = module.exports;
32 //------------------------------------------------------------------------------
33 // copy of the original console object
34 //------------------------------------------------------------------------------
35 var WinConsole = window.console;
37 //------------------------------------------------------------------------------
38 // whether to use the logger
39 //------------------------------------------------------------------------------
40 var UseLogger = false;
42 //------------------------------------------------------------------------------
44 //------------------------------------------------------------------------------
47 //------------------------------------------------------------------------------
48 // used for unimplemented methods
49 //------------------------------------------------------------------------------
52 //------------------------------------------------------------------------------
53 // used for unimplemented methods
54 //------------------------------------------------------------------------------
55 console.useLogger = function (value) {
56 if (arguments.length) UseLogger = !!value;
59 if (logger.useConsole()) {
60 throw new Error("console and logger are too intertwingly");
67 //------------------------------------------------------------------------------
68 console.log = function() {
69 if (logger.useConsole()) return;
70 logger.log.apply(logger, [].slice.call(arguments));
73 //------------------------------------------------------------------------------
74 console.error = function() {
75 if (logger.useConsole()) return;
76 logger.error.apply(logger, [].slice.call(arguments));
79 //------------------------------------------------------------------------------
80 console.warn = function() {
81 if (logger.useConsole()) return;
82 logger.warn.apply(logger, [].slice.call(arguments));
85 //------------------------------------------------------------------------------
86 console.info = function() {
87 if (logger.useConsole()) return;
88 logger.info.apply(logger, [].slice.call(arguments));
91 //------------------------------------------------------------------------------
92 console.debug = function() {
93 if (logger.useConsole()) return;
94 logger.debug.apply(logger, [].slice.call(arguments));
97 //------------------------------------------------------------------------------
98 console.assert = function(expression) {
99 if (expression) return;
101 var message = logger.format.apply(logger.format, [].slice.call(arguments, 1));
102 console.log("ASSERT: " + message);
105 //------------------------------------------------------------------------------
106 console.clear = function() {};
108 //------------------------------------------------------------------------------
109 console.dir = function(object) {
110 console.log("%o", object);
113 //------------------------------------------------------------------------------
114 console.dirxml = function(node) {
115 console.log(node.innerHTML);
118 //------------------------------------------------------------------------------
119 console.trace = noop;
121 //------------------------------------------------------------------------------
122 console.group = console.log;
124 //------------------------------------------------------------------------------
125 console.groupCollapsed = console.log;
127 //------------------------------------------------------------------------------
128 console.groupEnd = noop;
130 //------------------------------------------------------------------------------
131 console.time = function(name) {
132 Timers[name] = new Date().valueOf();
135 //------------------------------------------------------------------------------
136 console.timeEnd = function(name) {
137 var timeStart = Timers[name];
139 console.warn("unknown timer: " + name);
143 var timeElapsed = new Date().valueOf() - timeStart;
144 console.log(name + ": " + timeElapsed + "ms");
147 //------------------------------------------------------------------------------
148 console.timeStamp = noop;
150 //------------------------------------------------------------------------------
151 console.profile = noop;
153 //------------------------------------------------------------------------------
154 console.profileEnd = noop;
156 //------------------------------------------------------------------------------
157 console.count = noop;
159 //------------------------------------------------------------------------------
160 console.exception = console.log;
162 //------------------------------------------------------------------------------
163 console.table = function(data, columns) {
164 console.log("%o", data);
167 //------------------------------------------------------------------------------
168 // return a new function that calls both functions passed as args
169 //------------------------------------------------------------------------------
170 function wrappedOrigCall(orgFunc, newFunc) {
172 var args = [].slice.call(arguments);
173 try { orgFunc.apply(WinConsole, args); } catch (e) {}
174 try { newFunc.apply(console, args); } catch (e) {}
178 //------------------------------------------------------------------------------
179 // For every function that exists in the original console object, that
180 // also exists in the new console object, wrap the new console method
181 // with one that calls both
182 //------------------------------------------------------------------------------
183 for (var key in console) {
184 if (typeof WinConsole[key] == "function") {
185 console[key] = wrappedOrigCall(WinConsole[key], console[key]);