Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

LogTransport.native.js 1.5KB

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