Fixes bug introduced in github issue #132 where file rolling needs to be handled differently for alwaysIncludePattern streams

This commit is contained in:
Issac Goldstand
2013-05-11 23:01:28 +03:00
parent 2da01cc611
commit dc632f4705
2 changed files with 78 additions and 18 deletions

View File

@@ -23,13 +23,13 @@ function DateRollingFileStream(filename, pattern, options, now) {
this.pattern = pattern || '.yyyy-MM-dd';
this.now = now || Date.now;
this.lastTimeWeWroteSomething = format.asString(this.pattern, new Date(this.now()));
this.alwaysIncludePattern = false;
this.baseFilename = filename;
this.alwaysIncludePattern = false;
if (options) {
if (options.alwaysIncludePattern) {
this.alwaysIncludePattern = true;
filename = filename + this.lastTimeWeWroteSomething;
filename = this.baseFilename + this.lastTimeWeWroteSomething;
}
delete options.alwaysIncludePattern;
if (Object.keys(options).length === 0) {
@@ -55,18 +55,26 @@ DateRollingFileStream.prototype.shouldRoll = function() {
};
DateRollingFileStream.prototype.roll = function(filename, callback) {
var that = this,
newFilename = this.baseFilename + this.previousTime;
var that = this;
debug("Starting roll");
async.series([
this.closeTheStream.bind(this),
deleteAnyExistingFile,
renameTheCurrentFile,
this.openTheStream.bind(this)
], callback);
if (this.alwaysIncludePattern) {
this.filename = this.baseFilename + this.lastTimeWeWroteSomething;
async.series([
this.closeTheStream.bind(this),
this.openTheStream.bind(this)
], callback);
} else {
var newFilename = this.baseFilename + this.previousTime;
async.series([
this.closeTheStream.bind(this),
deleteAnyExistingFile,
renameTheCurrentFile,
this.openTheStream.bind(this)
], callback);
}
function deleteAnyExistingFile(cb) {
//on windows, you can get a EEXIST error if you rename a file to an existing file
//so, we'll try to delete the file we're renaming to first
@@ -77,13 +85,8 @@ DateRollingFileStream.prototype.roll = function(filename, callback) {
}
function renameTheCurrentFile(cb) {
if (this.alwaysIncludePattern) {
// no-op
cb();
} else {
debug("Renaming the " + filename + " -> " + newFilename);
fs.rename(filename, newFilename, cb);
}
debug("Renaming the " + filename + " -> " + newFilename);
fs.rename(filename, newFilename, cb);
}
};