Making it logging 2.0 compatible
This commit is contained in:
@ -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<Message>) {
|
||||
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<string, Files>();
|
||||
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) {
|
||||
|
Reference in New Issue
Block a user