123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274 |
- const fs = require('fs');
- const path = require('path');
- const {logger} = require("./logger");
- const {rustConfig} = require("../config");
- const http = require("./http");
- const readline = require('readline');
- // 检查目录是否存在,不存在新建
- function checkPathSync(filePath) {
- // 将相对路径转换为绝对路径
- const directoryPath = path.resolve(filePath);
- // 同步检查目录是否存在
- if (!fs.existsSync(directoryPath)) {
- // 目录不存在,需要创建它
- logger.info('目录不存在,正在创建...');
- try {
- // 同步创建目录
- fs.mkdirSync(directoryPath, {recursive: true});
- logger.info('目录已成功创建');
- } catch (error) {
- // 创建目录失败
- logger.error('创建目录失败:', error);
- return false
- }
- } else {
- // 目录已存在
- logger.info('目录已存在');
- }
- return true
- }
- // 检查文件是否存在
- function checkFilePath(filePath) {
- // 同步检查文件是否存在
- const directoryPath = path.resolve(filePath);
- if (fs.existsSync(directoryPath)) {
- logger.info('文件存在:' + filePath);
- return true
- } else {
- logger.error('文件不存在:' + filePath);
- return false
- }
- }
- // 创建文件
- function writeFile(filePath, text, breakFun) {
- fs.writeFile(filePath, text, (err) => {
- if (err) {
- logger.error('写入文件时发生错误:', err);
- breakFun(err)
- } else {
- logger.log('文件已成功写入!');
- breakFun(null, true)
- }
- });
- }
- // 同步复制
- function copyFileSync(source, destination) {
- try {
- // 同步复制文件
- fs.copyFileSync(source, destination);
- // logger.info("成功");
- return true;
- } catch (err) {
- return false;
- // logger.info("失败");
- }
- }
- // // 读取日志(倒叙)指定行数
- // function readLastNLines(filePath, n) {
- // const content = fs.readFileSync(filePath, 'utf8');
- // const lines = content.trim().split('\n');
- // const lastNLines = lines.slice(-n).join('\n');
- //
- // // 此处处理或输出最后n行的内容
- // // console.log(lastNLines);
- // return lastNLines.split("\n").reverse();
- // }
- // 下载文件
- function dowFile(url, fileName, dowPath, headers) {
- return http.dowFile(url, fileName, dowPath, headers)
- }
- // 删除文件
- function delFile() {
- }
- /***********************************/
- function getLastFile(dirPath, number, callback) {
- fs.readdir(dirPath, async (err, files) => {
- if (err) {
- logger.error(`无法列出目录。`, err);
- // process.exit(1);
- } else {
- let fileList = [];
- //提示日志默认是根据日志生成的,拿到的files 也是 按照生成日志排序的,所以
- // 按照倒叙获取,如果一个文件内容满足num,就不在继续查询,如果不够,继续获取文件
- // logger.info("文件:",files)
- for (file of files) {
- const filePath = path.join(dirPath, file);
- const stats = await fs.promises.stat(filePath);
- if (stats.isFile()) {
- fileList.push({name: file, time: stats.birthtime.getTime()});
- // logger.info("文件:" + filePath + "时间:" + stats.birthtime.getTime())
- }
- }
- fileList.sort((a, b) => b.time - a.time);
- // for (a of fileList) {
- // logger.info("文件cccccccc22222222:" + a.name + "\ttime:" + a.time)
- // }
- lastFileList = fileList.slice(0, number != -1 ? number : fileList.length);
- lastFileNameList = lastFileList.map((item) => item.name);
- callback(lastFileNameList.reverse(), lastFileList.reverse());
- }
- });
- }
- // 读取日志(倒叙)指定行数
- async function readLastNLines(dirPath, filePathList, n) {
- try {
- // for (var i = 0; i < filePathList.length; i++) {
- // var item = filePathList[i];
- // logger.info("日志文件2222222:" + item)
- // }
- const fileList = [];
- var count = 0;
- for (var i = filePathList.length - 1; i >= 0; i--) {
- var item = filePathList[i];
- const filepath = `${dirPath}/${item}`;
- logger.info("日志文件:" + filepath)
- //老版本 加载整个文件
- // const content = fs.readFileSync(`${filepath}`, "utf8");
- // let lines = content.trim().split("\n");
- //新版本:流式读取和逐行处理
- const lines = await readLinesFromEnd(filepath, n - count);
- // logger.info("日:" + lines)
- fileList.push({
- filePath: item,
- lines: lines,
- })
- count += lines.length;
- if (count >= n) {
- break
- }
- }
- fileList.reverse();
- // const allFile = [].concat(...fileList.map((item) => item.lines));
- // let lastNLines = allFile.slice(-n).join("\n");
- // return lastNLines.split("\n").reverse();
- const allFile = [].concat(...fileList.map((item) => item.lines));
- const lastNLines = allFile.slice(-n).reverse();
- return lastNLines;
- } catch (e) {
- logger.info('获取日志异常了~~1111111111111111', e);
- }
- }
- async function readLinesFromEnd(filePath, maxLines) {
- const lines = [];
- const fileSize = fs.statSync(filePath).size;
- const chunkSize = 1024 * 10 * 5; // 每次读取的块大小
- if (fileSize < chunkSize) {
- const fileStream = fs.createReadStream(filePath, {
- start: Math.max(0, 0),
- end: fileSize
- });
- 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;
- }
- }
- } 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;
- }
- 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 = {
- dowFile,
- checkFilePath,
- checkPathSync,
- writeFile,
- copyFileSync,
- readLastNLines,
- getLastFile
- }
|