Skip to content

Instantly share code, notes, and snippets.

@csanz
Forked from tmpvar/.gitignore
Created July 12, 2011 07:51
Show Gist options
  • Save csanz/1077574 to your computer and use it in GitHub Desktop.
Save csanz/1077574 to your computer and use it in GitHub Desktop.
A console.log implementation that plays "nice" with large amounts of data. It Keeps node alive until the output has flushed to the screen.

Install

npm install console.log

Usage

require('console.log') and go about your day

How it works

Simple, node will stay alive until there are no callbacks left in the queue. Assuming the application is not forcibly terminated (i.e. ^C, killall -9 node, etc...) it will run until all of the data passed to console.log has been flushed to the terminal

/*
A console.log that won't leave you hanging when node exits
*/
console.log = function() {
var res = process.stdout.write(format.apply(this, arguments) + '\n');
// this is the first time stdout got backed up
if (!res && !process.stdout.pendingWrite) {
process.stdout.pendingWrite = true;
// magic sauce: keep node alive until stdout has flushed
process.stdout.once('drain', function () {
process.stdout.draining = false;
});
}
};
{
"author": "Elijah Insua <tmpvar@gmail.com> (http://tmpvar.com)",
"name": "console.log",
"description": "A console.log implementation that plays *nice* with large amounts of data. It Keeps node alive until the output has flushed to the screen.",
"version": "0.1.1",
"homepage": "https://gist.github.com/1077544",
"repository": {
"type": "git",
"url": "git://gist.github.com/1077544.git"
},
"main": "console.log.js",
"engines": {
"node": "*"
},
"dependencies": {},
"devDependencies": {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment