2012-09-05 08:58:28 +08:00
|
|
|
var BaseRollingFileStream = require('./BaseRollingFileStream'),
|
2012-09-18 06:46:39 +08:00
|
|
|
format = require('../date_format'),
|
|
|
|
async = require('async'),
|
|
|
|
fs = require('fs'),
|
2012-09-05 08:58:28 +08:00
|
|
|
util = require('util');
|
|
|
|
|
|
|
|
module.exports = DateRollingFileStream;
|
|
|
|
|
2013-03-24 09:50:13 +08:00
|
|
|
var debug;
|
|
|
|
if (process.env.NODE_DEBUG && /\blog4js\b/.test(process.env.NODE_DEBUG)) {
|
|
|
|
debug = function(message) { console.error('LOG4JS: (DateRollingFileStream) %s', message); };
|
|
|
|
} else {
|
|
|
|
debug = function() { };
|
2012-09-18 06:46:39 +08:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
this.lastTimeWeWroteSomething = format.asString(this.pattern, new Date(this.now()));
|
|
|
|
this.baseFilename = filename;
|
2013-05-02 19:56:33 +08:00
|
|
|
|
2013-05-05 11:44:01 +08:00
|
|
|
if (options) {
|
|
|
|
if (options.alwaysIncludePattern) {
|
|
|
|
filename = filename + this.lastTimeWeWroteSomething;
|
|
|
|
}
|
|
|
|
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);
|
2012-09-05 08:58:28 +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() {
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
|
|
|
DateRollingFileStream.prototype.roll = function(filename, callback) {
|
|
|
|
var that = this,
|
2013-05-02 19:56:33 +08:00
|
|
|
newFilename = this.baseFilename + this.previousTime;
|
2012-09-18 06:46:39 +08:00
|
|
|
|
|
|
|
debug("Starting roll");
|
|
|
|
|
|
|
|
async.series([
|
2013-03-20 06:14:27 +08:00
|
|
|
this.closeTheStream.bind(this),
|
|
|
|
deleteAnyExistingFile,
|
|
|
|
renameTheCurrentFile,
|
|
|
|
this.openTheStream.bind(this)
|
2012-09-18 06:46:39 +08:00
|
|
|
], 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();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function renameTheCurrentFile(cb) {
|
|
|
|
debug("Renaming the " + filename + " -> " + newFilename);
|
|
|
|
fs.rename(filename, newFilename, cb);
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|