Blame view

.pnpm-store/v11/projects/0e47ef389c0c12bdfaeb86af6e6a20bb/src/utils/print/printRunDiagnostics.ts 2.09 KB
383e20a3   杨鑫   拉取
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
  /**
   * 单次「点击打印」任务的时间线(仅 JS,不依赖改 AAR)。
   * Logcat 搜关键词: [print-diag]
   * 预览页打印中可点「Log」弹窗整段复制截图。
   */
  
  let t0 = 0
  const lines: string[] = []
  const MAX_LINES = 120
  
  export function startPrintRunDiagnostics (): void {
    t0 = Date.now()
    lines.length = 0
    printRunDiag('session_start')
  }
  
  export function printRunDiag (msg: string, extra?: Record<string, string | number | boolean | undefined>): void {
    const dt = Date.now() - t0
    let s = `[+${dt}ms] ${msg}`
    if (extra && Object.keys(extra).length) {
      s += ` | ${Object.entries(extra)
        .filter(([, v]) => v !== undefined && v !== '')
        .map(([k, v]) => `${k}=${String(v)}`)
        .join(' ')}`
    }
    lines.push(s)
    if (lines.length > MAX_LINES) {
      lines.splice(0, lines.length - MAX_LINES)
    }
    try {
      console.warn('[print-diag]', s)
    } catch (_) {}
  }
  
  export function getPrintRunDiagnosticsText (): string {
    return lines.join('\n')
  }
  
  /** 弹窗安全长度(避免部分机型 showModal 截断过长无提示) */
  export function getPrintRunDiagnosticsTextForModal (maxChars = 3800): string {
    const full = getPrintRunDiagnosticsText()
    if (full.length <= maxChars) return full
    return `...(start omitted, ${full.length - maxChars} chars)\n\n${full.slice(-maxChars)}`
  }
  
  const STORAGE_LAST_PRINT_DIAG = 'lastPrintRunDiagnostics'
  const STORAGE_LAST_PRINT_DIAG_AT = 'lastPrintRunDiagnosticsAt'
  
  /** 打印结束后写入 storage,Printer 页可查看最近一次任务时间线 */
  export function persistPrintRunDiagnostics (): void {
    try {
      uni.setStorageSync(STORAGE_LAST_PRINT_DIAG, getPrintRunDiagnosticsText())
      uni.setStorageSync(STORAGE_LAST_PRINT_DIAG_AT, String(Date.now()))
    } catch (_) {}
  }
  
  export function getPersistedPrintRunDiagnostics (): string {
    try {
      return String(uni.getStorageSync(STORAGE_LAST_PRINT_DIAG) || '')
    } catch (_) {
      return ''
    }
  }
  
  export function getPersistedPrintRunDiagnosticsAt (): number {
    try {
      return Number(uni.getStorageSync(STORAGE_LAST_PRINT_DIAG_AT) || 0) || 0
    } catch (_) {
      return 0
    }
  }