0% found this document useful (0 votes)
10 views2 pages

Dev Tools Console Patching

This document describes code for patching console logging functions to disable them during side effect free replaying. It stores the original console functions, defines a disabled log function, and uses it to override the console functions when disabling logs. When re-enabling logs it restores the original functions.

Uploaded by

mahoraga
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views2 pages

Dev Tools Console Patching

This document describes code for patching console logging functions to disable them during side effect free replaying. It stores the original console functions, defines a disabled log function, and uses it to override the console functions when disabling logs. When re-enabling logs it restores the original functions.

Uploaded by

mahoraga
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

/**

* Copyright (c) Meta Platforms, Inc. and affiliates.


*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/

// This is a DevTools fork of shared/ConsolePatchingDev.


// The shared console patching code is DEV-only.
// We can't use it since DevTools only ships production builds.

// Helpers to patch [Link] 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 (disabledDepth === 0) {
/* eslint-disable react-internal/no-production-logging */
prevLog = [Link];
prevInfo = [Link];
prevWarn = [Link];
prevError = [Link];
prevGroup = [Link];
prevGroupCollapsed = [Link];
prevGroupEnd = [Link];
// [Link]
const props = {
configurable: true,
enumerable: true,
value: disabledLog,
writable: true,
};
// $FlowFixMe[cannot-write] Flow thinks console is immutable.
[Link](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 {


disabledDepth--;
if (disabledDepth === 0) {
/* eslint-disable react-internal/no-production-logging */
const props = {
configurable: true,
enumerable: true,
writable: true,
};
// $FlowFixMe[cannot-write] Flow thinks console is immutable.
[Link](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) {
[Link](
'disabledDepth fell below zero. ' +
'This is a bug in React. Please file an issue.',
);
}
}

You might also like