Skip to content
Merged
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
50 changes: 41 additions & 9 deletions images/chromium-headful/client/src/plugins/log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ interface Logger {
}

declare global {
const $log: Logger

interface Window {
$log: Logger
}
Expand All @@ -21,15 +19,49 @@ declare module 'vue/types/vue' {
}
}

const noop = () => {}
const noopError = (_: Error) => {}

const realLoggers: Logger = {
error: (error: Error) => console.error('[%cNEKO%c] %cERR', 'color: #498ad8;', '', 'color: #d84949;', error),
warn: (...log: any[]) => console.warn('[%cNEKO%c] %cWRN', 'color: #498ad8;', '', 'color: #eae364;', ...log),
info: (...log: any[]) => console.info('[%cNEKO%c] %cINF', 'color: #498ad8;', '', 'color: #4ac94c;', ...log),
debug: (...log: any[]) => console.log('[%cNEKO%c] %cDBG', 'color: #498ad8;', '', 'color: #eae364;', ...log),
}

const offLoggers: Logger = {
error: noopError,
warn: noop,
info: noop,
debug: noop,
}

const LOG_METHODS: (keyof Logger)[] = ['error', 'warn', 'info', 'debug']
const DISABLED_LEVELS = ['off', 'none', '']

function createLoggerForLevel(level: string): Logger {
const normalized = level.toLowerCase()
if (DISABLED_LEVELS.includes(normalized)) return offLoggers

const enabledIndex = LOG_METHODS.indexOf(normalized as keyof Logger)
if (enabledIndex === -1) return offLoggers

const logger: Logger = { ...offLoggers }
for (let i = 0; i <= enabledIndex; i++) {
const method = LOG_METHODS[i]
;(logger as any)[method] = realLoggers[method]
}
return logger
}

function getLogLevel(): string {
const params = new URL(location.href).searchParams
return params.get('log_level') ?? params.get('logLevel') ?? 'off'
}

const plugin: PluginObject<undefined> = {
install(Vue) {
window.$log = {
error: (error: Error) => console.error('[%cNEKO%c] %cERR', 'color: #498ad8;', '', 'color: #d84949;', error),
warn: (...log: any[]) => console.warn('[%cNEKO%c] %cWRN', 'color: #498ad8;', '', 'color: #eae364;', ...log),
info: (...log: any[]) => console.info('[%cNEKO%c] %cINF', 'color: #498ad8;', '', 'color: #4ac94c;', ...log),
debug: (...log: any[]) => console.log('[%cNEKO%c] %cDBG', 'color: #498ad8;', '', 'color: #eae364;', ...log),
}

window.$log = createLoggerForLevel(getLogLevel())
Vue.prototype.$log = window.$log
},
}
Expand Down
Loading