Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add _writev again for batch write logs #58

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 15 additions & 0 deletions lib/RollingFileWriteStream.js
Expand Up @@ -115,6 +115,21 @@ class RollingFileWriteStream extends Writable {
this.currentFileStream.end("", this.options.encoding, callback);
}

_writev(chunks, callback) {
const buffers = [];
const encoding = 'buffer';
const len = chunks.length;

for (let i = 0, chunk = null; i < len; ++i) {
chunk = chunks[i];
buffers.push(encoding === chunk.encoding ?
chunk.chunk :
new Buffer(chunk.chunk, chunk.encoding));
}

return this._write(Buffer.concat(buffers), encoding, callback);
}

_write(chunk, encoding, callback) {
this._shouldRoll().then(() => {
debug(
Expand Down
21 changes: 12 additions & 9 deletions test/RollingFileWriteStream-test.js
Expand Up @@ -1130,9 +1130,10 @@ describe("RollingFileWriteStream", () => {
fs.ensureFileSync(fileObj.path);
fs.writeFileSync(fileObj.path, "This is exactly 30 bytes long\n");
s = new RollingFileWriteStream(fileObj.path, { maxSize: 35 });
s.write("one\n", "utf8"); //34
s.write("two\n", "utf8"); //38 - file should be rotated next time
s.write("three\n", "utf8", done); // this should be in a new file.
s.write("one\n", "utf8", function () { //34
s.write("two\n", "utf8"); //38 - file should be rotated next time
s.write("three\n", "utf8", done); // this should be in a new file.
});
});

after(done => {
Expand Down Expand Up @@ -1169,9 +1170,10 @@ describe("RollingFileWriteStream", () => {
maxSize: 35,
flags: "a+"
});
s.write("one\n", "utf8"); //34
s.write("two\n", "utf8"); //38 - file should be rotated next time
s.write("three\n", "utf8", done); // this should be in a new file.
s.write("one\n", "utf8", function () { //34
s.write("two\n", "utf8"); //38 - file should be rotated next time
s.write("three\n", "utf8", done); // this should be in a new file.
});
});

after(done => {
Expand Down Expand Up @@ -1211,9 +1213,10 @@ describe("RollingFileWriteStream", () => {
maxSize: 30,
numToKeep: 5
});
s.write("This is exactly 30 bytes long\n", "utf8"); // base.1 -> base.2, base -> base.1
s.write("This is exactly 30 bytes long\n", "utf8"); // base.2 -> base.3, base.1 -> base.2, base -> base.1
s.write("three\n", "utf8", done); // base.3 -> base.4, base.2 -> base.3, base.1 -> base.2, base -> base.1
s.write("This is exactly 30 bytes long\n", "utf8", function () { // base.1 -> base.2, base -> base.1
s.write("This is exactly 30 bytes long\n", "utf8"); // base.2 -> base.3, base.1 -> base.2, base -> base.1
s.write("three\n", "utf8", done); // base.3 -> base.4, base.2 -> base.3, base.1 -> base.2, base -> base.1
});
});

after(done => {
Expand Down