beef up RotatingFileStream internal debugging

This commit is contained in:
Trent Mick 2016-04-21 00:31:46 -07:00
parent a094cee092
commit dfa12b2c97

View file

@ -1243,6 +1243,7 @@ RotatingFileStream = function RotatingFileStream(options) {
this.rotQueue = []; this.rotQueue = [];
this.rotating = false; this.rotating = false;
if (rotateAfterOpen) { if (rotateAfterOpen) {
this._debug('rotateAfterOpen -> call rotate()');
this.rotate(); this.rotate();
} else { } else {
this._setupNextRot(); this._setupNextRot();
@ -1251,6 +1252,21 @@ RotatingFileStream = function RotatingFileStream(options) {
util.inherits(RotatingFileStream, EventEmitter); util.inherits(RotatingFileStream, EventEmitter);
RotatingFileStream.prototype._debug = function () {
// Set this to `true` to add debug logging.
if (false) {
if (arguments.length === 0) {
return true;
}
var args = Array.prototype.slice.call(arguments);
args[0] = '[' + (new Date().toISOString()) + ', '
+ this.path + '] ' + args[0];
console.log.apply(this, args);
} else {
return false;
}
};
RotatingFileStream.prototype._setupNextRot = function () { RotatingFileStream.prototype._setupNextRot = function () {
this.rotAt = this._calcRotTime(1); this.rotAt = this._calcRotTime(1);
this._setRotationTimer(); this._setRotationTimer();
@ -1266,7 +1282,10 @@ RotatingFileStream.prototype._setRotationTimer = function () {
delay = TIMEOUT_MAX; delay = TIMEOUT_MAX;
} }
this.timeout = setTimeout( this.timeout = setTimeout(
function () { self.rotate(); }, function () {
self._debug('_setRotationTimer timeout -> call rotate()');
self.rotate();
},
delay); delay);
if (typeof (this.timeout.unref) === 'function') { if (typeof (this.timeout.unref) === 'function') {
this.timeout.unref(); this.timeout.unref();
@ -1275,13 +1294,11 @@ RotatingFileStream.prototype._setRotationTimer = function () {
RotatingFileStream.prototype._calcRotTime = RotatingFileStream.prototype._calcRotTime =
function _calcRotTime(periodOffset) { function _calcRotTime(periodOffset) {
var _DEBUG = false; this._debug('_calcRotTime: %s%s', this.periodNum, this.periodScope);
if (_DEBUG)
console.log('-- _calcRotTime: %s%s', this.periodNum, this.periodScope);
var d = new Date(); var d = new Date();
if (_DEBUG) console.log(' now local: %s', d); this._debug(' now local: %s', d);
if (_DEBUG) console.log(' now utc: %s', d.toISOString()); this._debug(' now utc: %s', d.toISOString());
var rotAt; var rotAt;
switch (this.periodScope) { switch (this.periodScope) {
case 'ms': case 'ms':
@ -1352,11 +1369,11 @@ function _calcRotTime(periodOffset) {
assert.fail(format('invalid period scope: "%s"', this.periodScope)); assert.fail(format('invalid period scope: "%s"', this.periodScope));
} }
if (_DEBUG) { if (this._debug()) {
console.log(' **rotAt**: %s (utc: %s)', rotAt, this._debug(' **rotAt**: %s (utc: %s)', rotAt,
new Date(rotAt).toUTCString()); new Date(rotAt).toUTCString());
var now = Date.now(); var now = Date.now();
console.log(' now: %s (%sms == %smin == %sh to go)', this._debug(' now: %s (%sms == %smin == %sh to go)',
now, now,
rotAt - now, rotAt - now,
(rotAt-now)/1000/60, (rotAt-now)/1000/60,
@ -1368,7 +1385,6 @@ function _calcRotTime(periodOffset) {
RotatingFileStream.prototype.rotate = function rotate() { RotatingFileStream.prototype.rotate = function rotate() {
// XXX What about shutdown? // XXX What about shutdown?
var self = this; var self = this;
var _DEBUG = false;
// If rotation period is > ~25 days, we have to break into multiple // If rotation period is > ~25 days, we have to break into multiple
// setTimeout's. See <https://github.com/joyent/node/issues/8656>. // setTimeout's. See <https://github.com/joyent/node/issues/8656>.
@ -1376,10 +1392,7 @@ RotatingFileStream.prototype.rotate = function rotate() {
return self._setRotationTimer(); return self._setRotationTimer();
} }
if (_DEBUG) { this._debug('rotate');
console.log('-- [%s, pid=%s] rotating %s',
new Date(), process.pid, self.path);
}
if (self.rotating) { if (self.rotating) {
throw new TypeError('cannot start a rotation when already rotating'); throw new TypeError('cannot start a rotation when already rotating');
} }
@ -1393,7 +1406,7 @@ RotatingFileStream.prototype.rotate = function rotate() {
toDel = self.path; toDel = self.path;
} }
n -= 1; n -= 1;
if (_DEBUG) console.log('rm %s', toDel); self._debug(' rm %s', toDel);
fs.unlink(toDel, function (delErr) { fs.unlink(toDel, function (delErr) {
//XXX handle err other than not exists //XXX handle err other than not exists
moves(); moves();
@ -1414,10 +1427,7 @@ RotatingFileStream.prototype.rotate = function rotate() {
if (!exists) { if (!exists) {
moves(); moves();
} else { } else {
if (_DEBUG) { self._debug(' mv %s %s', before, after);
console.log('[pid %s] mv %s %s',
process.pid, before, after);
}
mv(before, after, function (mvErr) { mv(before, after, function (mvErr) {
if (mvErr) { if (mvErr) {
self.emit('error', mvErr); self.emit('error', mvErr);
@ -1431,7 +1441,7 @@ RotatingFileStream.prototype.rotate = function rotate() {
} }
function finish() { function finish() {
if (_DEBUG) console.log('[pid %s] open %s', process.pid, self.path); self._debug(' open %s', self.path);
self.stream = fs.createWriteStream(self.path, self.stream = fs.createWriteStream(self.path,
{flags: 'a', encoding: 'utf8'}); {flags: 'a', encoding: 'utf8'});
var q = self.rotQueue, len = q.length; var q = self.rotQueue, len = q.length;