2021-05-08 20:11:15 +00:00
|
|
|
import { Lock } from "@hibas123/utils";
|
2019-03-23 15:50:12 +00:00
|
|
|
import * as fs from "fs";
|
2019-03-26 01:50:26 +00:00
|
|
|
import * as path from "path";
|
2021-05-08 20:11:15 +00:00
|
|
|
import { Adapter, Message, Formatted } from "@hibas123/logging";
|
2019-04-01 03:51:27 +00:00
|
|
|
|
2019-04-05 02:40:34 +00:00
|
|
|
const MAX_FILE_SIZE = 500000000;
|
2019-03-23 15:50:12 +00:00
|
|
|
|
|
|
|
export class LoggingFiles implements Adapter {
|
|
|
|
file: Files;
|
2021-05-08 20:43:46 +00:00
|
|
|
constructor(private filename: string, private maxFileSize = MAX_FILE_SIZE) {}
|
2019-03-23 15:50:12 +00:00
|
|
|
|
2021-05-08 20:11:15 +00:00
|
|
|
init() {
|
2020-04-09 16:17:32 +00:00
|
|
|
if (!this.file) {
|
|
|
|
this.file = Files.getFile(this.filename);
|
2021-05-08 20:11:15 +00:00
|
|
|
this.file.init(this.maxFileSize);
|
2020-04-09 16:17:32 +00:00
|
|
|
}
|
2019-03-23 15:50:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
flush(sync: boolean) {
|
|
|
|
this.file.flush(sync);
|
|
|
|
}
|
|
|
|
|
|
|
|
onMessage(message: Message) {
|
2021-05-08 20:11:15 +00:00
|
|
|
let msg = Buffer.from(Formatted.strip(message.text) + "\n");
|
2019-03-23 15:50:12 +00:00
|
|
|
this.file.write(msg);
|
|
|
|
}
|
2019-04-05 02:40:34 +00:00
|
|
|
|
|
|
|
close() {
|
|
|
|
this.file.close();
|
2020-04-09 16:17:32 +00:00
|
|
|
this.file = undefined;
|
2019-04-05 02:40:34 +00:00
|
|
|
}
|
2019-03-23 15:50:12 +00:00
|
|
|
}
|
|
|
|
|
2021-05-08 20:11:15 +00:00
|
|
|
//TODO: Optimise write path
|
|
|
|
|
2021-05-08 20:53:32 +00:00
|
|
|
const Debounce = (callback: () => void, iv = 500) => {
|
|
|
|
let to: any;
|
|
|
|
|
|
|
|
return {
|
|
|
|
trigger: () => {
|
|
|
|
if (!to) {
|
|
|
|
to = setTimeout(() => {
|
|
|
|
to = undefined;
|
|
|
|
callback();
|
|
|
|
}, iv);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2019-03-23 15:50:12 +00:00
|
|
|
export class Files {
|
2019-04-05 02:40:34 +00:00
|
|
|
private open = 0;
|
|
|
|
|
2019-03-23 15:50:12 +00:00
|
|
|
private static files = new Map<string, Files>();
|
|
|
|
static getFile(filename: string): Files {
|
|
|
|
filename = path.resolve(filename);
|
|
|
|
let file = this.files.get(filename);
|
|
|
|
if (!file) {
|
|
|
|
file = new Files(filename);
|
|
|
|
this.files.set(filename, file);
|
|
|
|
}
|
2019-04-05 02:40:34 +00:00
|
|
|
file.open++;
|
2019-03-23 15:50:12 +00:00
|
|
|
return file;
|
|
|
|
}
|
|
|
|
|
2019-04-05 02:40:34 +00:00
|
|
|
private maxFileSize = MAX_FILE_SIZE;
|
2019-03-23 15:50:12 +00:00
|
|
|
private size: number = 0;
|
|
|
|
private stream: fs.WriteStream = undefined;
|
|
|
|
private lock = new Lock();
|
2021-05-08 20:53:32 +00:00
|
|
|
private debounce = Debounce(() => this.checkQueue);
|
2019-03-23 15:50:12 +00:00
|
|
|
|
2021-05-08 20:43:46 +00:00
|
|
|
#initialized = false;
|
2019-03-23 15:50:12 +00:00
|
|
|
|
2020-04-09 16:17:32 +00:00
|
|
|
public get initlialized() {
|
2021-05-08 20:43:46 +00:00
|
|
|
return this.#initialized;
|
2020-04-09 16:17:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private constructor(private file: string) {}
|
2019-03-23 15:50:12 +00:00
|
|
|
|
2019-04-05 02:40:34 +00:00
|
|
|
public async init(maxFileSize: number) {
|
2021-05-08 20:43:46 +00:00
|
|
|
if (this.#initialized) return;
|
2019-03-23 15:50:12 +00:00
|
|
|
let lock = await this.lock.getLock();
|
2019-04-05 02:40:34 +00:00
|
|
|
this.maxFileSize == maxFileSize;
|
2020-04-09 16:17:32 +00:00
|
|
|
await this.initializeFile();
|
2021-05-08 20:43:46 +00:00
|
|
|
this.#initialized = true;
|
2019-03-23 15:50:12 +00:00
|
|
|
lock.release();
|
2020-04-09 16:17:32 +00:00
|
|
|
this.checkQueue();
|
2019-03-23 15:50:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private async initializeFile(new_file = false) {
|
|
|
|
try {
|
|
|
|
if (this.stream) {
|
|
|
|
this.stream.close();
|
|
|
|
}
|
|
|
|
const folder = path.dirname(this.file);
|
|
|
|
if (folder) {
|
2020-04-09 16:17:32 +00:00
|
|
|
if (!(await fsExists(folder))) {
|
|
|
|
await fsMkDir(folder).catch(() => {}); //Could happen, if two seperate instances want to create the same folder so ignoring
|
2019-03-23 15:50:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let size = 0;
|
|
|
|
if (await fsExists(this.file)) {
|
|
|
|
let stats = await fsStat(this.file);
|
2019-04-05 02:40:34 +00:00
|
|
|
if (new_file || stats.size >= this.maxFileSize) {
|
2019-03-23 15:50:12 +00:00
|
|
|
if (await fsExists(this.file + ".old"))
|
|
|
|
await fsUnlink(this.file + ".old");
|
2020-04-09 16:17:32 +00:00
|
|
|
await fsMove(this.file, this.file + ".old");
|
2019-03-23 15:50:12 +00:00
|
|
|
} else {
|
|
|
|
size = stats.size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-09 16:17:32 +00:00
|
|
|
this.stream = fs.createWriteStream(this.file, { flags: "a" });
|
2019-03-23 15:50:12 +00:00
|
|
|
this.size = size;
|
|
|
|
} catch (e) {
|
|
|
|
console.log(e);
|
2021-05-08 20:43:46 +00:00
|
|
|
//TODO: is this the right behavior?
|
2019-03-23 15:50:12 +00:00
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private queue: Buffer[] = [];
|
|
|
|
|
|
|
|
async checkQueue() {
|
|
|
|
if (this.lock.locked) return;
|
|
|
|
let lock = await this.lock.getLock();
|
|
|
|
let msg: Buffer;
|
2020-04-09 16:17:32 +00:00
|
|
|
while ((msg = this.queue.shift())) {
|
2019-03-23 15:50:12 +00:00
|
|
|
await this.write_to_file(msg);
|
|
|
|
}
|
|
|
|
lock.release();
|
|
|
|
}
|
|
|
|
|
2019-04-05 02:40:34 +00:00
|
|
|
public async close() {
|
|
|
|
await this.flush(false);
|
|
|
|
this.open--;
|
|
|
|
if (this.open <= 0) {
|
2020-04-09 16:17:32 +00:00
|
|
|
this.stream.close();
|
2019-04-05 02:40:34 +00:00
|
|
|
Files.files.delete(this.file);
|
|
|
|
}
|
|
|
|
}
|
2021-05-08 20:43:46 +00:00
|
|
|
|
2019-03-23 15:50:12 +00:00
|
|
|
public flush(sync: boolean) {
|
|
|
|
if (sync) {
|
|
|
|
// if sync flush, the process most likely is in failstate, so checkQueue stopped its work.
|
|
|
|
let msg: Buffer;
|
2020-04-09 16:17:32 +00:00
|
|
|
while ((msg = this.queue.shift())) {
|
2019-03-23 15:50:12 +00:00
|
|
|
this.stream.write(msg);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return Promise.resolve().then(async () => {
|
|
|
|
const lock = await this.lock.getLock();
|
|
|
|
lock.release();
|
|
|
|
await this.checkQueue();
|
2020-04-09 16:17:32 +00:00
|
|
|
});
|
2019-03-23 15:50:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private async write_to_file(data: Buffer) {
|
|
|
|
try {
|
2020-04-09 16:17:32 +00:00
|
|
|
if (
|
|
|
|
data.byteLength < this.maxFileSize &&
|
|
|
|
this.size + data.byteLength > this.maxFileSize
|
|
|
|
) {
|
|
|
|
await this.initializeFile(true);
|
2019-03-23 15:50:12 +00:00
|
|
|
}
|
|
|
|
this.size += data.byteLength;
|
|
|
|
this.stream.write(data);
|
|
|
|
} catch (err) {
|
2019-04-01 03:51:27 +00:00
|
|
|
// TODO: Better error handling!
|
2019-03-23 15:50:12 +00:00
|
|
|
console.error(err);
|
|
|
|
this.initializeFile(false);
|
|
|
|
this.write_to_file(data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public write(data: Buffer) {
|
|
|
|
this.queue.push(data);
|
2021-05-08 20:53:32 +00:00
|
|
|
this.debounce.trigger();
|
2019-03-23 15:50:12 +00:00
|
|
|
}
|
2019-04-05 02:40:34 +00:00
|
|
|
|
2020-04-09 16:17:32 +00:00
|
|
|
public dispose() {}
|
2019-03-23 15:50:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function fsUnlink(path) {
|
2021-05-08 20:11:15 +00:00
|
|
|
return new Promise<void>((resolve, reject) => {
|
2019-03-23 15:50:12 +00:00
|
|
|
fs.unlink(path, (err) => {
|
|
|
|
if (err) reject(err);
|
|
|
|
else resolve();
|
2020-04-09 16:17:32 +00:00
|
|
|
});
|
|
|
|
});
|
2019-03-23 15:50:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function fsStat(path: string) {
|
|
|
|
return new Promise<fs.Stats>((resolve, reject) => {
|
|
|
|
fs.stat(path, (err, stats) => {
|
|
|
|
if (err) reject(err);
|
|
|
|
else resolve(stats);
|
2020-04-09 16:17:32 +00:00
|
|
|
});
|
|
|
|
});
|
2019-03-23 15:50:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function fsMove(oldPath: string, newPath: string) {
|
2021-05-08 20:11:15 +00:00
|
|
|
return new Promise<void>((resolve, reject) => {
|
2019-03-23 15:50:12 +00:00
|
|
|
let callback = (err?) => {
|
2020-04-09 16:17:32 +00:00
|
|
|
if (err) reject(err);
|
|
|
|
else resolve();
|
|
|
|
};
|
2019-03-23 15:50:12 +00:00
|
|
|
|
|
|
|
fs.rename(oldPath, newPath, function (err) {
|
|
|
|
if (err) {
|
2020-04-09 16:17:32 +00:00
|
|
|
if (err.code === "EXDEV") {
|
2019-03-23 15:50:12 +00:00
|
|
|
copy();
|
|
|
|
} else {
|
2020-04-09 16:17:32 +00:00
|
|
|
callback(err);
|
2019-03-23 15:50:12 +00:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2020-04-09 16:17:32 +00:00
|
|
|
callback();
|
2019-03-23 15:50:12 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
function copy() {
|
|
|
|
fs.copyFile(oldPath, newPath, (err) => {
|
2020-04-09 16:17:32 +00:00
|
|
|
if (err) callback(err);
|
2019-03-23 15:50:12 +00:00
|
|
|
else fs.unlink(oldPath, callback);
|
2020-04-09 16:17:32 +00:00
|
|
|
});
|
2019-03-23 15:50:12 +00:00
|
|
|
}
|
2020-04-09 16:17:32 +00:00
|
|
|
});
|
2019-03-23 15:50:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function fsExists(path: string) {
|
|
|
|
return new Promise<boolean>((resolve, reject) => {
|
|
|
|
fs.exists(path, resolve);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function fsMkDir(path: string) {
|
2021-05-08 20:11:15 +00:00
|
|
|
return new Promise<void>((resolve, reject) => {
|
2020-04-09 16:17:32 +00:00
|
|
|
fs.mkdir(path, (err) => (err ? reject(err) : resolve()));
|
2019-03-23 15:50:12 +00:00
|
|
|
});
|
2020-04-09 16:17:32 +00:00
|
|
|
}
|