Skip to content

Instantly share code, notes, and snippets.

@jspiewak
Forked from bnoordhuis/trace-all-events.js
Last active December 20, 2015 20:59
Show Gist options
  • Save jspiewak/6194708 to your computer and use it in GitHub Desktop.
Save jspiewak/6194708 to your computer and use it in GitHub Desktop.
Assign each object an id and log emitted events with the id.
(function() {
var id = 0;
function generateId() { return id++; };
Object.prototype.id = function() {
var newId = generateId();
this.id = function() { return newId; };
return newId;
};
var EventEmitter = require('events').EventEmitter;
var inspect = require('util').inspect;
var emit_ = EventEmitter.prototype.emit;
EventEmitter.prototype.emit = function(name) {
var args = Array.prototype.slice.call(arguments);
if (!(this === process.stderr && name === 'drain')) {
console.log("Event '%s' on '%s', arguments: %s",
name, this.id(), inspect(args.slice(1), false, 2));
}
return emit_.apply(this, args);
};
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment