-
-
Notifications
You must be signed in to change notification settings - Fork 6
Refactor content script logging & status bar to reduce noise and execution overhead #682
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 3 commits
1c0ca29
ef34ca4
fbe1f47
8d8549a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import { SettingsStorage } from '@dhruv-techapps/acf-store'; | ||
Check warning on line 1 in apps/acf-extension/src/content_scripts/logger.ts
|
||
import { EnhancedLogger, ELoggingLevel } from '@dhruv-techapps/core-common'; | ||
Check warning on line 2 in apps/acf-extension/src/content_scripts/logger.ts
|
||
|
||
export class ContentScriptLogger { | ||
private static instance: ContentScriptLogger; | ||
private logger: EnhancedLogger; | ||
private initialized = false; | ||
|
||
private constructor() { | ||
this.logger = EnhancedLogger.getInstance(); | ||
} | ||
|
||
public static getInstance(): ContentScriptLogger { | ||
if (!ContentScriptLogger.instance) { | ||
ContentScriptLogger.instance = new ContentScriptLogger(); | ||
} | ||
return ContentScriptLogger.instance; | ||
} | ||
|
||
public async initialize(): Promise<void> { | ||
if (this.initialized) { | ||
return; | ||
} | ||
|
||
try { | ||
const settings = await new SettingsStorage().getSettings(); | ||
Check warning on line 26 in apps/acf-extension/src/content_scripts/logger.ts
|
||
const logging = settings.logging; | ||
Check warning on line 27 in apps/acf-extension/src/content_scripts/logger.ts
|
||
|
||
if (logging) { | ||
// Convert string enum to number enum | ||
let level = ELoggingLevel.WARN; | ||
switch (logging.level) { | ||
case 'error': | ||
level = ELoggingLevel.ERROR; | ||
break; | ||
case 'warn': | ||
level = ELoggingLevel.WARN; | ||
break; | ||
case 'info': | ||
level = ELoggingLevel.INFO; | ||
break; | ||
case 'debug': | ||
level = ELoggingLevel.DEBUG; | ||
break; | ||
case 'trace': | ||
level = ELoggingLevel.TRACE; | ||
break; | ||
} | ||
|
||
this.logger.configure({ | ||
level, | ||
enableVerbose: logging.enableVerbose, | ||
useRingBuffer: logging.useRingBuffer, | ||
ringBufferSize: logging.ringBufferSize | ||
}); | ||
} | ||
|
||
this.initialized = true; | ||
} catch (error) { | ||
// Fallback to default configuration | ||
console.warn('Failed to load logging settings, using defaults:', error); | ||
this.initialized = true; | ||
} | ||
} | ||
|
||
public error(scopes: string[], message: string, meta?: unknown): void { | ||
this.logger.error(scopes, message, meta); | ||
} | ||
|
||
public warn(scopes: string[], message: string, meta?: unknown): void { | ||
this.logger.warn(scopes, message, meta); | ||
} | ||
|
||
public info(scopes: string[], message: string, meta?: unknown): void { | ||
this.logger.info(scopes, message, meta); | ||
} | ||
|
||
public debug(scopes: string[], message: string, meta?: unknown): void { | ||
this.logger.debug(scopes, message, meta); | ||
} | ||
|
||
public trace(scopes: string[], message: string, meta?: unknown): void { | ||
this.logger.trace(scopes, message, meta); | ||
} | ||
|
||
public getRingBuffer() { | ||
return this.logger.getRingBuffer(); | ||
} | ||
|
||
public clearRingBuffer(): void { | ||
this.logger.clearRingBuffer(); | ||
} | ||
} | ||
|
||
// Global logger instance | ||
export const logger = ContentScriptLogger.getInstance(); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,77 @@ | ||
import { StatusBar } from '@dhruv-techapps/shared-status-bar'; | ||
import { SettingsStorage } from '@dhruv-techapps/acf-store'; | ||
import { EnhancedStatusBar, StatusBar, EStatusBarMode } from '@dhruv-techapps/shared-status-bar'; | ||
|
||
export const statusBar = new StatusBar(); | ||
class StatusBarFactory { | ||
private static instance: StatusBarFactory; | ||
private statusBar?: EnhancedStatusBar | StatusBar; | ||
private initialized = false; | ||
|
||
private constructor() {} | ||
|
||
public static getInstance(): StatusBarFactory { | ||
if (!StatusBarFactory.instance) { | ||
StatusBarFactory.instance = new StatusBarFactory(); | ||
} | ||
return StatusBarFactory.instance; | ||
} | ||
|
||
public async getStatusBar(): Promise<EnhancedStatusBar | StatusBar> { | ||
if (!this.initialized) { | ||
await this.initialize(); | ||
} | ||
return this.statusBar!; | ||
} | ||
|
||
private async initialize(): Promise<void> { | ||
if (this.initialized) { | ||
return; | ||
} | ||
|
||
try { | ||
const settings = await new SettingsStorage().getSettings(); | ||
|
||
// Use enhanced status bar with settings integration | ||
const enhancedStatusBar = new EnhancedStatusBar(); | ||
|
||
// Configure based on settings | ||
const statusBarMode = settings.statusBarMode ?? EStatusBarMode.FULL; | ||
const enableStatusBar = settings.enableStatusBar ?? true; | ||
const location = settings.statusBar === 'hide' ? 'hide' : settings.statusBar; | ||
|
||
enhancedStatusBar.configure({ | ||
enabled: enableStatusBar && location !== 'hide', | ||
mode: statusBarMode, | ||
location | ||
}); | ||
|
||
// Set location for backward compatibility | ||
if (enableStatusBar && location !== 'hide') { | ||
await enhancedStatusBar.setLocation(location); | ||
} | ||
|
||
this.statusBar = enhancedStatusBar; | ||
this.initialized = true; | ||
} catch (error) { | ||
// Fallback to legacy status bar | ||
console.warn('Failed to load status bar settings, using legacy:', error); | ||
this.statusBar = new StatusBar(); | ||
this.initialized = true; | ||
} | ||
} | ||
} | ||
|
||
// Create proxy to maintain existing API | ||
const factory = StatusBarFactory.getInstance(); | ||
|
||
export const statusBar = new Proxy({} as EnhancedStatusBar | StatusBar, { | ||
get(target, prop) { | ||
return async (...args: any[]) => { | ||
const instance = await factory.getStatusBar(); | ||
const method = (instance as any)[prop]; | ||
if (typeof method === 'function') { | ||
return method.apply(instance, args); | ||
} | ||
return method; | ||
}; | ||
} | ||
}); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The manual string-to-enum conversion could be simplified using a mapping object or by ensuring both enum definitions use the same value types to avoid this conversion logic entirely.
Copilot uses AI. Check for mistakes.