diff options
author | Andrei Belov <defan@nginx.com> | 2019-08-22 21:33:54 +0300 |
---|---|---|
committer | Andrei Belov <defan@nginx.com> | 2019-08-22 21:33:54 +0300 |
commit | a07c4d30a64f781f93730576b5dced32422a9935 (patch) | |
tree | 06ebfaa66845a057b8069014c5379b2dcfc80861 /src/nodejs/unit-http/utils.js | |
parent | 8a579acddeae0c0106e15d82aa7220ac01deba84 (diff) | |
parent | c47af243b0e805376c4ec908f21e07dc811b33f0 (diff) | |
download | unit-a07c4d30a64f781f93730576b5dced32422a9935.tar.gz unit-a07c4d30a64f781f93730576b5dced32422a9935.tar.bz2 |
Merged with the default branch.1.10.0-1
Diffstat (limited to 'src/nodejs/unit-http/utils.js')
-rw-r--r-- | src/nodejs/unit-http/utils.js | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/src/nodejs/unit-http/utils.js b/src/nodejs/unit-http/utils.js new file mode 100644 index 00000000..e1e51b0e --- /dev/null +++ b/src/nodejs/unit-http/utils.js @@ -0,0 +1,73 @@ +var noop = exports.noop = function(){}; + +exports.extend = function extend(dest, source) { + for (var prop in source) { + dest[prop] = source[prop]; + } +}; + +exports.eventEmitterListenerCount = + require('events').EventEmitter.listenerCount || + function(emitter, type) { return emitter.listeners(type).length; }; + +exports.bufferAllocUnsafe = Buffer.allocUnsafe ? + Buffer.allocUnsafe : + function oldBufferAllocUnsafe(size) { return new Buffer(size); }; + +exports.bufferFromString = Buffer.from ? + Buffer.from : + function oldBufferFromString(string, encoding) { + return new Buffer(string, encoding); + }; + +exports.BufferingLogger = function createBufferingLogger(identifier, uniqueID) { + try { + var logFunction = require('debug')(identifier); + } + catch(e) { + logFunction = noop; + logFunction.enabled = false; + } + + if (logFunction.enabled) { + var logger = new BufferingLogger(identifier, uniqueID, logFunction); + var debug = logger.log.bind(logger); + debug.printOutput = logger.printOutput.bind(logger); + debug.enabled = logFunction.enabled; + return debug; + } + logFunction.printOutput = noop; + return logFunction; +}; + +function BufferingLogger(identifier, uniqueID, logFunction) { + this.logFunction = logFunction; + this.identifier = identifier; + this.uniqueID = uniqueID; + this.buffer = []; +} + +BufferingLogger.prototype.log = function() { + this.buffer.push([ new Date(), Array.prototype.slice.call(arguments) ]); + return this; +}; + +BufferingLogger.prototype.clear = function() { + this.buffer = []; + return this; +}; + +BufferingLogger.prototype.printOutput = function(logFunction) { + if (!logFunction) { logFunction = this.logFunction; } + var uniqueID = this.uniqueID; + this.buffer.forEach(function(entry) { + var date = entry[0].toLocaleString(); + var args = entry[1].slice(); + var formatString = args[0]; + if (formatString !== (void 0) && formatString !== null) { + formatString = '%s - %s - ' + formatString.toString(); + args.splice(0, 1, formatString, date, uniqueID); + logFunction.apply(global, args); + } + }); +}; |