Skip to content

Instantly share code, notes, and snippets.

@tmpvar
Created July 12, 2011 07:19
Show Gist options
  • Save tmpvar/1077544 to your computer and use it in GitHub Desktop.
Save tmpvar/1077544 to your computer and use it in GitHub Desktop.

Revisions

  1. tmpvar revised this gist Jul 14, 2011. 2 changed files with 2 additions and 2 deletions.
    2 changes: 1 addition & 1 deletion console.log.js
    Original file line number Diff line number Diff line change
    @@ -49,7 +49,7 @@ console.log = function() {

    // magic sauce: keep node alive until stdout has flushed
    process.stdout.once('drain', function () {
    process.stdout.draining = false;
    process.stdout.pendingWrite = false;
    });
    }
    };
    2 changes: 1 addition & 1 deletion package.json
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,7 @@
    "author": "Elijah Insua <[email protected]> (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.2",
    "version": "0.1.3",
    "homepage": "https://gist.github.com/1077544",
    "repository": {
    "type": "git",
  2. tmpvar revised this gist Jul 12, 2011. 3 changed files with 44 additions and 3 deletions.
    2 changes: 2 additions & 0 deletions .gitignore
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,2 @@
    gmon.out
    v8.log
    43 changes: 41 additions & 2 deletions console.log.js
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,48 @@
    /*
    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;
    @@ -13,4 +52,4 @@ console.log = function() {
    process.stdout.draining = false;
    });
    }
    };
    };
    2 changes: 1 addition & 1 deletion package.json
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,7 @@
    "author": "Elijah Insua <[email protected]> (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",
    "version": "0.1.2",
    "homepage": "https://gist.github.com/1077544",
    "repository": {
    "type": "git",
  3. tmpvar revised this gist Jul 12, 2011. 2 changed files with 3 additions and 3 deletions.
    4 changes: 2 additions & 2 deletions console.log.js
    Original file line number Diff line number Diff line change
    @@ -1,8 +1,8 @@
    /*
    A console.log that won't leave you hanging when node exits
    */
    console.log = function(d) {
    var res = process.stdout.write(d + '\n');
    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) {
    2 changes: 1 addition & 1 deletion package.json
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,7 @@
    "author": "Elijah Insua <[email protected]> (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.0",
    "version": "0.1.1",
    "homepage": "https://gist.github.com/1077544",
    "repository": {
    "type": "git",
  4. tmpvar revised this gist Jul 12, 2011. 1 changed file with 11 additions and 0 deletions.
    11 changes: 11 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,11 @@
    # 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
  5. tmpvar revised this gist Jul 12, 2011. 1 changed file with 17 additions and 0 deletions.
    17 changes: 17 additions & 0 deletions package.json
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,17 @@
    {
    "author": "Elijah Insua <[email protected]> (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.0",
    "homepage": "https://gist.github.com/1077544",
    "repository": {
    "type": "git",
    "url": "git://gist.github.com/1077544.git"
    },
    "main": "console.log.js",
    "engines": {
    "node": "*"
    },
    "dependencies": {},
    "devDependencies": {}
    }
  6. tmpvar created this gist Jul 12, 2011.
    16 changes: 16 additions & 0 deletions console.log.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,16 @@
    /*
    A console.log that won't leave you hanging when node exits
    */
    console.log = function(d) {
    var res = process.stdout.write(d + '\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;
    });
    }
    };