UNPKG

@intuitionrobotics/thunderstorm

Version:
40 lines 1.52 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.LogClient_File = void 0; const ts_common_1 = require("@intuitionrobotics/ts-common"); const fs = require("fs"); class LogClient_File extends ts_common_1.LogClient_BaseRotate { 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' }); } } exports.LogClient_File = LogClient_File; //# sourceMappingURL=LogClient_File.js.map