您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

LogTransport.native.ts 1.6KB

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