Skip to content

Commit

Permalink
No more event name export
Browse files Browse the repository at this point in the history
  • Loading branch information
sdolard committed Mar 29, 2012
1 parent c4e7331 commit 2e54f63
Showing 1 changed file with 13 additions and 24 deletions.
37 changes: 13 additions & 24 deletions lib/logtofile.js
Expand Up @@ -26,14 +26,7 @@ fs = require('fs'),
EventEmitter = require('events').EventEmitter,
util = require('util'),
zlib = require('zlib'),
DEFAULT_WRITE_DELAY = 200,
E_ERROR = 'error', // event
E_WRITTING = 'writting', // event
E_WRITTEN = 'written', // event
E_WRITE = 'write', // event
E_BACKUPED = 'backuped', // event
E_GZIPPING = 'gzipping', // event
E_GZIPPED = 'gzipped'; // event
DEFAULT_WRITE_DELAY = 200;


/**
Expand All @@ -42,6 +35,9 @@ E_GZIPPED = 'gzipped'; // event
* It can be called to in buffer drain context
*/
function onTimeout(logFile, origin) {
var
buffer;

// Two possible origin
// - drain
// - timeout
Expand All @@ -54,25 +50,25 @@ function onTimeout(logFile, origin) {
}

if (logFile._writtenSize < logFile.fileMaxSize) {
var buffer = logFile._buffers.shift();
buffer = logFile._buffers.shift();
if (!buffer) {
logFile._eemit(E_WRITTEN, logFile.filePath);
logFile._eemit('written', logFile.filePath);
logFile._waitDrain = false; // No more buffer to write, timer can restart
return;
}

logFile._writableStream.write(buffer.b.slice(0, buffer.usedBytes));
if (!logFile._waitDrain) {
// If it's first time we start to write, we emit this event
logFile._eemit(E_WRITTING, logFile.filePath);
logFile._eemit('writting', logFile.filePath);
logFile._waitDrain = true; // Timer has not to run
}
logFile._eemit(E_WRITE, logFile.filePath);
logFile._eemit('write', logFile.filePath);
logFile._writtenSize += buffer.usedBytes;
return;
}
if(logFile._writtenSize !== 0 && logFile._buffers.length === 0) {
logFile._eemit(E_WRITTEN, logFile.filePath);
logFile._eemit('written', logFile.filePath);
}
logFile._rotationPending = true;
logFile._writableStream.destroySoon(); // this will flush before then close
Expand Down Expand Up @@ -347,12 +343,12 @@ LogToFile.prototype._doFileRotation = function() {
compressFile = function(filePath) {
var
gzippedFilePath = filePath + '.gz';
me._eemit(E_GZIPPING, filePath, gzippedFilePath);
me._eemit('gzipping', filePath, gzippedFilePath);
me.log('gzipping %s...', filePath);
compressReadStream = fs.createReadStream(filePath);
compressWriteStream = fs.createWriteStream(gzippedFilePath);
compressWriteStream.on('close', function() {
me._eemit(E_GZIPPED, filePath, gzippedFilePath);
me._eemit('gzipped', filePath, gzippedFilePath);
me.log('"%s" compression done.', filePath);
fs.unlink(filePath, function() {
me.log("me._createWriteStream(1);");
Expand Down Expand Up @@ -383,7 +379,7 @@ LogToFile.prototype._doFileRotation = function() {
me._eexception(err);
return;
}
me._eemit(E_BACKUPED, oldFilePath, filePath);
me._eemit('backuped', oldFilePath, filePath);
if (me.gzipBackupFile) {
compressFile(filePath);
} else {
Expand Down Expand Up @@ -486,7 +482,7 @@ LogToFile.prototype._eexception = function(exception) {
error.code = exception.code;
}

this.emit(E_ERROR, error);
this.emit('error', error);
if (this.verbose) {
console.log(error.stack);
}
Expand Down Expand Up @@ -514,11 +510,4 @@ LogToFile.prototype._eemit = function(event){
* Exports
*******************************************************************************/
exports.create = function(config) { return new LogToFile(config); };
exports.E_ERROR = E_ERROR;
exports.E_WRITTING = E_WRITTING;
exports.E_WRITE = E_WRITE;
exports.E_WRITTEN = E_WRITTEN;
exports.E_BACKUPED = E_BACKUPED;
exports.E_GZIPPING = E_GZIPPING;
exports.E_GZIPPED = E_GZIPPED;

0 comments on commit 2e54f63

Please sign in to comment.