Skip to content

Instantly share code, notes, and snippets.

@itsjustcon
Created April 26, 2014 22:32
Show Gist options
  • Save itsjustcon/11332917 to your computer and use it in GitHub Desktop.
Save itsjustcon/11332917 to your computer and use it in GitHub Desktop.
Tool to enable/disable standard output (loggers, errors, etc.)
var stdout_write = undefined;
var Log = function (value) {
var shouldRelock = Log.unlock();
/** Forward call to console.log */
console.log.apply(this, arguments);
if (shouldRelock)
Log.lock();
}
Log.prototype.lock = function () {
/** Re-stub process.stdout.write */
stdout_write = process.stdout.write;
process.stdout.write = function () {}
}
/** Returns false if log isn't locked. */
Log.prototype.unlock = function () {
var locked = stdout_write ? true : false;
if (locked) /** Restore process.stdout.write */
process.stdout.write = stdout_write;
return !locked;
}
module.exports = Log;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment