Skip to content

Instantly share code, notes, and snippets.

@jwietelmann
Created January 28, 2016 20:23
Show Gist options
  • Save jwietelmann/d771ecb303f0f06439bc to your computer and use it in GitHub Desktop.
Save jwietelmann/d771ecb303f0f06439bc to your computer and use it in GitHub Desktop.
// 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