|
@@ -143,7 +143,7 @@ async function readLastNLines(dirPath, filePathList, n) {
|
|
|
|
|
|
//新版本:流式读取和逐行处理
|
|
//新版本:流式读取和逐行处理
|
|
const lines = await readLinesFromEnd(filepath, n - count);
|
|
const lines = await readLinesFromEnd(filepath, n - count);
|
|
- logger.info("日:" + lines)
|
|
|
|
|
|
+ // logger.info("日:" + lines)
|
|
fileList.push({
|
|
fileList.push({
|
|
filePath: item,
|
|
filePath: item,
|
|
lines: lines,
|
|
lines: lines,
|
|
@@ -171,17 +171,12 @@ async function readLastNLines(dirPath, filePathList, n) {
|
|
async function readLinesFromEnd(filePath, maxLines) {
|
|
async function readLinesFromEnd(filePath, maxLines) {
|
|
const lines = [];
|
|
const lines = [];
|
|
const fileSize = fs.statSync(filePath).size;
|
|
const fileSize = fs.statSync(filePath).size;
|
|
- const chunkSize = 1024*10*5; // 每次读取的块大小
|
|
|
|
|
|
+ const chunkSize = 1024 * 10 * 5; // 每次读取的块大小
|
|
|
|
|
|
-
|
|
|
|
-
|
|
|
|
- let forNum = (fileSize / chunkSize) + 1;
|
|
|
|
- for (let i = 1;i<= forNum; i++) {
|
|
|
|
- let position = fileSize - chunkSize *i;
|
|
|
|
- let z = position + chunkSize
|
|
|
|
|
|
+ if (fileSize < chunkSize) {
|
|
const fileStream = fs.createReadStream(filePath, {
|
|
const fileStream = fs.createReadStream(filePath, {
|
|
- start: Math.max(position, 0),
|
|
|
|
- end: z
|
|
|
|
|
|
+ start: Math.max(0, 0),
|
|
|
|
+ end: fileSize
|
|
});
|
|
});
|
|
|
|
|
|
let buffer = '';
|
|
let buffer = '';
|
|
@@ -210,6 +205,43 @@ async function readLinesFromEnd(filePath, maxLines) {
|
|
position = 0;
|
|
position = 0;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
+ } else {
|
|
|
|
+ let forNum = (fileSize / chunkSize) + 1;
|
|
|
|
+ for (let i = 1; i <= forNum; i++) {
|
|
|
|
+ let position = fileSize - chunkSize * i;
|
|
|
|
+ let z = position + chunkSize
|
|
|
|
+ const fileStream = fs.createReadStream(filePath, {
|
|
|
|
+ start: Math.max(position, 0),
|
|
|
|
+ end: z
|
|
|
|
+ });
|
|
|
|
+
|
|
|
|
+ let buffer = '';
|
|
|
|
+ for await (const chunk of fileStream) {
|
|
|
|
+ buffer = chunk + buffer;
|
|
|
|
+ let lineEndIndex = buffer.length;
|
|
|
|
+
|
|
|
|
+ while (lineEndIndex >= 0 && lines.length < maxLines) {
|
|
|
|
+ const lineStartIndex = buffer.lastIndexOf('\n', lineEndIndex - 1);
|
|
|
|
+ if (lineStartIndex === -1) {
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+ const line = buffer.substring(lineStartIndex + 1, lineEndIndex).trim();
|
|
|
|
+ if (line) {
|
|
|
|
+ lines.push(line);
|
|
|
|
+ }
|
|
|
|
+ lineEndIndex = lineStartIndex;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (lines.length >= maxLines) {
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ position -= chunkSize;
|
|
|
|
+ if (position < 0) {
|
|
|
|
+ position = 0;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
return lines;
|
|
return lines;
|