feat(desktop): add log-level verbosity helpers

This commit is contained in:
91he66
2026-08-17 13:20:14 +08:00
parent c796ae6dfd
commit 6d3c413f8e
2 changed files with 51 additions and 0 deletions
+17
View File
@@ -0,0 +1,17 @@
/** One Cordis logger severity, mirroring `@deepseek-ai/cordis`'s `LoggerType`. */
export type LogType = 'error' | 'info' | 'warn' | 'debug'
/** User-selectable log verbosity threshold. */
export type LogLevel = 'debug' | 'info' | 'warn' | 'error'
const VERBOSITY: Record<LogType, number> = { error: 0, warn: 1, info: 2, debug: 3 }
/** Whether a message of `type` should be emitted at `threshold`. */
export function shouldEmit(type: LogType, threshold: LogLevel): boolean {
return VERBOSITY[type] <= VERBOSITY[threshold]
}
/** Whether a message of `type` belongs in the per-day error log. */
export function isErrorType(type: LogType): boolean {
return type === 'warn' || type === 'error'
}
@@ -0,0 +1,34 @@
import { describe, expect, it } from 'vitest'
import { isErrorType, shouldEmit } from '../src/log-level.ts'
describe('shouldEmit', () => {
it('emits nothing above the threshold', () => {
expect(shouldEmit('debug', 'info')).toBe(false)
expect(shouldEmit('info', 'info')).toBe(true)
expect(shouldEmit('warn', 'info')).toBe(true)
expect(shouldEmit('error', 'info')).toBe(true)
})
it('emits only errors at the error threshold', () => {
expect(shouldEmit('error', 'error')).toBe(true)
expect(shouldEmit('warn', 'error')).toBe(false)
expect(shouldEmit('info', 'error')).toBe(false)
expect(shouldEmit('debug', 'error')).toBe(false)
})
it('emits everything at the debug threshold', () => {
expect(shouldEmit('debug', 'debug')).toBe(true)
expect(shouldEmit('info', 'debug')).toBe(true)
expect(shouldEmit('warn', 'debug')).toBe(true)
expect(shouldEmit('error', 'debug')).toBe(true)
})
})
describe('isErrorType', () => {
it('treats warn and error as the error log', () => {
expect(isErrorType('warn')).toBe(true)
expect(isErrorType('error')).toBe(true)
expect(isErrorType('info')).toBe(false)
expect(isErrorType('debug')).toBe(false)
})
})