Skip to content
This repository was archived by the owner on Jul 18, 2018. It is now read-only.
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
154 changes: 64 additions & 90 deletions normalizeconsole.js
Original file line number Diff line number Diff line change
@@ -1,91 +1,65 @@

(function(console) {
/*********************************************************************************************
* Make sure console exists because IE blows up if it's not open and you attempt to access it
* Create some dummy functions if we need to, so we don't have to if/else everything
*********************************************************************************************/
console||(console = window.console = {
// all this "a, b, c, d, e" garbage is to make the IDEs happy, since they can't do variable argument lists
/**
* @param a
* @param [b]
* @param [c]
* @param [d]
* @param [e]
*/
log: function(a, b, c, d, e) {},
/**
* @param a
* @param [b]
* @param [c]
* @param [d]
* @param [e]
*/
info: function(a, b, c, d, e) {},
/**
* @param a
* @param [b]
* @param [c]
* @param [d]
* @param [e]
*/
warn: function(a, b, c, d, e) {},
/**
* @param a
* @param [b]
* @param [c]
* @param [d]
* @param [e]
*/
error: function(a, b, c, d, e) {}
});

// le sigh, IE, oh IE, how we fight... fix Function.prototype.bind as needed
if (!Function.prototype.bind) {
//credits: taken from bind_even_never in this discussion: https://prototype.lighthouseapp.com/projects/8886/tickets/215-optimize-bind-bindaseventlistener#ticket-215-9
Function.prototype.bind = function(context) {
var fn = this, args = Array.prototype.slice.call(arguments, 1);
return function(){
/**
* Console Normalizer, https://github.com/Zenovations/console-normalizer
* @version 2013-04-10
* @license MIT
*/
(function (root) {
"use strict";
if(typeof root !== "object"){
return;
}
/*********************************************************************************************
* Make sure console exists because IE blows up if it's not open and you attempt to access it
* Create some dummy functions if we need to, so we don't have to if/else everything
*********************************************************************************************/
var console = root.console || {
error : function () {},
info : function () {},
log : function () {},
warn : function () {}
};

//credits: taken from bind_even_never in this discussion: https://prototype.lighthouseapp.com/projects/8886/tickets/215-optimize-bind-bindaseventlistener#ticket-215-9
Function.prototype.bind = Function.prototype.bind || function (context) {
var fn = this,
args = Array.prototype.slice.call(arguments, 1);
return function () {
return fn.apply(context, Array.prototype.concat.apply(args, arguments));
};
};
}

// IE 9 won't allow us to call console.log.apply (WTF IE!) It also reports typeof(console.log) as 'object' (UNH!)
// but together, those two errors can be useful in allowing us to fix stuff so it works right
if( typeof(console.log) === 'object' ) {
// Array.forEach doesn't work in IE 8 so don't try that :(
console.log = Function.prototype.call.bind(console.log, console);
console.info = Function.prototype.call.bind(console.info, console);
console.warn = Function.prototype.call.bind(console.warn, console);
console.error = Function.prototype.call.bind(console.error, console);
}

/**
* Support group and groupEnd functions
*/
('group' in console) ||
(console.group = function(msg) {
console.info("\n------------\n"+msg+"\n------------");
});
('groupEnd' in console) ||
(console.groupEnd = function() {
//console.log("\n\n");
});

/**
* Support time and timeEnd functions
*/
('time' in console) ||
(function() {
var trackedTimes = {};
console.time = function(msg) {
trackedTimes[msg] = new Date().getTime();
};
console.timeEnd = function(msg) {
var end = new Date().getTime(), time = (msg in trackedTimes)? end - trackedTimes[msg] : 0;
console.info(msg+': '+time+'ms')
}
}());

})(window.console);
};
};

// IE 9 won't allow us to call console.log.apply (WTF IE!) It also reports typeof(console.log) as 'object' (UNH!)
// but together, those two errors can be useful in allowing us to fix stuff so it works right
if (typeof(console.log) === 'object') {
console.error = Function.prototype.call.bind(console.error, console);
console.info = Function.prototype.call.bind(console.info, console);
console.log = Function.prototype.call.bind(console.log, console);
console.warn = Function.prototype.call.bind(console.warn, console);
}

/**
* Support group and groupEnd functions
*/
console.group = console.group || function (msg) {
console.info("\n------------\n" + msg + "\n------------");
};

console.groupEnd = console.groupEnd || function () {};
/**
* Support time and timeEnd functions
*/

console.trackedTimes = {};

console.time = console.time || function (msg) {
console.trackedTimes[msg] = new Date().getTime();
};

console.timeEnd = console.timeEnd || function (msg) {
var end = new Date().getTime(),
time = end - (console.trackedTimes[msg] || end);
console.info(msg + ': ' + time + 'ms');
};

root.console = console;
}(window));