traceperf 0.1.0 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (75) hide show
  1. package/README.md +110 -1
  2. package/dist/browser.d.ts +67 -5
  3. package/dist/browser.js +207 -36
  4. package/dist/browser.js.map +1 -1
  5. package/dist/esm/browser.d.ts +129 -0
  6. package/dist/esm/browser.js +426 -0
  7. package/dist/esm/browser.js.map +1 -0
  8. package/dist/esm/core/config.d.ts +77 -0
  9. package/dist/esm/core/config.js +112 -0
  10. package/dist/esm/core/config.js.map +1 -0
  11. package/dist/esm/core/constants.d.ts +78 -0
  12. package/dist/esm/core/constants.js +106 -0
  13. package/dist/esm/core/constants.js.map +1 -0
  14. package/dist/esm/core/index.d.ts +3 -0
  15. package/dist/esm/core/index.js +4 -0
  16. package/dist/esm/core/index.js.map +1 -0
  17. package/dist/esm/core/logger.d.ts +84 -0
  18. package/dist/esm/core/logger.js +170 -0
  19. package/dist/esm/core/logger.js.map +1 -0
  20. package/dist/esm/formatters/ascii.d.ts +52 -0
  21. package/dist/esm/formatters/ascii.js +100 -0
  22. package/dist/esm/formatters/ascii.js.map +1 -0
  23. package/dist/esm/formatters/cli.d.ts +50 -0
  24. package/dist/esm/formatters/cli.js +96 -0
  25. package/dist/esm/formatters/cli.js.map +1 -0
  26. package/dist/esm/formatters/index.d.ts +3 -0
  27. package/dist/esm/formatters/index.js +4 -0
  28. package/dist/esm/formatters/index.js.map +1 -0
  29. package/dist/esm/formatters/json.d.ts +45 -0
  30. package/dist/esm/formatters/json.js +85 -0
  31. package/dist/esm/formatters/json.js.map +1 -0
  32. package/dist/esm/index.d.ts +10 -0
  33. package/dist/esm/index.js +29 -0
  34. package/dist/esm/index.js.map +1 -0
  35. package/dist/esm/trackers/execution.d.ts +88 -0
  36. package/dist/esm/trackers/execution.js +214 -0
  37. package/dist/esm/trackers/execution.js.map +1 -0
  38. package/dist/esm/trackers/index.d.ts +2 -0
  39. package/dist/esm/trackers/index.js +3 -0
  40. package/dist/esm/trackers/index.js.map +1 -0
  41. package/dist/esm/trackers/performance.d.ts +82 -0
  42. package/dist/esm/trackers/performance.js +106 -0
  43. package/dist/esm/trackers/performance.js.map +1 -0
  44. package/dist/esm/types/config.d.ts +110 -0
  45. package/dist/esm/types/config.js +2 -0
  46. package/dist/esm/types/config.js.map +1 -0
  47. package/dist/esm/types/index.d.ts +34 -0
  48. package/dist/esm/types/index.js +3 -0
  49. package/dist/esm/types/index.js.map +1 -0
  50. package/dist/esm/types/logger.d.ts +127 -0
  51. package/dist/esm/types/logger.js +2 -0
  52. package/dist/esm/types/logger.js.map +1 -0
  53. package/dist/esm/utils/colors.d.ts +87 -0
  54. package/dist/esm/utils/colors.js +134 -0
  55. package/dist/esm/utils/colors.js.map +1 -0
  56. package/dist/esm/utils/index.d.ts +3 -0
  57. package/dist/esm/utils/index.js +4 -0
  58. package/dist/esm/utils/index.js.map +1 -0
  59. package/dist/esm/utils/stack.d.ts +63 -0
  60. package/dist/esm/utils/stack.js +110 -0
  61. package/dist/esm/utils/stack.js.map +1 -0
  62. package/dist/esm/utils/timing.d.ts +65 -0
  63. package/dist/esm/utils/timing.js +99 -0
  64. package/dist/esm/utils/timing.js.map +1 -0
  65. package/dist/index.js +13 -0
  66. package/dist/index.js.map +1 -1
  67. package/dist/trackers/execution.d.ts +23 -7
  68. package/dist/trackers/execution.js +100 -29
  69. package/dist/trackers/execution.js.map +1 -1
  70. package/dist/types/config.js +2 -1
  71. package/dist/types/index.d.ts +32 -0
  72. package/dist/types/index.js +18 -2
  73. package/dist/types/index.js.map +1 -1
  74. package/dist/types/logger.js +2 -1
  75. package/package.json +16 -2
@@ -0,0 +1,214 @@
1
+ import { CallStackManager, getFunctionName } from '../utils/stack';
2
+ import { getHighResTime, getDuration } from '../utils/timing';
3
+ import { AsciiArtGenerator } from '../formatters/ascii';
4
+ /**
5
+ * Execution tracker for tracking function calls and generating flow charts
6
+ */
7
+ export class ExecutionTracker {
8
+ /**
9
+ * Create a new ExecutionTracker instance
10
+ *
11
+ * @param options - Tracker options
12
+ */
13
+ constructor(options = {}) {
14
+ var _a;
15
+ this._executions = [];
16
+ this._currentExecution = null;
17
+ this._instrumentedFunctions = new Map();
18
+ this._isInstrumented = false;
19
+ this._callStack = new CallStackManager();
20
+ this._asciiGenerator = new AsciiArtGenerator({ boxWidth: options.boxWidth });
21
+ this._defaultThreshold = (_a = options.defaultThreshold) !== null && _a !== void 0 ? _a : 100; // ms
22
+ }
23
+ /**
24
+ * Track the execution of a function
25
+ *
26
+ * @param fn - The function to track
27
+ * @param options - Options for tracking
28
+ * @returns The return value of the tracked function
29
+ */
30
+ track(fn, options) {
31
+ var _a, _b, _c;
32
+ const fnName = (options === null || options === void 0 ? void 0 : options.label) || getFunctionName(fn);
33
+ const threshold = (_a = options === null || options === void 0 ? void 0 : options.threshold) !== null && _a !== void 0 ? _a : this._defaultThreshold;
34
+ const includeMemory = (_b = options === null || options === void 0 ? void 0 : options.includeMemory) !== null && _b !== void 0 ? _b : true;
35
+ const enableNestedTracking = (_c = options === null || options === void 0 ? void 0 : options.enableNestedTracking) !== null && _c !== void 0 ? _c : true;
36
+ // Get memory usage before execution
37
+ const startMemory = includeMemory ? process.memoryUsage().heapUsed : undefined;
38
+ // Start timing
39
+ const startTime = getHighResTime();
40
+ // Add to call stack
41
+ const level = this._callStack.push(fnName);
42
+ // Create execution record
43
+ const execution = {
44
+ name: fnName,
45
+ duration: 0,
46
+ isSlow: false,
47
+ level: level - 1, // 0-based level
48
+ startTime,
49
+ children: [],
50
+ };
51
+ // Set parent-child relationship
52
+ if (this._currentExecution) {
53
+ execution.parent = this._currentExecution;
54
+ this._currentExecution.children.push(execution);
55
+ }
56
+ else {
57
+ // This is a root execution
58
+ this._executions.push(execution);
59
+ }
60
+ // Save the previous current execution
61
+ const previousExecution = this._currentExecution;
62
+ // Set this as the current execution
63
+ this._currentExecution = execution;
64
+ // Flag to track if we're already instrumenting to avoid recursion
65
+ const wasInstrumented = this._isInstrumented;
66
+ try {
67
+ // If this is the top-level call and nested tracking is enabled, instrument the function
68
+ if (enableNestedTracking && !wasInstrumented) {
69
+ this._isInstrumented = true;
70
+ // Instrument the function to track nested calls
71
+ const instrumentedFn = this.instrumentFunction(fn);
72
+ return instrumentedFn();
73
+ }
74
+ else {
75
+ // Execute the function normally
76
+ return fn();
77
+ }
78
+ }
79
+ finally {
80
+ // End timing
81
+ const endTime = getHighResTime();
82
+ execution.endTime = endTime;
83
+ // Calculate duration
84
+ execution.duration = getDuration(startTime);
85
+ // Check if it's a bottleneck
86
+ execution.isSlow = execution.duration > threshold;
87
+ // Calculate memory usage
88
+ if (includeMemory && startMemory !== undefined) {
89
+ execution.memoryUsage = process.memoryUsage().heapUsed - startMemory;
90
+ }
91
+ // Restore the previous current execution
92
+ this._currentExecution = previousExecution;
93
+ // Remove from call stack
94
+ this._callStack.pop();
95
+ // Reset instrumentation flag if this was the top-level call
96
+ if (!wasInstrumented) {
97
+ this._isInstrumented = false;
98
+ }
99
+ }
100
+ }
101
+ /**
102
+ * Instrument a function to track nested function calls
103
+ *
104
+ * @param fn - The function to instrument
105
+ * @returns The instrumented function
106
+ */
107
+ instrumentFunction(fn) {
108
+ // If we've already instrumented this function, return the instrumented version
109
+ if (this._instrumentedFunctions.has(fn)) {
110
+ return this._instrumentedFunctions.get(fn);
111
+ }
112
+ // Create a new instrumented function
113
+ const instrumentedFn = () => {
114
+ try {
115
+ // We can't actually modify the function at runtime to track nested calls
116
+ // in a generic way. The best we can do is execute the function and rely
117
+ // on the call stack for visualization.
118
+ //
119
+ // For proper nested function tracking, users should wrap each function
120
+ // they want to track with tracePerf.track() or use a more advanced
121
+ // instrumentation approach like aspect-oriented programming or
122
+ // code transformation.
123
+ return fn();
124
+ }
125
+ catch (error) {
126
+ // Re-throw any errors
127
+ throw error;
128
+ }
129
+ };
130
+ // Cache the instrumented function
131
+ this._instrumentedFunctions.set(fn, instrumentedFn);
132
+ return instrumentedFn;
133
+ }
134
+ /**
135
+ * Get the current call stack
136
+ *
137
+ * @returns The current call stack
138
+ */
139
+ getCallStack() {
140
+ return this._callStack.getStack();
141
+ }
142
+ /**
143
+ * Generate a visual representation of the execution flow
144
+ *
145
+ * @returns ASCII flow chart of the execution
146
+ */
147
+ generateFlowChart() {
148
+ // Convert execution records to execution data for the ASCII generator
149
+ const executionData = this.prepareExecutionData(this._executions);
150
+ // Generate the flow chart
151
+ return this._asciiGenerator.generateFlowChart(executionData);
152
+ }
153
+ /**
154
+ * Prepare execution data for the ASCII generator
155
+ *
156
+ * @param executions - The execution records to prepare
157
+ * @returns A list of execution data
158
+ */
159
+ prepareExecutionData(executions) {
160
+ const result = [];
161
+ // Process each root execution
162
+ executions.forEach(execution => {
163
+ // Add the root execution
164
+ result.push({
165
+ name: execution.name,
166
+ duration: execution.duration,
167
+ isSlow: execution.isSlow,
168
+ memoryUsage: execution.memoryUsage,
169
+ level: execution.level,
170
+ });
171
+ // Add children recursively
172
+ this.addChildrenToResult(execution.children, result);
173
+ });
174
+ return result;
175
+ }
176
+ /**
177
+ * Add children to the result list
178
+ *
179
+ * @param children - The children to add
180
+ * @param result - The result list
181
+ */
182
+ addChildrenToResult(children, result) {
183
+ children.forEach(child => {
184
+ // Add the child
185
+ result.push({
186
+ name: child.name,
187
+ duration: child.duration,
188
+ isSlow: child.isSlow,
189
+ memoryUsage: child.memoryUsage,
190
+ level: child.level,
191
+ });
192
+ // Add grandchildren recursively
193
+ this.addChildrenToResult(child.children, result);
194
+ });
195
+ }
196
+ /**
197
+ * Clear all execution records
198
+ */
199
+ clear() {
200
+ this._executions = [];
201
+ this._currentExecution = null;
202
+ this._callStack.clear();
203
+ this._instrumentedFunctions.clear();
204
+ }
205
+ /**
206
+ * Get all execution records
207
+ *
208
+ * @returns All execution records
209
+ */
210
+ getExecutions() {
211
+ return [...this._executions];
212
+ }
213
+ }
214
+ //# sourceMappingURL=execution.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"execution.js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,QAAQ,EAAE,KAAK,CAAC,QAAQ;gBACxB,MAAM,EAAE,KAAK,CAAC,MAAM;gBACpB,WAAW,EAAE,KAAK,CAAC,WAAW;gBAC9B,KAAK,EAAE,KAAK,CAAC,KAAK;aACnB,CAAC,CAAC;YAEH,gCAAgC;YAChC,IAAI,CAAC,mBAAmB,CAAC,KAAK,CAAC,QAAQ,EAAE,MAAM,CAAC,CAAC;QACnD,CAAC,CAAC,CAAC;IACL,CAAC;IAED;;OAEG;IACI,KAAK;QACV,IAAI,CAAC,WAAW,GAAG,EAAE,CAAC;QACtB,IAAI,CAAC,iBAAiB,GAAG,IAAI,CAAC;QAC9B,IAAI,CAAC,UAAU,CAAC,KAAK,EAAE,CAAC;QACxB,IAAI,CAAC,sBAAsB,CAAC,KAAK,EAAE,CAAC;IACtC,CAAC;IAED;;;;OAIG;IACI,aAAa;QAClB,OAAO,CAAC,GAAG,IAAI,CAAC,WAAW,CAAC,CAAC;IAC/B,CAAC;CACF"}
@@ -0,0 +1,2 @@
1
+ export * from './execution';
2
+ export * from './performance';
@@ -0,0 +1,3 @@
1
+ export * from './execution';
2
+ export * from './performance';
3
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/trackers/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAC;AAC5B,cAAc,eAAe,CAAC"}
@@ -0,0 +1,82 @@
1
+ import { IPerformanceMonitor } from '../types';
2
+ /**
3
+ * Performance monitor for tracking execution times and memory usage
4
+ */
5
+ export declare class PerformanceMonitor implements IPerformanceMonitor {
6
+ private _timers;
7
+ private _defaultThreshold;
8
+ /**
9
+ * Create a new PerformanceMonitor instance
10
+ *
11
+ * @param options - Monitor options
12
+ */
13
+ constructor(options?: {
14
+ defaultThreshold?: number;
15
+ });
16
+ /**
17
+ * Start timing an operation
18
+ *
19
+ * @param label - Label for the operation
20
+ * @returns A unique identifier for the timing operation
21
+ */
22
+ startTimer(label: string): string;
23
+ /**
24
+ * End timing an operation
25
+ *
26
+ * @param id - The identifier returned by startTimer
27
+ * @returns The duration in milliseconds
28
+ * @throws Error if the timer doesn't exist
29
+ */
30
+ endTimer(id: string): number;
31
+ /**
32
+ * Check if an operation exceeds the performance threshold
33
+ *
34
+ * @param duration - The duration in milliseconds
35
+ * @param threshold - The threshold to check against
36
+ * @returns Whether the operation is considered slow
37
+ */
38
+ isBottleneck(duration: number, threshold?: number): boolean;
39
+ /**
40
+ * Get memory usage information
41
+ *
42
+ * @returns Memory usage in bytes
43
+ */
44
+ getMemoryUsage(): {
45
+ heapUsed: number;
46
+ heapTotal: number;
47
+ external: number;
48
+ rss: number;
49
+ };
50
+ /**
51
+ * Get memory difference between two points
52
+ *
53
+ * @param before - Memory usage before
54
+ * @param after - Memory usage after
55
+ * @returns Memory difference in bytes
56
+ */
57
+ getMemoryDiff(before: {
58
+ heapUsed: number;
59
+ heapTotal: number;
60
+ external: number;
61
+ rss: number;
62
+ }, after: {
63
+ heapUsed: number;
64
+ heapTotal: number;
65
+ external: number;
66
+ rss: number;
67
+ }): {
68
+ heapUsed: number;
69
+ heapTotal: number;
70
+ external: number;
71
+ rss: number;
72
+ };
73
+ /**
74
+ * Generate a suggestion for a bottleneck
75
+ *
76
+ * @param functionName - The name of the function
77
+ * @param duration - The duration in milliseconds
78
+ * @param memoryUsage - Memory usage in bytes
79
+ * @returns A suggestion for improving performance
80
+ */
81
+ generateSuggestion(functionName: string, duration: number, memoryUsage?: number): string;
82
+ }
@@ -0,0 +1,106 @@
1
+ import { getHighResTime, getDuration } from '../utils/timing';
2
+ import { v4 as uuidv4 } from 'uuid';
3
+ /**
4
+ * Performance monitor for tracking execution times and memory usage
5
+ */
6
+ export class PerformanceMonitor {
7
+ /**
8
+ * Create a new PerformanceMonitor instance
9
+ *
10
+ * @param options - Monitor options
11
+ */
12
+ constructor(options = {}) {
13
+ var _a;
14
+ this._timers = new Map();
15
+ this._defaultThreshold = (_a = options.defaultThreshold) !== null && _a !== void 0 ? _a : 100; // ms
16
+ }
17
+ /**
18
+ * Start timing an operation
19
+ *
20
+ * @param label - Label for the operation
21
+ * @returns A unique identifier for the timing operation
22
+ */
23
+ startTimer(label) {
24
+ const id = `${label}-${uuidv4()}`;
25
+ this._timers.set(id, getHighResTime());
26
+ return id;
27
+ }
28
+ /**
29
+ * End timing an operation
30
+ *
31
+ * @param id - The identifier returned by startTimer
32
+ * @returns The duration in milliseconds
33
+ * @throws Error if the timer doesn't exist
34
+ */
35
+ endTimer(id) {
36
+ const startTime = this._timers.get(id);
37
+ if (!startTime) {
38
+ throw new Error(`Timer with id ${id} not found`);
39
+ }
40
+ const duration = getDuration(startTime);
41
+ this._timers.delete(id);
42
+ return duration;
43
+ }
44
+ /**
45
+ * Check if an operation exceeds the performance threshold
46
+ *
47
+ * @param duration - The duration in milliseconds
48
+ * @param threshold - The threshold to check against
49
+ * @returns Whether the operation is considered slow
50
+ */
51
+ isBottleneck(duration, threshold) {
52
+ const effectiveThreshold = threshold !== null && threshold !== void 0 ? threshold : this._defaultThreshold;
53
+ return duration > effectiveThreshold;
54
+ }
55
+ /**
56
+ * Get memory usage information
57
+ *
58
+ * @returns Memory usage in bytes
59
+ */
60
+ getMemoryUsage() {
61
+ const memoryUsage = process.memoryUsage();
62
+ return {
63
+ heapUsed: memoryUsage.heapUsed,
64
+ heapTotal: memoryUsage.heapTotal,
65
+ external: memoryUsage.external,
66
+ rss: memoryUsage.rss,
67
+ };
68
+ }
69
+ /**
70
+ * Get memory difference between two points
71
+ *
72
+ * @param before - Memory usage before
73
+ * @param after - Memory usage after
74
+ * @returns Memory difference in bytes
75
+ */
76
+ getMemoryDiff(before, after) {
77
+ return {
78
+ heapUsed: after.heapUsed - before.heapUsed,
79
+ heapTotal: after.heapTotal - before.heapTotal,
80
+ external: after.external - before.external,
81
+ rss: after.rss - before.rss,
82
+ };
83
+ }
84
+ /**
85
+ * Generate a suggestion for a bottleneck
86
+ *
87
+ * @param functionName - The name of the function
88
+ * @param duration - The duration in milliseconds
89
+ * @param memoryUsage - Memory usage in bytes
90
+ * @returns A suggestion for improving performance
91
+ */
92
+ generateSuggestion(functionName, duration, memoryUsage) {
93
+ // Simple heuristic-based suggestions
94
+ if (memoryUsage && memoryUsage > 10 * 1024 * 1024) {
95
+ return `🛠 Potential Fix: Check for memory leaks or large object allocations in ${functionName}`;
96
+ }
97
+ if (duration > 1000) {
98
+ return `🛠 Potential Fix: Consider optimizing or adding caching to ${functionName}`;
99
+ }
100
+ if (duration > 500) {
101
+ return `🛠 Potential Fix: Look for blocking operations in ${functionName}`;
102
+ }
103
+ return `🛠 Potential Fix: Review ${functionName} for performance optimizations`;
104
+ }
105
+ }
106
+ //# sourceMappingURL=performance.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"performance.js","sourceRoot":"","sources":["../../../src/trackers/performance.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,cAAc,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAC;AAC9D,OAAO,EAAE,EAAE,IAAI,MAAM,EAAE,MAAM,MAAM,CAAC;AAEpC;;GAEG;AACH,MAAM,OAAO,kBAAkB;IAI7B;;;;OAIG;IACH,YAAY,UAAyC,EAAE;;QAR/C,YAAO,GAAkC,IAAI,GAAG,EAAE,CAAC;QASzD,IAAI,CAAC,iBAAiB,GAAG,MAAA,OAAO,CAAC,gBAAgB,mCAAI,GAAG,CAAC,CAAC,KAAK;IACjE,CAAC;IAED;;;;;OAKG;IACI,UAAU,CAAC,KAAa;QAC7B,MAAM,EAAE,GAAG,GAAG,KAAK,IAAI,MAAM,EAAE,EAAE,CAAC;QAClC,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,EAAE,EAAE,cAAc,EAAE,CAAC,CAAC;QACvC,OAAO,EAAE,CAAC;IACZ,CAAC;IAED;;;;;;OAMG;IACI,QAAQ,CAAC,EAAU;QACxB,MAAM,SAAS,GAAG,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,EAAE,CAAC,C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CAAC;YAClD,OAAO,2EAA2E,YAAY,EAAE,CAAC;QACnG,CAAC;QAED,IAAI,QAAQ,GAAG,IAAI,EAAE,CAAC;YACpB,OAAO,8DAA8D,YAAY,EAAE,CAAC;QACtF,CAAC;QAED,IAAI,QAAQ,GAAG,GAAG,EAAE,CAAC;YACnB,OAAO,qDAAqD,YAAY,EAAE,CAAC;QAC7E,CAAC;QAED,OAAO,4BAA4B,YAAY,gCAAgC,CAAC;IAClF,CAAC;CACF"}
@@ -0,0 +1,110 @@
1
+ /**
2
+ * LogLevel - Defines the severity levels for logging
3
+ */
4
+ export type LogLevel = 'debug' | 'info' | 'warn' | 'error';
5
+ /**
6
+ * LogMode - Defines the operational modes for the logger
7
+ */
8
+ export type LogMode = 'dev' | 'staging' | 'prod' | string;
9
+ /**
10
+ * Transport - Interface for log output destinations
11
+ */
12
+ export interface ITransport {
13
+ /**
14
+ * Write a log entry to the destination
15
+ *
16
+ * @param entry - The formatted log entry to write
17
+ */
18
+ write(entry: string): void;
19
+ }
20
+ /**
21
+ * Formatter - Interface for log formatters
22
+ */
23
+ export interface IFormatter {
24
+ /**
25
+ * Format a log entry
26
+ *
27
+ * @param level - The log level
28
+ * @param message - The log message
29
+ * @param args - Additional arguments
30
+ * @param meta - Metadata for the log entry
31
+ * @returns The formatted log entry
32
+ */
33
+ format(level: LogLevel, message: string | object, args: any[], meta: LogMeta): string;
34
+ }
35
+ /**
36
+ * LogMeta - Metadata for log entries
37
+ */
38
+ export interface LogMeta {
39
+ timestamp: Date;
40
+ level: LogLevel;
41
+ indentLevel: number;
42
+ [key: string]: any;
43
+ }
44
+ /**
45
+ * TrackOptions - Options for function execution tracking
46
+ */
47
+ export interface ITrackOptions {
48
+ /**
49
+ * Custom label for the tracked function
50
+ */
51
+ label?: string;
52
+ /**
53
+ * Performance threshold in milliseconds
54
+ * Functions exceeding this threshold will be flagged as bottlenecks
55
+ */
56
+ threshold?: number;
57
+ /**
58
+ * Whether to include memory usage tracking
59
+ */
60
+ includeMemory?: boolean;
61
+ /**
62
+ * Whether to suppress logging for this tracking
63
+ */
64
+ silent?: boolean;
65
+ }
66
+ /**
67
+ * LoggerConfig - Configuration options for the logger
68
+ */
69
+ export interface ILoggerConfig {
70
+ /**
71
+ * Operational mode
72
+ * @default 'dev'
73
+ */
74
+ mode?: LogMode;
75
+ /**
76
+ * Minimum log level to display
77
+ * @default 'debug'
78
+ */
79
+ level?: LogLevel;
80
+ /**
81
+ * Whether to colorize output
82
+ * @default true
83
+ */
84
+ colorize?: boolean;
85
+ /**
86
+ * Whether to include timestamps in logs
87
+ * @default true
88
+ */
89
+ timestamp?: boolean;
90
+ /**
91
+ * Default performance threshold in milliseconds
92
+ * @default 100
93
+ */
94
+ performanceThreshold?: number;
95
+ /**
96
+ * Number of spaces for each indentation level
97
+ * @default 2
98
+ */
99
+ indentSize?: number;
100
+ /**
101
+ * Log output destinations
102
+ * @default [ConsoleTransport]
103
+ */
104
+ transports?: ITransport[];
105
+ /**
106
+ * Log formatters
107
+ * @default [CliFormatter]
108
+ */
109
+ formatters?: IFormatter[];
110
+ }
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=config.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"config.js","sourceRoot":"","sources":["../../../src/types/config.ts"],"names":[],"mappings":""}
@@ -0,0 +1,34 @@
1
+ export * from './config';
2
+ export * from './logger';
3
+ /**
4
+ * Options for tracking function execution
5
+ */
6
+ export interface ITrackOptions {
7
+ /**
8
+ * Custom label for the function
9
+ */
10
+ label?: string;
11
+ /**
12
+ * Performance threshold in milliseconds
13
+ */
14
+ threshold?: number;
15
+ /**
16
+ * Whether to include memory usage in the tracking
17
+ */
18
+ includeMemory?: boolean;
19
+ /**
20
+ * Whether to track nested function calls
21
+ *
22
+ * Note: This option doesn't automatically track all nested function calls.
23
+ * It only enables the infrastructure for tracking nested calls, but you still
24
+ * need to manually track each nested function using one of the approaches
25
+ * described in the documentation.
26
+ *
27
+ * @default true
28
+ */
29
+ enableNestedTracking?: boolean;
30
+ /**
31
+ * Whether to suppress logging
32
+ */
33
+ silent?: boolean;
34
+ }
@@ -0,0 +1,3 @@
1
+ export * from './config';
2
+ export * from './logger';
3
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/types/index.ts"],"names":[],"mappings":"AAAA,cAAc,UAAU,CAAC;AACzB,cAAc,UAAU,CAAC"}