mirror of
https://git.stamm.me/OpenServer/NodeLogging.git
synced 2024-11-25 02:29:23 +00:00
Fixing bug with not closed file while moving to new destination
This commit is contained in:
parent
59c571e860
commit
687748a784
40
out/index.js
40
out/index.js
@ -61,6 +61,9 @@ class LoggingFiles {
|
||||
}
|
||||
async initializeFile(new_file = false) {
|
||||
try {
|
||||
if (this.stream) {
|
||||
this.stream.close();
|
||||
}
|
||||
const folder = path.dirname(this.file);
|
||||
if (folder) {
|
||||
if (!await fsExists(folder)) {
|
||||
@ -70,7 +73,7 @@ class LoggingFiles {
|
||||
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 >= maxFileSize) {
|
||||
if (await fsExists(this.file + ".old"))
|
||||
await fsUnlink(this.file + ".old");
|
||||
await fsMove(this.file, this.file + ".old");
|
||||
@ -99,11 +102,18 @@ class LoggingFiles {
|
||||
lock.release();
|
||||
}
|
||||
async write_to_file(data) {
|
||||
if (data.byteLength < maxFileSize && this.size + data.byteLength > maxFileSize) {
|
||||
let f = await this.initializeFile(true);
|
||||
try {
|
||||
if (data.byteLength < maxFileSize && this.size + data.byteLength > maxFileSize) {
|
||||
await this.initializeFile(true);
|
||||
}
|
||||
this.size += data.byteLength;
|
||||
this.stream.write(data);
|
||||
}
|
||||
catch (err) {
|
||||
console.error(err);
|
||||
this.initializeFile(false);
|
||||
this.write_to_file(data);
|
||||
}
|
||||
this.size += data.byteLength;
|
||||
this.stream.write(data);
|
||||
}
|
||||
write(data) {
|
||||
this.queue.push(data);
|
||||
@ -299,14 +309,20 @@ function fsMove(oldPath, newPath) {
|
||||
callback();
|
||||
});
|
||||
function copy() {
|
||||
var readStream = fs.createReadStream(oldPath);
|
||||
var writeStream = fs.createWriteStream(newPath);
|
||||
readStream.on('error', callback);
|
||||
writeStream.on('error', callback);
|
||||
readStream.on('close', function () {
|
||||
fs.unlink(oldPath, callback);
|
||||
fs.copyFile(oldPath, newPath, (err) => {
|
||||
if (err)
|
||||
callback(err);
|
||||
else
|
||||
fs.unlink(oldPath, callback);
|
||||
});
|
||||
readStream.pipe(writeStream);
|
||||
// var readStream = fs.createReadStream(oldPath);
|
||||
// var writeStream = fs.createWriteStream(newPath);
|
||||
// readStream.on('error', callback);
|
||||
// writeStream.on('error', callback);
|
||||
// readStream.on('close', function () {
|
||||
// fs.unlink(oldPath, callback);
|
||||
// });
|
||||
// readStream.pipe(writeStream);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
File diff suppressed because one or more lines are too long
26
out/test.js
26
out/test.js
@ -1,6 +1,7 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
const index_1 = require("./index");
|
||||
const crypto_1 = require("crypto");
|
||||
index_1.Logging.log("test");
|
||||
index_1.Logging.log("i", "am", { a: "an" }, 1000);
|
||||
index_1.Logging.error(new Error("fehler 001"));
|
||||
@ -26,9 +27,24 @@ cus2.log("Hello from custom Logger 2");
|
||||
cus22.log("Hello from custom Logger 22");
|
||||
cus2.log("Hello from custom Logger 2");
|
||||
index_1.Logging.console_out = false;
|
||||
// Logging.waitForSetup().then(() => {
|
||||
// for (let i = 0; i < 7000; i++) {
|
||||
// Logging.log(randomBytes(50000).toString("hex"))
|
||||
// }
|
||||
// });
|
||||
async function benchmark(count, message_size) {
|
||||
await index_1.Logging.waitForSetup();
|
||||
const randData = crypto_1.randomBytes(message_size).toString("hex");
|
||||
const t = process.hrtime();
|
||||
for (let i = 0; i < count; i++) {
|
||||
index_1.Logging.log(randData);
|
||||
}
|
||||
const diff = process.hrtime(t);
|
||||
const NS_PER_SEC = 1e9;
|
||||
await index_1.Logging.waitForSetup();
|
||||
const ns = diff[0] * NS_PER_SEC + diff[1];
|
||||
console.log(`Benchmark took ${ns / 1000000}ms for ${count} messages with a size of ${message_size} characters`);
|
||||
console.log(`This is equal to ${(ns / 1000000) / count} ms per message`);
|
||||
}
|
||||
index_1.Logging.waitForSetup().then(async () => {
|
||||
console.log("Large data benchmark:");
|
||||
await benchmark(7000, 50000);
|
||||
console.log("Realdata data benchmark:");
|
||||
await benchmark(100000, 100);
|
||||
});
|
||||
//# sourceMappingURL=test.js.map
|
@ -1 +1 @@
|
||||
{"version":3,"file":"test.js","sourceRoot":"","sources":["../src/test.ts"],"names":[],"mappings":";;AAAA,mCAA+C;AAG/C,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,IAAI,GAAG,GAAG,IAAI,mBAAW,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,CAAC,CAAC;AAC5C,GAAG,CAAC,GAAG,CAAC,0BAA0B,CAAC,CAAA;AAEnC,IAAI,IAAI,GAAG,IAAI,mBAAW,CAAC,OAAO,CAAC,CAAC;AACpC,IAAI,CAAC,GAAG,CAAC,4BAA4B,CAAC,CAAA;AAEtC,IAAI,KAAK,GAAG,IAAI,mBAAW,CAAC,OAAO,CAAC,CAAC;AACrC,KAAK,CAAC,GAAG,CAAC,6BAA6B,CAAC,CAAA;AACxC,IAAI,CAAC,GAAG,CAAC,4BAA4B,CAAC,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AO,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC;IAC/B,MAAM,UAAU,GAAG,GAAG,CAAC;IACvB,MAAM,eAAO,CAAC,YAAY,EAAE,CAAC;IAC7B,MAAM,EAAE,GAAG,IAAI,CAAC,CAAC,CAAC,GAAG,UAAU,GAAG,IAAI,CAAC,CAAC,CAAC,CAAC;IAC1C,OAAO,CAAC,GAAG,CAAC,kBAAkB,EAAE,GAAG,OAAO,UAAU,KAAK,4BAA4B,YAAY,aAAa,CAAC,CAAC;IAChH,OAAO,CAAC,GAAG,CAAC,oBAAoB,CAAC,EAAE,GAAG,OAAO,CAAC,GAAG,KAAK,iBAAiB,CAAC,CAAA;AAC3E,CAAC;AAED,eAAO,CAAC,YAAY,EAAE,CAAC,IAAI,CAAC,KAAK,IAAI,EAAE;IACpC,OAAO,CAAC,GAAG,CAAC,uBAAuB,CAAC,CAAA;IACpC,MAAM,SAAS,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC;IAE7B,OAAO,CAAC,GAAG,CAAC,0BAA0B,CAAC,CAAA;IACvC,MAAM,SAAS,CAAC,MAAM,EAAE,GAAG,CAAC,CAAA;AAC/B,CAAC,CAAC,CAAC"}
|
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@hibas123/nodelogging",
|
||||
"version": "1.3.16",
|
||||
"version": "1.3.17",
|
||||
"description": "",
|
||||
"main": "out/index.js",
|
||||
"types": "out/index.d.ts",
|
||||
|
40
src/index.ts
40
src/index.ts
@ -89,6 +89,9 @@ class LoggingFiles {
|
||||
|
||||
private async initializeFile(new_file = false) {
|
||||
try {
|
||||
if (this.stream) {
|
||||
this.stream.close();
|
||||
}
|
||||
const folder = path.dirname(this.file);
|
||||
if (folder) {
|
||||
if (!await fsExists(folder)) {
|
||||
@ -99,7 +102,7 @@ class LoggingFiles {
|
||||
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 >= maxFileSize) {
|
||||
if (await fsExists(this.file + ".old"))
|
||||
await fsUnlink(this.file + ".old");
|
||||
await fsMove(this.file, this.file + ".old")
|
||||
@ -130,11 +133,17 @@ class LoggingFiles {
|
||||
}
|
||||
|
||||
private async write_to_file(data: Buffer) {
|
||||
if (data.byteLength < maxFileSize && this.size + data.byteLength > maxFileSize) {
|
||||
let f = await this.initializeFile(true);
|
||||
try {
|
||||
if (data.byteLength < maxFileSize && this.size + data.byteLength > maxFileSize) {
|
||||
await this.initializeFile(true)
|
||||
}
|
||||
this.size += data.byteLength;
|
||||
this.stream.write(data);
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
this.initializeFile(false);
|
||||
this.write_to_file(data);
|
||||
}
|
||||
this.size += data.byteLength;
|
||||
this.stream.write(data);
|
||||
}
|
||||
|
||||
public write(data: Buffer) {
|
||||
@ -324,7 +333,6 @@ function fsStat(path: string) {
|
||||
|
||||
function fsMove(oldPath: string, newPath: string) {
|
||||
return new Promise((resolve, reject) => {
|
||||
|
||||
let callback = (err?) => {
|
||||
if (err) reject(err)
|
||||
else resolve()
|
||||
@ -343,17 +351,21 @@ function fsMove(oldPath: string, newPath: string) {
|
||||
});
|
||||
|
||||
function copy() {
|
||||
var readStream = fs.createReadStream(oldPath);
|
||||
var writeStream = fs.createWriteStream(newPath);
|
||||
fs.copyFile(oldPath, newPath, (err) => {
|
||||
if (err) callback(err)
|
||||
else fs.unlink(oldPath, callback);
|
||||
})
|
||||
// var readStream = fs.createReadStream(oldPath);
|
||||
// var writeStream = fs.createWriteStream(newPath);
|
||||
|
||||
readStream.on('error', callback);
|
||||
writeStream.on('error', callback);
|
||||
// readStream.on('error', callback);
|
||||
// writeStream.on('error', callback);
|
||||
|
||||
readStream.on('close', function () {
|
||||
fs.unlink(oldPath, callback);
|
||||
});
|
||||
// readStream.on('close', function () {
|
||||
// fs.unlink(oldPath, callback);
|
||||
// });
|
||||
|
||||
readStream.pipe(writeStream);
|
||||
// readStream.pipe(writeStream);
|
||||
}
|
||||
})
|
||||
}
|
||||
|
27
src/test.ts
27
src/test.ts
@ -31,8 +31,25 @@ cus22.log("Hello from custom Logger 22")
|
||||
cus2.log("Hello from custom Logger 2")
|
||||
|
||||
Logging.console_out = false;
|
||||
// Logging.waitForSetup().then(() => {
|
||||
// for (let i = 0; i < 7000; i++) {
|
||||
// Logging.log(randomBytes(50000).toString("hex"))
|
||||
// }
|
||||
// });
|
||||
async function benchmark(count: number, message_size: number) {
|
||||
await Logging.waitForSetup();
|
||||
const randData = randomBytes(message_size).toString("hex")
|
||||
const t = process.hrtime();
|
||||
for (let i = 0; i < count; i++) {
|
||||
Logging.log(randData)
|
||||
}
|
||||
const diff = process.hrtime(t);
|
||||
const NS_PER_SEC = 1e9;
|
||||
await Logging.waitForSetup();
|
||||
const ns = diff[0] * NS_PER_SEC + diff[1];
|
||||
console.log(`Benchmark took ${ns / 1000000}ms for ${count} messages with a size of ${message_size} characters`);
|
||||
console.log(`This is equal to ${(ns / 1000000) / count} ms per message`)
|
||||
}
|
||||
|
||||
Logging.waitForSetup().then(async () => {
|
||||
console.log("Large data benchmark:")
|
||||
await benchmark(7000, 50000);
|
||||
|
||||
console.log("Realdata data benchmark:")
|
||||
await benchmark(100000, 100)
|
||||
});
|
Loading…
Reference in New Issue
Block a user