2019-08-28 10:31:38 +00:00
|
|
|
// @flow
|
|
|
|
|
|
|
|
import { NativeModules } from 'react-native';
|
|
|
|
import { format } from 'util';
|
|
|
|
|
|
|
|
// Some code adapted from https://github.com/houserater/react-native-lumberjack
|
|
|
|
// License: MIT
|
|
|
|
|
|
|
|
const { LogBridge } = NativeModules;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the stack trace for a given @code {Error} object.
|
|
|
|
*
|
2021-03-16 15:59:33 +00:00
|
|
|
* @param {Error} e - The error.
|
2019-08-28 10:31:38 +00:00
|
|
|
* @returns {string} - The stack trace.
|
|
|
|
*/
|
|
|
|
function stackToString(e) {
|
|
|
|
let ce;
|
|
|
|
let s = e.stack;
|
|
|
|
|
|
|
|
if (typeof e.cause === 'function' && (ce = e.cause())) {
|
|
|
|
s += `\nCaused by: ${stackToString(ce)}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-11-10 10:11:29 +00:00
|
|
|
* Constructs a log transport object for use with @jitsi/logger.
|
2019-08-28 10:31:38 +00:00
|
|
|
*
|
|
|
|
* @returns {Object} - The transport object.
|
|
|
|
*/
|
|
|
|
function buildTransport() {
|
|
|
|
return [
|
|
|
|
'trace',
|
|
|
|
'debug',
|
|
|
|
'info',
|
|
|
|
'log',
|
|
|
|
'warn',
|
|
|
|
'error'
|
|
|
|
].reduce((logger, logName) => {
|
2019-10-15 20:47:23 +00:00
|
|
|
logger[logName] = (timestamp: string, ...args: Array<string>) => {
|
|
|
|
// It ignores the timestamp argument, because LogBridge will add it on the native side anyway
|
2019-08-28 10:31:38 +00:00
|
|
|
const nargs = args.map(arg => {
|
|
|
|
if (arg instanceof Error) {
|
|
|
|
const errorBody = {
|
|
|
|
message: arg.message,
|
|
|
|
code: arg.code,
|
|
|
|
stack: stackToString(arg)
|
|
|
|
};
|
|
|
|
|
|
|
|
return `Error(${arg.name})${JSON.stringify(errorBody)}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
return arg;
|
|
|
|
});
|
|
|
|
const message = format(...nargs);
|
|
|
|
|
|
|
|
LogBridge[logName](message);
|
|
|
|
};
|
|
|
|
|
|
|
|
return logger;
|
|
|
|
}, {});
|
|
|
|
}
|
|
|
|
|
|
|
|
export default buildTransport();
|