123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186 |
- /*
- *
- * 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("./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]);
- }
- }
|