(namespace: string)
| 50 | * @returns Logger function, or a shared no-op if disabled. |
| 51 | */ |
| 52 | export function createDebug(namespace: string): DebugLogger { |
| 53 | if (!isEnabled(namespace, process.env.DEBUG ?? "")) { |
| 54 | return NOOP; |
| 55 | } |
| 56 | |
| 57 | const colors = useColors(); |
| 58 | const color = colors ? selectColor(namespace) : undefined; |
| 59 | const prefix = |
| 60 | color !== undefined |
| 61 | ? `\x1b[38;5;${color};1m ${namespace}\x1b[0m` |
| 62 | : ` ${namespace}`; |
| 63 | const suffixOpen = color !== undefined ? `\x1b[38;5;${color}m` : ""; |
| 64 | const suffixClose = color !== undefined ? `\x1b[0m` : ""; |
| 65 | let prev = 0; |
| 66 | |
| 67 | const logger = (format: unknown, ...args: unknown[]): void => { |
| 68 | const now = Date.now(); |
| 69 | const diff = prev === 0 ? 0 : now - prev; |
| 70 | prev = now; |
| 71 | |
| 72 | const body = |
| 73 | args.length === 0 && typeof format === "string" |
| 74 | ? format |
| 75 | : formatWithOptions({ colors }, format, ...args); |
| 76 | |
| 77 | process.stderr.write( |
| 78 | `${prefix} ${body} ${suffixOpen}+${diff}ms${suffixClose}\n`, |
| 79 | ); |
| 80 | }; |
| 81 | |
| 82 | return Object.assign(logger, { enabled: true }); |
| 83 | } |
no test coverage detected