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