| ... | @@ -1,28 +1,47 @@ | ... | @@ -1,28 +1,47 @@ |
| 1 | /** | 1 | /** |
| 2 | * By using `lib/log.ts`, your application gets easy scoped logging as well as | 2 | * by using `lib/log.ts`, your application gets easy scoped logging as well as |
| 3 | * integration with terminal widgets such as `lib/progress.ts`. | 3 | * integration with terminal widgets such as `lib/progress.ts`. |
| 4 | * | 4 | * |
| 5 | * The pattern for using this module is to shadow the global `console` with a | 5 | * the pattern for using this module is to shadow the global `console` with a |
| 6 | * local scope: | 6 | * per-file logging scope: |
| 7 | * | 7 | * |
| 8 | * import * as log from "@clo/lib/log"; | 8 | * ```ts |
| 9 | * const console = log.scoped("http"); | 9 | * import * as log from "@clo/lib/log"; |
| 10 | * console("hi!"); // debug | 10 | * const console = log.scoped("http"); |
| | 11 | * console.info("Hello world!"); // info message |
| | 12 | * console.log("hi!"); // debug message only |
| | 13 | * ``` |
| 11 | * | 14 | * |
| 12 | * Or to use the global socpe, import the namespace as `console`. | 15 | * or to use the global scope, import the module's namespace as `console`. |
| 13 | * | 16 | * |
| 14 | * import * as console from "@clo/lib/log"; | 17 | * ```ts |
| | 18 | * import * as console from "@clo/lib/log"; |
| | 19 | * ``` |
| 15 | * | 20 | * |
| 16 | * Now, the code reads familiarly and output is organized. | 21 | * now, the code reads familiarly (`console.log` is universally understood), |
| | 22 | * but the output is organized into relevant scopes. |
| 17 | * | 23 | * |
| 18 | * This module offers a Node.js integration to have logs go to the standard | 24 | * in addition to static log messages, a system for interactive I/O |
| 19 | * output. Custom writers can be used with `lib/log/headless.ts`. | 25 | * {@linkcode Widget} is provided by calling {@linkcode startWidget}. these |
| | 26 | * allow showing temporary or interactive information, such as program status |
| | 27 | * or input prompts. a powerful example of this system in action is |
| | 28 | * `lib/progress.ts`, which uses a log widget by default to show status. |
| | 29 | * (TODO: widgets cannot recieve "input" data yet) |
| | 30 | * |
| | 31 | * this module offers two environment integrations: |
| | 32 | * - in node.js, log messages are colored depending on the level and show |
| | 33 | * widgets directly under the long using ANSI cursor controls. |
| | 34 | * - otherwise, logs are surfaced using the global `console` API |
| | 35 | * |
| | 36 | * custom log integrations can be built on top of this module by calling |
| | 37 | * `log.tee()` to duplicate all messages elsewhere. for example, a project may set up |
| 20 | * | 38 | * |
| 21 | * @module | 39 | * @module |
| 22 | */ | 40 | */ |
| 23 | | 41 | |
| 24 | /** | 42 | /** |
| 25 | * Logging scopes implement part of the `Console` API | 43 | * logging scopes implement part of the `Console` API. the `log` module itself |
| | 44 | * satisfies this interface. |
| 26 | */ | 45 | */ |
| 27 | export interface Scope { | 46 | export interface Scope { |
| 28 | /** emit an informational message */ | 47 | /** emit an informational message */ |
| ... | @@ -44,15 +63,33 @@ export interface Scope { | ... | @@ -44,15 +63,33 @@ export interface Scope { |
| 44 | | 63 | |
| 45 | /** create a nested sub-scope */ | 64 | /** create a nested sub-scope */ |
| 46 | scoped(name: string): Scope; | 65 | scoped(name: string): Scope; |
| | 66 | /** redirect the logging output of this scope somewhere else */ |
| | 67 | tee(writer: (message: Message) => void): ts.Dispose; |
| 47 | } | 68 | } |
| 48 | | 69 | |
| 49 | /** custom scopes are colored based on the name */ | 70 | export const originalLogArgs = Symbol("originalLogArgs"); |
| 50 | export function scoped(name: string): Scope { | 71 | export interface Message { |
| 51 | return globalLog.scoped(name); | 72 | level: "error" | "warn" | "info" | "debug"; |
| | 73 | /** ANSI-styled unicode text */ |
| | 74 | text: string; |
| | 75 | /** datetime in milliseconds since UNIX epoch */ |
| | 76 | time: number; |
| | 77 | /** scope name */ |
| | 78 | scope?: string | null; |
| | 79 | /** captured stack. */ |
| | 80 | stack?: stack.Frame[]; |
| | 81 | /** arbitrary data from the logging source. */ |
| | 82 | custom?: Partial<Record<string, unknown>>; |
| | 83 | /** |
| | 84 | * original logging arguments, if present. this field is indexed by a symbol |
| | 85 | * so that it is lost during JSON serialization, as callers are allowed to log |
| | 86 | * non-serializable data. |
| | 87 | */ |
| | 88 | [originalLogArgs]?: unknown[]; |
| 52 | } | 89 | } |
| 53 | | 90 | |
| 54 | // these functions implement `Scope`. If a function takes in `Scope`, the | 91 | // these functions implement `Scope` for the module's namespace. that means if |
| 55 | // namespace import of this file can be passed to it. | 92 | // a function takes in `Scope`, this file's namespace satisfies that. |
| 56 | | 93 | |
| 57 | /** emit an informational message on the global log scope */ | 94 | /** emit an informational message on the global log scope */ |
| 58 | export function info(...args: unknown[]) { | 95 | export function info(...args: unknown[]) { |
| ... | @@ -79,10 +116,34 @@ export function debug(...args: unknown[]) { | ... | @@ -79,10 +116,34 @@ export function debug(...args: unknown[]) { |
| 79 | globalLog.debug(...args); | 116 | globalLog.debug(...args); |
| 80 | } | 117 | } |
| 81 | | 118 | |
| | 119 | export function scoped(name: string): Scope { |
| | 120 | return globalLog.scoped(name); |
| | 121 | } |
| | 122 | |
| | 123 | /** |
| | 124 | * a widget is an interactive display that persists at the end |
| | 125 | * of the log. this can be used to implement status bars, progress |
| | 126 | * indicators, and other human I/O. only 'format' is required. |
| | 127 | * |
| | 128 | * ```ts |
| | 129 | * using _ = log.startWidget({ |
| | 130 | * format: (now) => `It is ${new Date().toString()} right now\n` |
| | 131 | * + `A random number: ${Math.random()}`, |
| | 132 | * // no trailing '\n' is needed. |
| | 133 | * }); |
| | 134 | * await async.delay(10000); |
| | 135 | * |
| | 136 | * import * as log from "@clo/lib/log.ts"; |
| | 137 | * import * as async from "@clo/lib/async.ts"; |
| | 138 | * ``` |
| | 139 | */ |
| | 140 | export function startWidget(widget: Widget): ts.Dispose { |
| | 141 | return globalWidgetHost.startWidget(widget); |
| | 142 | } |
| | 143 | |
| 82 | /** | 144 | /** |
| 83 | * no built-in prefix or formatting. ensures the text does not interweave. data | 145 | * no built-in prefix or formatting. ensures the text does not interweave. data |
| 84 | * will be flushed in the next frame or when drawing is | 146 | * will be flushed in the next frame or when drawing is {@link getDrawLock|unlocked}. |
| 85 | * {@link getDrawLock|unlocked}. | | |
| 86 | */ | 147 | */ |
| 87 | export function writeLine(text: string) { | 148 | export function writeLine(text: string) { |
| 88 | globalWidgetHost.writeLine(text); | 149 | globalWidgetHost.writeLine(text); |
| ... | @@ -96,48 +157,403 @@ export function getDrawLock(): ts.Dispose { | ... | @@ -96,48 +157,403 @@ export function getDrawLock(): ts.Dispose { |
| 96 | return globalWidgetHost.getDrawLock(); | 157 | return globalWidgetHost.getDrawLock(); |
| 97 | } | 158 | } |
| 98 | | 159 | |
| 99 | /** | 160 | export function headlessScope(dispatch: DispatchFunction): Scope { |
| 100 | * a widget is an interactive display that persists at the end | 161 | return new Scope(dispatch); |
| 101 | * of the log. this can be used to implement status bars, progress | | |
| 102 | * indicators, and other human i/o. only 'format' is required. | | |
| 103 | */ | | |
| 104 | export function startWidget(widget: Widget): ts.Dispose { | | |
| 105 | return globalWidgetHost.startWidget(widget); | | |
| 106 | } | 162 | } |
| 107 | | 163 | |
| | 164 | /** See {@linkcode startWidget} */ |
| 108 | export interface Widget { | 165 | export interface Widget { |
| 109 | /** | 166 | /** |
| 110 | * return the widget's text. return null to detach the widget. | 167 | * return the widget's text. return null to detach the widget. |
| 111 | * may get called more often than the specified `fps`. | 168 | * may get called more often than the specified `fps`. |
| 112 | * supports color codes but not ansi cursor movements. | 169 | * supports color codes but not ansi cursor movements. |
| 113 | */ | 170 | */ format( |
| 114 | format(now: ReturnType<typeof performance.now>): string | null; | 171 | now: ReturnType<typeof performance.now>, |
| 115 | /** 'null' to never update (use 'onChange'). defaults to 12 fps */ | 172 | ): |
| 116 | fps?: number | null; | 173 | | string |
| 117 | /** Subscribe to manual widget updates. Call `rerender` when needed. */ | 174 | | null; /** 'null' to never update (use 'onChange'). defaults to 12 fps */ |
| | 175 | fps?: |
| | 176 | | number |
| | 177 | | null; /** Subscribe to manual widget updates. Call `rerender` when needed. */ |
| 118 | onChange?(rerender: () => void): () => void; | 178 | onChange?(rerender: () => void): () => void; |
| 119 | // /** | 179 | /** listen for keyboard events. */ |
| 120 | // * listen for keyboard events. | 180 | onKey?(key: string): void; |
| 121 | // */ | | |
| 122 | // onKey?(key: string): void; | | |
| 123 | } | 181 | } |
| 124 | | 182 | |
| 125 | const globalWidgetHost = /* @__PURE__ */ ((): headless.WidgetHost => { | 183 | /** {@linkcode widgetHost}'s input takes terminal i/o as well as timing APIs */ |
| | 184 | export interface HeadlessWidgetEnv { |
| | 185 | /** Recieves ANSI escape sequences for interactive data. */ |
| | 186 | writeInteractive(text: string): void; |
| | 187 | /** Recieves log content (from `writeLine`). */ |
| | 188 | writeOutput(text: string): void; |
| | 189 | /** Monotonic. */ |
| | 190 | now(): ReturnType<typeof performance.now>; |
| | 191 | /** 0ms indicates "one frame". */ |
| | 192 | wait(ms: number, cb: () => void): () => void; |
| | 193 | /** Called often. */ |
| | 194 | getSize(): { columns: number; rows: number }; |
| | 195 | /** Called to enable input events */ |
| | 196 | onInput?(write: (bytes: Uint8Array | string) => void): () => void; |
| | 197 | } |
| | 198 | |
| | 199 | /** an implementation of an ANSI-based widget host */ |
| | 200 | export interface HeadlessWidgetHost { |
| | 201 | /** see the top-level {@linkcode writeLine} function */ |
| | 202 | writeLine(text: string): void; |
| | 203 | /** see the top-level {@linkcode getDrawLock} function */ |
| | 204 | getDrawLock(): ts.Dispose; |
| | 205 | /** see the top-level {@linkcode startWidget} function */ |
| | 206 | startWidget(widget: Widget): ts.Dispose; |
| | 207 | /** stop all widgets and remove all timers. */ |
| | 208 | cancel(): void; |
| | 209 | } |
| | 210 | |
| | 211 | interface WidgetState { |
| | 212 | frameTime: number; |
| | 213 | next: number; |
| | 214 | unsub: (() => void) | null; |
| | 215 | } |
| | 216 | |
| | 217 | /** |
| | 218 | * terminal widget rendering is done by specifying all system APIs up front in |
| | 219 | * an interface, creating an instance of the "widget host". |
| | 220 | * |
| | 221 | * some notes on widget rendering: |
| | 222 | * - Never flush immediately (exception for |
| | 223 | * {@linkcode WidgetHost["getDrawLock"]|getDrawLock}), always do it next tick. |
| | 224 | * - Maximum of one `wait` call at once. When the expected time suddenly |
| | 225 | * shrinks, the timer is rescheduled. |
| | 226 | * - When redrawing widget lines, three tricks are done to reduce flickering: |
| | 227 | * 1. Tell the terminal not to flicker (ansi.syncStart/syncEnd) |
| | 228 | * 2. A simple prefix-based diffing algorithm for skipping unchanged text |
| | 229 | * 3. Avoid clearing a line before redrawing it. |
| | 230 | * Points 2 and 3 are used for terminals that are slow or do not support sync. |
| | 231 | */ |
| | 232 | export function headlessWidgetHost(env: HeadlessWidgetEnv): HeadlessWidgetHost { |
| | 233 | const { writeOutput, writeInteractive, now, wait, getSize } = env; |
| | 234 | type Cancel = ReturnType<typeof wait>; |
| | 235 | |
| | 236 | let timer: Cancel | null = null; |
| | 237 | |
| | 238 | let locks = 0; |
| | 239 | let redrawTime = 0; |
| | 240 | let lastFlush = 0; |
| | 241 | let buffer = ""; |
| | 242 | const widgets: Widget[] = []; |
| | 243 | const internals: WidgetState[] = []; |
| | 244 | let lines: string[] = []; |
| | 245 | |
| | 246 | function redrawCallback() { |
| | 247 | timer = null; |
| | 248 | redrawTime = (lastFlush = now()) - 0.00001; // windows time precision workaround |
| | 249 | |
| | 250 | if (!lines.length && !widgets.length) { |
| | 251 | buffer && writeOutput(buffer); |
| | 252 | buffer = ""; |
| | 253 | return; |
| | 254 | } |
| | 255 | |
| | 256 | const { columns, rows } = getSize(); |
| | 257 | let newWidgetLines: string[] = []; |
| | 258 | let next = Infinity; |
| | 259 | if (widgets[0]) { |
| | 260 | for (let w = 0, { length } = widgets; w < length; w += 1) { |
| | 261 | const outText = UNWRAP(widgets[w]).format(lastFlush); |
| | 262 | if (!outText) { |
| | 263 | widgets.splice(w, 1); |
| | 264 | UNWRAP(internals.splice(w, 1)[0]).unsub?.(); |
| | 265 | w -= 1; |
| | 266 | length -= 1; |
| | 267 | continue; |
| | 268 | } |
| | 269 | const rowsLeft = Math.max(1, rows - newWidgetLines.length - 1); |
| | 270 | if (rowsLeft === 1) break; |
| | 271 | const lines = outText.split("\n").slice(0, rowsLeft); |
| | 272 | newWidgetLines.push( |
| | 273 | ...lines.map((line) => ansi.trimToWidth(line, columns - 1)), |
| | 274 | ); |
| | 275 | |
| | 276 | next = Math.min(next, UNWRAP(internals[w]).frameTime); |
| | 277 | } |
| | 278 | |
| | 279 | newWidgetLines = newWidgetLines.slice(0, rows - 1); |
| | 280 | } |
| | 281 | |
| | 282 | if (next < Infinity) redrawSoon(next); |
| | 283 | |
| | 284 | if (!newWidgetLines[0]) { |
| | 285 | if (lines.length) { |
| | 286 | let clearLinesTop = Math.min( |
| | 287 | lines.length, |
| | 288 | string.countNewlines(buffer), |
| | 289 | ); |
| | 290 | writeInteractive( |
| | 291 | ansi.startOfLine + |
| | 292 | // skip up the shared widget space |
| | 293 | ansi.cursorUp(lines.length) + |
| | 294 | // clear the lines to contain `buffer` |
| | 295 | (ansi.clearFullLine + ansi.startOfNextLine) |
| | 296 | .repeat(clearLinesTop) + |
| | 297 | ansi.cursorUp(clearLinesTop), |
| | 298 | ); |
| | 299 | lines = []; |
| | 300 | } |
| | 301 | buffer && writeOutput(buffer); |
| | 302 | buffer = ""; |
| | 303 | return; |
| | 304 | } |
| | 305 | |
| | 306 | if (buffer) { |
| | 307 | // do not perform diffing since the entire screen is moving down |
| | 308 | // TODO: should i handle wrapping? |
| | 309 | let clearLinesTop = Math.min( |
| | 310 | lines.length, |
| | 311 | string.countNewlines(buffer), |
| | 312 | ); |
| | 313 | const oldLines = lines.slice(clearLinesTop); |
| | 314 | writeInteractive( |
| | 315 | ansi.syncStart + ansi.startOfLine + |
| | 316 | // skip up the shared widget space |
| | 317 | ansi.cursorUp(lines.length - clearLinesTop + 1) + |
| | 318 | // clear the lines to contain `buffer` |
| | 319 | ansi.clearFullLine + |
| | 320 | (ansi.cursorUp(1) + ansi.clearFullLine) |
| | 321 | .repeat(clearLinesTop - 1), |
| | 322 | ); |
| | 323 | // then write output lines on standard out |
| | 324 | writeOutput(buffer); |
| | 325 | writeInteractive( |
| | 326 | // the widget text |
| | 327 | newWidgetLines.map((newLine, i) => |
| | 328 | (newLine.includes("\x1b") && !newLine.endsWith(ansi.reset) |
| | 329 | ? newLine + ansi.reset |
| | 330 | : newLine) + |
| | 331 | // clear rest of line if needed |
| | 332 | (oldLines[i] && |
| | 333 | ansi.widthInTerminal(oldLines[i]) > |
| | 334 | ansi.widthInTerminal(newLine) |
| | 335 | ? ansi.clearToEndOfLine |
| | 336 | : "") + |
| | 337 | "\n" |
| | 338 | ).join("") + ansi.syncEnd, |
| | 339 | ); |
| | 340 | } else { |
| | 341 | const clearLinesBottom = Math.min( |
| | 342 | lines.length, |
| | 343 | Math.max(0, lines.length - (newWidgetLines?.length ?? 0)), |
| | 344 | ); |
| | 345 | writeInteractive( |
| | 346 | ansi.syncStart + ansi.startOfLine + |
| | 347 | // clear the bottom lines |
| | 348 | (clearLinesBottom |
| | 349 | ? (ansi.cursorUp(1) + ansi.clearToEndOfLine) |
| | 350 | .repeat(clearLinesBottom) |
| | 351 | : "") + |
| | 352 | // skip up the widget space |
| | 353 | ansi.cursorUp(lines.length - clearLinesBottom) + |
| | 354 | // the widget text |
| | 355 | newWidgetLines.map((newLine, i) => |
| | 356 | (newLine.includes("\x1b") && !newLine.endsWith(ansi.reset) |
| | 357 | ? newLine + ansi.reset |
| | 358 | : newLine) + |
| | 359 | // clear rest of line if needed |
| | 360 | (lines[i] && |
| | 361 | ansi.widthInTerminal(lines[i]) > |
| | 362 | ansi.widthInTerminal(newLine) |
| | 363 | ? ansi.clearToEndOfLine |
| | 364 | : "") + |
| | 365 | "\n" |
| | 366 | ).join("") + |
| | 367 | ansi.syncEnd, |
| | 368 | ); |
| | 369 | } |
| | 370 | lines = newWidgetLines; |
| | 371 | |
| | 372 | buffer = ""; |
| | 373 | } |
| | 374 | |
| | 375 | function redrawSoon(ms: number) { |
| | 376 | if (locks > 0 || (ms === 0 && timer)) return; |
| | 377 | const newRedrawTime = now() + ms; |
| | 378 | if (timer) { |
| | 379 | if (redrawTime < newRedrawTime) return; |
| | 380 | timer(); // cancel previous |
| | 381 | timer = null; |
| | 382 | } |
| | 383 | redrawTime = newRedrawTime + 1; |
| | 384 | timer = wait(ms, redrawCallback); |
| | 385 | } |
| | 386 | |
| | 387 | function flushAndClear() { |
| | 388 | timer?.(); |
| | 389 | timer = null; |
| | 390 | if (lines.length > 0) { |
| | 391 | // clear widgets |
| | 392 | } |
| | 393 | if (buffer.length > 0) writeOutput(buffer), buffer = ""; |
| | 394 | } |
| | 395 | |
| | 396 | return { |
| | 397 | writeLine(chunk) { |
| | 398 | buffer += chunk + "\n"; |
| | 399 | redrawSoon(0); |
| | 400 | }, |
| | 401 | getDrawLock() { |
| | 402 | if (locks === 0) flushAndClear(); |
| | 403 | locks += 1; |
| | 404 | return ts.defer(() => { |
| | 405 | locks -= 1; |
| | 406 | if (locks === 0) { |
| | 407 | if (buffer.length > 0) writeOutput(buffer), buffer = ""; |
| | 408 | // TODO: reinit widget |
| | 409 | } |
| | 410 | }); |
| | 411 | }, |
| | 412 | startWidget(w) { |
| | 413 | if (!widgets.includes(w)) { |
| | 414 | const state: WidgetState = { |
| | 415 | next: 0, |
| | 416 | unsub: null, |
| | 417 | frameTime: 1000 / (w.fps ?? 0), |
| | 418 | }; |
| | 419 | widgets.push(w); |
| | 420 | internals.push(state); |
| | 421 | state.unsub = w.onChange?.(() => { |
| | 422 | state.next = 0; |
| | 423 | redrawSoon(0); |
| | 424 | }) ?? null; |
| | 425 | redrawSoon(0); |
| | 426 | } |
| | 427 | return ts.defer(() => { |
| | 428 | const i = widgets.indexOf(w); |
| | 429 | widgets.splice(i, 1); |
| | 430 | UNWRAP(internals.splice(i, 1)[0]).unsub?.(); |
| | 431 | redrawSoon(0); |
| | 432 | }); |
| | 433 | }, |
| | 434 | cancel() { |
| | 435 | flushAndClear(); |
| | 436 | }, |
| | 437 | }; |
| | 438 | } |
| | 439 | |
| | 440 | const logColors = node.process?.stderr.isTTY ?? false; |
| | 441 | |
| | 442 | let formatLine = /* @__PURE__ */ (() => { |
| | 443 | const fwo = node.builtin("util")?.formatWithOptions; |
| | 444 | if (fwo) return (...args: unknown[]) => fwo({ colors: logColors }, ...args); |
| | 445 | return (...args: unknown[]) => |
| | 446 | args.map((arg) => { |
| | 447 | if (typeof arg === "string") return arg; |
| | 448 | try { |
| | 449 | return JSON.stringify(arg); |
| | 450 | } catch { |
| | 451 | return "[incompatible json " + typeof arg + "]"; |
| | 452 | } |
| | 453 | }).join(" "); |
| | 454 | })(); |
| | 455 | |
| | 456 | let withinDispatch = false; |
| | 457 | let withinStackCapture = false; |
| | 458 | |
| | 459 | const Scope = class Scope implements Scope { |
| | 460 | name: string | undefined; |
| | 461 | // TODO: this abstraction implementation has low performance. making every |
| | 462 | // scope define it's own dispatch is needed to correctly implement `tee`. |
| | 463 | // since it is possible to implement this in simple and non-recursive way, i |
| | 464 | // feel okay adding the `tee` API. |
| | 465 | #dispatches: DispatchFunction[] = []; |
| | 466 | #dispatch: DispatchFunction = (m) => this.#dispatches.forEach((cb) => cb(m)); |
| | 467 | |
| | 468 | constructor( |
| | 469 | dispatch: (m: Message) => void, |
| | 470 | name: string | undefined = undefined, |
| | 471 | ) { |
| | 472 | this.name = name; |
| | 473 | this.#dispatches = [dispatch]; |
| | 474 | } |
| | 475 | |
| | 476 | #log(level: Message["level"], args: unknown[]) { |
| | 477 | let frames; |
| | 478 | if (!withinStackCapture) { |
| | 479 | withinStackCapture = true; |
| | 480 | frames = stack.capture().slice(2); |
| | 481 | withinStackCapture = false; |
| | 482 | } |
| | 483 | const m = { |
| | 484 | level, |
| | 485 | scope: this.name, |
| | 486 | get text() { |
| | 487 | const value = formatLine(...args); |
| | 488 | Object.defineProperty(this, "text", { value }); |
| | 489 | return value; |
| | 490 | }, |
| | 491 | time: Date.now(), |
| | 492 | stack: frames, |
| | 493 | [originalLogArgs]: args, |
| | 494 | }; |
| | 495 | if (withinDispatch) return void globalOutputFunction(m); |
| | 496 | withinDispatch = true; |
| | 497 | this.#dispatch(m); |
| | 498 | withinDispatch = false; |
| | 499 | } |
| | 500 | |
| | 501 | info: (...args: unknown[]) => void = (...args: unknown[]) => { |
| | 502 | this.#log("info", args); |
| | 503 | }; |
| | 504 | warn: (...args: unknown[]) => void = (...args: unknown[]) => { |
| | 505 | this.#log("warn", args); |
| | 506 | }; |
| | 507 | error: (...args: unknown[]) => void = (...args: unknown[]) => { |
| | 508 | this.#log("error", args); |
| | 509 | }; |
| | 510 | log: (...args: unknown[]) => void = (...args: unknown[]) => { |
| | 511 | this.#log("debug", args); |
| | 512 | }; |
| | 513 | debug: (...args: unknown[]) => void = (...args: unknown[]) => { |
| | 514 | this.#log("debug", args); |
| | 515 | }; |
| | 516 | |
| | 517 | scoped(name: string): Scope { |
| | 518 | const current = this.name; |
| | 519 | return new Scope( |
| | 520 | this.#dispatch, |
| | 521 | name ? current ? `${current}/${name}` : name : current, |
| | 522 | ); |
| | 523 | } |
| | 524 | |
| | 525 | tee(destination: DispatchFunction) { |
| | 526 | this.#dispatches.push(destination); |
| | 527 | return ts.defer(() => |
| | 528 | this.#dispatches.splice(this.#dispatches.indexOf(destination), 1) |
| | 529 | ); |
| | 530 | } |
| | 531 | }; |
| | 532 | |
| | 533 | const globalWidgetHost = /* @__PURE__ */ (() => { |
| | 534 | // TODO; write this in a tree-shakable manner |
| 126 | const { process } = node; | 535 | const { process } = node; |
| 127 | if (!process || !process.stderr.isTTY) { | 536 | if (!process || !process.stderr.isTTY) { |
| | 537 | // return a no-op |
| | 538 | let warned = false; |
| 128 | return { | 539 | return { |
| 129 | // TODO: allow passing a level here | 540 | writeLine: (line: string) => console.log(line), |
| 130 | writeLine: (line) => console["log"](line), | | |
| 131 | getDrawLock: () => ts.defer(() => {}), | 541 | getDrawLock: () => ts.defer(() => {}), |
| 132 | startWidget: (w) => { | 542 | startWidget: (w: Widget) => { |
| | 543 | if (!warned) { |
| | 544 | console.warn( |
| | 545 | '"@clo/lib/log.ts"\'s startWidget was called in an environment ' + |
| | 546 | "that does not support the Node.js 'process' API. Widgets" + |
| | 547 | "will not be visible.", |
| | 548 | ); |
| | 549 | } |
| 133 | const close = w.onChange?.(() => {}); | 550 | const close = w.onChange?.(() => {}); |
| 134 | return ts.defer(close ?? (() => {})); | 551 | return ts.defer(close ?? (() => {})); |
| 135 | }, | 552 | }, |
| 136 | cancel: () => {}, | 553 | cancel: () => {}, |
| 137 | }; | 554 | }; |
| 138 | } | 555 | } |
| 139 | const widget = headless.widgetHost({ | 556 | const widget = headlessWidgetHost({ |
| 140 | // TODO:allow passing a level here to put debug, warn and errors on stderr | | |
| 141 | writeOutput: (string) => process.stdout.write(string), | 557 | writeOutput: (string) => process.stdout.write(string), |
| 142 | writeInteractive: (string) => process.stderr.write(string), | 558 | writeInteractive: (string) => process.stderr.write(string), |
| 143 | now: () => performance.now(), | 559 | now: () => performance.now(), |
| ... | @@ -149,13 +565,70 @@ const globalWidgetHost = /* @__PURE__ */ ((): headless.WidgetHost => { | ... | @@ -149,13 +565,70 @@ const globalWidgetHost = /* @__PURE__ */ ((): headless.WidgetHost => { |
| 149 | }); | 565 | }); |
| 150 | process.addListener("beforeExit", () => widget.cancel()); | 566 | process.addListener("beforeExit", () => widget.cancel()); |
| 151 | process.addListener("exit", () => widget.cancel()); | 567 | process.addListener("exit", () => widget.cancel()); |
| | 568 | |
| | 569 | // Make sure the default `console` will not interweave with widgets |
| | 570 | try { |
| | 571 | const console = globalThis.console as unknown as Record<string, Function>; |
| | 572 | for (const key of Object.keys(console)) { |
| | 573 | const fn = console[key]; |
| | 574 | if (typeof fn === "function") { |
| | 575 | console[key] = function (...args: unknown[]) { |
| | 576 | using _ = getDrawLock(); |
| | 577 | fn.apply(this, args); |
| | 578 | }; |
| | 579 | } |
| | 580 | } |
| | 581 | } catch {} |
| | 582 | |
| 152 | return widget; | 583 | return widget; |
| 153 | })(); | 584 | })(); |
| 154 | const globalLog = /* @__PURE__ */ headless.logger({ | | |
| 155 | log: globalWidgetHost, | | |
| 156 | colors: node.process?.stderr.isTTY ?? false, | | |
| 157 | }); | | |
| 158 | | 585 | |
| | 586 | const levelToAnsi: Record<Message["level"], string> = { |
| | 587 | info: `${ansi.fgBlue}info`, |
| | 588 | warn: `${ansi.fgYellow}warn`, |
| | 589 | error: `${ansi.fgRed}error`, |
| | 590 | debug: `${ansi.dim}dbg`, |
| | 591 | }; |
| | 592 | |
| | 593 | let globalOutputFunction!: DispatchFunction; |
| | 594 | const globalLog = /* @__PURE__ */ (() => { |
| | 595 | const colors = node.process?.stderr.isTTY ?? false; |
| | 596 | globalOutputFunction = node.process |
| | 597 | // In Node.js, coordinate with the widget host |
| | 598 | ? ({ level, scope, text }) => { |
| | 599 | if (!text) return globalWidgetHost.writeLine(""); |
| | 600 | const prefix = colors |
| | 601 | // colorful |
| | 602 | ? `${levelToAnsi[level]}${ |
| | 603 | scope ? `(${scope})` : "" |
| | 604 | }${ansi.fgReset}${ansi.dim}:${ansi.reset} ` |
| | 605 | // colorless |
| | 606 | : scope |
| | 607 | ? `${level}(${scope}): ` |
| | 608 | : `${level}: `; |
| | 609 | globalWidgetHost.writeLine(prefix + text); |
| | 610 | } |
| | 611 | // Otherwise, forward to `console` |
| | 612 | : (m) => { |
| | 613 | let { level, [originalLogArgs]: args = [m.text], scope } = m; |
| | 614 | if (scope) { |
| | 615 | const arg0 = args[0]; |
| | 616 | const prefix = `[${scope}]`; |
| | 617 | if (typeof arg0 === "string") args[0] = `${prefix} ${arg0}`; |
| | 618 | else args.unshift(prefix); |
| | 619 | } |
| | 620 | console[level](...args); |
| | 621 | }; |
| | 622 | return new Scope(globalOutputFunction); |
| | 623 | })(); |
| | 624 | |
| | 625 | export interface DispatchFunction { |
| | 626 | (message: Message): void; |
| | 627 | } |
| | 628 | |
| | 629 | import * as ansi from "lib/string/ansi.ts"; |
| 159 | import * as node from "lib/node.ts"; | 630 | import * as node from "lib/node.ts"; |
| 160 | import * as headless from "lib/log/headless.ts"; | 631 | import * as stack from "lib/log/stack.ts"; |
| | 632 | import * as string from "lib/string.ts"; |
| 161 | import * as ts from "lib/ts.ts"; | 633 | import * as ts from "lib/ts.ts"; |
| | 634 | import { UNWRAP } from "lib/assert.ts"; |