Merge pull request #136 from issacg/dontalwaysrename-bug

Dontalwaysrename bug
This commit is contained in:
Gareth Jones
2013-05-15 23:52:57 -07:00
2 changed files with 77 additions and 10 deletions

View File

@@ -24,10 +24,12 @@ function DateRollingFileStream(filename, pattern, options, now) {
this.now = now || Date.now;
this.lastTimeWeWroteSomething = format.asString(this.pattern, new Date(this.now()));
this.baseFilename = filename;
this.alwaysIncludePattern = false;
if (options) {
if (options.alwaysIncludePattern) {
filename = filename + this.lastTimeWeWroteSomething;
this.alwaysIncludePattern = true;
filename = this.baseFilename + this.lastTimeWeWroteSomething;
}
delete options.alwaysIncludePattern;
if (Object.keys(options).length === 0) {
@@ -53,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