github.com/tilt-dev/tilt@v0.33.15-0.20240515162809-0a22ed45d8a0/web/src/testlogs.tsx (about)

     1  import LogStore from "./LogStore"
     2  
     3  export type Line = string | { text: string; fields?: any }
     4  
     5  function now() {
     6    return new Date().toString()
     7  }
     8  
     9  // Adds lines to a log store.
    10  // We accept lines expressed as var args or as an array.
    11  // (Expressing them as var args can hit call stack maximums if you're not careful).
    12  export function appendLines(
    13    logStore: LogStore,
    14    name: string,
    15    ...lineOrList: Line[] | Line[][]
    16  ) {
    17    appendLinesForManifestAndSpan(logStore, name, name, ...lineOrList)
    18  }
    19  
    20  // Adds lines to a log store.
    21  // We accept lines expressed as var args or as an array.
    22  // (Expressing them as var args can hit call stack maximums if you're not careful).
    23  export function appendLinesForManifestAndSpan(
    24    logStore: LogStore,
    25    manifestName: string,
    26    spanId: string,
    27    ...lineOrList: Line[] | Line[][]
    28  ) {
    29    let lines = lineOrList.flat()
    30    let fromCheckpoint = logStore.checkpoint
    31    let toCheckpoint = fromCheckpoint + lines.length
    32  
    33    let spans = {} as any
    34    spanId = spanId || "_"
    35    spans[spanId] = { manifestName: manifestName }
    36  
    37    let segments = []
    38    for (let line of lines) {
    39      let obj = { time: now(), spanId: spanId, text: "" } as any
    40      if (typeof line == "string") {
    41        obj.text = line
    42      } else {
    43        for (let key in line) {
    44          obj[key] = (line as any)[key]
    45        }
    46      }
    47      segments.push(obj)
    48    }
    49  
    50    logStore.append({ spans, segments, fromCheckpoint, toCheckpoint })
    51  }