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