Name: js-handler/node_modules/nodeunit/deps/console.log.js
| 1: | /* |
| 2: | A console.log that won't leave you hanging when node exits |
| 3: | 90% of this file was ripped from node.js |
| 4: | |
| 5: | License: see: https://github.com/joyent/node/blob/master/lib/console.js |
| 6: | */ |
| 7: | |
| 8: | // console object |
| 9: | var formatRegExp = /%[sdj]/g; |
| 10: | function format(f) { |
| 11: | var util = require('util'); |
| 12: | |
| 13: | if (typeof f !== 'string') { |
| 14: | var objects = []; |
| 15: | for (var i = 0; i < arguments.length; i++) { |
| 16: | objects.push(util.inspect(arguments[i])); |
| 17: | } |
| 18: | return objects.join(' '); |
| 19: | } |
| 20: | |
| 21: | |
| 22: | var i = 1; |
| 23: | var args = arguments; |
| 24: | var str = String(f).replace(formatRegExp, function(x) { |
| 25: | switch (x) { |
| 26: | case '%s': return String(args[i++]); |
| 27: | case '%d': return Number(args[i++]); |
| 28: | case '%j': return JSON.stringify(args[i++]); |
| 29: | default: |
| 30: | return x; |
| 31: | } |
| 32: | }); |
| 33: | for (var len = args.length, x = args[i]; i < len; x = args[++i]) { |
| 34: | if (x === null || typeof x !== 'object') { |
| 35: | str += ' ' + x; |
| 36: | } else { |
| 37: | str += ' ' + util.inspect(x); |
| 38: | } |
| 39: | } |
| 40: | return str; |
| 41: | } |
| 42: | |
| 43: | console.log = function() { |
| 44: | var res = process.stdout.write(format.apply(this, arguments) + '\n'); |
| 45: | |
| 46: | // this is the first time stdout got backed up |
| 47: | if (!res && !process.stdout.pendingWrite) { |
| 48: | process.stdout.pendingWrite = true; |
| 49: | |
| 50: | // magic sauce: keep node alive until stdout has flushed |
| 51: | process.stdout.once('drain', function () { |
| 52: | process.stdout.draining = false; |
| 53: | }); |
| 54: | } |
| 55: | }; |
