From 29fee51b63e570ca68949b24e3a44401d873c97c Mon Sep 17 00:00:00 2001 From: Fabian Stamm Date: Thu, 9 Aug 2018 19:52:01 +0200 Subject: [PATCH] Redesign File writing and some tweaks --- out/index.d.ts | 52 ++++++---- out/index.js | 252 +++++++++++++++++++++++++--------------------- out/index.js.map | 2 +- out/lock.d.ts | 12 +++ out/lock.js | 37 +++++++ out/lock.js.map | 1 + out/test.js | 10 +- out/test.js.map | 2 +- src/index.ts | 257 +++++++++++++++++++++++++++-------------------- src/lock.ts | 36 +++++++ src/test.ts | 11 +- 11 files changed, 418 insertions(+), 254 deletions(-) create mode 100644 out/lock.d.ts create mode 100644 out/lock.js create mode 100644 out/lock.js.map create mode 100644 src/lock.ts diff --git a/out/index.d.ts b/out/index.d.ts index 2d71142..ffb4148 100644 --- a/out/index.d.ts +++ b/out/index.d.ts @@ -1,27 +1,37 @@ /// import { EventEmitter } from "events"; -export declare class Logging { - private static logFileLocation; - static stdout: boolean; - private static fileStream; - private static errorStream; - private static fileSize; - private static errorSize; - private static writing; - private static queue; - static events: EventEmitter; - static config(logfolder: string, stdout: boolean): void; - static debug(...message: any[]): void; - static log(...message: any[]): void; - static warning(...message: any[]): void; - static logWithCustomColors(type: LoggingTypes, colors: string, ...message: any[]): void; - static error(error: Error | string): void; - static errorMessage(...message: any[]): void; - private static message(type, message, customColors?); - private static writeMessageToFile(message, error?); - private static checkQueue(); - private static initializeFile(); +export interface LoggingBaseOptions { + logfile: string; + errorfile: string; + console_out: boolean; } +export declare class LoggingBase { + private config; + private writeLock; + private fileStream; + private errorStream; + private fileSize; + private errorSize; + private queue; + constructor(options?: Partial); + console_out: boolean; + waitForSetup(): Promise; + private setup(); + events: EventEmitter; + debug(...message: any[]): void; + log(...message: any[]): void; + warning(...message: any[]): void; + logWithCustomColors(type: LoggingTypes, colors: string, ...message: any[]): void; + error(error: Error | string): void; + errorMessage(...message: any[]): void; + private message(type, message, customColors?); + private writeMessageToFile(message, error?); + private checkQueue(); + private writeToLogFile(data); + private writeToErrorFile(data); + private initializeFile(file, new_file?); +} +export declare const Logging: LoggingBase; export default Logging; export declare enum LoggingTypes { Log = 0, diff --git a/out/index.js b/out/index.js index ff4c34c..32e3ac3 100644 --- a/out/index.js +++ b/out/index.js @@ -4,6 +4,7 @@ const util = require("util"); const fs = require("fs"); const events_1 = require("events"); const path = require("path"); +const lock_1 = require("./lock"); const Reset = "\x1b[0m"; const Bright = "\x1b[1m"; const Dim = "\x1b[2m"; @@ -29,36 +30,71 @@ const BgCyan = "\x1b[46m"; const BgWhite = "\x1b[47m"; const maxFileSize = 500000000; const OriginalErrorStackFunction = Error.prototype.prepareStackTrace; -class Logging { - static config(logfolder, stdout) { - this.logFileLocation = logfolder; - this.stdout = stdout; +class LoggingBase { + constructor(options) { + this.writeLock = new lock_1.default(); + this.fileSize = 0; + this.errorSize = 0; + this.queue = new Array(); + this.events = new events_1.EventEmitter(); + if (!options) + options = {}; + this.config = Object.assign({ + console_out: true, + logfile: "./logs/all.log", + errorfile: "./logs/error.log" + }, options); + this.setup(); } - static debug(...message) { - Logging.message(LoggingTypes.Debug, message); + get console_out() { + return this.config.console_out; } - static log(...message) { - Logging.message(LoggingTypes.Log, message); + set console_out(value) { + this.config.console_out = value; } - static warning(...message) { - Logging.message(LoggingTypes.Warning, message); + async waitForSetup() { + (await this.writeLock.getLock()).release(); } - static logWithCustomColors(type, colors, ...message) { - Logging.message(type, message, colors); + 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) { + debug(...message) { + this.message(LoggingTypes.Debug, message); + } + log(...message) { + this.message(LoggingTypes.Log, message); + } + warning(...message) { + this.message(LoggingTypes.Warning, message); + } + logWithCustomColors(type, colors, ...message) { + this.message(type, message, colors); + } + error(error) { 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) { - Logging.message(LoggingTypes.Error, message); + errorMessage(...message) { + this.message(LoggingTypes.Error, message); } - static async message(type, message, customColors) { + async message(type, message, customColors) { var consoleLogFormat = Reset; if (!customColors) { switch (type) { @@ -97,132 +133,122 @@ 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); index = m.indexOf("\x1b"); } 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 }); } - static writeMessageToFile(message, error) { - Logging.queue.push({ message: message.replace("\n", " "), error: error }); - Logging.checkQueue(); + writeMessageToFile(message, error) { + if (!this.writeLock.locked && !this.fileStream && !(error || this.errorStream)) + return; + this.queue.push({ message: message.replace("\n", " "), error: error }); + this.checkQueue(); } - static async checkQueue() { + async checkQueue() { try { - if (Logging.writing) + if (this.writeLock.locked) return; - if (Logging.queue.length <= 0) + if (this.queue.length <= 0) return; - Logging.writing = true; - var message = Logging.queue[0]; + 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); } } - static async initializeFile() { - if (this.fileStream && this.errorStream) - return; - if (!this.logFileLocation) - return; - this.writing = true; + async writeToLogFile(data) { + 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); + } + async writeToErrorFile(data) { + 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); + } + async initializeFile(file, new_file = true) { 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 }; } } -Logging.logFileLocation = "./logs/"; -Logging.stdout = true; -Logging.fileSize = 0; -Logging.errorSize = 0; -Logging.writing = false; -Logging.queue = new Array(); -Logging.events = new events_1.EventEmitter(); -exports.Logging = Logging; -exports.default = Logging; +exports.LoggingBase = LoggingBase; +exports.Logging = new LoggingBase(); +exports.default = exports.Logging; function fsUnlink(path) { return new Promise((resolve, reject) => { fs.unlink(path, (err) => { diff --git a/out/index.js.map b/out/index.js.map index 2455613..a1d997e 100644 --- a/out/index.js.map +++ b/out/index.js.map @@ -1 +1 @@ -{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;AAAA,6BAA6B;AAC7B,yBAAyB;AACzB,mCAAsC;AACtC,6BAA6B;AAE7B,MAAM,KAAK,GAAG,SAAS,CAAA;AACvB,MAAM,MAAM,GAAG,SAAS,CAAA;AACxB,MAAM,GAAG,GAAG,SAAS,CAAA;AACrB,MAAM,UAAU,GAAG,SAAS,CAAA;AAC5B,MAAM,KAAK,GAAG,SAAS,CAAA;AACvB,MAAM,OAAO,GAAG,SAAS,CAAA;AACzB,MAAM,MAAM,GAAG,SAAS,CAAA;AAExB,MAAM,OAAO,GAAG,UAAU,CAAA;AAC1B,MAAM,KAAK,GAAG,UAAU,CAAA;AACxB,MAAM,OAAO,GAAG,UAAU,CAAA;AAC1B,MAAM,QAAQ,GAAG,UAAU,CAAA;AAC3B,MAAM,MAAM,GAAG,UAAU,CAAA;AACzB,MAAM,SAAS,GAAG,UAAU,CAAA;AAC5B,MAAM,MAAM,GAAG,UAAU,CAAA;AACzB,MAAM,OAAO,GAAG,UAAU,CAAA;AAE1B,MAAM,OAAO,GAAG,UAAU,CAAA;AAC1B,MA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9C,IAAI,EAAE,WAAW,CAAC,aAAa,EAAE;iBACnC,CAAC;SACP;KACH;IAAC,OAAO,GAAG,EAAE,GAAG;IACjB,OAAO,EAAE,IAAI,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC,EAAE,CAAC;AACvC,CAAC;AAED,IAAY,YAKX;AALD,WAAY,YAAY;IACrB,6CAAG,CAAA;IACH,qDAAO,CAAA;IACP,iDAAK,CAAA;IACL,iDAAK,CAAA;AACR,CAAC,EALW,YAAY,GAAZ,oBAAY,KAAZ,oBAAY,QAKvB"} \ No newline at end of file diff --git a/out/lock.d.ts b/out/lock.d.ts new file mode 100644 index 0000000..9f7d876 --- /dev/null +++ b/out/lock.d.ts @@ -0,0 +1,12 @@ +export declare type Release = { + release: () => void; +}; +export default class Lock { + private _locked; + readonly locked: boolean; + private toCome; + constructor(); + getLock(): Promise; + private lock(); + private release(); +} diff --git a/out/lock.js b/out/lock.js new file mode 100644 index 0000000..3799100 --- /dev/null +++ b/out/lock.js @@ -0,0 +1,37 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +class Lock { + constructor() { + this._locked = false; + this.toCome = []; + this.release = this.release.bind(this); + } + get locked() { + return this._locked; + } + async getLock() { + if (!this._locked) + return { release: this.lock() }; + else { + return new Promise((resolve) => { + this.toCome.push(() => { + resolve({ release: this.lock() }); + }); + }); + } + } + lock() { + this._locked = true; + return this.release; + } + async release() { + if (this.toCome.length > 0) { + this.toCome.shift()(); + } + else { + this._locked = false; + } + } +} +exports.default = Lock; +//# sourceMappingURL=lock.js.map \ No newline at end of file diff --git a/out/lock.js.map b/out/lock.js.map new file mode 100644 index 0000000..9eff807 --- /dev/null +++ b/out/lock.js.map @@ -0,0 +1 @@ +{"version":3,"file":"lock.js","sourceRoot":"","sources":["../src/lock.ts"],"names":[],"mappings":";;AACA;IAOG;QANQ,YAAO,GAAY,KAAK,CAAC;QAIzB,WAAM,GAAmB,EAAE,CAAC;QAGjC,IAAI,CAAC,OAAO,GAAG,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IAC1C,CAAC;IAPD,IAAI,MAAM;QACP,OAAO,IAAI,CAAC,OAAO,CAAC;IACvB,CAAC;IAOD,KAAK,CAAC,OAAO;QACV,IAAI,CAAC,IAAI,CAAC,OAAO;YAAE,OAAO,EAAE,OAAO,EAAE,IAAI,CAAC,IAAI,EAAE,EAAE,CAAC;aAC9C;YACF,OAAO,IAAI,OAAO,CAAU,CAAC,OAAO,EAAE,EAAE;gBACrC,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,EAAE;oBACnB,OAAO,CAAC,EAAE,OAAO,EAAE,IAAI,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC;gBACrC,CAAC,CAAC,CAAA;YACL,CAAC,CAAC,CAAA;SACJ;IACJ,CAAC;IAEO,IAAI;QACT,IAAI,CAAC,OAAO,GAAG,IAAI,CAAC;QACpB,OAAO,IAAI,CAAC,OAAO,CAAC;IACvB,CAAC;IAEO,KAAK,CAAC,OAAO;QAClB,IAAI,IAAI,CAAC,MAAM,CAAC,MAAM,GAAG,CAAC,EAAE;YACzB,IAAI,CAAC,MAAM,CAAC,KAAK,EAAE,EAAE,CAAC;SACxB;aAAM;YACJ,IAAI,CAAC,OAAO,GAAG,KAAK,CAAC;SACvB;IACJ,CAAC;CACH;AAlCD,uBAkCC"} \ No newline at end of file diff --git a/out/test.js b/out/test.js index 7ad54cf..3ce0e04 100644 --- a/out/test.js +++ b/out/test.js @@ -11,8 +11,10 @@ index_1.Logging.log("\x1b[31m\x1b[31m\x1b[31m\x1b[31m\x1b[31m\x1b[31m TEST \x1b[ let err = new Error(); if (typeof err.stack !== "string") console.log("Stacktrace invalid", err.stack); -index_1.Logging.stdout = false; -for (let i = 0; i < 7000; i++) { - index_1.Logging.log(crypto_1.randomBytes(50000).toString("hex")); -} +index_1.Logging.console_out = false; +index_1.Logging.waitForSetup().then(() => { + for (let i = 0; i < 7000; i++) { + index_1.Logging.log(crypto_1.randomBytes(50000).toString("hex")); + } +}); //# sourceMappingURL=test.js.map \ No newline at end of file diff --git a/out/test.js.map b/out/test.js.map index 869c7c4..27a96f3 100644 --- a/out/test.js.map +++ b/out/test.js.map @@ -1 +1 @@ -{"version":3,"file":"test.js","sourceRoot":"","sources":["../src/test.ts"],"names":[],"mappings":";;AAAA,mCAAkC;AAClC,mCAAqC;AAErC,eAAO,CAAC,GAAG,CAAC,MAAM,CAAC,CAAA;AACnB,eAAO,CAAC,GAAG,CAAC,GAAG,EAAE,IAAI,EAAE,EAAE,CAAC,EAAE,IAAI,EAAE,EAAE,IAAI,CAAC,CAAC;AAC1C,eAAO,CAAC,KAAK,CAAC,IAAI,KAAK,CAAC,YAAY,CAAC,CAAC,CAAC;AACvC,eAAO,CAAC,KAAK,CAAC,kBAAkB,CAAC,CAAC;AAClC,eAAO,CAAC,YAAY,CAAC,GAAG,EAAE,IAAI,EAAE,IAAI,EAAE,OAAO,CAAC,CAAC;AAE/C,eAAO,CAAC,GAAG,CAAC,gFAAgF,CAAC,CAAA;AAE7F,IAAI,GAAG,GAAG,IAAI,KAAK,EAAE,CAAA;AACrB,IAAI,OAAO,GAAG,CAAC,KAAK,KAAK,QAAQ;IAAE,OAAO,CAAC,GAAG,CAAC,oBAAoB,EAAE,GAAG,CAAC,KAAK,CAAC,CAAA;AAE/E,eAAO,CAAC,MAAM,GAAG,KAAK,CAAC;AACvB,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,IAAI,EAAE,CAAC,EAAE,EAAE;IAC5B,eAAO,CAAC,GAAG,CAAC,oBAAW,CAAC,KAAK,CAAC,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC,CAAA;CACjD"} \ No newline at end of file +{"version":3,"file":"test.js","sourceRoot":"","sources":["../src/test.ts"],"names":[],"mappings":";;AAAA,mCAAkC;AAClC,mCAAqC;AAErC,eAAO,CAAC,GAAG,CAAC,MAAM,CAAC,CAAA;AACnB,eAAO,CAAC,GAAG,CAAC,GAAG,EAAE,IAAI,EAAE,EAAE,CAAC,EAAE,IAAI,EAAE,EAAE,IAAI,CAAC,CAAC;AAC1C,eAAO,CAAC,KAAK,CAAC,IAAI,KAAK,CAAC,YAAY,CAAC,CAAC,CAAC;AACvC,eAAO,CAAC,KAAK,CAAC,kBAAkB,CAAC,CAAC;AAClC,eAAO,CAAC,YAAY,CAAC,GAAG,EAAE,IAAI,EAAE,IAAI,EAAE,OAAO,CAAC,CAAC;AAE/C,eAAO,CAAC,GAAG,CAAC,gFAAgF,CAAC,CAAA;AAE7F,IAAI,GAAG,GAAG,IAAI,KAAK,EAAE,CAAA;AACrB,IAAI,OAAO,GAAG,CAAC,KAAK,KAAK,QAAQ;IAAE,OAAO,CAAC,GAAG,CAAC,oBAAoB,EAAE,GAAG,CAAC,KAAK,CAAC,CAAA;AAC/E,eAAO,CAAC,WAAW,GAAG,KAAK,CAAC;AAC5B,eAAO,CAAC,YAAY,EAAE,CAAC,IAAI,CAAC,GAAG,EAAE;IAC9B,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,IAAI,EAAE,CAAC,EAAE,EAAE;QAC5B,eAAO,CAAC,GAAG,CAAC,oBAAW,CAAC,KAAK,CAAC,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC,CAAA;KACjD;AACJ,CAAC,CAAC,CAAC"} \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index c796488..bf5cdd0 100644 --- a/src/index.ts +++ b/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 = (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) { + if (!options) options = {}; + this.config = Object.assign({ + 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) { diff --git a/src/lock.ts b/src/lock.ts new file mode 100644 index 0000000..5205f78 --- /dev/null +++ b/src/lock.ts @@ -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 { + if (!this._locked) return { release: this.lock() }; + else { + return new Promise((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; + } + } +} \ No newline at end of file diff --git a/src/test.ts b/src/test.ts index 6ba3367..ccd18aa 100644 --- a/src/test.ts +++ b/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")) -} \ No newline at end of file +Logging.console_out = false; +Logging.waitForSetup().then(() => { + for (let i = 0; i < 7000; i++) { + Logging.log(randomBytes(50000).toString("hex")) + } +}); \ No newline at end of file