Fixed the file appender tests

This commit is contained in:
Gareth Jones
2011-12-20 08:49:21 +11:00
parent 78de73a274
commit a999d8fc00
4 changed files with 47 additions and 31 deletions

View File

@@ -20,13 +20,18 @@ function fileAppender (file, layout, logSize, numBackups) {
numBackups = numBackups === 0 ? 1 : numBackups;
function openTheStream(file, fileSize, numFiles) {
var stream = new streams.BufferedWriteStream(
new streams.RollingFileStream(
file,
fileSize,
numFiles
)
);
var stream;
if (fileSize) {
stream = new streams.BufferedWriteStream(
new streams.RollingFileStream(
file,
fileSize,
numFiles
)
);
} else {
stream = new streams.BufferedWriteStream(fs.createWriteStream(file, { encoding: "utf8", mode: 0644, flags: 'a' }));
}
stream.on("error", function (err) {
console.error("log4js.fileAppender - Writing to file %s, error happened ", file, err);
});

View File

@@ -84,11 +84,12 @@ function RollingFileStream (filename, size, backups, options) {
this.options = options || { encoding: "utf8", mode: 0644, flags: 'a' };
this.rolling = false;
this.writesWhileRolling = [];
this.bytesQueued = 0;
throwErrorIfArgumentsAreNotValid();
RollingFileStream.super_.call(this, this.filename, this.options);
this.bytesWritten = currentFileSize(this.filename);
this.bytesQueued = currentFileSize(this.filename);
function currentFileSize(file) {
var fileSize = 0;
@@ -114,9 +115,9 @@ RollingFileStream.prototype.write = function(data, encoding) {
return false;
} else {
var canWrite = RollingFileStream.super_.prototype.write.call(this, data, encoding);
//this.bytesWritten += data.length;
console.log("bytesWritten: %d, max: %d", this.bytesWritten, this.size);
if (this.bytesWritten >= this.size) {
console.log("bytesQueued: %d, max: %d", this.bytesQueued, this.size);
this.bytesQueued += data.length;
if (this.bytesQueued >= this.size) {
this.roll();
}
return canWrite;
@@ -185,6 +186,7 @@ RollingFileStream.prototype.roll = function () {
that.fd = fd;
that.writable = true;
fs.close(oldLogFileFD, function() {
that.bytesQueued = 0;
that.bytesWritten = 0;
cb();
});
@@ -197,9 +199,10 @@ RollingFileStream.prototype.roll = function () {
var toWrite;
while ((toWrite = that.writesWhileRolling.shift())) {
RollingFileStream.super_.prototype.write.call(that, toWrite.data, toWrite.encoding);
that.bytesWritten += toWrite.data.length;
that.bytesQueued += toWrite.data.length;
}
that.rolling = false;
that.flush();
cb();
}