Skip to content

Instantly share code, notes, and snippets.

@lleaff
Created February 27, 2017 15:15
Show Gist options
  • Save lleaff/a83d6c1fa985a1a4b426fe8ba106f3fc to your computer and use it in GitHub Desktop.
Save lleaff/a83d6c1fa985a1a4b426fe8ba106f3fc to your computer and use it in GitHub Desktop.
Better console.log debugging
self.DEBUG_ON = true;
function DEBUG(...args) {
if (!DEBUG_ON) { return; }
const stack = (new Error().stack).match(/\s+at .*\s+at ([^\(]+)/);
const caller = stack && stack[1] ? stack[1].replace(/\s+$/, '') : 'top_level';
(console.debug || console.log)(`[DEBUG@(${caller})]:`, ...args);
}
Object.defineProperty(Object.prototype, 'DEBUG', {
value: function DEBUG_PIPE(msg, ...args) {
DEBUG(msg, this, ...args);
return this;
},
enumerable: false
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment