8000 moved streams code around, added stub for DateRollingFileStream · Web5design/log4js-node@be1272c · GitHub
[go: up one dir, main page]

Skip to content

Commit be1272c

Browse files
author
Gareth Jones
committed
moved streams code around, added stub for DateRollingFileStream
1 parent cbc1dd3 commit be1272c

9 files changed

+349
-271
lines changed

lib/streams.js

Lines changed: 0 additions & 268 deletions
This file was deleted.

lib/streams/BaseRollingFileStream.js

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
var fs = require('fs'),
2+
util = require('util');
3+
4+
function debug(message) {
5+
// util.debug(message);
6+
// console.log(message);
7+
}
8+
9+
module.exports = BaseRollingFileStream;
10+
11+
function BaseRollingFileStream(filename, options) {
12+
this.filename = filename;
13+
this.options = options || { encoding: 'utf8', mode: 0644, flags: 'a' };
14+
this.rolling = false;
15+
this.writesWhileRolling = [];
16+
this.currentSize = 0;
17+
18+
function currentFileSize(file) {
19+
var fileSize = 0;
20+
try {
21+
fileSize = fs.statSync(file).size;
22+
} catch (e) {
23+
// file does not exist
24+
}
25+
return fileSize;
26+
}
27+
28+
function throwErrorIfArgumentsAreNotValid() {
29+
if (!filename) {
30+
throw new Error("You must specify a filename");
31+
}
32+
}
33+
34+
throwErrorIfArgumentsAreNotValid();
35+
36+
BaseRollingFileStream.super_.call(this, this.filename, this.options);
37+
this.currentSize = currentFileSize(this.filename);
38+
}
39+
util.inherits(BaseRollingFileStream, fs.FileWriteStream);
40+
41+
BaseRollingFileStream.prototype.initRolling = function() {
42+
var that = this;
43+
44+
function emptyRollingQueue() {
45+
debug("emptying the rolling queue");
46+
var toWrite;
47+
while ((toWrite = that.writesWhileRolling.shift())) {
48+
BaseRollingFileStream.super_.prototype.write.call(that, toWrite.data, toWrite.encoding);
49+
that.currentSize += toWrite.data.length;
50+
if (that.shouldRoll()) {
51+
that.flush();
52+
return true;
53+
}
54+
}
55+
that.flush();
56+
return false;
57+
}
58+
59+
this.rolling = true;
60+
this.roll(this.filename, function() {
61+
that.currentSize = 0;
62+
that.rolling = emptyRollingQueue();
63+
if (that.rolling) {
64+
process.nextTick(function() { that.initRolling(); });
65+
}
66+
});
67+
};
68+
69+
BaseRollingFileStream.prototype.write = function(data, encoding) {
70+
if (this.rolling) {
71+
this.writesWhileRolling.push({ data: data, encoding: encoding });
72+
return false;
73+
} else {
74+
var canWrite = BaseRollingFileStream.super_.prototype.write.call(this, data, encoding);
75+
this.currentSize += data.length;
76+
debug('current size = ' + this.currentSize);
77+
if (this.shouldRoll()) {
78+
this.initRolling();
79+
}
80+
return canWrite;
81+
}
82+
};
83+
84+
BaseRollingFileStream.prototype.shouldRoll = function() {
85+
return false; // default behaviour is never to roll
86+
};
87+
88+
BaseRollingFileStream.prototype.roll = function(filename, callback) {
89+
callback(); // default behaviour is not to do anything
90+
};
91+

0 commit comments

Comments
 (0)
0