Add more time functions

This commit is contained in:
Fabian Stamm
2025-10-21 13:59:24 +02:00
parent 3de5f368ef
commit 3526766e68
5 changed files with 1453 additions and 2876 deletions

4227
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -1,6 +1,6 @@
{
"name": "@hibas123/logging",
"version": "4.0.1",
"version": "4.0.2",
"description": "",
"type": "module",
"main": "esm/index.js",
@ -10,7 +10,6 @@
"prepublishOnly": "npm run build",
"build": "tsc",
"test": "tsc && node esm/test.js",
"postpublish": "denreg publish",
"bench": "tsc && node benchmark.js",
"prof": "tsc && ndb --prof benchmark.js"
},
@ -27,10 +26,10 @@
"readme.md"
],
"devDependencies": {
"concurrently": "^6.0.2",
"concurrently": "^9.2.1",
"ndb": "^1.1.5",
"nodemon": "^2.0.7",
"ts-node": "^9.1.1",
"typescript": "^4.2.4"
"nodemon": "^3.1.10",
"ts-node": "^10.9.2",
"typescript": "^5.9.3"
}
}

View File

@ -31,6 +31,7 @@ export interface ILoggingOptions {
export interface INativeFunctions {
startTimer(): any;
diffTime(start: any, end: any): number;
endTimer(start: any): number;
}
@ -42,6 +43,9 @@ export const DefaultNativeFunctions = {
return Date.now();
}
},
diffTime: (start: any, end: any) => {
return end - start;
},
endTimer: (start: any) => {
if (browser && window.performance && window.performance.now) {
return window.performance.now() - start;
@ -59,7 +63,7 @@ const InitialisedAdapters = Symbol("@hibas123/logging:initialisedAdapters");
export abstract class LoggingInterface implements ILoggingInterface {
#names: string[];
#timerMap = new Map<string, { name: string; start: any }>();
#timerMap = new Map<string, { name: string; start: any, marks: { time: any, name: string }[] }>();
get names() {
return [...this.#names];
@ -123,13 +127,18 @@ export abstract class LoggingInterface implements ILoggingInterface {
time(id?: string) {
if (!id) id = Math.floor(Math.random() * 899999 + 100000).toString();
this.#timerMap.set(id, {
let timer = {
name: id,
start: LoggingBase.nativeFunctions.startTimer(),
});
marks: [],
};
this.#timerMap.set(id, timer);
return {
id,
mark: (label: string) => {
timer.marks.push({ time: LoggingBase.nativeFunctions.startTimer(), name: label });
},
end: () => this.timeEnd(id),
};
}
@ -137,7 +146,8 @@ export abstract class LoggingInterface implements ILoggingInterface {
timeEnd(id: string) {
let timer = this.#timerMap.get(id);
if (timer) {
let diff = LoggingBase.nativeFunctions.endTimer(timer.start);
let end_time = LoggingBase.nativeFunctions.startTimer();
let diff = LoggingBase.nativeFunctions.diffTime(timer.start, end_time);
this.message(LoggingTypes.Debug, this.#names, [
Format.green(`[${timer.name}]`),
@ -146,6 +156,27 @@ export abstract class LoggingInterface implements ILoggingInterface {
"ms",
]);
if (timer.marks.length > 0) {
let last_mark = timer.start;
for (let mark of timer.marks) {
let diff = LoggingBase.nativeFunctions.diffTime(last_mark, mark.time);
this.message(LoggingTypes.Debug, [...this.#names, timer.name], [
Format.green(`[${mark.name}]`),
`->`,
Format.blue(diff.toFixed(4)),
"ms",
]);
last_mark = mark.time;
}
let diff = LoggingBase.nativeFunctions.diffTime(last_mark, end_time);
this.message(LoggingTypes.Debug, [...this.#names, timer.name], [
Format.green(`[end]`),
`->`,
Format.blue(diff.toFixed(4)),
"ms",
]);
}
return diff;
}
return -1;
@ -273,8 +304,7 @@ export class LoggingBase extends LoggingInterface {
file_raw.file = newF;
}
file = `${file_raw.file || "<unknown>"}:${file_raw.line}:${
file_raw.column || 0
file = `${file_raw.file || "<unknown>"}:${file_raw.line}:${file_raw.column || 0
}`;
}
@ -425,7 +455,7 @@ function getCallerFile() {
};
}
}
} catch (err) {}
} catch (err) { }
return { file: undefined, line: 0 };
}

View File

@ -17,10 +17,10 @@ const NodeJS = typeof process !== undefined;
export class ConsoleAdapter implements Adapter {
level: LoggingTypes = LoggingTypes.Debug;
constructor(private colors: boolean = true) {}
constructor(private colors: boolean = true) { }
init() {}
flush() {}
init() { }
flush() { }
setLevel(level: LoggingTypes) {
this.level = level;
@ -132,22 +132,22 @@ export class ConsoleAdapter implements Adapter {
let color = "";
switch (format.color) {
case Colors.RED:
color = "red";
color = "#ff5f5f";
break;
case Colors.GREEN:
color = "green";
color = "#62ff5f";
break;
case Colors.YELLOW:
color = "gold";
color = "#ffea37";
break;
case Colors.BLUE:
color = "blue";
color = "#379aff";
break;
case Colors.MAGENTA:
color = "magenta";
color = "#f837ff";
break;
case Colors.CYAN:
color = "cyan";
color = "#37e4ff";
break;
case Colors.WHITE:
color = "white";
@ -161,22 +161,22 @@ export class ConsoleAdapter implements Adapter {
let color = "";
switch (format.bgcolor) {
case Colors.RED:
color = "red";
color = "#ff5f5f";
break;
case Colors.GREEN:
color = "green";
color = "#62ff5f";
break;
case Colors.YELLOW:
color = "gold";
color = "#ffea37";
break;
case Colors.BLUE:
color = "blue";
color = "#379aff";
break;
case Colors.MAGENTA:
color = "magenta";
color = "#f837ff";
break;
case Colors.CYAN:
color = "cyan";
color = "#37e4ff";
break;
case Colors.WHITE:
color = "white";

View File

@ -240,6 +240,7 @@ export interface Adapter {
}
export interface ILoggingTimer {
mark: (label: string) => void;
end: () => number;
}