You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

LogTransport.native.js 1.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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] = (timestamp: string, ...args: Array<string>) => {
  36. // It ignores the timestamp argument, because LogBridge will add it on the native side anyway
  37. const nargs = args.map(arg => {
  38. if (arg instanceof Error) {
  39. const errorBody = {
  40. message: arg.message,
  41. code: arg.code,
  42. stack: stackToString(arg)
  43. };
  44. return `Error(${arg.name})${JSON.stringify(errorBody)}`;
  45. }
  46. return arg;
  47. });
  48. const message = format(...nargs);
  49. LogBridge[logName](message);
  50. };
  51. return logger;
  52. }, {});
  53. }
  54. export default buildTransport();