-
Notifications
You must be signed in to change notification settings - Fork 47.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add experimental DebugTracing logger for internal use #18531
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
f25862a
Add feature flag: enableDebugTracing
0012141
Added debug tracing
e4bdfb3
Added formatting/colors to debug tracing
4bcfd36
Renamed DebugTrace test to be internal only
efb4470
Log changes based on feedback:
1e4d03d
Log additional state update and thennable
b4e0700
Re-use update.priority without recalculating it
866dbb4
Add DebugTraceMode to limit logging by subtree
a334555
Merged changes from .old to .new
916873e
Experimental gate DebugTracing test
dea13f1
Addressed PR feedback and tweaked a few things:
ae278a6
Disabled debug tracing feature flag by default; declared as variant
c3daacc
Renamed references to be consistently named (trace -> tracing)
76f26f6
Backed out conditional symbol export because it was breaking tests
c3a2c4b
Add more test gating to fix broken targets
f3678bc
Added redundant feature flag guards to exported functions to make sur…
b44b002
Added a few more feature flag guards and renamed DebugTracingMode exp…
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,226 @@ | ||
/** | ||
* 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 | ||
*/ | ||
|
||
import type {Wakeable} from 'shared/ReactTypes'; | ||
|
||
import {enableDebugTracing} from 'shared/ReactFeatureFlags'; | ||
|
||
const nativeConsole: Object = console; | ||
let nativeConsoleLog: null | Function = null; | ||
|
||
const pendingGroupArgs: Array<any> = []; | ||
let printedGroupIndex: number = -1; | ||
|
||
function group(...groupArgs): void { | ||
pendingGroupArgs.push(groupArgs); | ||
|
||
if (nativeConsoleLog === null) { | ||
nativeConsoleLog = nativeConsole.log; | ||
nativeConsole.log = log; | ||
} | ||
} | ||
|
||
function groupEnd(): void { | ||
pendingGroupArgs.pop(); | ||
while (printedGroupIndex >= pendingGroupArgs.length) { | ||
nativeConsole.groupEnd(); | ||
printedGroupIndex--; | ||
} | ||
|
||
if (pendingGroupArgs.length === 0) { | ||
nativeConsole.log = nativeConsoleLog; | ||
nativeConsoleLog = null; | ||
} | ||
} | ||
|
||
function log(...logArgs): void { | ||
if (printedGroupIndex < pendingGroupArgs.length - 1) { | ||
for (let i = printedGroupIndex + 1; i < pendingGroupArgs.length; i++) { | ||
const groupArgs = pendingGroupArgs[i]; | ||
nativeConsole.group(...groupArgs); | ||
} | ||
printedGroupIndex = pendingGroupArgs.length - 1; | ||
} | ||
if (typeof nativeConsoleLog === 'function') { | ||
nativeConsoleLog(...logArgs); | ||
} else { | ||
nativeConsole.log(...logArgs); | ||
} | ||
} | ||
|
||
const REACT_LOGO_STYLE = | ||
'background-color: #20232a; color: #61dafb; padding: 0 2px;'; | ||
|
||
export function logCommitStarted(priorityLabel: string): void { | ||
if (__DEV__) { | ||
if (enableDebugTracing) { | ||
group( | ||
`%c⚛️%c commit%c (priority: ${priorityLabel})`, | ||
REACT_LOGO_STYLE, | ||
'', | ||
'font-weight: normal;', | ||
); | ||
} | ||
} | ||
} | ||
|
||
export function logCommitStopped(): void { | ||
if (__DEV__) { | ||
if (enableDebugTracing) { | ||
groupEnd(); | ||
} | ||
} | ||
} | ||
|
||
const PossiblyWeakMap = typeof WeakMap === 'function' ? WeakMap : Map; | ||
// $FlowFixMe: Flow cannot handle polymorphic WeakMaps | ||
const wakeableIDs: WeakMap<Wakeable, number> = new PossiblyWeakMap(); | ||
let wakeableID: number = 0; | ||
function getWakeableID(wakeable: Wakeable): number { | ||
if (!wakeableIDs.has(wakeable)) { | ||
wakeableIDs.set(wakeable, wakeableID++); | ||
} | ||
return ((wakeableIDs.get(wakeable): any): number); | ||
} | ||
|
||
export function logComponentSuspended( | ||
componentName: string, | ||
wakeable: Wakeable, | ||
): void { | ||
if (__DEV__) { | ||
if (enableDebugTracing) { | ||
const id = getWakeableID(wakeable); | ||
const display = (wakeable: any).displayName || wakeable; | ||
log( | ||
`%c⚛️%c ${componentName} suspended`, | ||
REACT_LOGO_STYLE, | ||
'color: #80366d; font-weight: bold;', | ||
id, | ||
display, | ||
); | ||
wakeable.then( | ||
() => { | ||
log( | ||
`%c⚛️%c ${componentName} resolved`, | ||
REACT_LOGO_STYLE, | ||
'color: #80366d; font-weight: bold;', | ||
id, | ||
display, | ||
); | ||
}, | ||
() => { | ||
log( | ||
`%c⚛️%c ${componentName} rejected`, | ||
REACT_LOGO_STYLE, | ||
'color: #80366d; font-weight: bold;', | ||
id, | ||
display, | ||
); | ||
}, | ||
); | ||
} | ||
} | ||
} | ||
|
||
export function logLayoutEffectsStarted(priorityLabel: string): void { | ||
if (__DEV__) { | ||
if (enableDebugTracing) { | ||
group( | ||
`%c⚛️%c layout effects%c (priority: ${priorityLabel})`, | ||
REACT_LOGO_STYLE, | ||
'', | ||
'font-weight: normal;', | ||
); | ||
} | ||
} | ||
} | ||
|
||
export function logLayoutEffectsStopped(): void { | ||
if (__DEV__) { | ||
if (enableDebugTracing) { | ||
groupEnd(); | ||
} | ||
} | ||
} | ||
|
||
export function logPassiveEffectsStarted(priorityLabel: string): void { | ||
if (__DEV__) { | ||
if (enableDebugTracing) { | ||
group( | ||
`%c⚛️%c passive effects%c (priority: ${priorityLabel})`, | ||
REACT_LOGO_STYLE, | ||
'', | ||
'font-weight: normal;', | ||
); | ||
} | ||
} | ||
} | ||
|
||
export function logPassiveEffectsStopped(): void { | ||
if (__DEV__) { | ||
if (enableDebugTracing) { | ||
groupEnd(); | ||
} | ||
} | ||
} | ||
|
||
export function logRenderStarted(priorityLabel: string): void { | ||
if (__DEV__) { | ||
if (enableDebugTracing) { | ||
group( | ||
`%c⚛️%c render%c (priority: ${priorityLabel})`, | ||
REACT_LOGO_STYLE, | ||
'', | ||
'font-weight: normal;', | ||
); | ||
} | ||
} | ||
} | ||
|
||
export function logRenderStopped(): void { | ||
if (__DEV__) { | ||
if (enableDebugTracing) { | ||
groupEnd(); | ||
} | ||
} | ||
} | ||
|
||
export function logForceUpdateScheduled( | ||
componentName: string, | ||
priorityLabel: string, | ||
): void { | ||
if (__DEV__) { | ||
if (enableDebugTracing) { | ||
log( | ||
`%c⚛️%c ${componentName} forced update %c(priority: ${priorityLabel})`, | ||
REACT_LOGO_STYLE, | ||
'color: #db2e1f; font-weight: bold;', | ||
'', | ||
); | ||
} | ||
} | ||
} | ||
|
||
export function logStateUpdateScheduled( | ||
componentName: string, | ||
priorityLabel: string, | ||
payloadOrAction: any, | ||
): void { | ||
if (__DEV__) { | ||
if (enableDebugTracing) { | ||
log( | ||
`%c⚛️%c ${componentName} updated state %c(priority: ${priorityLabel})`, | ||
REACT_LOGO_STYLE, | ||
'color: #01a252; font-weight: bold;', | ||
'', | ||
payloadOrAction, | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does it mean that if I somehow end up with undefined
type
in a stable release, after this change it would start matchingREACT_DEBUG_TRACING_MODE_TYPE
(which would also beundefined
in stable), and thus I won't get to the catch-all default case that throws? This seems bad.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
REACT_DEBUG_TRACING_MODE_TYPE
isn't undefined in stable, justReact.DebugTracingMode
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(I backed out the conditional symbol export)