2013-05-30 05:58:09 +08:00
|
|
|
"use strict";
|
|
|
|
var BaseRollingFileStream = require('./BaseRollingFileStream')
|
2013-06-24 06:51:10 +08:00
|
|
|
, debug = require('../debug')('DateRollingFileStream')
|
2013-05-30 05:58:09 +08:00
|
|
|
, format = require('../date_format')
|
|
|
|
, async = require('async')
|
|
|
|
, fs = require('fs')
|
|
|
|
, util = require('util');
|
2012-09-05 08:58:28 +08:00
|
|
|
|
|
|
|
module.exports = DateRollingFileStream;
|
|
|
|
|
2013-05-05 11:44:01 +08:00
|
|
|
function DateRollingFileStream(filename, pattern, options, now) {
|
|
|
|
debug("Now is " + now);
|
|
|
|
if (pattern && typeof(pattern) === 'object') {
|
|
|
|
now = options;
|
|
|
|
options = pattern;
|
|
|
|
pattern = null;
|
|
|
|
}
|
|
|
|
this.pattern = pattern || '.yyyy-MM-dd';
|
|
|
|
this.now = now || Date.now;
|
2014-04-23 23:07:11 +08:00
|
|
|
|
|
|
|
if (fs.existsSync(filename)) {
|
|
|
|
var stat = fs.statSync(filename);
|
|
|
|
this.lastTimeWeWroteSomething = format.asString(this.pattern, stat.mtime);
|
|
|
|
} else {
|
|
|
|
this.lastTimeWeWroteSomething = format.asString(this.pattern, new Date(this.now()));
|
|
|
|
}
|
|
|
|
|
2013-05-05 11:44:01 +08:00
|
|
|
this.baseFilename = filename;
|
2013-05-12 04:01:28 +08:00
|
|
|
this.alwaysIncludePattern = false;
|
2013-05-30 05:58:09 +08:00
|
|
|
|
2013-05-05 11:44:01 +08:00
|
|
|
if (options) {
|
|
|
|
if (options.alwaysIncludePattern) {
|
2013-05-09 18:09:59 +08:00
|
|
|
this.alwaysIncludePattern = true;
|
2013-05-12 04:01:28 +08:00
|
|
|
filename = this.baseFilename + this.lastTimeWeWroteSomething;
|
2013-05-05 11:44:01 +08:00
|
|
|
}
|
|
|
|
delete options.alwaysIncludePattern;
|
2013-05-08 17:05:32 +08:00
|
|
|
if (Object.keys(options).length === 0) {
|
2013-05-05 11:44:01 +08:00
|
|
|
options = null;
|
2013-05-02 19:56:33 +08:00
|
|
|
}
|
2013-05-05 11:44:01 +08:00
|
|
|
}
|
|
|
|
debug("this.now is " + this.now + ", now is " + now);
|
2013-05-30 05:58:09 +08:00
|
|
|
|
2013-05-05 11:44:01 +08:00
|
|
|
DateRollingFileStream.super_.call(this, filename, options);
|
2012-09-05 08:58:28 +08:00
|
|
|
}
|
|
|
|
util.inherits(DateRollingFileStream, BaseRollingFileStream);
|
2012-09-18 06:46:39 +08:00
|
|
|
|
|
|
|
DateRollingFileStream.prototype.shouldRoll = function() {
|
2013-05-30 05:58:09 +08:00
|
|
|
var lastTime = this.lastTimeWeWroteSomething,
|
|
|
|
thisTime = format.asString(this.pattern, new Date(this.now()));
|
|
|
|
|
|
|
|
debug("DateRollingFileStream.shouldRoll with now = " +
|
|
|
|
this.now() + ", thisTime = " + thisTime + ", lastTime = " + lastTime);
|
|
|
|
|
|
|
|
this.lastTimeWeWroteSomething = thisTime;
|
|
|
|
this.previousTime = lastTime;
|
|
|
|
|
|
|
|
return thisTime !== lastTime;
|
2012-09-18 06:46:39 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
DateRollingFileStream.prototype.roll = function(filename, callback) {
|
2013-05-30 05:58:09 +08:00
|
|
|
var that = this;
|
|
|
|
|
|
|
|
debug("Starting roll");
|
|
|
|
|
|
|
|
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
|
|
|
|
fs.unlink(newFilename, function (err) {
|
|
|
|
//ignore err: if we could not delete, it's most likely that it doesn't exist
|
|
|
|
cb();
|
|
|
|
});
|
|
|
|
}
|
2012-09-18 06:46:39 +08:00
|
|
|
|
2013-05-30 05:58:09 +08:00
|
|
|
function renameTheCurrentFile(cb) {
|
|
|
|
debug("Renaming the " + filename + " -> " + newFilename);
|
|
|
|
fs.rename(filename, newFilename, cb);
|
|
|
|
}
|
2012-09-18 06:46:39 +08:00
|
|
|
|
|
|
|
};
|