1
0
mirror of https://git.stamm.me/OpenServer/NodeLogging.git synced 2024-11-14 23:01:04 +00:00

Updating filewriter to comply to new Adapter interface

This commit is contained in:
Fabian Stamm 2020-04-09 18:17:32 +02:00
parent c5098934a1
commit c7c968753f
3 changed files with 63 additions and 51 deletions

8
package-lock.json generated
View File

@ -1,13 +1,13 @@
{ {
"name": "@hibas123/nodelogging", "name": "@hibas123/nodelogging",
"version": "2.1.5", "version": "2.3.0",
"lockfileVersion": 1, "lockfileVersion": 1,
"requires": true, "requires": true,
"dependencies": { "dependencies": {
"@hibas123/logging": { "@hibas123/logging": {
"version": "2.2.2", "version": "2.3.0",
"resolved": "https://registry.npmjs.org/@hibas123/logging/-/logging-2.2.2.tgz", "resolved": "https://npm.hibas123.de/@hibas123%2flogging/-/logging-2.3.0.tgz",
"integrity": "sha512-45ajce5iFAKIYK74VfNVWtUCkk3nXSV2pCrnY7edszPrME8HWgrrkHMxc4yo3cIBpZNP9WN3O2TnDseasCvcjw==", "integrity": "sha512-VB/aOrup5AFOL4Ck5c7jF1Qy5Jr5tDfjhMHI0Tbe4fzkA2pagpTcIgdm+p0rQ9sa/rO2FKpd0kpy11b3W5NC8g==",
"requires": { "requires": {
"@hibas123/utils": "^2.2.3" "@hibas123/utils": "^2.2.3"
} }

View File

@ -1,6 +1,6 @@
{ {
"name": "@hibas123/nodelogging", "name": "@hibas123/nodelogging",
"version": "2.2.2", "version": "2.3.0",
"description": "", "description": "",
"main": "out/index.js", "main": "out/index.js",
"types": "out/index.d.ts", "types": "out/index.d.ts",
@ -32,7 +32,7 @@
"typescript": "^3.8.3" "typescript": "^3.8.3"
}, },
"dependencies": { "dependencies": {
"@hibas123/logging": "^2.2.2", "@hibas123/logging": "^2.3.0",
"@hibas123/utils": "^2.2.3" "@hibas123/utils": "^2.2.3"
} }
} }

View File

@ -3,19 +3,23 @@ import * as fs from "fs";
import * as path from "path"; import * as path from "path";
import { Adapter, Message, LoggingTypes } from "@hibas123/logging"; import { Adapter, Message, LoggingTypes } from "@hibas123/logging";
const MAX_FILE_SIZE = 500000000; const MAX_FILE_SIZE = 500000000;
export class LoggingFiles implements Adapter { export class LoggingFiles implements Adapter {
file: Files; file: Files;
constructor(filename: string, private error = false, private maxFileSize = MAX_FILE_SIZE) { constructor(
this.file = Files.getFile(filename); private filename: string,
} private error = false,
private maxFileSize = MAX_FILE_SIZE,
private noPrefix = false
) {}
init(observable: ObservableInterface<Message>) { init(observable: ObservableInterface<Message>) {
observable.subscribe(this.onMessage.bind(this)); observable.subscribe(this.onMessage.bind(this));
return this.file.init(this.maxFileSize); if (!this.file) {
this.file = Files.getFile(this.filename);
return this.file.init(this.maxFileSize);
}
} }
flush(sync: boolean) { flush(sync: boolean) {
@ -24,10 +28,13 @@ export class LoggingFiles implements Adapter {
onMessage(message: Message) { onMessage(message: Message) {
// Just ignore all non error messages, if this.error is set // Just ignore all non error messages, if this.error is set
if (this.error && message.type !== LoggingTypes.Error) if (this.error && message.type !== LoggingTypes.Error) return;
return;
let txt = message.text.formatted.map(fmt => fmt.map(f => f.text).join("") + "\n").join(""); let prefix = "";
if (message.name) prefix = `[${message.name}]=>`;
let txt = message.text.formatted
.map((fmt) => prefix + fmt.map((f) => f.text).join("") + "\n")
.join("");
let msg = Buffer.from(txt); let msg = Buffer.from(txt);
this.file.write(msg); this.file.write(msg);
@ -35,6 +42,7 @@ export class LoggingFiles implements Adapter {
close() { close() {
this.file.close(); this.file.close();
this.file = undefined;
} }
} }
@ -58,19 +66,22 @@ export class Files {
private stream: fs.WriteStream = undefined; private stream: fs.WriteStream = undefined;
private lock = new Lock(); private lock = new Lock();
public initialized = false; private $initialized = false;
private constructor(private file: string) { } public get initlialized() {
return this.$initialized;
}
private constructor(private file: string) {}
public async init(maxFileSize: number) { public async init(maxFileSize: number) {
if (this.initialized) if (this.$initialized) return;
return;
let lock = await this.lock.getLock(); let lock = await this.lock.getLock();
this.maxFileSize == maxFileSize; this.maxFileSize == maxFileSize;
await this.initializeFile() await this.initializeFile();
this.initialized = true; this.$initialized = true;
lock.release(); lock.release();
this.checkQueue() this.checkQueue();
} }
private async initializeFile(new_file = false) { private async initializeFile(new_file = false) {
@ -80,8 +91,8 @@ export class Files {
} }
const folder = path.dirname(this.file); const folder = path.dirname(this.file);
if (folder) { if (folder) {
if (!await fsExists(folder)) { if (!(await fsExists(folder))) {
await fsMkDir(folder).catch(() => { }); //Could happen, if two seperate instances want to create the same folder so ignoring await fsMkDir(folder).catch(() => {}); //Could happen, if two seperate instances want to create the same folder so ignoring
} }
} }
@ -91,13 +102,13 @@ export class Files {
if (new_file || stats.size >= this.maxFileSize) { if (new_file || stats.size >= this.maxFileSize) {
if (await fsExists(this.file + ".old")) if (await fsExists(this.file + ".old"))
await fsUnlink(this.file + ".old"); await fsUnlink(this.file + ".old");
await fsMove(this.file, this.file + ".old") await fsMove(this.file, this.file + ".old");
} else { } else {
size = stats.size; size = stats.size;
} }
} }
this.stream = fs.createWriteStream(this.file, { flags: "a" }) this.stream = fs.createWriteStream(this.file, { flags: "a" });
this.size = size; this.size = size;
} catch (e) { } catch (e) {
console.log(e); console.log(e);
@ -112,7 +123,7 @@ export class Files {
if (this.lock.locked) return; if (this.lock.locked) return;
let lock = await this.lock.getLock(); let lock = await this.lock.getLock();
let msg: Buffer; let msg: Buffer;
while (msg = this.queue.shift()) { while ((msg = this.queue.shift())) {
await this.write_to_file(msg); await this.write_to_file(msg);
} }
lock.release(); lock.release();
@ -122,7 +133,7 @@ export class Files {
await this.flush(false); await this.flush(false);
this.open--; this.open--;
if (this.open <= 0) { if (this.open <= 0) {
this.stream.close() this.stream.close();
Files.files.delete(this.file); Files.files.delete(this.file);
} }
} }
@ -130,7 +141,7 @@ export class Files {
if (sync) { if (sync) {
// if sync flush, the process most likely is in failstate, so checkQueue stopped its work. // if sync flush, the process most likely is in failstate, so checkQueue stopped its work.
let msg: Buffer; let msg: Buffer;
while (msg = this.queue.shift()) { while ((msg = this.queue.shift())) {
this.stream.write(msg); this.stream.write(msg);
} }
} else { } else {
@ -138,14 +149,17 @@ export class Files {
const lock = await this.lock.getLock(); const lock = await this.lock.getLock();
lock.release(); lock.release();
await this.checkQueue(); await this.checkQueue();
}) });
} }
} }
private async write_to_file(data: Buffer) { private async write_to_file(data: Buffer) {
try { try {
if (data.byteLength < this.maxFileSize && this.size + data.byteLength > this.maxFileSize) { if (
await this.initializeFile(true) data.byteLength < this.maxFileSize &&
this.size + data.byteLength > this.maxFileSize
) {
await this.initializeFile(true);
} }
this.size += data.byteLength; this.size += data.byteLength;
this.stream.write(data); this.stream.write(data);
@ -159,12 +173,10 @@ export class Files {
public write(data: Buffer) { public write(data: Buffer) {
this.queue.push(data); this.queue.push(data);
this.checkQueue() this.checkQueue();
} }
public dispose() { public dispose() {}
}
} }
function fsUnlink(path) { function fsUnlink(path) {
@ -172,8 +184,8 @@ function fsUnlink(path) {
fs.unlink(path, (err) => { fs.unlink(path, (err) => {
if (err) reject(err); if (err) reject(err);
else resolve(); else resolve();
}) });
}) });
} }
function fsStat(path: string) { function fsStat(path: string) {
@ -181,36 +193,36 @@ function fsStat(path: string) {
fs.stat(path, (err, stats) => { fs.stat(path, (err, stats) => {
if (err) reject(err); if (err) reject(err);
else resolve(stats); else resolve(stats);
}) });
}) });
} }
function fsMove(oldPath: string, newPath: string) { function fsMove(oldPath: string, newPath: string) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
let callback = (err?) => { let callback = (err?) => {
if (err) reject(err) if (err) reject(err);
else resolve() else resolve();
} };
fs.rename(oldPath, newPath, function (err) { fs.rename(oldPath, newPath, function (err) {
if (err) { if (err) {
if (err.code === 'EXDEV') { if (err.code === "EXDEV") {
copy(); copy();
} else { } else {
callback(err) callback(err);
} }
return; return;
} }
callback() callback();
}); });
function copy() { function copy() {
fs.copyFile(oldPath, newPath, (err) => { fs.copyFile(oldPath, newPath, (err) => {
if (err) callback(err) if (err) callback(err);
else fs.unlink(oldPath, callback); else fs.unlink(oldPath, callback);
}) });
} }
}) });
} }
function fsExists(path: string) { function fsExists(path: string) {
@ -221,6 +233,6 @@ function fsExists(path: string) {
function fsMkDir(path: string) { function fsMkDir(path: string) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
fs.mkdir(path, (err) => err ? reject(err) : resolve()); fs.mkdir(path, (err) => (err ? reject(err) : resolve()));
}); });
} }