| 1 | /** |
| 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | * |
| 4 | * This source code is licensed under the MIT license found in the |
| 5 | * LICENSE file in the root directory of this source tree. |
| 6 | * |
| 7 | * @flow |
| 8 | */ |
| 9 | |
| 10 | // This is a DevTools fork of shared/ConsolePatchingDev. |
| 11 | // The shared console patching code is DEV-only. |
| 12 | // We can't use it since DevTools only ships production builds. |
| 13 | |
| 14 | // Helpers to patch console.logs to avoid logging during side-effect free |
| 15 | // replaying on render function. This currently only patches the object |
| 16 | // lazily which won't cover if the log function was extracted eagerly. |
| 17 | // We could also eagerly patch the method. |
| 18 | |
| 19 | let disabledDepth = 0; |
| 20 | let prevLog; |
| 21 | let prevInfo; |
| 22 | let prevWarn; |
| 23 | let prevError; |
| 24 | let prevGroup; |
| 25 | let prevGroupCollapsed; |
| 26 | let prevGroupEnd; |
| 27 | |
| 28 | function disabledLog() {} |
| 29 | disabledLog.__reactDisabledLog = true; |
| 30 | |
| 31 | export function disableLogs(): void { |
| 32 | if (disabledDepth === 0) { |
| 33 | prevLog = console.log; |
| 34 | prevInfo = console.info; |
| 35 | prevWarn = console.warn; |
| 36 | prevError = console.error; |
| 37 | prevGroup = console.group; |
| 38 | prevGroupCollapsed = console.groupCollapsed; |
| 39 | prevGroupEnd = console.groupEnd; |
| 40 | // https://github.com/facebook/react/issues/19099 |
| 41 | const props = { |
| 42 | configurable: true, |
| 43 | enumerable: true, |
| 44 | value: disabledLog, |
| 45 | writable: true, |
| 46 | }; |
| 47 | // $FlowFixMe[cannot-write] Flow thinks console is immutable. |
| 48 | Object.defineProperties(console, { |
| 49 | info: props, |
| 50 | log: props, |
| 51 | warn: props, |
| 52 | error: props, |
| 53 | group: props, |
| 54 | groupCollapsed: props, |
| 55 | groupEnd: props, |
| 56 | }); |
| 57 | /* eslint-enable react-internal/no-production-logging */ |
| 58 | } |
| 59 | disabledDepth++; |
| 60 | } |
| 61 | |
| 62 | export function reenableLogs(): void { |
| 63 | disabledDepth--; |
| 64 | if (disabledDepth === 0) { |
| 65 | const props = { |
| 66 | configurable: true, |
| 67 | enumerable: true, |
| 68 | writable: true, |
| 69 | }; |
| 70 | // $FlowFixMe[cannot-write] Flow thinks console is immutable. |
| 71 | Object.defineProperties(console, { |
| 72 | log: {...props, value: prevLog}, |
| 73 | info: {...props, value: prevInfo}, |
| 74 | warn: {...props, value: prevWarn}, |
| 75 | error: {...props, value: prevError}, |
| 76 | group: {...props, value: prevGroup}, |
| 77 | groupCollapsed: {...props, value: prevGroupCollapsed}, |
| 78 | groupEnd: {...props, value: prevGroupEnd}, |
| 79 | }); |
| 80 | /* eslint-enable react-internal/no-production-logging */ |
| 81 | } |
| 82 | if (disabledDepth < 0) { |
| 83 | console.error( |
| 84 | 'disabledDepth fell below zero. ' + |
| 85 | 'This is a bug in React. Please file an issue.', |
| 86 | ); |
| 87 | } |
| 88 | } |