Compare commits

...

2 Commits

Author SHA1 Message Date
Fabian Stamm
d39e13dfe1 Add another strategy to logging, which reduces memory leak risk 2021-05-08 22:53:32 +02:00
Fabian Stamm
0742490527 Changing filewrite behavior 2021-05-08 22:43:46 +02:00
2 changed files with 26 additions and 14 deletions

View File

@ -1,6 +1,6 @@
{
"name": "@hibas123/nodelogging",
"version": "3.0.4",
"version": "3.0.6",
"description": "",
"main": "out/index.js",
"types": "out/index.d.ts",
@ -32,7 +32,7 @@
"typescript": "^4.2.4"
},
"dependencies": {
"@hibas123/logging": "^3.0.3",
"@hibas123/logging": "^3.0.6",
"@hibas123/utils": "^2.2.18"
}
}

View File

@ -7,12 +7,7 @@ const MAX_FILE_SIZE = 500000000;
export class LoggingFiles implements Adapter {
file: Files;
constructor(
private filename: string,
private error = false,
private maxFileSize = MAX_FILE_SIZE,
private noPrefix = false
) {}
constructor(private filename: string, private maxFileSize = MAX_FILE_SIZE) {}
init() {
if (!this.file) {
@ -38,6 +33,21 @@ export class LoggingFiles implements Adapter {
//TODO: Optimise write path
const Debounce = (callback: () => void, iv = 500) => {
let to: any;
return {
trigger: () => {
if (!to) {
to = setTimeout(() => {
to = undefined;
callback();
}, iv);
}
},
};
};
export class Files {
private open = 0;
@ -57,21 +67,22 @@ export class Files {
private size: number = 0;
private stream: fs.WriteStream = undefined;
private lock = new Lock();
private debounce = Debounce(() => this.checkQueue);
private $initialized = false;
#initialized = false;
public get initlialized() {
return this.$initialized;
return this.#initialized;
}
private constructor(private file: string) {}
public async init(maxFileSize: number) {
if (this.$initialized) return;
if (this.#initialized) return;
let lock = await this.lock.getLock();
this.maxFileSize == maxFileSize;
await this.initializeFile();
this.$initialized = true;
this.#initialized = true;
lock.release();
this.checkQueue();
}
@ -104,7 +115,7 @@ export class Files {
this.size = size;
} catch (e) {
console.log(e);
//ToDo is this the right behavior?
//TODO: is this the right behavior?
process.exit(1);
}
}
@ -129,6 +140,7 @@ export class Files {
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.
@ -165,7 +177,7 @@ export class Files {
public write(data: Buffer) {
this.queue.push(data);
this.checkQueue();
this.debounce.trigger();
}
public dispose() {}