Permalink
Browse files

Fixes for version v0.10 streams, breaks log4js for older versions of …

…node
  • Loading branch information...
1 parent 5e242c9 commit 65e490cbd2d05b59f0cb6359ff3558350a10e967 Gareth Jones committed Mar 19, 2013
View
@@ -1,4 +1,3 @@
language: node_js
node_js:
- - 0.6
- - 0.8
+ - 0.10
View
@@ -1,4 +1,4 @@
-var log4js = require('./lib/log4js');
+var log4js = require('../lib/log4js');
//log the cheese logger messages to a file, and the console ones as well.
log4js.configure({
appenders: [
View
@@ -1,4 +1,4 @@
-var log4js = require('./lib/log4js')
+var log4js = require('../lib/log4js')
, log
, i = 0;
log4js.configure({
@@ -22,7 +22,7 @@ process.on('exit', function() {
function appender(filename, pattern, layout) {
layout = layout || layouts.basicLayout;
- var logFile = new streams.BufferedWriteStream(new streams.DateRollingFileStream(filename, pattern));
+ var logFile = new streams.DateRollingFileStream(filename, pattern);
openFiles.push(logFile);
return function(logEvent) {
View
@@ -22,53 +22,51 @@ process.on('exit', function() {
* @param numBackups - the number of log files to keep after logSize has been reached (default 5)
*/
function fileAppender (file, layout, logSize, numBackups) {
- var bytesWritten = 0;
- file = path.normalize(file);
- layout = layout || layouts.basicLayout;
- numBackups = numBackups === undefined ? 5 : numBackups;
- //there has to be at least one backup if logSize has been specified
- numBackups = numBackups === 0 ? 1 : numBackups;
+ var bytesWritten = 0;
+ file = path.normalize(file);
+ layout = layout || layouts.basicLayout;
+ numBackups = numBackups === undefined ? 5 : numBackups;
+ //there has to be at least one backup if logSize has been specified
+ numBackups = numBackups === 0 ? 1 : numBackups;
- function openTheStream(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);
- });
- return stream;
+ function openTheStream(file, fileSize, numFiles) {
+ var stream;
+ if (fileSize) {
+ stream = new streams.RollingFileStream(
+ file,
+ fileSize,
+ numFiles
+ );
+ } else {
+ stream = 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);
+ });
+ return stream;
+ }
- var logFile = openTheStream(file, logSize, numBackups);
-
- // push file to the stack of open handlers
- openFiles.push(logFile);
-
- return function(loggingEvent) {
- logFile.write(layout(loggingEvent) + eol, "utf8");
- };
+ var logFile = openTheStream(file, logSize, numBackups);
+
+ // push file to the stack of open handlers
+ openFiles.push(logFile);
+
+ return function(loggingEvent) {
+ logFile.write(layout(loggingEvent) + eol, "utf8");
+ };
}
function configure(config, options) {
- var layout;
- if (config.layout) {
- layout = layouts.layout(config.layout.type, config.layout);
- }
+ var layout;
+ if (config.layout) {
+ layout = layouts.layout(config.layout.type, config.layout);
+ }
- if (options && options.cwd && !config.absolute) {
- config.filename = path.join(options.cwd, config.filename);
- }
+ if (options && options.cwd && !config.absolute) {
+ config.filename = path.join(options.cwd, config.filename);
+ }
- return fileAppender(config.filename, layout, config.maxLogSize, config.backups);
+ return fileAppender(config.filename, layout, config.maxLogSize, config.backups);
}
exports.appender = fileAppender;
@@ -1,5 +1,6 @@
var fs = require('fs'),
- util = require('util');
+stream = require('stream'),
+util = require('util');
function debug(message) {
// console.log(message);
@@ -8,85 +9,64 @@ function debug(message) {
module.exports = BaseRollingFileStream;
function BaseRollingFileStream(filename, options) {
+ debug("In BaseRollingFileStream");
+ this.filename = filename;
+ this.options = options || { encoding: 'utf8', mode: 0644, flags: 'a' };
+ this.currentSize = 0;
-debug("In BaseRollingFileStream");
- this.filename = filename;
- this.options = options || { encoding: 'utf8', mode: 0644, flags: 'a' };
- this.rolling = false;
- this.writesWhileRolling = [];
- this.currentSize = 0;
- this.rollBeforeWrite = false;
-
- function currentFileSize(file) {
- var fileSize = 0;
- try {
- fileSize = fs.statSync(file).size;
- } catch (e) {
- // file does not exist
- }
- return fileSize;
+ function currentFileSize(file) {
+ var fileSize = 0;
+ try {
+ fileSize = fs.statSync(file).size;
+ } catch (e) {
+ // file does not exist
}
+ return fileSize;
+ }
- function throwErrorIfArgumentsAreNotValid() {
- if (!filename) {
- throw new Error("You must specify a filename");
- }
+ function throwErrorIfArgumentsAreNotValid() {
+ if (!filename) {
+ throw new Error("You must specify a filename");
}
+ }
- throwErrorIfArgumentsAreNotValid();
-debug("Calling BaseRollingFileStream.super");
- BaseRollingFileStream.super_.call(this, this.filename, this.options);
- this.currentSize = currentFileSize(this.filename);
+ throwErrorIfArgumentsAreNotValid();
+ debug("Calling BaseRollingFileStream.super");
+ BaseRollingFileStream.super_.call(this);
+ this.openTheStream();
+ this.currentSize = currentFileSize(this.filename);
}
-util.inherits(BaseRollingFileStream, fs.FileWriteStream);
+util.inherits(BaseRollingFileStream, stream.Writable);
-BaseRollingFileStream.prototype.initRolling = function() {
- var that = this;
+BaseRollingFileStream.prototype._write = function(chunk, encoding, callback) {
+ var that = this;
+ function writeTheChunk() {
+ debug("writing the chunk to the underlying stream");
+ that.currentSize += chunk.length;
+ that.theStream.write(chunk, encoding, callback);
+ }
- function emptyRollingQueue() {
- debug("emptying the rolling queue");
- var toWrite;
- while ((toWrite = that.writesWhileRolling.shift())) {
- BaseRollingFileStream.super_.prototype.write.call(that, toWrite.data, toWrite.encoding);
- that.currentSize += toWrite.data.length;
- if (that.shouldRoll()) {
- that.flush();
- return true;
- }
- }
- that.flush();
- return false;
- }
+ debug("in _write");
- this.rolling = true;
- this.roll(this.filename, function() {
- that.currentSize = 0;
- that.rolling = emptyRollingQueue();
- if (that.rolling) {
- process.nextTick(function() { that.initRolling(); });
- }
- });
+ if (this.shouldRoll()) {
+ this.currentSize = 0;
+ this.roll(this.filename, writeTheChunk);
+ } else {
+ writeTheChunk();
+ }
};
-BaseRollingFileStream.prototype.write = function(data, encoding) {
- var canWrite = false;
- if (this.rolling) {
- this.writesWhileRolling.push({ data: data, encoding: encoding });
- } else {
- if (this.rollBeforeWrite && this.shouldRoll()) {
- this.writesWhileRolling.push({ data: data, encoding: encoding });
- this.initRolling();
- } else {
- canWrite = BaseRollingFileStream.super_.prototype.write.call(this, data, encoding);
- this.currentSize += data.length;
- debug('current size = ' + this.currentSize);
+BaseRollingFileStream.prototype.openTheStream = function(cb) {
+ debug("opening the underlying stream");
+ this.theStream = fs.createWriteStream(this.filename, this.options);
+ if (cb) {
+ this.theStream.on("open", cb);
+ }
+};
- if (!this.rollBeforeWrite && this.shouldRoll()) {
- this.initRolling();
- }
- }
- }
- return canWrite;
+BaseRollingFileStream.prototype.closeTheStream = function(cb) {
+ debug("closing the underlying stream");
+ this.theStream.end(null, null, cb);
};
BaseRollingFileStream.prototype.shouldRoll = function() {
@@ -1,78 +0,0 @@
-var events = require('events'),
- Dequeue = require('dequeue'),
- util = require('util');
-
-module.exports = BufferedWriteStream;
-
-function BufferedWriteStream(stream) {
- var that = this;
- this.stream = stream;
- this.buffer = new Dequeue();
- this.canWrite = false;
- this.bytes = 0;
-
- this.stream.on("open", function() {
- that.canWrite = true;
- that.flushBuffer();
- });
-
- this.stream.on("error", function (err) {
- that.emit("error", err);
- });
-
- this.stream.on("drain", function() {
- that.canWrite = true;
- that.flushBuffer();
- });
-}
-
-util.inherits(BufferedWriteStream, events.EventEmitter);
-
-Object.defineProperty(
- BufferedWriteStream.prototype,
- "fd",
- {
- get: function() { return this.stream.fd; },
- set: function(newFd) {
- this.stream.fd = newFd;
- this.bytes = 0;
- }
- }
-);
-
-Object.defineProperty(
- BufferedWriteStream.prototype,
- "bytesWritten",
- {
- get: function() { return this.bytes; }
- }
-);
-
-BufferedWriteStream.prototype.write = function(data, encoding) {
- this.buffer.push({ data: data, encoding: encoding });
- this.flushBuffer();
-};
-
-BufferedWriteStream.prototype.end = function(data, encoding) {
- if (data) {
- this.buffer.push({ data: data, encoding: encoding });
- }
- this.flushBufferEvenIfCannotWrite();
-};
-
-BufferedWriteStream.prototype.writeToStream = function(toWrite) {
- this.bytes += toWrite.data.length;
- this.canWrite = this.stream.write(toWrite.data, toWrite.encoding);
-};
-
-BufferedWriteStream.prototype.flushBufferEvenIfCannotWrite = function() {
- while (this.buffer.length > 0) {
- this.writeToStream(this.buffer.shift());
- }
-};
-
-BufferedWriteStream.prototype.flushBuffer = function() {
- while (this.buffer.length > 0 && this.canWrite) {
- this.writeToStream(this.buffer.shift());
- }
-};
@@ -23,9 +23,7 @@ function DateRollingFileStream(filename, pattern, options, now) {
debug("this.now is " + this.now + ", now is " + now);
DateRollingFileStream.super_.call(this, filename, options);
- this.rollBeforeWrite = true;
}
-
util.inherits(DateRollingFileStream, BaseRollingFileStream);
DateRollingFileStream.prototype.shouldRoll = function() {
@@ -45,14 +43,12 @@ DateRollingFileStream.prototype.roll = function(filename, callback) {
newFilename = filename + this.previousTime;
debug("Starting roll");
- debug("Queueing up data until we've finished rolling");
- debug("Flushing underlying stream");
- this.flush();
async.series([
- deleteAnyExistingFile,
- renameTheCurrentFile,
- openANewFile
+ this.closeTheStream.bind(this),
+ deleteAnyExistingFile,
+ renameTheCurrentFile,
+ this.openTheStream.bind(this)
], callback);
function deleteAnyExistingFile(cb) {
@@ -69,21 +65,4 @@ DateRollingFileStream.prototype.roll = function(filename, callback) {
fs.rename(filename, newFilename, cb);
}
- function openANewFile(cb) {
- debug("Opening a new file");
- fs.open(
- filename,
- that.options.flags,
- that.options.mode,
- function (err, fd) {
- debug("opened new file");
- var oldLogFileFD = that.fd;
- that.fd = fd;
- that.writable = true;
- fs.close(oldLogFileFD, cb);
- }
- );
- }
-
-
};
Oops, something went wrong.

0 comments on commit 65e490c

Please sign in to comment.