1/** 2 * Copyright (c) 650 Industries. 3 * Copyright (c) Meta Platforms, Inc. and affiliates. 4 * 5 * This source code is licensed under the MIT license found in the 6 * LICENSE file in the root directory of this source tree. 7 */ 8import { Platform } from 'react-native'; 9let LogBox; 10/** 11 * LogBox displays logs in the app. 12 */ 13if (__DEV__) { 14 const LogBoxData = require('./Data/LogBoxData'); 15 const { parseLogBoxLog, parseInterpolation } = require('./Data/parseLogBoxLog'); 16 let originalConsoleError; 17 let consoleErrorImpl; 18 let isLogBoxInstalled = false; 19 LogBox = { 20 install() { 21 if (isLogBoxInstalled) { 22 return; 23 } 24 isLogBoxInstalled = true; 25 // Trigger lazy initialization of module. 26 // require("../NativeModules/specs/NativeLogBox"); 27 // IMPORTANT: we only overwrite `console.error` and `console.warn` once. 28 // When we uninstall we keep the same reference and only change its 29 // internal implementation 30 const isFirstInstall = originalConsoleError == null; 31 if (isFirstInstall) { 32 originalConsoleError = console.error.bind(console); 33 console.error = (...args) => { 34 consoleErrorImpl?.(...args); 35 }; 36 } 37 consoleErrorImpl = registerError; 38 if (Platform.isTesting) { 39 LogBoxData.setDisabled(true); 40 } 41 }, 42 uninstall() { 43 if (!isLogBoxInstalled) { 44 return; 45 } 46 isLogBoxInstalled = false; 47 // IMPORTANT: we don't re-assign to `console` in case the method has been 48 // decorated again after installing LogBox. E.g.: 49 // Before uninstalling: original > LogBox > OtherErrorHandler 50 // After uninstalling: original > LogBox (noop) > OtherErrorHandler 51 consoleErrorImpl = originalConsoleError; 52 delete console.disableLogBox; 53 }, 54 isInstalled() { 55 return isLogBoxInstalled; 56 }, 57 ignoreLogs(patterns) { 58 LogBoxData.addIgnorePatterns(patterns); 59 }, 60 ignoreAllLogs(value) { 61 LogBoxData.setDisabled(value == null ? true : value); 62 }, 63 clearAllLogs() { 64 LogBoxData.clear(); 65 }, 66 addLog(log) { 67 if (isLogBoxInstalled) { 68 LogBoxData.addLog(log); 69 } 70 }, 71 addException(error) { 72 if (isLogBoxInstalled) { 73 LogBoxData.addException(error); 74 } 75 }, 76 }; 77 const isWarningModuleWarning = (...args) => { 78 return typeof args[0] === 'string' && args[0].startsWith('Warning: '); 79 }; 80 const registerError = (...args) => { 81 // Let errors within LogBox itself fall through. 82 if (LogBoxData.isLogBoxErrorMessage(args[0])) { 83 originalConsoleError?.(...args); 84 return; 85 } 86 try { 87 if (!isWarningModuleWarning(...args)) { 88 // Only show LogBox for the 'warning' module, otherwise pass through. 89 // By passing through, this will get picked up by the React console override, 90 // potentially adding the component stack. React then passes it back to the 91 // React Native ExceptionsManager, which reports it to LogBox as an error. 92 // 93 // The 'warning' module needs to be handled here because React internally calls 94 // `console.error('Warning: ')` with the component stack already included. 95 originalConsoleError?.(...args); 96 return; 97 } 98 const { category, message, componentStack } = parseLogBoxLog(args); 99 if (!LogBoxData.isMessageIgnored(message.content)) { 100 // Interpolate the message so they are formatted for adb and other CLIs. 101 // This is different than the message.content above because it includes component stacks. 102 const interpolated = parseInterpolation(args); 103 originalConsoleError?.(interpolated.message.content); 104 LogBoxData.addLog({ 105 // Always show the static rendering issues as full screen since they 106 // are too confusing otherwise. 107 level: /did not match\. Server:/.test(message.content) ? 'fatal' : 'error', 108 category, 109 message, 110 componentStack, 111 }); 112 } 113 } 114 catch (err) { 115 LogBoxData.reportUnexpectedLogBoxError(err); 116 } 117 }; 118} 119else { 120 LogBox = { 121 install() { }, 122 uninstall() { }, 123 isInstalled() { 124 return false; 125 }, 126 ignoreLogs(patterns) { }, 127 ignoreAllLogs(value) { }, 128 clearAllLogs() { }, 129 addLog(log) { }, 130 addException(ex) { }, 131 }; 132} 133export default LogBox; 134//# sourceMappingURL=LogBox.web.js.map