Files
react-native/Libraries/Core/setUpDeveloperTools.js
T
Christoph Nakazawa db9fc38893 Log console.log invocations to the Metro terminal.
Summary:
People ask "How do you use `console.log` with React Native?" and there is no good answer. This diff aims to stop people from asking this question.

See https://fb.workplace.com/groups/rn.core/permalink/2372327062999018/ for context.

This logging relies on network requests which can cause logs to show up out-of-order. To reduce the likelihood I queue every log message on the server for a maximum of 200ms. There could be other methods, like using websocket, but that seems more complex than is necessary at least in the beginning.

I considered various throttling strategies because this could be quite chatty and possibly problematic, however I think we can just ship this and iterate based on feedback. On my very underpowered laptop I logged a random number every 10 milliseconds and it didn't cause any issues or slowdown.

Reviewed By: gaearon

Differential Revision: D15559151

fbshipit-source-id: 552001622af0937ae3a37d2bd8c1b96e7ca52020
2019-05-31 00:45:03 -07:00

39 lines
1.0 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
* @format
*/
'use strict';
/**
* Sets up developer tools for React Native.
* You can use this module directly, or just require InitializeCore.
*/
if (__DEV__) {
if (!global.__RCTProfileIsProfiling) {
// not when debugging in chrome
// TODO(t12832058) This check is broken
if (!window.document) {
require('./Devtools/setupDevtools');
}
// Set up inspector
const JSInspector = require('../JSInspector/JSInspector');
JSInspector.registerAgent(require('../JSInspector/NetworkAgent'));
}
const logToConsole = require('./Devtools/logToConsole');
['log', 'warn', 'info', 'trace'].forEach(level => {
const originalFunction = console[level];
// $FlowFixMe Overwrite console methods
console[level] = function(...args) {
logToConsole(level, args);
originalFunction.apply(console, args);
};
});
}