diff --git a/package-lock.json b/package-lock.json index 4899eab..c8c15b8 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,26 +1,26 @@ { "name": "@hibas123/nodelogging", - "version": "1.6.0", + "version": "2.0.0", "lockfileVersion": 1, "requires": true, "dependencies": { "@hibas123/logging": { - "version": "1.1.0", - "resolved": "https://registry.npmjs.org/@hibas123/logging/-/logging-1.1.0.tgz", - "integrity": "sha512-EaDyvvWDD+SikLgP6WTvTCR6Sk9Kp2BZkDbUk0Rz4pF2B4dGyO8yRI8qhBEtjbMXGqedeM5mQDGgvY5QUDevVg==", + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/@hibas123/logging/-/logging-2.0.0.tgz", + "integrity": "sha512-v4e7xiXGvAKpq6nxLA2TVoEy/DTT7DZkqEKh8eZ1XiAQAJh0PnmF1GP/TdHWvDTPMG/U8kMHArrGllVxM6xtkg==", "requires": { - "@hibas123/utils": "^2.0.2" + "@hibas123/utils": "^2.0.5" } }, "@hibas123/utils": { - "version": "2.0.2", - "resolved": "https://registry.npmjs.org/@hibas123/utils/-/utils-2.0.2.tgz", - "integrity": "sha512-5yrpuuUv0YlL2BixiTQLta4EmPLMNZ9Waorymu6dbERiSIcBxDs8mcJDUDW7CGiewpHHQUMsqLycLUYPDx0LWw==" + "version": "2.0.5", + "resolved": "https://registry.npmjs.org/@hibas123/utils/-/utils-2.0.5.tgz", + "integrity": "sha512-LI1Xw0QArpm0JqCUmuxRG7dgmjvyLPmJehcNRqHrWuCwmDRoPFe0PwmZsp8/saz6pCL2A4u0utVHWG2PG4lSyg==" }, "@types/node": { - "version": "11.11.7", - "resolved": "https://registry.npmjs.org/@types/node/-/node-11.11.7.tgz", - "integrity": "sha512-bHbRcyD6XpXVLg42QYaQCjvDXaCFkvb3WbCIxSDmhGbJYVroxvYzekk9QGg1beeIawfvSLkdZpP0h7jxE4ihnA==", + "version": "11.13.0", + "resolved": "https://registry.npmjs.org/@types/node/-/node-11.13.0.tgz", + "integrity": "sha512-rx29MMkRdVmzunmiA4lzBYJNnXsW/PhG4kMBy2ATsYaDjGGR75dCFEVVROKpNwlVdcUX3xxlghKQOeDPBJobng==", "dev": true }, "abbrev": { @@ -805,7 +805,8 @@ "ansi-regex": { "version": "2.1.1", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "aproba": { "version": "1.2.0", @@ -826,12 +827,14 @@ "balanced-match": { "version": "1.0.0", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "brace-expansion": { "version": "1.1.11", "bundled": true, "dev": true, + "optional": true, "requires": { "balanced-match": "^1.0.0", "concat-map": "0.0.1" @@ -846,17 +849,20 @@ "code-point-at": { "version": "1.1.0", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "concat-map": { "version": "0.0.1", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "console-control-strings": { "version": "1.1.0", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "core-util-is": { "version": "1.0.2", @@ -973,7 +979,8 @@ "inherits": { "version": "2.0.3", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "ini": { "version": "1.3.5", @@ -985,6 +992,7 @@ "version": "1.0.0", "bundled": true, "dev": true, + "optional": true, "requires": { "number-is-nan": "^1.0.0" } @@ -999,6 +1007,7 @@ "version": "3.0.4", "bundled": true, "dev": true, + "optional": true, "requires": { "brace-expansion": "^1.1.7" } @@ -1006,12 +1015,14 @@ "minimist": { "version": "0.0.8", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "minipass": { "version": "2.3.5", "bundled": true, "dev": true, + "optional": true, "requires": { "safe-buffer": "^5.1.2", "yallist": "^3.0.0" @@ -1030,6 +1041,7 @@ "version": "0.5.1", "bundled": true, "dev": true, + "optional": true, "requires": { "minimist": "0.0.8" } @@ -1110,7 +1122,8 @@ "number-is-nan": { "version": "1.0.1", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "object-assign": { "version": "4.1.1", @@ -1122,6 +1135,7 @@ "version": "1.4.0", "bundled": true, "dev": true, + "optional": true, "requires": { "wrappy": "1" } @@ -1207,7 +1221,8 @@ "safe-buffer": { "version": "5.1.2", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "safer-buffer": { "version": "2.1.2", @@ -1243,6 +1258,7 @@ "version": "1.0.2", "bundled": true, "dev": true, + "optional": true, "requires": { "code-point-at": "^1.0.0", "is-fullwidth-code-point": "^1.0.0", @@ -1262,6 +1278,7 @@ "version": "3.0.1", "bundled": true, "dev": true, + "optional": true, "requires": { "ansi-regex": "^2.0.0" } @@ -1305,12 +1322,14 @@ "wrappy": { "version": "1.0.2", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "yallist": { "version": "3.0.3", "bundled": true, - "dev": true + "dev": true, + "optional": true } } }, @@ -2767,9 +2786,9 @@ "dev": true }, "typescript": { - "version": "3.3.4000", - "resolved": "https://registry.npmjs.org/typescript/-/typescript-3.3.4000.tgz", - "integrity": "sha512-jjOcCZvpkl2+z7JFn0yBOoLQyLoIkNZAs/fYJkUG6VKy6zLPHJGfQJYFHzibB6GJaF/8QrcECtlQ5cpvRHSMEA==", + "version": "3.4.1", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-3.4.1.tgz", + "integrity": "sha512-3NSMb2VzDQm8oBTLH6Nj55VVtUEpe/rgkIzMir0qVoLyjDZlnMBva0U6vDiV3IH+sl/Yu6oP5QwsAQtHPmDd2Q==", "dev": true }, "undefsafe": { diff --git a/package.json b/package.json index 7e0ec62..300a423 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@hibas123/nodelogging", - "version": "1.7.0", + "version": "2.0.1", "description": "", "main": "out/index.js", "types": "out/index.d.ts", @@ -20,13 +20,13 @@ "author": "Fabian Stamm", "license": "MIT", "devDependencies": { - "@types/node": "^11.11.7", + "@types/node": "^11.13.0", "concurrently": "^4.1.0", "nodemon": "^1.17.4", - "typescript": "^3.3.4000" + "typescript": "^3.4.1" }, "dependencies": { - "@hibas123/logging": "^1.1.0", - "@hibas123/utils": "^2.0.2" + "@hibas123/logging": "^2.0.0", + "@hibas123/utils": "^2.0.5" } -} +} \ No newline at end of file diff --git a/src/filewriter.ts b/src/filewriter.ts index 0b7fa0f..4b4fcf6 100644 --- a/src/filewriter.ts +++ b/src/filewriter.ts @@ -4,18 +4,18 @@ import * as path from "path"; import { Adapter, Message, LoggingTypes } from "@hibas123/logging"; -const maxFileSize = 500000000; +const MAX_FILE_SIZE = 500000000; export class LoggingFiles implements Adapter { file: Files; - constructor(filename: string, private error = false) { + constructor(filename: string, private error = false, private maxFileSize = MAX_FILE_SIZE) { this.file = Files.getFile(filename); } init(observable: ObservableInterface) { observable.subscribe(this.onMessage.bind(this)); - return this.file.init(); + return this.file.init(this.maxFileSize); } flush(sync: boolean) { @@ -26,20 +26,21 @@ export class LoggingFiles implements Adapter { // Just ignore all non error messages, if this.error is set if (this.error && message.type !== LoggingTypes.Error) return; - let txt = message.text.formatted.join("\n") + "\n"; - txt = txt.replace(/[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g, ""); - let index = txt.indexOf("\x1b"); - while (index >= 0) { - txt = txt.substring(0, index) + txt.substring(index + 5, txt.length); - index = txt.indexOf("\x1b"); - } + let txt = message.text.formatted.map(fmt => fmt.map(f => f.text).join("") + "\n").join(""); + let msg = Buffer.from(txt); this.file.write(msg); } + + close() { + this.file.close(); + } } export class Files { + private open = 0; + private static files = new Map(); static getFile(filename: string): Files { filename = path.resolve(filename); @@ -48,9 +49,11 @@ export class Files { file = new Files(filename); this.files.set(filename, file); } + file.open++; return file; } + private maxFileSize = MAX_FILE_SIZE; private size: number = 0; private stream: fs.WriteStream = undefined; private lock = new Lock(); @@ -59,10 +62,11 @@ export class Files { private constructor(private file: string) { } - public async init() { + public async init(maxFileSize: number) { if (this.initialized) return; let lock = await this.lock.getLock(); + this.maxFileSize == maxFileSize; await this.initializeFile() this.initialized = true; lock.release(); @@ -84,7 +88,7 @@ export class Files { let size = 0; if (await fsExists(this.file)) { let stats = await fsStat(this.file); - if (new_file || stats.size >= maxFileSize) { + if (new_file || stats.size >= this.maxFileSize) { if (await fsExists(this.file + ".old")) await fsUnlink(this.file + ".old"); await fsMove(this.file, this.file + ".old") @@ -114,6 +118,14 @@ export class Files { lock.release(); } + public async close() { + await this.flush(false); + this.open--; + if (this.open <= 0) { + this.stream.close() + Files.files.delete(this.file); + } + } public flush(sync: boolean) { if (sync) { // if sync flush, the process most likely is in failstate, so checkQueue stopped its work. @@ -132,7 +144,7 @@ export class Files { private async write_to_file(data: Buffer) { try { - if (data.byteLength < maxFileSize && this.size + data.byteLength > maxFileSize) { + if (data.byteLength < this.maxFileSize && this.size + data.byteLength > this.maxFileSize) { await this.initializeFile(true) } this.size += data.byteLength; @@ -149,6 +161,10 @@ export class Files { this.queue.push(data); this.checkQueue() } + + public dispose() { + + } } function fsUnlink(path) {