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