File size: 4,384 Bytes
e079a73
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import {LogBox} from "react-native"
import {configureReanimatedLogger, ReanimatedLogLevel} from "react-native-reanimated"

export interface LogEntry {
  timestamp: number
  level: "debug" | "info" | "warn" | "error"
  message: string
  source?: string
  metadata?: Record<string, unknown>
}

class LogRingBuffer {
  private logs: LogEntry[] = []
  private maxAgeMs = 10 * 60 * 1000
  private maxEntries = 10000
  private isIntercepting = false

  append(entry: Omit<LogEntry, "timestamp">) {
    this.logs.push({...entry, timestamp: Date.now()})
    this.prune()
  }

  getRecentLogs(): LogEntry[] {
    this.prune()
    return [...this.logs]
  }

  clear() {
    this.logs = []
  }

  getStats(): {count: number; oldestTimestamp: number | null; newestTimestamp: number | null} {
    this.prune()
    return {
      count: this.logs.length,
      oldestTimestamp: this.logs.length > 0 ? this.logs[0].timestamp : null,
      newestTimestamp: this.logs.length > 0 ? this.logs[this.logs.length - 1].timestamp : null,
    }
  }

  private prune() {
    const cutoff = Date.now() - this.maxAgeMs
    this.logs = this.logs.filter((l) => l.timestamp > cutoff)
    if (this.logs.length > this.maxEntries) {
      this.logs = this.logs.slice(-this.maxEntries)
    }
  }

  startConsoleInterception() {
    if (this.isIntercepting) return
    this.isIntercepting = true

    const ignoredLogs = [
      /Failed to open debugger. Please check that the dev server is running and reload the app./,
      /Require cycle:/,
      /is missing the required default export./,
      /Attempted to import the module/,
      /The action 'RESET' with payload/,
      /The action 'POP_TO_TOP' was not handled/,
      /socket-0 binding/,
      /socket-0 bound to/,
      /backTitleFontFamily prop is not available on Android/,
      /disableBackButtonMenu prop is not available on Android/,
      /backTitleVisible prop is not available on Android/,
      /topInsetEnabled prop is not available on Android/,
      /largeTitleHideShadow prop is not available on Android/,
      /largeTitleFontWeight prop is not available on Android/,
      /userInterfaceStyle prop is not available on Android/,
      /largeTitleFontFamily prop is not available on Android/,
      /Points are coincident/,
    ]

    LogBox.ignoreLogs(ignoredLogs)

    if (__DEV__) {
      const withoutIgnored =
        (logger: (...args: unknown[]) => void) =>
        (...args: unknown[]) => {
          const output = args.join(" ")
          if (!ignoredLogs.some((log) => log.test(output))) {
            logger(...args)
          }
        }

      console.log = withoutIgnored(console.log)
      console.info = withoutIgnored(console.info)
      console.warn = withoutIgnored(console.warn)
      console.error = withoutIgnored(console.error)
    }

    configureReanimatedLogger({
      level: ReanimatedLogLevel.warn,
      strict: false,
    })

    const originalConsole = {
      log: console.log,
      info: console.info,
      warn: console.warn,
      error: console.error,
      debug: console.debug,
    }

    const appendToBuffer = this.append.bind(this)

    const createInterceptor =
      (level: "debug" | "info" | "warn" | "error", originalFn: (...args: unknown[]) => void) =>
      (...args: unknown[]) => {
        originalFn.apply(console, args)
        appendToBuffer({
          level,
          message: args
            .map((a) => {
              if (a === null) return "null"
              if (a === undefined) return "undefined"
              if (typeof a === "object") {
                try {
                  return JSON.stringify(a)
                } catch {
                  return String(a)
                }
              }
              return String(a)
            })
            .join(" "),
          source: "console",
        })
      }

    console.log = createInterceptor("info", originalConsole.log)
    console.info = createInterceptor("info", originalConsole.info)
    console.warn = createInterceptor("warn", originalConsole.warn)
    console.error = createInterceptor("error", originalConsole.error)
    console.debug = createInterceptor("debug", originalConsole.debug)
  }
}

export const logBuffer = new LogRingBuffer()
export default logBuffer