Files
react/packages/react-devtools-shared/src/Logger.js
T
Mengdi Chen 3ddbedd052 [devtools] log more events + metadata (#24951)
* [devtools] log more events + metadata

* better typing

* consistent string type
2022-07-19 15:22:09 -04:00

71 lines
1.9 KiB
JavaScript

/**
* 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 strict-local
*/
import {enableLogger} from 'react-devtools-feature-flags';
export type LogEvent =
| {|
+event_name: 'loaded-dev-tools',
|}
| {|
+event_name: 'error',
+error_message: string | null,
+error_stack: string | null,
+error_component_stack: string | null,
|}
| {|
+event_name: 'selected-components-tab',
|}
| {|
+event_name: 'selected-profiler-tab',
|}
| {|
+event_name: 'load-hook-names',
+event_status: 'success' | 'error' | 'timeout' | 'unknown',
+duration_ms: number,
+inspected_element_display_name: string | null,
+inspected_element_number_of_hooks: number | null,
|}
| {|
+event_name: 'select-element',
|}
| {|
+event_name: 'inspect-element-button-clicked',
|}
| {|
+event_name: 'profiling-start',
|};
export type LogFunction = (LogEvent, ?Object) => void | Promise<void>;
let logFunctions: Array<LogFunction> = [];
export const logEvent: LogFunction =
enableLogger === true
? function logEvent(event: LogEvent, metadata: ?Object): void {
logFunctions.forEach(log => {
log(event, metadata);
});
}
: function logEvent() {};
export const registerEventLogger =
enableLogger === true
? function registerEventLogger(logFunction: LogFunction): () => void {
if (enableLogger) {
logFunctions.push(logFunction);
return function unregisterEventLogger() {
logFunctions = logFunctions.filter(log => log !== logFunction);
};
}
return () => {};
}
: function registerEventLogger(logFunction: LogFunction) {
return () => {};
};