UNPKG

@intuitionrobotics/thunderstorm

Version:
39 lines 1.43 kB
import { LogClient_BaseRotate } from "@intuitionrobotics/ts-common"; import * as fs from "fs"; import { WriteStream } from "fs"; export class LogClient_File extends LogClient_BaseRotate { pathToFolder; buffer; constructor(name, pathToFolder, maxEntries = 10, maxSize = 1024 * 1024) { super(name, maxEntries, maxSize); this.pathToFolder = pathToFolder; if (!fs.existsSync(pathToFolder)) fs.mkdirSync(pathToFolder, { recursive: true }); const defaultLogfile = this.getFileName(); if (fs.existsSync(defaultLogfile)) this.bufferSize = fs.statSync(`${defaultLogfile}`).size; this.prepare(); } getFileName(index = 0) { return `${this.pathToFolder}/${this.name}-${index}.txt`; } printLogMessage(log) { this.buffer.write(log); } rotateBuffer(fromIndex, toIndex) { if (fs.existsSync(this.getFileName(fromIndex))) { console.log(`rotating ${fromIndex} => ${toIndex}`); fs.renameSync(this.getFileName(fromIndex), this.getFileName(toIndex)); } } cleanup() { const fileName = this.getFileName(this.maxEntries - 1); if (fs.existsSync(fileName)) fs.unlinkSync(fileName); this.buffer.end(); } prepare() { this.buffer = fs.createWriteStream(this.getFileName(), { flags: 'a' }); } } //# sourceMappingURL=LogClient_File.js.map