Skip to content

Commit cf9de86

Browse files
committed
log: custom logging using loglevel & enum LogLevel
1 parent effebaa commit cf9de86

File tree

3 files changed

+52
-10
lines changed

3 files changed

+52
-10
lines changed

packages/backend/src/altNodes/jsonNodeConversion.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { PluginSettings } from "types";
33
import { variableToColorName } from "../tailwind/conversionTables";
44
import { HasGeometryTrait, Node, Paint } from "../api_types";
55
import { calculateRectangleFromBoundingBox } from "../common/commonPosition";
6+
import { bench } from "../log";
67

78
// Performance tracking counters
89
export let getNodeByIdAsyncTime = 0;
@@ -547,7 +548,7 @@ export const nodesToJSON = async (
547548

548549
console.log("[debug] initial nodeJson", { ...nodes[0] });
549550

550-
console.log(
551+
bench(
551552
`[benchmark][inside nodesToJSON] JSON_REST_V1 export: ${Date.now() - exportJsonStart}ms`,
552553
);
553554

@@ -574,7 +575,7 @@ export const nodesToJSON = async (
574575
}
575576
}
576577

577-
console.log(
578+
bench(
578579
`[benchmark][inside nodesToJSON] Process node pairs: ${Date.now() - processNodesStart}ms`,
579580
);
580581

packages/backend/src/code.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {
2222
processColorVariablesTime,
2323
resetPerformanceCounters,
2424
} from "./altNodes/jsonNodeConversion";
25+
import { bench } from "./log";
2526

2627
export const run = async (settings: PluginSettings) => {
2728
resetPerformanceCounters();
@@ -44,7 +45,7 @@ export const run = async (settings: PluginSettings) => {
4445
console.log("convertedSelection", convertedSelection);
4546
} else {
4647
convertedSelection = await nodesToJSON(selection, settings);
47-
console.log(`[benchmark] nodesToJSON: ${Date.now() - nodeToJSONStart}ms`);
48+
bench(`[benchmark] nodesToJSON: ${Date.now() - nodeToJSONStart}ms`);
4849
console.log("nodeJson", convertedSelection);
4950
}
5051

@@ -59,38 +60,38 @@ export const run = async (settings: PluginSettings) => {
5960

6061
const convertToCodeStart = Date.now();
6162
const code = await convertToCode(convertedSelection, settings);
62-
console.log(
63+
bench(
6364
`[benchmark] convertToCode: ${Date.now() - convertToCodeStart}ms`,
6465
);
6566

6667
const generatePreviewStart = Date.now();
6768
const htmlPreview = await generateHTMLPreview(convertedSelection, settings);
68-
console.log(
69+
bench(
6970
`[benchmark] generateHTMLPreview: ${Date.now() - generatePreviewStart}ms`,
7071
);
7172

7273
const colorPanelStart = Date.now();
7374
const colors = retrieveGenericSolidUIColors(framework);
7475
const gradients = retrieveGenericLinearGradients(framework);
75-
console.log(
76+
bench(
7677
`[benchmark] color and gradient panel: ${Date.now() - colorPanelStart}ms`,
7778
);
78-
console.log(
79+
bench(
7980
`[benchmark] total generation time: ${Date.now() - nodeToJSONStart}ms`,
8081
);
8182

8283
// Log performance statistics
83-
console.log(
84+
bench(
8485
`[benchmark] getNodeByIdAsync: ${getNodeByIdAsyncTime}ms (${getNodeByIdAsyncCalls} calls, avg: ${(getNodeByIdAsyncTime / getNodeByIdAsyncCalls || 1).toFixed(2)}ms)`,
8586
);
86-
console.log(
87+
bench(
8788
`[benchmark] getStyledTextSegments: ${getStyledTextSegmentsTime}ms (${getStyledTextSegmentsCalls} calls, avg: ${
8889
getStyledTextSegmentsCalls > 0
8990
? (getStyledTextSegmentsTime / getStyledTextSegmentsCalls).toFixed(2)
9091
: 0
9192
}ms)`,
9293
);
93-
console.log(
94+
bench(
9495
`[benchmark] processColorVariables: ${processColorVariablesTime}ms (${processColorVariablesCalls} calls, avg: ${
9596
processColorVariablesCalls > 0
9697
? (processColorVariablesTime / processColorVariablesCalls).toFixed(2)

packages/backend/src/log.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
const enum LogLevel {
2+
None = 0,
3+
Benchmark,
4+
Error,
5+
Warn,
6+
Info,
7+
Log,
8+
}
9+
10+
let loglevel = LogLevel.None;
11+
12+
const kConsoleLog = console.log;
13+
const kConsoleInfo = console.info;
14+
const kConsoleError = console.error;
15+
const kConsoleWarn = console.warn;
16+
17+
export const bench = (message?: any, ...optionalParams: any[]) => {
18+
if (loglevel < LogLevel.Benchmark) return;
19+
kConsoleLog(message, optionalParams);
20+
}
21+
22+
console.log = function(message?: any, ...optionalParams: any[]) {
23+
if (loglevel < LogLevel.Log) return;
24+
kConsoleLog(message, optionalParams);
25+
}
26+
27+
console.info = function(message?: any, ...optionalParams: any[]) {
28+
if (loglevel < LogLevel.Info) return;
29+
kConsoleInfo(message, optionalParams);
30+
}
31+
32+
console.warn = function(message?: any, ...optionalParams: any[]) {
33+
if (loglevel < LogLevel.Warn) return;
34+
kConsoleWarn(message, optionalParams);
35+
}
36+
37+
console.error = function(message?: any, ...optionalParams: any[]) {
38+
if (loglevel < LogLevel.Error) return;
39+
kConsoleError(message, optionalParams);
40+
}

0 commit comments

Comments
 (0)