Created
January 28, 2016 20:23
-
-
Save jwietelmann/d771ecb303f0f06439bc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Stupid JavaScript and console tricks. | |
// Trap and record console things and errors. | |
function MosquitoNet() {} | |
MosquitoNet.history = []; | |
MosquitoNet.writeHistory = function(type, args) { | |
MosquitoNet.history.push({ type: type, args: args }); | |
} | |
MosquitoNet.shadowConsoleFunction = function(name) { | |
var oldFn = console[name]; | |
console[name] = MosquitoNet[name] = function() { | |
MosquitoNet.writeHistory(name, Array.prototype.slice.call(arguments)); | |
oldFn.apply(console, arguments); | |
}; | |
} | |
MosquitoNet.throw = function(err) { | |
MosquitoNet.writeHistory('throw', arguments); | |
throw err; | |
} | |
MosquitoNet.wrap = function(fn) { | |
MosquitoNet.shadowConsoleFunction('log'); | |
MosquitoNet.shadowConsoleFunction('warn'); | |
MosquitoNet.shadowConsoleFunction('error'); | |
var newFn = function() { | |
try { | |
fn.apply(fn, arguments); | |
} catch(err) { | |
this.throw(err); | |
} | |
} | |
return newFn.bind(this); | |
} | |
MosquitoNet.toString = function() { | |
return MosquitoNet.history.map(function(item) { | |
var str = ''; | |
if(item.type === 'throw') { | |
str += '----- Uncaught Error -----\n'; | |
str += item.args[0].stack || item.args[0]; | |
} else { | |
str += '----- console.' + item.type + ' -----\n'; | |
str += item.args.join('\n'); | |
} | |
return str; | |
}).join('\n\n') + '\n'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment