Skip to content

Commit 450044a

Browse files
committed
Add toggle to unpatch global console
1 parent 818c480 commit 450044a

File tree

12 files changed

+383
-353
lines changed

12 files changed

+383
-353
lines changed

fixtures/devtools/standalone/index.html

Lines changed: 280 additions & 276 deletions
Large diffs are not rendered by default.

packages/react-devtools-core/src/standalone.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import Bridge from 'react-devtools-shared/src/bridge';
1818
import Store from 'react-devtools-shared/src/devtools/store';
1919
import {
2020
getAppendComponentStack,
21-
getEnableDoubleLogging,
21+
getsuppressDoubleLogging,
2222
getBreakOnConsoleErrors,
2323
getSavedComponentFilters,
2424
} from 'react-devtools-shared/src/utils';
@@ -299,8 +299,8 @@ function startServer(
299299
window.__REACT_DEVTOOLS_APPEND_COMPONENT_STACK__ = ${JSON.stringify(
300300
getAppendComponentStack(),
301301
)};
302-
window.__REACT_DEVTOOLS_ENABLE_DOUBLE_LOGGING__ = ${JSON.stringify(
303-
getEnableDoubleLogging(),
302+
window.__REACT_DEVTOOLS_SUPPRESS_DOUBLE_LOGGING__ = ${JSON.stringify(
303+
getsuppressDoubleLogging(),
304304
)};
305305
window.__REACT_DEVTOOLS_BREAK_ON_CONSOLE_ERRORS__ = ${JSON.stringify(
306306
getBreakOnConsoleErrors(),

packages/react-devtools-extensions/src/main.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import Store from 'react-devtools-shared/src/devtools/store';
77
import {getBrowserName, getBrowserTheme} from './utils';
88
import {LOCAL_STORAGE_TRACE_UPDATES_ENABLED_KEY} from 'react-devtools-shared/src/constants';
99
import {
10-
getEnableDoubleLogging,
10+
getsuppressDoubleLogging,
1111
getAppendComponentStack,
1212
getBreakOnConsoleErrors,
1313
getSavedComponentFilters,
@@ -31,15 +31,15 @@ let panelCreated = false;
3131
// Instead it relies on the extension to pass filters through.
3232
function syncSavedPreferences() {
3333
const appendComponentStack = getAppendComponentStack();
34-
const enableDoubleLogging = getEnableDoubleLogging();
34+
const suppressDoubleLogging = getsuppressDoubleLogging();
3535
const breakOnConsoleErrors = getBreakOnConsoleErrors();
3636
const componentFilters = getSavedComponentFilters();
3737
chrome.devtools.inspectedWindow.eval(
3838
`window.__REACT_DEVTOOLS_APPEND_COMPONENT_STACK__ = ${JSON.stringify(
3939
appendComponentStack,
4040
)};
41-
window.__REACT_DEVTOOLS_ENABLE_DOUBLE_LOGGING__ = ${JSON.stringify(
42-
enableDoubleLogging,
41+
window.__REACT_DEVTOOLS_SUPPRESS_DOUBLE_LOGGING__ = ${JSON.stringify(
42+
suppressDoubleLogging,
4343
)};
4444
window.__REACT_DEVTOOLS_BREAK_ON_CONSOLE_ERRORS__ = ${JSON.stringify(
4545
breakOnConsoleErrors,

packages/react-devtools-inline/src/backend.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@ function startActivation(contentWindow: window) {
2222

2323
const {
2424
appendComponentStack,
25-
enableDoubleLogging,
25+
suppressDoubleLogging,
2626
breakOnConsoleErrors,
2727
componentFilters,
2828
} = data;
2929

3030
contentWindow.__REACT_DEVTOOLS_APPEND_COMPONENT_STACK__ = appendComponentStack;
3131
contentWindow.__REACT_DEVTOOLS_BREAK_ON_CONSOLE_ERRORS__ = breakOnConsoleErrors;
32-
contentWindow.__REACT_DEVTOOLS_ENABLE_DOUBLE_LOGGING__ = enableDoubleLogging;
32+
contentWindow.__REACT_DEVTOOLS_SUPPRESS_DOUBLE_LOGGING__ = suppressDoubleLogging;
3333
contentWindow.__REACT_DEVTOOLS_COMPONENT_FILTERS__ = componentFilters;
3434

3535
// TRICKY
@@ -41,7 +41,7 @@ function startActivation(contentWindow: window) {
4141
if (contentWindow !== window) {
4242
window.__REACT_DEVTOOLS_APPEND_COMPONENT_STACK__ = appendComponentStack;
4343
window.__REACT_DEVTOOLS_BREAK_ON_CONSOLE_ERRORS__ = breakOnConsoleErrors;
44-
window.__REACT_DEVTOOLS_ENABLE_DOUBLE_LOGGING__ = enableDoubleLogging;
44+
window.__REACT_DEVTOOLS_SUPPRESS_DOUBLE_LOGGING__ = suppressDoubleLogging;
4545
window.__REACT_DEVTOOLS_COMPONENT_FILTERS__ = componentFilters;
4646
}
4747

packages/react-devtools-inline/src/frontend.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
getAppendComponentStack,
1010
getBreakOnConsoleErrors,
1111
getSavedComponentFilters,
12-
getEnableDoubleLogging,
12+
getsuppressDoubleLogging,
1313
} from 'react-devtools-shared/src/utils';
1414
import {
1515
MESSAGE_TYPE_GET_SAVED_PREFERENCES,
@@ -40,7 +40,7 @@ export function initialize(
4040
{
4141
type: MESSAGE_TYPE_SAVED_PREFERENCES,
4242
appendComponentStack: getAppendComponentStack(),
43-
enableDoubleLogging: getEnableDoubleLogging(),
43+
suppressDoubleLogging: getsuppressDoubleLogging(),
4444
breakOnConsoleErrors: getBreakOnConsoleErrors(),
4545
componentFilters: getSavedComponentFilters(),
4646
},

packages/react-devtools-shared/src/backend/agent.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -446,11 +446,11 @@ export default class Agent extends EventEmitter<{|
446446
updateConsolePatchSettings = ({
447447
appendComponentStack,
448448
breakOnConsoleErrors,
449-
enableDoubleLogging,
449+
suppressDoubleLogging,
450450
}: {|
451451
appendComponentStack: boolean,
452452
breakOnConsoleErrors: boolean,
453-
enableDoubleLogging: boolean,
453+
suppressDoubleLogging: boolean,
454454
|}) => {
455455
// If the frontend preference has change,
456456
// or in the case of React Native- if the backend is just finding out the preference-

packages/react-devtools-shared/src/bridge.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ type NativeStyleEditor_SetValueParams = {|
8888
type UpdateConsolePatchSettingsParams = {|
8989
appendComponentStack: boolean,
9090
breakOnConsoleErrors: boolean,
91-
enableDoubleLogging: boolean,
91+
suppressDoubleLogging: boolean,
9292
|};
9393

9494
type BackendEvents = {|

packages/react-devtools-shared/src/constants.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export const LOCAL_STORAGE_SHOULD_BREAK_ON_CONSOLE_ERRORS =
3131
'React::DevTools::breakOnConsoleErrors';
3232

3333
export const LOCAL_STORAGE_SHOULD_ENABLE_DOUBLE_LOGGING =
34-
'React::DevTools::enableDoubleLogging';
34+
'React::DevTools::suppressDoubleLogging';
3535

3636
export const LOCAL_STORAGE_SHOULD_PATCH_CONSOLE_KEY =
3737
'React::DevTools::appendComponentStack';

packages/react-devtools-shared/src/devtools/views/Settings/DebuggingSettings.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ export default function DebuggingSettings(_: {||}) {
1717
const {
1818
appendComponentStack,
1919
breakOnConsoleErrors,
20-
enableDoubleLogging,
21-
setEnableDoubleLogging,
20+
suppressDoubleLogging,
21+
setsuppressDoubleLogging,
2222
setAppendComponentStack,
2323
setBreakOnConsoleErrors,
2424
} = useContext(SettingsContext);
@@ -41,12 +41,12 @@ export default function DebuggingSettings(_: {||}) {
4141
<label>
4242
<input
4343
type="checkbox"
44-
checked={enableDoubleLogging}
44+
checked={!suppressDoubleLogging}
4545
onChange={({currentTarget}) =>
46-
setEnableDoubleLogging(currentTarget.checked)
46+
setsuppressDoubleLogging(!currentTarget.checked)
4747
}
4848
/>{' '}
49-
Enable double logging
49+
Suppress console during development-only second render pass
5050
</label>
5151
</div>
5252
<div className={styles.Setting}>

packages/react-devtools-shared/src/devtools/views/Settings/SettingsContext.js

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ type Context = {|
3939
// Specified as a separate prop so it can trigger a re-render of FixedSizeList.
4040
lineHeight: number,
4141

42-
enableDoubleLogging: boolean,
43-
setEnableDoubleLogging: (value: boolean) => void,
42+
suppressDoubleLogging: boolean,
43+
setsuppressDoubleLogging: (value: boolean) => void,
4444

4545
appendComponentStack: boolean,
4646
setAppendComponentStack: (value: boolean) => void,
@@ -85,8 +85,8 @@ function SettingsContextController({
8585
);
8686

8787
const [
88-
enableDoubleLogging,
89-
setEnableDoubleLogging,
88+
suppressDoubleLogging,
89+
setsuppressDoubleLogging,
9090
] = useLocalStorage<boolean>(
9191
LOCAL_STORAGE_SHOULD_ENABLE_DOUBLE_LOGGING,
9292
false,
@@ -160,9 +160,14 @@ function SettingsContextController({
160160
bridge.send('updateConsolePatchSettings', {
161161
appendComponentStack,
162162
breakOnConsoleErrors,
163-
enableDoubleLogging,
163+
suppressDoubleLogging,
164164
});
165-
}, [bridge, appendComponentStack, breakOnConsoleErrors, enableDoubleLogging]);
165+
}, [
166+
bridge,
167+
appendComponentStack,
168+
breakOnConsoleErrors,
169+
suppressDoubleLogging,
170+
]);
166171

167172
useEffect(() => {
168173
bridge.send('setTraceUpdatesEnabled', traceUpdatesEnabled);
@@ -172,15 +177,15 @@ function SettingsContextController({
172177
() => ({
173178
appendComponentStack,
174179
breakOnConsoleErrors,
175-
enableDoubleLogging,
180+
suppressDoubleLogging,
176181
displayDensity,
177182
lineHeight:
178183
displayDensity === 'compact'
179184
? COMPACT_LINE_HEIGHT
180185
: COMFORTABLE_LINE_HEIGHT,
181186
setAppendComponentStack,
182187
setBreakOnConsoleErrors,
183-
setEnableDoubleLogging,
188+
setsuppressDoubleLogging,
184189
setDisplayDensity,
185190
setTheme,
186191
setTraceUpdatesEnabled,
@@ -191,10 +196,10 @@ function SettingsContextController({
191196
appendComponentStack,
192197
breakOnConsoleErrors,
193198
displayDensity,
194-
enableDoubleLogging,
199+
suppressDoubleLogging,
195200
setAppendComponentStack,
196201
setBreakOnConsoleErrors,
197-
setEnableDoubleLogging,
202+
setsuppressDoubleLogging,
198203
setDisplayDensity,
199204
setTheme,
200205
setTraceUpdatesEnabled,

packages/react-devtools-shared/src/utils.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,14 +262,14 @@ export function getBreakOnConsoleErrors(): boolean {
262262
return false;
263263
}
264264

265-
export function setEnableDoubleLogging(value: boolean): void {
265+
export function setsuppressDoubleLogging(value: boolean): void {
266266
localStorageSetItem(
267267
LOCAL_STORAGE_SHOULD_ENABLE_DOUBLE_LOGGING,
268268
JSON.stringify(value),
269269
);
270270
}
271271

272-
export function getEnableDoubleLogging(): boolean {
272+
export function getsuppressDoubleLogging(): boolean {
273273
try {
274274
const raw = localStorageGetItem(LOCAL_STORAGE_SHOULD_ENABLE_DOUBLE_LOGGING);
275275
if (raw != null) {

packages/shared/ConsolePatchingDev.js

Lines changed: 65 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
// replaying on render function. This currently only patches the object
1212
// lazily which won't cover if the log function was extracted eagerly.
1313
// We could also eagerly patch the method.
14+
import {canUseDOM} from './ExecutionEnvironment';
1415

1516
let disabledDepth = 0;
1617
let prevLog;
@@ -20,65 +21,85 @@ let prevError;
2021
let prevGroup;
2122
let prevGroupCollapsed;
2223
let prevGroupEnd;
24+
const {__REACT_DEVTOOLS_SUPPRESS_DOUBLE_LOGGING__: suppressLog} = canUseDOM
25+
? window
26+
: global;
2327

2428
function disabledLog() {}
2529
disabledLog.__reactDisabledLog = true;
2630

31+
function patchConsole() {
32+
/* eslint-disable react-internal/no-production-logging */
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 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-disable react-internal/no-production-logging */
58+
}
59+
60+
function unPatchConsole() {
61+
/* eslint-disable react-internal/no-production-logging */
62+
const props = {
63+
configurable: true,
64+
enumerable: true,
65+
writable: true,
66+
};
67+
// $FlowFixMe Flow thinks console is immutable.
68+
Object.defineProperties(console, {
69+
log: {...props, value: prevLog},
70+
info: {...props, value: prevInfo},
71+
warn: {...props, value: prevWarn},
72+
error: {...props, value: prevError},
73+
group: {...props, value: prevGroup},
74+
groupCollapsed: {...props, value: prevGroupCollapsed},
75+
groupEnd: {...props, value: prevGroupEnd},
76+
});
77+
/* eslint-enable react-internal/no-production-logging */
78+
}
79+
80+
let suppressDoubleLogging = suppressLog;
2781
export function disableLogs(): void {
2882
if (__DEV__) {
2983
if (disabledDepth === 0) {
30-
/* eslint-disable react-internal/no-production-logging */
31-
prevLog = console.log;
32-
prevInfo = console.info;
33-
prevWarn = console.warn;
34-
prevError = console.error;
35-
prevGroup = console.group;
36-
prevGroupCollapsed = console.groupCollapsed;
37-
prevGroupEnd = console.groupEnd;
38-
// https://github.com/facebook/react/issues/19099
39-
const props = {
40-
configurable: true,
41-
enumerable: true,
42-
value: disabledLog,
43-
writable: true,
44-
};
45-
// $FlowFixMe Flow thinks console is immutable.
46-
Object.defineProperties(console, {
47-
info: props,
48-
log: props,
49-
warn: props,
50-
error: props,
51-
group: props,
52-
groupCollapsed: props,
53-
groupEnd: props,
54-
});
55-
/* eslint-enable react-internal/no-production-logging */
84+
if (!suppressDoubleLogging) {
85+
patchConsole();
86+
}
5687
}
57-
disabledDepth++;
5888
}
89+
disabledDepth++;
5990
}
6091

6192
export function reenableLogs(): void {
6293
if (__DEV__) {
6394
disabledDepth--;
6495
if (disabledDepth === 0) {
65-
/* eslint-disable react-internal/no-production-logging */
66-
const props = {
67-
configurable: true,
68-
enumerable: true,
69-
writable: true,
70-
};
71-
// $FlowFixMe Flow thinks console is immutable.
72-
Object.defineProperties(console, {
73-
log: {...props, value: prevLog},
74-
info: {...props, value: prevInfo},
75-
warn: {...props, value: prevWarn},
76-
error: {...props, value: prevError},
77-
group: {...props, value: prevGroup},
78-
groupCollapsed: {...props, value: prevGroupCollapsed},
79-
groupEnd: {...props, value: prevGroupEnd},
80-
});
81-
/* eslint-enable react-internal/no-production-logging */
96+
try {
97+
if (!suppressDoubleLogging) {
98+
unPatchConsole();
99+
}
100+
} finally {
101+
suppressDoubleLogging = suppressLog;
102+
}
82103
}
83104
if (disabledDepth < 0) {
84105
console.error(

0 commit comments

Comments
 (0)