Redesign File writing and some tweaks
This commit is contained in:
257
src/index.ts
257
src/index.ts
@ -2,6 +2,7 @@ import * as util from "util";
|
||||
import * as fs from "fs";
|
||||
import { EventEmitter } from "events";
|
||||
import * as path from "path";
|
||||
import Lock from "./lock";
|
||||
|
||||
const Reset = "\x1b[0m"
|
||||
const Bright = "\x1b[1m"
|
||||
@ -33,55 +34,94 @@ const maxFileSize = 500000000;
|
||||
|
||||
const OriginalErrorStackFunction = (<any>Error.prototype).prepareStackTrace
|
||||
|
||||
export class Logging {
|
||||
private static logFileLocation: string = "./logs/";
|
||||
public static stdout: boolean = true;
|
||||
export interface LoggingBaseOptions {
|
||||
logfile: string;
|
||||
errorfile: string;
|
||||
console_out: boolean;
|
||||
}
|
||||
|
||||
private static fileStream: fs.WriteStream;
|
||||
private static errorStream: fs.WriteStream;
|
||||
private static fileSize: number = 0;
|
||||
private static errorSize: number = 0;
|
||||
export class LoggingBase {
|
||||
private config: LoggingBaseOptions;
|
||||
private writeLock = new Lock();
|
||||
|
||||
private static writing = false;
|
||||
private static queue = new Array<{ message: string, error: boolean }>();
|
||||
private fileStream: fs.WriteStream;
|
||||
private errorStream: fs.WriteStream;
|
||||
private fileSize: number = 0;
|
||||
private errorSize: number = 0;
|
||||
|
||||
static events: EventEmitter = new EventEmitter();
|
||||
private queue = new Array<{ message: string, error: boolean }>();
|
||||
|
||||
static config(logfolder: string, stdout: boolean) {
|
||||
this.logFileLocation = logfolder;
|
||||
this.stdout = stdout;
|
||||
constructor(options?: Partial<LoggingBaseOptions>) {
|
||||
if (!options) options = {};
|
||||
this.config = Object.assign(<LoggingBaseOptions>{
|
||||
console_out: true,
|
||||
logfile: "./logs/all.log",
|
||||
errorfile: "./logs/error.log"
|
||||
}, options);
|
||||
this.setup();
|
||||
}
|
||||
|
||||
static debug(...message: any[]) {
|
||||
Logging.message(LoggingTypes.Debug, message);
|
||||
get console_out() {
|
||||
return this.config.console_out;
|
||||
}
|
||||
|
||||
static log(...message: any[]) {
|
||||
Logging.message(LoggingTypes.Log, message);
|
||||
set console_out(value) {
|
||||
this.config.console_out = value;
|
||||
}
|
||||
|
||||
static warning(...message: any[]) {
|
||||
Logging.message(LoggingTypes.Warning, message);
|
||||
public async waitForSetup() {
|
||||
(await this.writeLock.getLock()).release();
|
||||
}
|
||||
|
||||
static logWithCustomColors(type: LoggingTypes, colors: string, ...message: any[]) {
|
||||
Logging.message(type, message, colors);
|
||||
private async setup() {
|
||||
let lock = await this.writeLock.getLock();
|
||||
if (this.config.logfile) {
|
||||
let f = await this.initializeFile(this.config.logfile, true);
|
||||
this.fileStream = f.stream;
|
||||
this.fileSize = f.size;
|
||||
}
|
||||
|
||||
if (this.config.errorfile) {
|
||||
let f = await this.initializeFile(this.config.errorfile, true);
|
||||
this.errorStream = f.stream;
|
||||
this.errorSize = f.size;
|
||||
}
|
||||
lock.release();
|
||||
this.checkQueue();
|
||||
}
|
||||
|
||||
static error(error: Error | string) {
|
||||
events: EventEmitter = new EventEmitter();
|
||||
|
||||
debug(...message: any[]) {
|
||||
this.message(LoggingTypes.Debug, message);
|
||||
}
|
||||
|
||||
log(...message: any[]) {
|
||||
this.message(LoggingTypes.Log, message);
|
||||
}
|
||||
|
||||
warning(...message: any[]) {
|
||||
this.message(LoggingTypes.Warning, message);
|
||||
}
|
||||
|
||||
logWithCustomColors(type: LoggingTypes, colors: string, ...message: any[]) {
|
||||
this.message(type, message, colors);
|
||||
}
|
||||
|
||||
error(error: Error | string) {
|
||||
if (typeof error === "string") {
|
||||
let e = new Error()
|
||||
Logging.message(LoggingTypes.Error, [error, ":", e.stack]);
|
||||
this.message(LoggingTypes.Error, [error, ":", e.stack]);
|
||||
} else {
|
||||
Logging.message(LoggingTypes.Error, [error.name, ":", error.message, ":", error.stack]);
|
||||
this.message(LoggingTypes.Error, [error.name, ":", error.message, ":", error.stack]);
|
||||
}
|
||||
}
|
||||
|
||||
static errorMessage(...message: any[]) {
|
||||
Logging.message(LoggingTypes.Error, message);
|
||||
errorMessage(...message: any[]) {
|
||||
this.message(LoggingTypes.Error, message);
|
||||
}
|
||||
|
||||
private static async message(type: LoggingTypes, message: any[] | string, customColors?: string) {
|
||||
private async message(type: LoggingTypes, message: any[] | string, customColors?: string) {
|
||||
var consoleLogFormat = Reset;
|
||||
if (!customColors) {
|
||||
switch (type) {
|
||||
@ -116,9 +156,12 @@ export class Logging {
|
||||
}
|
||||
let file = getCallerFile()
|
||||
let date = new Date().toISOString().replace(/T/, ' ').replace(/\..+/, '');
|
||||
var m = `[${LoggingTypes[type]}][${file.file}:${file.line}][${date}]: ${mb}`;
|
||||
if (this.stdout) console.log(consoleLogFormat + m + Reset);
|
||||
let prefix = `[${LoggingTypes[type]}][${file.file}:${file.line}][${date}]: `;
|
||||
let message_lines = mb.split("\n").map(line => prefix + line);
|
||||
|
||||
if (this.config.console_out) message_lines.forEach(line => console.log(consoleLogFormat + line + Reset));
|
||||
|
||||
let m = message_lines.join("\n");
|
||||
let index = m.indexOf("\x1b");
|
||||
while (index >= 0) {
|
||||
m = m.substring(0, index) + m.substring(index + 5, m.length);
|
||||
@ -126,111 +169,107 @@ export class Logging {
|
||||
}
|
||||
|
||||
m = m.replace(/[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g, "");
|
||||
|
||||
if (this.logFileLocation) {
|
||||
if ((!this.fileStream || !this.errorStream) && !this.writing) {
|
||||
Logging.initializeFile();
|
||||
}
|
||||
Logging.writeMessageToFile(m, type === LoggingTypes.Error);
|
||||
}
|
||||
Logging.events.emit("message", { type: type, message: mb });
|
||||
this.writeMessageToFile(m, type === LoggingTypes.Error);
|
||||
this.events.emit("message", { type: type, message: mb });
|
||||
}
|
||||
|
||||
private static writeMessageToFile(message: string, error?: boolean) {
|
||||
Logging.queue.push({ message: message.replace("\n", " "), error: error });
|
||||
Logging.checkQueue();
|
||||
private writeMessageToFile(message: string, error?: boolean) {
|
||||
if (!this.writeLock.locked && !this.fileStream && !(error || this.errorStream)) return;
|
||||
this.queue.push({ message: message.replace("\n", " "), error: error });
|
||||
this.checkQueue();
|
||||
}
|
||||
|
||||
private static async checkQueue() {
|
||||
private async checkQueue() {
|
||||
try {
|
||||
if (Logging.writing) return;
|
||||
if (Logging.queue.length <= 0) return;
|
||||
Logging.writing = true;
|
||||
var message = Logging.queue[0];
|
||||
if (this.writeLock.locked) return;
|
||||
if (this.queue.length <= 0) return;
|
||||
let lock = await this.writeLock.getLock();
|
||||
var message = this.queue.shift();
|
||||
message.message += "\n";
|
||||
let data = new Buffer(message.message, "utf8");
|
||||
if (data.byteLength < maxFileSize && data.byteLength + Logging.fileSize > maxFileSize) {
|
||||
Logging.fileStream.close();
|
||||
if (await fsExists(this.logFileLocation + "all.log.old"))
|
||||
await fsUnlink(this.logFileLocation + "all.log.old");
|
||||
await fsMove(this.logFileLocation + "all.log", this.logFileLocation + "all.log.old")
|
||||
Logging.fileStream = fs.createWriteStream(this.logFileLocation + "all.log");
|
||||
Logging.fileSize = 0;
|
||||
}
|
||||
Logging.fileSize += data.byteLength;
|
||||
Logging.fileStream.write(data, async () => {
|
||||
if (message.error) {
|
||||
if (data.byteLength < maxFileSize && data.byteLength + Logging.errorSize > maxFileSize) {
|
||||
Logging.errorStream.close();
|
||||
if (await fsExists(this.logFileLocation + "error.log.old"))
|
||||
await fsUnlink(this.logFileLocation + "error.log.old");
|
||||
await fsMove(this.logFileLocation + "error.log", this.logFileLocation + "error.log.old")
|
||||
Logging.errorStream = fs.createWriteStream(this.logFileLocation + "error.log");
|
||||
Logging.errorSize = 0;
|
||||
}
|
||||
Logging.errorSize += data.byteLength;
|
||||
Logging.errorStream.write(data, () => {
|
||||
Logging.queue.splice(Logging.queue.indexOf(message), 1);
|
||||
Logging.writing = false;
|
||||
Logging.checkQueue();
|
||||
});
|
||||
} else {
|
||||
Logging.queue.splice(Logging.queue.indexOf(message), 1);
|
||||
Logging.writing = false;
|
||||
Logging.checkQueue();
|
||||
}
|
||||
});
|
||||
await this.writeToLogFile(data);
|
||||
if (message.error) await this.writeToErrorFile(data);
|
||||
lock.release();
|
||||
if (this.queue.length > 0) this.checkQueue();
|
||||
} catch (e) {
|
||||
console.log(e)
|
||||
}
|
||||
}
|
||||
|
||||
private static async initializeFile() {
|
||||
if (this.fileStream && this.errorStream) return;
|
||||
if (!this.logFileLocation) return;
|
||||
this.writing = true;
|
||||
private async writeToLogFile(data: Buffer) {
|
||||
if (data.byteLength < maxFileSize && this.fileSize + data.byteLength > maxFileSize) {
|
||||
let f = await this.initializeFile(this.config.logfile, true);
|
||||
this.fileStream = f.stream;
|
||||
this.fileSize = f.size;
|
||||
}
|
||||
this.fileSize += data.byteLength;
|
||||
this.fileStream.write(data);
|
||||
}
|
||||
|
||||
private async writeToErrorFile(data: Buffer) {
|
||||
if (data.byteLength < maxFileSize && this.errorSize + data.byteLength > maxFileSize) {
|
||||
let f = await this.initializeFile(this.config.errorfile, true);
|
||||
this.errorStream = f.stream;
|
||||
this.errorSize = f.size;
|
||||
}
|
||||
this.errorSize += data.byteLength;
|
||||
this.errorStream.write(data);
|
||||
}
|
||||
|
||||
private async initializeFile(file: string, new_file = true): Promise<{ stream: fs.WriteStream, size: number }> {
|
||||
try {
|
||||
await new Promise((resolve, reject) => {
|
||||
fs.exists(this.logFileLocation, (exists) => {
|
||||
if (!exists) {
|
||||
fs.mkdir(this.logFileLocation, (err) => {
|
||||
if (err) {
|
||||
reject(err);
|
||||
} else {
|
||||
resolve();
|
||||
}
|
||||
});
|
||||
} else resolve();
|
||||
});
|
||||
const folder = path.dirname(file);
|
||||
if (folder)
|
||||
fs.exists(folder, (exists) => {
|
||||
if (!exists) {
|
||||
fs.mkdir(folder, (err) => {
|
||||
if (err) {
|
||||
reject(err);
|
||||
} else {
|
||||
resolve();
|
||||
}
|
||||
});
|
||||
} else resolve();
|
||||
});
|
||||
});
|
||||
|
||||
if (await fsExists(this.logFileLocation + "all.log")) {
|
||||
if (await fsExists(this.logFileLocation + "all.log.old"))
|
||||
await fsUnlink(this.logFileLocation + "all.log.old");
|
||||
await fsMove(this.logFileLocation + "all.log", this.logFileLocation + "all.log.old")
|
||||
}
|
||||
|
||||
if (await fsExists(this.logFileLocation + "error.log")) {
|
||||
let stats = await fsStat(this.logFileLocation + "error.log")
|
||||
if (stats.size > maxFileSize) {
|
||||
if (await fsExists(this.logFileLocation + "error.log.old"))
|
||||
await fsUnlink(this.logFileLocation + "error.log.old");
|
||||
await fsMove(this.logFileLocation + "error.log", this.logFileLocation + "error.log.old")
|
||||
let size = 0;
|
||||
if (await fsExists(file)) {
|
||||
let stats = await fsStat(file);
|
||||
if (new_file || stats.size > maxFileSize) {
|
||||
if (await fsExists(file + ".old"))
|
||||
await fsUnlink(file + ".old");
|
||||
await fsMove(file, file + ".old")
|
||||
} else {
|
||||
this.errorSize = stats.size;
|
||||
size = stats.size;
|
||||
}
|
||||
}
|
||||
|
||||
this.fileStream = fs.createWriteStream(this.logFileLocation + "all.log", { flags: "a" });
|
||||
this.errorStream = fs.createWriteStream(this.logFileLocation + "error.log", { flags: "a" });
|
||||
this.writing = false;
|
||||
this.checkQueue();
|
||||
return { stream: fs.createWriteStream(file, { flags: "a" }), size: size };
|
||||
|
||||
// if (await fsExists(this.logFileLocation + "error.log")) {
|
||||
// let stats = await fsStat(this.logFileLocation + "error.log")
|
||||
// if (stats.size > maxFileSize) {
|
||||
// if (await fsExists(this.logFileLocation + "error.log.old"))
|
||||
// await fsUnlink(this.logFileLocation + "error.log.old");
|
||||
// await fsMove(this.logFileLocation + "error.log", this.logFileLocation + "error.log.old")
|
||||
// } else {
|
||||
// this.errorSize = stats.size;
|
||||
// }
|
||||
// }
|
||||
|
||||
// this.fileStream = fs.createWriteStream(this.logFileLocation + "all.log", { flags: "a" });
|
||||
// this.errorStream = fs.createWriteStream(this.logFileLocation + "error.log", { flags: "a" });
|
||||
// this.writing = false;
|
||||
// this.checkQueue();
|
||||
} catch (e) {
|
||||
console.log(e);
|
||||
}
|
||||
return { size: 0, stream: undefined };
|
||||
}
|
||||
}
|
||||
|
||||
export const Logging = new LoggingBase();
|
||||
export default Logging;
|
||||
|
||||
function fsUnlink(path) {
|
||||
|
36
src/lock.ts
Normal file
36
src/lock.ts
Normal file
@ -0,0 +1,36 @@
|
||||
export type Release = { release: () => void };
|
||||
export default class Lock {
|
||||
private _locked: boolean = false;
|
||||
get locked() {
|
||||
return this._locked;
|
||||
}
|
||||
private toCome: (() => void)[] = [];
|
||||
|
||||
constructor() {
|
||||
this.release = this.release.bind(this);
|
||||
}
|
||||
|
||||
async getLock(): Promise<Release> {
|
||||
if (!this._locked) return { release: this.lock() };
|
||||
else {
|
||||
return new Promise<Release>((resolve) => {
|
||||
this.toCome.push(() => {
|
||||
resolve({ release: this.lock() });
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
private lock() {
|
||||
this._locked = true;
|
||||
return this.release;
|
||||
}
|
||||
|
||||
private async release() {
|
||||
if (this.toCome.length > 0) {
|
||||
this.toCome.shift()();
|
||||
} else {
|
||||
this._locked = false;
|
||||
}
|
||||
}
|
||||
}
|
11
src/test.ts
11
src/test.ts
@ -11,8 +11,9 @@ Logging.log("\x1b[31m\x1b[31m\x1b[31m\x1b[31m\x1b[31m\x1b[31m TEST \x1b[31m\x1b[
|
||||
|
||||
let err = new Error()
|
||||
if (typeof err.stack !== "string") console.log("Stacktrace invalid", err.stack)
|
||||
|
||||
Logging.stdout = false;
|
||||
for (let i = 0; i < 7000; i++) {
|
||||
Logging.log(randomBytes(50000).toString("hex"))
|
||||
}
|
||||
Logging.console_out = false;
|
||||
Logging.waitForSetup().then(() => {
|
||||
for (let i = 0; i < 7000; i++) {
|
||||
Logging.log(randomBytes(50000).toString("hex"))
|
||||
}
|
||||
});
|
Reference in New Issue
Block a user