1. /**
    
  2.  * Copyright (c) Meta Platforms, Inc. and 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. 
    
  8. import ReactSharedInternals from 'shared/ReactSharedInternals';
    
  9. 
    
  10. let suppressWarning = false;
    
  11. export function setSuppressWarning(newSuppressWarning) {
    
  12.   if (__DEV__) {
    
  13.     suppressWarning = newSuppressWarning;
    
  14.   }
    
  15. }
    
  16. 
    
  17. // In DEV, calls to console.warn and console.error get replaced
    
  18. // by calls to these methods by a Babel plugin.
    
  19. //
    
  20. // In PROD (or in packages without access to React internals),
    
  21. // they are left as they are instead.
    
  22. 
    
  23. export function warn(format, ...args) {
    
  24.   if (__DEV__) {
    
  25.     if (!suppressWarning) {
    
  26.       printWarning('warn', format, args);
    
  27.     }
    
  28.   }
    
  29. }
    
  30. 
    
  31. export function error(format, ...args) {
    
  32.   if (__DEV__) {
    
  33.     if (!suppressWarning) {
    
  34.       printWarning('error', format, args);
    
  35.     }
    
  36.   }
    
  37. }
    
  38. 
    
  39. function printWarning(level, format, args) {
    
  40.   // When changing this logic, you might want to also
    
  41.   // update consoleWithStackDev.www.js as well.
    
  42.   if (__DEV__) {
    
  43.     const ReactDebugCurrentFrame = ReactSharedInternals.ReactDebugCurrentFrame;
    
  44.     const stack = ReactDebugCurrentFrame.getStackAddendum();
    
  45.     if (stack !== '') {
    
  46.       format += '%s';
    
  47.       args = args.concat([stack]);
    
  48.     }
    
  49. 
    
  50.     // eslint-disable-next-line react-internal/safe-string-coercion
    
  51.     const argsWithFormat = args.map(item => String(item));
    
  52.     // Careful: RN currently depends on this prefix
    
  53.     argsWithFormat.unshift('Warning: ' + format);
    
  54.     // We intentionally don't use spread (or .apply) directly because it
    
  55.     // breaks IE9: https://github.com/facebook/react/issues/13610
    
  56.     // eslint-disable-next-line react-internal/no-production-logging
    
  57.     Function.prototype.apply.call(console[level], console, argsWithFormat);
    
  58.   }
    
  59. }