Skip to content

Commit 42ac240

Browse files
rickhanloniifacebook-github-bot
authored andcommitted
Don't wrap console methods for metro logging in Chrome debugger (#26883)
Summary: Pull Request resolved: #26883 This diff fixes an issue reported in #26788 where logs in the Chrome console were showing a different location than previous versions. In the change here, we stop wrapping the console functions to attach the metro websocket in any environment that isn't a native app environment. We do this by checking `global.nativeLoggingHook` which is bound only by native apps and not environments like the Chrome DevTools. Changelog: [General][Fixed] - Fix wrong lines logging in Chrome debugger Reviewed By: cpojer, gaearon Differential Revision: D17951707 fbshipit-source-id: f045ea9abaa8aecc6afb8eca7db9842146a3d872
1 parent 50c59aa commit 42ac240

File tree

2 files changed

+60
-17
lines changed

2 files changed

+60
-17
lines changed

Libraries/Core/setUpDeveloperTools.js

+50-17
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212

1313
import Platform from '../Utilities/Platform';
1414

15+
declare var console: typeof console & {
16+
_isPolyfilled: boolean,
17+
};
18+
1519
/**
1620
* Sets up developer tools for React Native.
1721
* You can use this module directly, or just require InitializeCore.
@@ -60,25 +64,54 @@ if (__DEV__) {
6064
JSInspector.registerAgent(require('../JSInspector/NetworkAgent'));
6165
}
6266

67+
// Note we can't check if console is "native" because it would appear "native" in JSC and Hermes.
68+
// We also can't check any properties that don't exist in the Chrome worker environment.
69+
// So we check a navigator property that's set to a particular value ("Netscape") in all real browsers.
70+
const isLikelyARealBrowser =
71+
global.navigator != null &&
72+
/* _
73+
* | |
74+
* _ __ ___| |_ ___ ___ __ _ _ __ ___
75+
* | '_ \ / _ \ __/ __|/ __/ _` | '_ \ / _ \
76+
* | | | | __/ |_\__ \ (_| (_| | |_) | __/
77+
* |_| |_|\___|\__|___/\___\__,_| .__/ \___|
78+
* | |
79+
* |_|
80+
*/
81+
global.navigator.appName === 'Netscape'; // Any real browser
82+
6383
if (!Platform.isTesting) {
6484
const HMRClient = require('../Utilities/HMRClient');
65-
[
66-
'trace',
67-
'info',
68-
'warn',
69-
'log',
70-
'group',
71-
'groupCollapsed',
72-
'groupEnd',
73-
'debug',
74-
].forEach(level => {
75-
const originalFunction = console[level];
76-
// $FlowFixMe Overwrite console methods
77-
console[level] = function(...args) {
78-
HMRClient.log(level, args);
79-
originalFunction.apply(console, args);
80-
};
81-
});
85+
86+
if (console._isPolyfilled) {
87+
// We assume full control over the console and send JavaScript logs to Metro.
88+
[
89+
'trace',
90+
'info',
91+
'warn',
92+
'log',
93+
'group',
94+
'groupCollapsed',
95+
'groupEnd',
96+
'debug',
97+
].forEach(level => {
98+
const originalFunction = console[level];
99+
// $FlowFixMe Overwrite console methods
100+
console[level] = function(...args) {
101+
HMRClient.log(level, args);
102+
originalFunction.apply(console, args);
103+
};
104+
});
105+
} else {
106+
// We assume the environment has a real rich console (like Chrome), and don't hijack it to log to Metro.
107+
// It's likely the developer is using rich console to debug anyway, and hijacking it would
108+
// lose the filenames in console.log calls: https://github.com/facebook/react-native/issues/26788.
109+
HMRClient.log('log', [
110+
`JavaScript logs will appear in your ${
111+
isLikelyARealBrowser ? 'browser' : 'environment'
112+
} console`,
113+
]);
114+
}
82115
}
83116

84117
require('./setUpReactRefresh');

Libraries/polyfills/console.js

+10
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,11 @@ if (global.nativeLoggingHook) {
569569
assert: consoleAssertPolyfill,
570570
};
571571

572+
Object.defineProperty(console, '_isPolyfilled', {
573+
value: true,
574+
enumerable: false,
575+
});
576+
572577
// If available, also call the original `console` method since that is
573578
// sometimes useful. Ex: on OS X, this will let you see rich output in
574579
// the Safari Web Inspector console.
@@ -620,4 +625,9 @@ if (global.nativeLoggingHook) {
620625
debug: log,
621626
table: log,
622627
};
628+
629+
Object.defineProperty(console, '_isPolyfilled', {
630+
value: true,
631+
enumerable: false,
632+
});
623633
}

0 commit comments

Comments
 (0)