0ce8cea89cebe32cae8392217a1f39f14e188d6a
[test/tct/web/api.git] /
1 cordova.define("cordova-plugin-console.console", function(require, exports, module) { /*
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 //------------------------------------------------------------------------------
23
24 var logger = require("./logger");
25 var utils  = require("cordova/utils");
26
27 //------------------------------------------------------------------------------
28 // object that we're exporting
29 //------------------------------------------------------------------------------
30 var console = module.exports;
31
32 //------------------------------------------------------------------------------
33 // copy of the original console object
34 //------------------------------------------------------------------------------
35 var WinConsole = window.console;
36
37 //------------------------------------------------------------------------------
38 // whether to use the logger
39 //------------------------------------------------------------------------------
40 var UseLogger = false;
41
42 //------------------------------------------------------------------------------
43 // Timers
44 //------------------------------------------------------------------------------
45 var Timers = {};
46
47 //------------------------------------------------------------------------------
48 // used for unimplemented methods
49 //------------------------------------------------------------------------------
50 function noop() {}
51
52 //------------------------------------------------------------------------------
53 // used for unimplemented methods
54 //------------------------------------------------------------------------------
55 console.useLogger = function (value) {
56     if (arguments.length) UseLogger = !!value;
57
58     if (UseLogger) {
59         if (logger.useConsole()) {
60             throw new Error("console and logger are too intertwingly");
61         }
62     }
63
64     return UseLogger;
65 };
66
67 //------------------------------------------------------------------------------
68 console.log = function() {
69     if (logger.useConsole()) return;
70     logger.log.apply(logger, [].slice.call(arguments));
71 };
72
73 //------------------------------------------------------------------------------
74 console.error = function() {
75     if (logger.useConsole()) return;
76     logger.error.apply(logger, [].slice.call(arguments));
77 };
78
79 //------------------------------------------------------------------------------
80 console.warn = function() {
81     if (logger.useConsole()) return;
82     logger.warn.apply(logger, [].slice.call(arguments));
83 };
84
85 //------------------------------------------------------------------------------
86 console.info = function() {
87     if (logger.useConsole()) return;
88     logger.info.apply(logger, [].slice.call(arguments));
89 };
90
91 //------------------------------------------------------------------------------
92 console.debug = function() {
93     if (logger.useConsole()) return;
94     logger.debug.apply(logger, [].slice.call(arguments));
95 };
96
97 //------------------------------------------------------------------------------
98 console.assert = function(expression) {
99     if (expression) return;
100
101     var message = logger.format.apply(logger.format, [].slice.call(arguments, 1));
102     console.log("ASSERT: " + message);
103 };
104
105 //------------------------------------------------------------------------------
106 console.clear = function() {};
107
108 //------------------------------------------------------------------------------
109 console.dir = function(object) {
110     console.log("%o", object);
111 };
112
113 //------------------------------------------------------------------------------
114 console.dirxml = function(node) {
115     console.log(node.innerHTML);
116 };
117
118 //------------------------------------------------------------------------------
119 console.trace = noop;
120
121 //------------------------------------------------------------------------------
122 console.group = console.log;
123
124 //------------------------------------------------------------------------------
125 console.groupCollapsed = console.log;
126
127 //------------------------------------------------------------------------------
128 console.groupEnd = noop;
129
130 //------------------------------------------------------------------------------
131 console.time = function(name) {
132     Timers[name] = new Date().valueOf();
133 };
134
135 //------------------------------------------------------------------------------
136 console.timeEnd = function(name) {
137     var timeStart = Timers[name];
138     if (!timeStart) {
139         console.warn("unknown timer: " + name);
140         return;
141     }
142
143     var timeElapsed = new Date().valueOf() - timeStart;
144     console.log(name + ": " + timeElapsed + "ms");
145 };
146
147 //------------------------------------------------------------------------------
148 console.timeStamp = noop;
149
150 //------------------------------------------------------------------------------
151 console.profile = noop;
152
153 //------------------------------------------------------------------------------
154 console.profileEnd = noop;
155
156 //------------------------------------------------------------------------------
157 console.count = noop;
158
159 //------------------------------------------------------------------------------
160 console.exception = console.log;
161
162 //------------------------------------------------------------------------------
163 console.table = function(data, columns) {
164     console.log("%o", data);
165 };
166
167 //------------------------------------------------------------------------------
168 // return a new function that calls both functions passed as args
169 //------------------------------------------------------------------------------
170 function wrappedOrigCall(orgFunc, newFunc) {
171     return function() {
172         var args = [].slice.call(arguments);
173         try { orgFunc.apply(WinConsole, args); } catch (e) {}
174         try { newFunc.apply(console,    args); } catch (e) {}
175     };
176 }
177
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]);
186     }
187 }
188
189 });