aiber-martin/www/cordova-js-src/plugin/ios/console.js
Martin Donnelly a4b8625d0f init
2020-12-17 16:23:12 +00:00

187 lines
6.7 KiB
JavaScript

/*
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
*/
// ------------------------------------------------------------------------------
var logger = require('cordova/plugin/ios/logger');
// ------------------------------------------------------------------------------
// object that we're exporting
// ------------------------------------------------------------------------------
var console = module.exports;
// ------------------------------------------------------------------------------
// copy of the original console object
// ------------------------------------------------------------------------------
var WinConsole = window.console;
// ------------------------------------------------------------------------------
// whether to use the logger
// ------------------------------------------------------------------------------
var UseLogger = false;
// ------------------------------------------------------------------------------
// Timers
// ------------------------------------------------------------------------------
var Timers = {};
// ------------------------------------------------------------------------------
// used for unimplemented methods
// ------------------------------------------------------------------------------
function noop () {}
// ------------------------------------------------------------------------------
// used for unimplemented methods
// ------------------------------------------------------------------------------
console.useLogger = function (value) {
if (arguments.length) UseLogger = !!value;
if (UseLogger) {
if (logger.useConsole()) {
throw new Error('console and logger are too intertwingly');
}
}
return UseLogger;
};
// ------------------------------------------------------------------------------
console.log = function () {
if (logger.useConsole()) return;
logger.log.apply(logger, [].slice.call(arguments));
};
// ------------------------------------------------------------------------------
console.error = function () {
if (logger.useConsole()) return;
logger.error.apply(logger, [].slice.call(arguments));
};
// ------------------------------------------------------------------------------
console.warn = function () {
if (logger.useConsole()) return;
logger.warn.apply(logger, [].slice.call(arguments));
};
// ------------------------------------------------------------------------------
console.info = function () {
if (logger.useConsole()) return;
logger.info.apply(logger, [].slice.call(arguments));
};
// ------------------------------------------------------------------------------
console.debug = function () {
if (logger.useConsole()) return;
logger.debug.apply(logger, [].slice.call(arguments));
};
// ------------------------------------------------------------------------------
console.assert = function (expression) {
if (expression) return;
var message = logger.format.apply(logger.format, [].slice.call(arguments, 1));
console.log('ASSERT: ' + message);
};
// ------------------------------------------------------------------------------
console.clear = function () {};
// ------------------------------------------------------------------------------
console.dir = function (object) {
console.log('%o', object);
};
// ------------------------------------------------------------------------------
console.dirxml = function (node) {
console.log(node.innerHTML);
};
// ------------------------------------------------------------------------------
console.trace = noop;
// ------------------------------------------------------------------------------
console.group = console.log;
// ------------------------------------------------------------------------------
console.groupCollapsed = console.log;
// ------------------------------------------------------------------------------
console.groupEnd = noop;
// ------------------------------------------------------------------------------
console.time = function (name) {
Timers[name] = new Date().valueOf();
};
// ------------------------------------------------------------------------------
console.timeEnd = function (name) {
var timeStart = Timers[name];
if (!timeStart) {
console.warn('unknown timer: ' + name);
return;
}
var timeElapsed = new Date().valueOf() - timeStart;
console.log(name + ': ' + timeElapsed + 'ms');
};
// ------------------------------------------------------------------------------
console.timeStamp = noop;
// ------------------------------------------------------------------------------
console.profile = noop;
// ------------------------------------------------------------------------------
console.profileEnd = noop;
// ------------------------------------------------------------------------------
console.count = noop;
// ------------------------------------------------------------------------------
console.exception = console.log;
// ------------------------------------------------------------------------------
console.table = function (data, columns) {
console.log('%o', data);
};
// ------------------------------------------------------------------------------
// return a new function that calls both functions passed as args
// ------------------------------------------------------------------------------
function wrappedOrigCall (orgFunc, newFunc) {
return function () {
var args = [].slice.call(arguments);
try { orgFunc.apply(WinConsole, args); } catch (e) {}
try { newFunc.apply(console, args); } catch (e) {}
};
}
// ------------------------------------------------------------------------------
// For every function that exists in the original console object, that
// also exists in the new console object, wrap the new console method
// with one that calls both
// ------------------------------------------------------------------------------
for (var key in console) {
if (typeof WinConsole[key] === 'function') {
console[key] = wrappedOrigCall(WinConsole[key], console[key]);
}
}