|
1 | | -import RNFS from 'react-native-fs'; |
2 | | - |
3 | | -const LOG_FILE_NAME = 'download-debug.log'; |
4 | | -const MAX_LOG_FILE_BYTES = 2 * 1024 * 1024; |
5 | | -const RETAINED_LOG_LINES = 4000; |
6 | | - |
7 | | -let writeQueue = Promise.resolve(); |
8 | | - |
9 | | -function getLogFilePath(): string { |
10 | | - return `${RNFS.DocumentDirectoryPath}/${LOG_FILE_NAME}`; |
11 | | -} |
12 | | - |
13 | | -function formatArg(arg: unknown): string { |
14 | | - if (arg instanceof Error) { |
15 | | - return `${arg.name}: ${arg.message}${arg.stack ? `\n${arg.stack}` : ''}`; |
16 | | - } |
17 | | - if (typeof arg === 'string') return arg; |
18 | | - if (typeof arg === 'number' || typeof arg === 'boolean' || arg == null) return String(arg); |
19 | | - try { |
20 | | - return JSON.stringify(arg); |
21 | | - } catch { |
22 | | - return String(arg); |
23 | | - } |
24 | | -} |
25 | | - |
26 | | -function appendPersistentLog(level: 'log' | 'warn' | 'error', args: unknown[]): void { |
27 | | - const timestamp = new Date().toISOString(); |
28 | | - const line = `[${timestamp}] ${level.toUpperCase()}: ${args.map(formatArg).join(' ')}\n`; |
29 | | - |
30 | | - writeQueue = writeQueue.then(async () => { |
31 | | - try { |
32 | | - const path = getLogFilePath(); |
33 | | - if (await RNFS.exists(path)) { |
34 | | - await RNFS.appendFile(path, line, 'utf8'); |
35 | | - } else { |
36 | | - await RNFS.writeFile(path, line, 'utf8'); |
37 | | - } |
38 | | - |
39 | | - const stat = await RNFS.stat(path); |
40 | | - const size = typeof stat.size === 'string' ? Number.parseInt(stat.size, 10) : stat.size; |
41 | | - if (size > MAX_LOG_FILE_BYTES) { |
42 | | - const content = await RNFS.readFile(path, 'utf8'); |
43 | | - const trimmed = content.split('\n').filter(Boolean).slice(-RETAINED_LOG_LINES).join('\n'); |
44 | | - await RNFS.writeFile(path, trimmed ? `${trimmed}\n` : '', 'utf8'); |
45 | | - } |
46 | | - } catch { |
47 | | - // Logging must never break app execution. |
48 | | - } |
49 | | - }); |
50 | | -} |
51 | | - |
52 | | -function capture(level: 'log' | 'warn' | 'error', args: unknown[]): void { |
53 | | - appendPersistentLog(level, args); |
54 | | -} |
55 | | - |
| 1 | +/** |
| 2 | + * Persistent file logging is disabled. Versions 0.0.88+ wrote every logger.* |
| 3 | + * call to download-debug.log on the JS thread (a synchronous string build plus a |
| 4 | + * react-native-fs write per call), which contended with the bridge and |
| 5 | + * contributed to UI stalls/ANRs. logger.* is now a no-op in release builds; in |
| 6 | + * dev it still mirrors to the console. |
| 7 | + */ |
56 | 8 | const logger = { |
57 | 9 | log: (...args: unknown[]): void => { |
58 | | - capture('log', args); |
59 | 10 | if (__DEV__) console.log(...args); // NOSONAR |
60 | 11 | }, |
61 | 12 | warn: (...args: unknown[]): void => { |
62 | | - capture('warn', args); |
63 | 13 | if (__DEV__) console.warn(...args); // NOSONAR |
64 | 14 | }, |
65 | 15 | error: (...args: unknown[]): void => { |
66 | | - capture('error', args); |
67 | 16 | if (__DEV__) console.error(...args); // NOSONAR |
68 | 17 | }, |
69 | | - getLogFilePath, |
70 | 18 | }; |
71 | 19 |
|
72 | 20 | export default logger; |
0 commit comments