Skip to content

Commit 4ed05ca

Browse files
gaearonfacebook-github-bot
authored andcommitted
Send early logs to Metro too
Summary: If you `console.log` early enough in the initialization sequence, it won't get sent to Metro because `hmrClient` isn't initialized yet. I've added a rolling array to catch at most 100 of those, and send them after we initialize. Changelog: [General] [Fixed] - Early logs don't get dropped by Metro now Reviewed By: cpojer Differential Revision: D17952097 fbshipit-source-id: 964b4735a6a7c3ccd115f44151139d718bf5b26d
1 parent 0d1b9b5 commit 4ed05ca

File tree

1 file changed

+45
-26
lines changed

1 file changed

+45
-26
lines changed

Libraries/Utilities/HMRClient.js

+45-26
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ let hmrClient = null;
2323
let hmrUnavailableReason: string | null = null;
2424
let currentCompileErrorMessage: string | null = null;
2525
let didConnect: boolean = false;
26+
let pendingLogs: Array<[LogLevel, Array<mixed>]> = [];
2627

2728
type LogLevel =
2829
| 'trace'
@@ -101,39 +102,46 @@ const HMRClient: HMRClientNativeInterface = {
101102
},
102103

103104
log(level: LogLevel, data: Array<mixed>) {
105+
if (!hmrClient) {
106+
// Catch a reasonable number of early logs
107+
// in case hmrClient gets initialized later.
108+
pendingLogs.push([level, data]);
109+
if (pendingLogs.length > 100) {
110+
pendingLogs.shift();
111+
}
112+
return;
113+
}
104114
try {
105-
if (hmrClient) {
106-
let message;
107-
if (global.Symbol) {
115+
let message;
116+
if (global.Symbol) {
117+
message = JSON.stringify({
118+
type: 'log',
119+
level,
120+
data: data.map(item =>
121+
typeof item === 'string'
122+
? item
123+
: require('pretty-format')(item, {
124+
escapeString: true,
125+
highlight: true,
126+
maxDepth: 3,
127+
min: true,
128+
plugins: [require('pretty-format').plugins.ReactElement],
129+
}),
130+
),
131+
});
132+
} else {
133+
try {
134+
message = JSON.stringify({type: 'log', level, data});
135+
} catch (error) {
108136
message = JSON.stringify({
109137
type: 'log',
110138
level,
111-
data: data.map(item =>
112-
typeof item === 'string'
113-
? item
114-
: require('pretty-format')(item, {
115-
escapeString: true,
116-
highlight: true,
117-
maxDepth: 3,
118-
min: true,
119-
plugins: [require('pretty-format').plugins.ReactElement],
120-
}),
121-
),
139+
data: [error.message],
122140
});
123-
} else {
124-
try {
125-
message = JSON.stringify({type: 'log', level, data});
126-
} catch (error) {
127-
message = JSON.stringify({
128-
type: 'log',
129-
level,
130-
data: [error.message],
131-
});
132-
}
133141
}
134-
135-
hmrClient.send(message);
136142
}
143+
144+
hmrClient.send(message);
137145
} catch (error) {
138146
// If sending logs causes any failures we want to silently ignore them
139147
// to ensure we do not cause infinite-logging loops.
@@ -244,6 +252,7 @@ Error: ${e.message}`;
244252
}
245253

246254
registerBundleEntryPoints(hmrClient);
255+
flushEarlyLogs(hmrClient);
247256
},
248257
};
249258

@@ -276,6 +285,16 @@ function registerBundleEntryPoints(client) {
276285
}
277286
}
278287

288+
function flushEarlyLogs(client) {
289+
try {
290+
pendingLogs.forEach(([level: LogLevel, data: Array<mixed>]) => {
291+
HMRClient.log(level, data);
292+
});
293+
} finally {
294+
pendingLogs.length = 0;
295+
}
296+
}
297+
279298
function dismissRedbox() {
280299
if (
281300
Platform.OS === 'ios' &&

0 commit comments

Comments
 (0)