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

View File

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

View File

@ -132,22 +132,22 @@ export class ConsoleAdapter implements Adapter {
let color = ""; let color = "";
switch (format.color) { switch (format.color) {
case Colors.RED: case Colors.RED:
color = "red"; color = "#ff5f5f";
break; break;
case Colors.GREEN: case Colors.GREEN:
color = "green"; color = "#62ff5f";
break; break;
case Colors.YELLOW: case Colors.YELLOW:
color = "gold"; color = "#ffea37";
break; break;
case Colors.BLUE: case Colors.BLUE:
color = "blue"; color = "#379aff";
break; break;
case Colors.MAGENTA: case Colors.MAGENTA:
color = "magenta"; color = "#f837ff";
break; break;
case Colors.CYAN: case Colors.CYAN:
color = "cyan"; color = "#37e4ff";
break; break;
case Colors.WHITE: case Colors.WHITE:
color = "white"; color = "white";
@ -161,22 +161,22 @@ export class ConsoleAdapter implements Adapter {
let color = ""; let color = "";
switch (format.bgcolor) { switch (format.bgcolor) {
case Colors.RED: case Colors.RED:
color = "red"; color = "#ff5f5f";
break; break;
case Colors.GREEN: case Colors.GREEN:
color = "green"; color = "#62ff5f";
break; break;
case Colors.YELLOW: case Colors.YELLOW:
color = "gold"; color = "#ffea37";
break; break;
case Colors.BLUE: case Colors.BLUE:
color = "blue"; color = "#379aff";
break; break;
case Colors.MAGENTA: case Colors.MAGENTA:
color = "magenta"; color = "#f837ff";
break; break;
case Colors.CYAN: case Colors.CYAN:
color = "cyan"; color = "#37e4ff";
break; break;
case Colors.WHITE: case Colors.WHITE:
color = "white"; color = "white";

View File

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