LogBox.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /**
  2. * Copyright (c) Facebook, Inc. and its affiliates.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. *
  7. * @flow
  8. * @format
  9. */
  10. 'use strict';
  11. import Platform from '../Utilities/Platform';
  12. import RCTLog from '../Utilities/RCTLog';
  13. import * as LogBoxData from './Data/LogBoxData';
  14. import {parseLogBoxLog, parseInterpolation} from './Data/parseLogBoxLog';
  15. import type {IgnorePattern} from './Data/LogBoxData';
  16. let LogBox;
  17. /**
  18. * LogBox displays logs in the app.
  19. */
  20. if (__DEV__) {
  21. // LogBox needs to insert itself early,
  22. // in order to access the component stacks appended by React DevTools.
  23. const {error, warn} = console;
  24. let errorImpl = error.bind(console);
  25. let warnImpl = warn.bind(console);
  26. (console: any).error = function(...args) {
  27. errorImpl(...args);
  28. };
  29. (console: any).warn = function(...args) {
  30. warnImpl(...args);
  31. };
  32. LogBox = {
  33. ignoreLogs: (patterns: $ReadOnlyArray<IgnorePattern>): void => {
  34. LogBoxData.addIgnorePatterns(patterns);
  35. },
  36. ignoreAllLogs: (value?: ?boolean): void => {
  37. LogBoxData.setDisabled(value == null ? true : value);
  38. },
  39. uninstall: (): void => {
  40. errorImpl = error;
  41. warnImpl = warn;
  42. delete (console: any).disableLogBox;
  43. },
  44. install: (): void => {
  45. // Trigger lazy initialization of module.
  46. require('../NativeModules/specs/NativeLogBox');
  47. errorImpl = function(...args) {
  48. registerError(...args);
  49. };
  50. warnImpl = function(...args) {
  51. registerWarning(...args);
  52. };
  53. if ((console: any).disableYellowBox === true) {
  54. LogBoxData.setDisabled(true);
  55. console.warn(
  56. 'console.disableYellowBox has been deprecated and will be removed in a future release. Please use LogBox.ignoreAllLogs(value) instead.',
  57. );
  58. }
  59. (Object.defineProperty: any)(console, 'disableYellowBox', {
  60. configurable: true,
  61. get: () => LogBoxData.isDisabled(),
  62. set: value => {
  63. LogBoxData.setDisabled(value);
  64. console.warn(
  65. 'console.disableYellowBox has been deprecated and will be removed in a future release. Please use LogBox.ignoreAllLogs(value) instead.',
  66. );
  67. },
  68. });
  69. if (Platform.isTesting) {
  70. LogBoxData.setDisabled(true);
  71. }
  72. RCTLog.setWarningHandler((...args) => {
  73. registerWarning(...args);
  74. });
  75. },
  76. };
  77. const isRCTLogAdviceWarning = (...args) => {
  78. // RCTLogAdvice is a native logging function designed to show users
  79. // a message in the console, but not show it to them in Logbox.
  80. return typeof args[0] === 'string' && args[0].startsWith('(ADVICE)');
  81. };
  82. const isWarningModuleWarning = (...args) => {
  83. return typeof args[0] === 'string' && args[0].startsWith('Warning: ');
  84. };
  85. const registerWarning = (...args): void => {
  86. // Let warnings within LogBox itself fall through.
  87. if (LogBoxData.isLogBoxErrorMessage(String(args[0]))) {
  88. error.call(console, ...args);
  89. return;
  90. }
  91. try {
  92. if (!isRCTLogAdviceWarning(...args)) {
  93. const {category, message, componentStack} = parseLogBoxLog(args);
  94. if (!LogBoxData.isMessageIgnored(message.content)) {
  95. // Be sure to pass LogBox warnings through.
  96. warn.call(console, ...args);
  97. LogBoxData.addLog({
  98. level: 'warn',
  99. category,
  100. message,
  101. componentStack,
  102. });
  103. }
  104. }
  105. } catch (err) {
  106. LogBoxData.reportLogBoxError(err);
  107. }
  108. };
  109. const registerError = (...args): void => {
  110. // Let errors within LogBox itself fall through.
  111. if (LogBoxData.isLogBoxErrorMessage(args[0])) {
  112. error.call(console, ...args);
  113. return;
  114. }
  115. try {
  116. if (!isWarningModuleWarning(...args)) {
  117. // Only show LogBox for the 'warning' module, otherwise pass through.
  118. // By passing through, this will get picked up by the React console override,
  119. // potentially adding the component stack. React then passes it back to the
  120. // React Native ExceptionsManager, which reports it to LogBox as an error.
  121. //
  122. // The 'warning' module needs to be handled here because React internally calls
  123. // `console.error('Warning: ')` with the component stack already included.
  124. error.call(console, ...args);
  125. return;
  126. }
  127. const format = args[0].replace('Warning: ', '');
  128. const filterResult = LogBoxData.checkWarningFilter(format);
  129. if (filterResult.suppressCompletely) {
  130. return;
  131. }
  132. let level = 'error';
  133. if (filterResult.suppressDialog_LEGACY === true) {
  134. level = 'warn';
  135. } else if (filterResult.forceDialogImmediately === true) {
  136. level = 'fatal'; // Do not downgrade. These are real bugs with same severity as throws.
  137. }
  138. // Unfortunately, we need to add the Warning: prefix back for downstream dependencies.
  139. args[0] = `Warning: ${filterResult.finalFormat}`;
  140. const {category, message, componentStack} = parseLogBoxLog(args);
  141. if (!LogBoxData.isMessageIgnored(message.content)) {
  142. // Interpolate the message so they are formatted for adb and other CLIs.
  143. // This is different than the message.content above because it includes component stacks.
  144. const interpolated = parseInterpolation(args);
  145. error.call(console, interpolated.message.content);
  146. LogBoxData.addLog({
  147. level,
  148. category,
  149. message,
  150. componentStack,
  151. });
  152. }
  153. } catch (err) {
  154. LogBoxData.reportLogBoxError(err);
  155. }
  156. };
  157. } else {
  158. LogBox = {
  159. ignoreLogs: (patterns: $ReadOnlyArray<IgnorePattern>): void => {
  160. // Do nothing.
  161. },
  162. ignoreAllLogs: (value?: ?boolean): void => {
  163. // Do nothing.
  164. },
  165. install: (): void => {
  166. // Do nothing.
  167. },
  168. uninstall: (): void => {
  169. // Do nothing.
  170. },
  171. };
  172. }
  173. module.exports = (LogBox: {
  174. ignoreLogs($ReadOnlyArray<IgnorePattern>): void,
  175. ignoreAllLogs(?boolean): void,
  176. install(): void,
  177. uninstall(): void,
  178. ...
  179. });