@meld-ts/core
    Preparing search index...

    Interface DebuggableTrait<Settings>

    debuggable 返回的 trait 接口,供 interface 声明合并使用。

    // biome-ignore lint/suspicious/noUnsafeDeclarationMerging: implTraits guarantees runtime implementation
    class MyService { ... }

    implTraits(MyService, debuggable({ name: 'MyService', color: 'color: #6bcb77' }))

    // biome-ignore lint/correctness/noUnusedVariables: trait type extension via implTraits
    interface MyService extends DebuggableTrait {}
    interface DebuggableTrait<Settings extends DebugSettings = DebugSettings> {
        debugConfig?: Partial<DebugConfiguration>;
        debugSettings?: Partial<Settings>;
        setDebug(debug?: boolean | Partial<Settings>): this;
        shouldDebug(
            scope?: string | Exclude<keyof Settings, "debug"> | null,
        ): boolean;
        debug(
            scope: string | Exclude<keyof Settings, "debug"> | null | undefined,
            ...args: unknown[],
        ): this;
        getStack(skipFrames?: number): string[];
    }

    Type Parameters

    Index

    Properties

    debugConfig?: Partial<DebugConfiguration>

    初始化配置,挂载在原型上,所有实例共享。

    可变——可在 constructor 中通过 this.debugConfig!.name = '...' 或直接赋值实例自有属性来按实例定制。改原型字段影响所有实例, 改实例自有属性仅影响该实例。

    debugSettings?: Partial<Settings>

    运行期调试开关,setDebug() 后成为实例自有属性,每个实例独立。

    可变——可直接读写 this.debugSettingssetDebug() 是推荐方式 (提供合并语义),但手动修改同样有效。

    Methods

    • 设置调试开关。

      • setDebug(true) — 开启全局调试
      • setDebug(false) — 关闭全局调试
      • setDebug({ fetch: true }) — 仅开启 fetch scope,与已有 settings 合并
      • setDebug() — 无参调用为 no-op

      Parameters

      • Optionaldebug: boolean | Partial<Settings>

      Returns this

    • 判断指定 scope 是否应输出调试信息。

      优先级(由高到低):

      1. settings[scope](精确匹配)
      2. settings[ns](取 scope 中第一个 . 前的命名空间)
      3. settings.debug(全局开关)

      Parameters

      • Optionalscope: string | Exclude<keyof Settings, "debug"> | null

      Returns boolean

      svc.setDebug({ debug: false, fetch: true })
      svc.shouldDebug('fetch') // true — scope 精确匹配
      svc.shouldDebug('fetch.request') // true — 继承 fetch 命名空间
      svc.shouldDebug('ws') // false — 未配置,回落到 debug: false
    • shouldDebug(scope)true 时输出调试信息,否则静默返回。

      输出格式(默认):[时间戳] [name] [scope] ...args 颜色和格式可通过 DebugConfiguration 定制。

      Parameters

      • scope: string | Exclude<keyof Settings, "debug"> | null | undefined

        当前调试 scope,传 null / undefined 表示无 scope

      • ...args: unknown[]

        要输出的内容

      Returns this

    • 获取当前调用栈,返回每帧的文本数组。

      基于 new Error().stack,兼容所有 JS 运行时(V8 / JSC / SpiderMonkey)。 V8 环境可用的 Error.captureStackTrace 能更精确排除内部帧, 但作为通用库不依赖非标准 API。

      Parameters

      • OptionalskipFrames: number

        — 额外跳过的栈帧数。默认 0 ("Error" 行始终被排除)。典型用法:传 1 跳过 getStack 自身, 传 2 再加上调用方的 wrapper。

      Returns string[]

      栈帧字符串数组,每帧已 trim 首尾空白

      // 输出从调用点开始的完整栈
      console.log(svc.getStack());

      // 跳过 getStack 自身,只展示更上层的调用栈
      console.log(svc.getStack(1));