Skip to content

Instantly share code, notes, and snippets.

@tmpvar
Created July 12, 2011 07:19
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save tmpvar/1077544 to your computer and use it in GitHub Desktop.
Save tmpvar/1077544 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.
gmon.out
v8.log

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
90% of this file was ripped from node.js
License: see: https://github.com/joyent/node/blob/master/lib/console.js
*/
// console object
var formatRegExp = /%[sdj]/g;
function format(f) {
var util = require('util');
if (typeof f !== 'string') {
var objects = [];
for (var i = 0; i < arguments.length; i++) {
objects.push(util.inspect(arguments[i]));
}
return objects.join(' ');
}
var i = 1;
var args = arguments;
var str = String(f).replace(formatRegExp, function(x) {
switch (x) {
case '%s': return String(args[i++]);
case '%d': return Number(args[i++]);
case '%j': return JSON.stringify(args[i++]);
default:
return x;
}
});
for (var len = args.length, x = args[i]; i < len; x = args[++i]) {
if (x === null || typeof x !== 'object') {
str += ' ' + x;
} else {
str += ' ' + util.inspect(x);
}
}
return str;
}
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.pendingWrite = 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.3",
"homepage": "https://gist.github.com/1077544",
"repository": {
"type": "git",
"url": "git://gist.github.com/1077544.git"
},
"main": "console.log.js",
"engines": {
"node": "*"
},
"dependencies": {},
"devDependencies": {}
}
@TooTallNate
Copy link

I'd consider this a node bug. You should try to get this merged into core.

@tmpvar
Copy link
Author

tmpvar commented Jul 12, 2011

yeah.. I need it now though. Pull request forthcoming.

@tmpvar
Copy link
Author

tmpvar commented Jul 12, 2011

nodejs/node-v0.x-archive#1312 -- new version to npm with the code that actually exists in node's console.log

@TooTallNate
Copy link

How does this module have access to the format function?

@tmpvar
Copy link
Author

tmpvar commented Jul 12, 2011

it does not, thanks! fixed and pushed to npm (0.1.2)

@jamielinux
Copy link

Hi there. It would be awesome from a distribution packaging point of view if you could include the full text of the MIT license in your software, usually in a file called LICENSE.

Thanks!

@mreinstein
Copy link

Has this node bug been fixed? The ticket I see for it is closed but the patch was rejected:

nodejs/node-v0.x-archive#1312

I'm fixing some old code (nodeunit) that relies on this behavior, and want to make sure it's safe to remove before I do.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment