Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions src/utils/logger.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,84 @@
import chalk from "chalk";

interface TimingEntry {
label: string;
startTime: number;
endTime?: number;
}

export class Logger {
private verbose: boolean;
private timings: Map<string, TimingEntry> = new Map();

constructor() {
this.verbose = process.env.VERBOSE === "true";
}

/**
* Start timing an operation
*/
startTimer(label: string): void {
this.timings.set(label, {
label,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MAJOR (80% confidence)

Using Date.now() for timing measurements can be inaccurate due to system clock adjustments. Consider using performance.now() for more precise timing measurements, especially for performance monitoring.

startTime: Date.now(),
});
if (this.verbose) {
console.log(chalk.magenta("⏱"), `Started: ${label}`);
}
}

/**
* End timing and log the duration
*/
endTimer(label: string): number {
const entry = this.timings.get(label);
if (!entry) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MINOR (70% confidence)

Returning 0 for non-existent timers could mask bugs. Consider throwing an error or returning null/undefined to make the error more explicit to calling code.

this.warning(`Timer "${label}" was never started`);
return 0;
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MINOR (90% confidence)

Redundant Date.now() call - you're calculating duration correctly but then overwriting entry.endTime with a new timestamp. Should be: entry.endTime = entry.startTime + duration;

const duration = Date.now() - entry.startTime;
entry.endTime = Date.now();

// Format duration for display
let durationStr: string;
if (duration < 1000) {
durationStr = `${duration}ms`;
} else if (duration < 60000) {
durationStr = `${(duration / 1000).toFixed(2)}s`;
} else {
const mins = Math.floor(duration / 60000);
const secs = ((duration % 60000) / 1000).toFixed(1);
durationStr = `${mins}m ${secs}s`;
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MINOR (80% confidence)

The endTimer method always logs to console regardless of verbose setting, while startTimer respects it. Consider making this behavior consistent - either both respect verbose or both always log.


console.log(chalk.magenta("⏱"), `${label}: ${chalk.bold(durationStr)}`);
return duration;
}

/**
* Get all timing data for analysis
*/
getTimingReport(): { label: string; duration: number }[] {
const report: { label: string; duration: number }[] = [];
for (const [label, entry] of this.timings) {
if (entry.endTime) {
report.push({
label,
duration: entry.endTime - entry.startTime,
});
}
}
return report;
}

/**
* Clear all timing data
*/
clearTimings(): void {
this.timings = new Map();
}

info(...args: any[]): void {
console.log(chalk.blue("ℹ"), ...args);
}
Expand Down