Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

LogTransport.native.ts 1.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import { NativeModules } from 'react-native';
  2. // eslint-disable-next-line lines-around-comment
  3. // @ts-expect-error
  4. import { format } from 'util';
  5. // Some code adapted from https://github.com/houserater/react-native-lumberjack
  6. // License: MIT
  7. const { LogBridge } = NativeModules;
  8. /**
  9. * Returns the stack trace for a given @code {Error} object.
  10. *
  11. * @param {Error} e - The error.
  12. * @returns {string} - The stack trace.
  13. */
  14. function stackToString(e: any) {
  15. let ce;
  16. let s = e.stack;
  17. if (typeof e.cause === 'function' && (ce = e.cause())) {
  18. s += `\nCaused by: ${stackToString(ce)}`;
  19. }
  20. return s;
  21. }
  22. /**
  23. * Constructs a log transport object for use with @jitsi/logger.
  24. *
  25. * @returns {Object} - The transport object.
  26. */
  27. function buildTransport() {
  28. return [
  29. 'trace',
  30. 'debug',
  31. 'info',
  32. 'log',
  33. 'warn',
  34. 'error'
  35. ].reduce((logger: any, logName) => {
  36. logger[logName] = (timestamp: string, ...args: Array<string>) => {
  37. // It ignores the timestamp argument, because LogBridge will add it on the native side anyway
  38. const nargs = args.map((arg: any) => {
  39. if (arg instanceof Error) {
  40. const errorBody = {
  41. message: arg.message,
  42. // @ts-ignore
  43. code: arg.code,
  44. stack: stackToString(arg)
  45. };
  46. return `Error(${arg.name})${JSON.stringify(errorBody)}`;
  47. }
  48. return arg;
  49. });
  50. const message = format(...nargs);
  51. LogBridge[logName](message);
  52. };
  53. return logger;
  54. }, {});
  55. }
  56. export default buildTransport();