|
@@ -142,7 +142,7 @@ async function readLastNLines(dirPath, filePathList, n) {
|
|
|
|
|
|
|
|
|
|
|
|
- const lines = await readLinesFromEnd(filepath,n-count);
|
|
|
+ const lines = await readLinesFromEnd(filepath, n - count);
|
|
|
logger.info("日:" + lines)
|
|
|
fileList.push({
|
|
|
filePath: item,
|
|
@@ -172,21 +172,19 @@ async function readLinesFromEnd(filePath, maxLines) {
|
|
|
const lines = [];
|
|
|
const fileSize = fs.statSync(filePath).size;
|
|
|
const chunkSize = 1024;
|
|
|
- let buffer = '';
|
|
|
|
|
|
|
|
|
- let forNum = (fileSize/chunkSize)+1;
|
|
|
- for (var i = 1 ;i <= forNum;i++){
|
|
|
- let position = fileSize - (chunkSize * i);
|
|
|
- if(position < 0){
|
|
|
- position = 0;
|
|
|
- }
|
|
|
|
|
|
+ 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: fileSize
|
|
|
+ end: z
|
|
|
});
|
|
|
|
|
|
+ let buffer = '';
|
|
|
for await (const chunk of fileStream) {
|
|
|
buffer = chunk + buffer;
|
|
|
let lineEndIndex = buffer.length;
|
|
@@ -212,63 +210,30 @@ async function readLinesFromEnd(filePath, maxLines) {
|
|
|
position = 0;
|
|
|
}
|
|
|
}
|
|
|
+ }
|
|
|
+
|
|
|
+ return lines;
|
|
|
+}
|
|
|
+
|
|
|
+async function readLines(filePath, maxLines) {
|
|
|
+ const fileStream = fs.createReadStream(filePath);
|
|
|
+ const rl = readline.createInterface({
|
|
|
+ input: fileStream,
|
|
|
+ crlfDelay: Infinity
|
|
|
+ });
|
|
|
+
|
|
|
+ const lines = [];
|
|
|
+ for await (const line of rl) {
|
|
|
+ lines.push(line);
|
|
|
if (lines.length >= maxLines) {
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
+ rl.close();
|
|
|
+ fileStream.close();
|
|
|
return lines;
|
|
|
}
|
|
|
- async function readLines(filePath, maxLines) {
|
|
|
- const fileStream = fs.createReadStream(filePath);
|
|
|
- const rl = readline.createInterface({
|
|
|
- input: fileStream,
|
|
|
- crlfDelay: Infinity
|
|
|
- });
|
|
|
-
|
|
|
- const lines = [];
|
|
|
- for await (const line of rl) {
|
|
|
- lines.push(line);
|
|
|
- if (lines.length >= maxLines) {
|
|
|
- break;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- rl.close();
|
|
|
- fileStream.close();
|
|
|
- return lines;
|
|
|
- }
|
|
|
|
|
|
|
|
|
module.exports = {
|