1var noop = exports.noop = function(){}; 2 3exports.extend = function extend(dest, source) { 4 for (var prop in source) { 5 dest[prop] = source[prop]; 6 } 7}; 8 9exports.eventEmitterListenerCount = 10 require('events').EventEmitter.listenerCount || 11 function(emitter, type) { return emitter.listeners(type).length; }; 12 13exports.bufferAllocUnsafe = Buffer.allocUnsafe ? 14 Buffer.allocUnsafe : 15 function oldBufferAllocUnsafe(size) { return new Buffer(size); }; 16 17exports.bufferFromString = Buffer.from ? 18 Buffer.from : 19 function oldBufferFromString(string, encoding) { 20 return new Buffer(string, encoding); 21 }; 22 23exports.BufferingLogger = function createBufferingLogger(identifier, uniqueID) { 24 try { 25 var logFunction = require('debug')(identifier); 26 } 27 catch(e) { 28 logFunction = noop; 29 logFunction.enabled = false; 30 } 31 32 if (logFunction.enabled) { 33 var logger = new BufferingLogger(identifier, uniqueID, logFunction); 34 var debug = logger.log.bind(logger); 35 debug.printOutput = logger.printOutput.bind(logger); 36 debug.enabled = logFunction.enabled; 37 return debug; 38 } 39 logFunction.printOutput = noop; 40 return logFunction; 41}; 42 43function BufferingLogger(identifier, uniqueID, logFunction) { 44 this.logFunction = logFunction; 45 this.identifier = identifier; 46 this.uniqueID = uniqueID; 47 this.buffer = []; 48} 49 50BufferingLogger.prototype.log = function() { 51 this.buffer.push([ new Date(), Array.prototype.slice.call(arguments) ]); 52 return this; 53}; 54 55BufferingLogger.prototype.clear = function() { 56 this.buffer = []; 57 return this; 58}; 59 60BufferingLogger.prototype.printOutput = function(logFunction) { 61 if (!logFunction) { logFunction = this.logFunction; } 62 var uniqueID = this.uniqueID; 63 this.buffer.forEach(function(entry) { 64 var date = entry[0].toLocaleString(); 65 var args = entry[1].slice(); 66 var formatString = args[0]; 67 if (formatString !== (void 0) && formatString !== null) { 68 formatString = '%s - %s - ' + formatString.toString(); 69 args.splice(0, 1, formatString, date, uniqueID); 70 logFunction.apply(global, args); 71 } 72 }); 73}; 74