init
Some checks failed
Close stale issues and PRs / stale (push) Has been cancelled

This commit is contained in:
2025-09-02 14:49:16 +08:00
commit 38ba663466
2885 changed files with 391107 additions and 0 deletions

View File

@@ -0,0 +1,18 @@
/**
* Constructs a log transport object for use with external API.
*
* @param {Array} levels - The log levels forwarded to the external API.
* @returns {Object} - The transport object.
*/
function buildTransport(levels: Array<string>) {
return levels.reduce((logger: any, level) => {
logger[level] = (...args: any) => {
APP.API.notifyLog(level, args);
};
return logger;
}, {});
}
export default buildTransport;

View File

@@ -0,0 +1,59 @@
/**
* Implements in memory logs storage, used for testing/debugging.
*
*/
export default class JitsiMeetInMemoryLogStorage {
logs: string[] = [];
/**
* Creates new <tt>JitsiMeetInMemoryLogStorage</tt>.
*/
constructor() {
/**
* Array of the log entries to keep.
*
* @type {array}
*/
this.logs = [];
}
/**
* Checks if this storage instance is ready.
*
* @returns {boolean} <tt>true</tt> when this storage is ready or
* <tt>false</tt> otherwise.
*/
isReady() {
return true;
}
/**
* Called by the <tt>LogCollector</tt> to store a series of log lines into
* batch.
*
* @param {string|Object[]} logEntries - An array containing strings
* representing log lines or aggregated lines objects.
* @returns {void}
*/
storeLogs(logEntries: (string | { text: string; })[]) {
for (let i = 0, len = logEntries.length; i < len; i++) {
const logEntry = logEntries[i];
if (typeof logEntry === 'object') {
this.logs.push(logEntry.text);
} else {
// Regular message
this.logs.push(logEntry);
}
}
}
/**
* Returns the logs stored in the memory.
*
* @returns {Array<string>} The collected log entries.
*/
getLogs() {
return this.logs;
}
}

View File

@@ -0,0 +1,69 @@
import { IStore } from '../../app/types';
import JitsiMeetJS from '../../base/lib-jitsi-meet';
import RTCStats from '../../rtcstats/RTCStats';
import { isRTCStatsEnabled } from '../../rtcstats/functions';
/**
* Implements log storage interface from the @jitsi/logger lib, as it stands
* now it only sends logs to the rtcstats server in case it is enabled.
*/
export default class JitsiMeetLogStorage {
getState: IStore['getState'];
/**
* Creates new <tt>JitsiMeetLogStorage</tt>.
*
* @param {Function} getState - The Redux store's {@code getState} method.
*/
constructor(getState: IStore['getState']) {
/**
* The Redux store's {@code getState} method.
*
* @type {Function}
*/
this.getState = getState;
}
/**
* The JitsiMeetLogStorage is ready we can use the rtcstats trace to send logs
* to the rtcstats server.
*
* @returns {boolean} <tt>true</tt> when this storage is ready or
* <tt>false</tt> otherwise.
*/
isReady() {
return JitsiMeetJS.rtcstats.isTraceAvailable();
}
/**
* Checks whether rtcstats logs storage is enabled.
*
* @returns {boolean} <tt>true</tt> when this storage can store logs to
* rtcstats, <tt>false</tt> otherwise.
*/
canStoreLogsRtcstats() {
const config = this.getState()['features/base/config'];
// RTCStats can run without sending app logs to the server.
// Be mindful that there exists another LogStorage instance withing lib-jitsi-meet,
// that is used to send logs generated there.
return config?.analytics?.rtcstatsStoreLogs && isRTCStatsEnabled(this.getState());
}
/**
* Called by the <tt>LogCollector</tt> to store a series of log lines into
* batch.
*
* @param {Array<string|Object>} logEntries - An array containing strings
* representing log lines or aggregated lines objects.
* @returns {void}
*/
storeLogs(logEntries: Array<string | any>) {
if (this.canStoreLogsRtcstats()) {
RTCStats.sendLogs(logEntries);
}
}
}

View File

@@ -0,0 +1,68 @@
import { NativeModules } from 'react-native';
// eslint-disable-next-line lines-around-comment
// @ts-expect-error
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.
*
* @param {Error} e - The error.
* @returns {string} - The stack trace.
*/
function stackToString(e: any) {
let ce;
let s = e.stack;
if (typeof e.cause === 'function' && (ce = e.cause())) {
s += `\nCaused by: ${stackToString(ce)}`;
}
return s;
}
/**
* Constructs a log transport object for use with @jitsi/logger.
*
* @returns {Object} - The transport object.
*/
function buildTransport() {
return [
'trace',
'debug',
'info',
'log',
'warn',
'error'
].reduce((logger: any, logName) => {
logger[logName] = (timestamp: string, ...args: Array<string>) => {
// It ignores the timestamp argument, because LogBridge will add it on the native side anyway
const nargs = args.map((arg: any) => {
if (arg instanceof Error) {
const errorBody = {
message: arg.message,
// @ts-ignore
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();

View File

@@ -0,0 +1 @@
export default {};

View File

@@ -0,0 +1,21 @@
/**
* The type of redux action which stores the log collector that will be
* submitting the logs to a service
*
* {
* type: SET_LOG_COLLECTOR,
* logCollector: Logger.LogCollector
* }
*/
export const SET_LOG_COLLECTOR = 'SET_LOG_COLLECTOR';
/**
* The type of redux action which sets the configuration of the feature
* base/logging.
*
* {
* type: SET_LOGGING_CONFIG,
* config: Object
* }
*/
export const SET_LOGGING_CONFIG = 'SET_LOGGING_CONFIG';

View File

@@ -0,0 +1,35 @@
import { SET_LOGGING_CONFIG, SET_LOG_COLLECTOR } from './actionTypes';
/**
* Stores a {@code Logger.LogCollector} instance which will be uploading logs.
*
* @param {Logger.LogCollector} logCollector - The log collector instance to be
* stored in the Redux state of base/logging feature.
* @returns {{
* type,
* logCollector: Object
* }}
*/
export function setLogCollector(logCollector?: Object) {
return {
type: SET_LOG_COLLECTOR,
logCollector
};
}
/**
* Sets the configuration of the feature base/logging.
*
* @param {Object} config - The configuration to set on the features
* base/logging.
* @returns {{
* type: SET_LOGGING_CONFIG,
* config: Object
* }}
*/
export function setLoggingConfig(config: Object) {
return {
type: SET_LOGGING_CONFIG,
config
};
}

View File

@@ -0,0 +1,44 @@
// @ts-expect-error
import Logger, { getLogger as _getLogger } from '@jitsi/logger';
import { once } from 'lodash-es';
import LogTransport from './LogTransport';
/**
* Options for building the logger. We disable the callee info on RN because it's
* almost always empty anyway.
*/
const DEFAULT_OPTS = {};
const DEFAULT_RN_OPTS = { disableCallerInfo: true };
/**
* Gets a logger for the given id.
*
* @param {string} id - Name for the logger.
* @returns {Object} - The logger object.
*/
export function getLogger(id: string) {
const opts = navigator.product === 'ReactNative' ? DEFAULT_RN_OPTS : DEFAULT_OPTS;
return _getLogger(id, undefined, opts);
}
/**
* Initializes native logging. This operations must be done as early as possible.
*/
export const _initLogging = once(() => {
if (navigator.product !== 'ReactNative') {
return;
}
// Lazy load it to avoid cycles in early web bootstrap code.
// eslint-disable-next-line @typescript-eslint/no-var-requires
const { default: JitsiMeetJS } = require('../lib-jitsi-meet/_');
Logger.setGlobalOptions(DEFAULT_RN_OPTS);
JitsiMeetJS.setGlobalLogOptions(DEFAULT_RN_OPTS);
Logger.removeGlobalTransport(console);
JitsiMeetJS.removeGlobalLogTransport(console);
Logger.addGlobalTransport(LogTransport);
JitsiMeetJS.addGlobalLogTransport(LogTransport);
});

View File

@@ -0,0 +1,302 @@
// @ts-expect-error
import Logger from '@jitsi/logger';
import { AnyAction } from 'redux';
import { IStore } from '../../app/types';
import { APP_WILL_MOUNT } from '../app/actionTypes';
import { CONFERENCE_FAILED, CONFERENCE_JOINED } from '../conference/actionTypes';
import { getCurrentConference } from '../conference/functions';
import { SET_CONFIG } from '../config/actionTypes';
import JitsiMeetJS, {
JitsiConferenceEvents
} from '../lib-jitsi-meet';
import { LIB_WILL_INIT } from '../lib-jitsi-meet/actionTypes';
import MiddlewareRegistry from '../redux/MiddlewareRegistry';
import { isTestModeEnabled } from '../testing/functions';
import buildExternalApiLogTransport from './ExternalApiLogTransport';
import JitsiMeetInMemoryLogStorage from './JitsiMeetInMemoryLogStorage';
import JitsiMeetLogStorage from './JitsiMeetLogStorage';
import { SET_LOGGING_CONFIG } from './actionTypes';
import { setLogCollector, setLoggingConfig } from './actions';
/**
* The Redux middleware of the feature base/logging.
*
* @param {Store} store - The Redux store.
* @returns {Function}
* @private
*/
MiddlewareRegistry.register(store => next => action => {
switch (action.type) {
case APP_WILL_MOUNT:
return _appWillMount(store, next, action);
case CONFERENCE_JOINED:
return _conferenceJoined(store, next, action);
case CONFERENCE_FAILED: {
const result = next(action);
const { logCollector } = store.getState()['features/base/logging'];
logCollector?.flush();
return result;
}
case LIB_WILL_INIT:
return _libWillInit(store, next, action);
case SET_CONFIG:
return _setConfig(store, next, action);
case SET_LOGGING_CONFIG:
return _setLoggingConfig(store, next, action);
}
return next(action);
});
/**
* Notifies the feature base/logging that the action {@link APP_WILL_MOUNT} is
* being dispatched within a specific Redux {@code store}.
*
* @param {Store} store - The Redux store in which the specified {@code action}
* is being dispatched.
* @param {Dispatch} next - The Redux {@code dispatch} function to dispatch the
* specified {@code action} to the specified {@code store}.
* @param {Action} action - The Redux action {@code APP_WILL_MOUNT} which is
* being dispatched in the specified {@code store}.
* @private
* @returns {Object} The new state that is the result of the reduction of the
* specified {@code action}.
*/
function _appWillMount({ getState }: IStore, next: Function, action: AnyAction) {
const { config } = getState()['features/base/logging'];
_setLogLevels(Logger, config);
// FIXME Until the logic of conference.js is rewritten into the React
// app we, JitsiMeetJS.init is to not be used for the React app.
// Consequently, LIB_WILL_INIT will not be dispatched. In the meantime, do
// the following:
typeof APP === 'undefined' || _setLogLevels(JitsiMeetJS, config);
return next(action);
}
/**
* Starts the log collector, after {@link CONFERENCE_JOINED} action is reduced.
*
* @param {Store} store - The Redux store in which the specified {@code action}
* is being dispatched.
* @param {Dispatch} next - The Redux {@code dispatch} function to dispatch the
* specified {@code action} to the specified {@code store}.
* @param {Action} action - The Redux action {@code CONFERENCE_JOINED} which is
* being dispatched in the specified {@code store}.
* @private
* @returns {*}
*/
function _conferenceJoined({ getState }: IStore, next: Function, action: AnyAction) {
// Wait until the joined event is processed, so that the JitsiMeetLogStorage
// will be ready.
const result = next(action);
const { conference } = action;
const { logCollector } = getState()['features/base/logging'];
if (logCollector && conference === getCurrentConference(getState())) {
// Make an attempt to flush in case a lot of logs have been cached,
// before the collector was started.
logCollector.flush();
// This event listener will flush the logs, before the statistics module
// is stopped.
//
// NOTE The LogCollector is not stopped, because this event can be
// triggered multiple times during single conference (whenever
// statistics module is stopped). That includes the case when Jicofo
// terminates a single person conference (one person left in the room
// waiting for someone to join). It will then restart the media session
// when someone eventually joins the room which will start the stats
// again.
conference.on(
JitsiConferenceEvents.BEFORE_STATISTICS_DISPOSED,
() => logCollector.flush()
);
}
return result;
}
/**
* Initializes logging in the app.
*
* @param {Store} store - The Redux store in which context the logging is to be
* initialized.
* @param {Object} loggingConfig - The configuration with which logging is to be
* initialized.
* @param {boolean} isTestingEnabled - Is debug logging enabled.
* @private
* @returns {void}
*/
function _initLogging({ dispatch, getState }: IStore,
loggingConfig: any, isTestingEnabled: boolean) {
const { logCollector } = getState()['features/base/logging'];
// Create the LogCollector and register it as the global log transport. It
// is done early to capture as much logs as possible. Captured logs will be
// cached, before the JitsiMeetLogStorage gets ready (RTCStats trace is
// available).
if (!logCollector && !loggingConfig.disableLogCollector) {
const { apiLogLevels, analytics: { rtcstatsLogFlushSizeBytes } = {} } = getState()['features/base/config'];
// The smaller the flush size the smaller the chance of losing logs, but
// the more often the logs will be sent to the server, by default the LogCollector
// will set once the logs reach 10KB or 30 seconds have passed since the last flush,
// this means if something happens between that interval and the logs don't get flushed
// they will be lost, for instance the meeting tab is closed, the browser crashes,
// an uncaught exception happens, etc.
// If undefined is passed the default values will be used,
const _logCollector = new Logger.LogCollector(new JitsiMeetLogStorage(getState), {
maxEntryLength: rtcstatsLogFlushSizeBytes
});
if (apiLogLevels && Array.isArray(apiLogLevels) && typeof APP === 'object') {
const transport = buildExternalApiLogTransport(apiLogLevels);
Logger.addGlobalTransport(transport);
JitsiMeetJS.addGlobalLogTransport(transport);
}
Logger.addGlobalTransport(_logCollector);
_logCollector.start();
dispatch(setLogCollector(_logCollector));
// The JitsiMeetInMemoryLogStorage can not be accessed on mobile through
// the 'executeScript' method like it's done in torture tests for WEB.
if (isTestingEnabled && typeof APP === 'object') {
APP.debugLogs = new JitsiMeetInMemoryLogStorage();
const debugLogCollector = new Logger.LogCollector(
APP.debugLogs, { storeInterval: 1000 });
Logger.addGlobalTransport(debugLogCollector);
JitsiMeetJS.addGlobalLogTransport(debugLogCollector);
debugLogCollector.start();
}
} else if (logCollector && loggingConfig.disableLogCollector) {
Logger.removeGlobalTransport(logCollector);
JitsiMeetJS.removeGlobalLogTransport(logCollector);
logCollector.stop();
dispatch(setLogCollector(undefined));
}
}
/**
* Notifies the feature base/logging that the action {@link LIB_WILL_INIT} is
* being dispatched within a specific Redux {@code store}.
*
* @param {Store} store - The Redux store in which the specified {@code action}
* is being dispatched.
* @param {Dispatch} next - The Redux {@code dispatch} function to dispatch the
* specified {@code action} to the specified {@code store}.
* @param {Action} action - The Redux action {@code LIB_WILL_INIT} which is
* being dispatched in the specified {@code store}.
* @private
* @returns {Object} The new state that is the result of the reduction of the
* specified {@code action}.
*/
function _libWillInit({ getState }: IStore, next: Function, action: AnyAction) {
// Adding the if in order to preserve the logic for web after enabling
// LIB_WILL_INIT action for web in initLib action.
if (typeof APP === 'undefined') {
_setLogLevels(JitsiMeetJS, getState()['features/base/logging'].config);
}
return next(action);
}
/**
* This feature that the action SET_CONFIG is being
* dispatched within a specific Redux store.
*
* @param {Store} store - The Redux store in which the specified action is being
* dispatched.
* @param {Dispatch} next - The Redux dispatch function to dispatch the
* specified action to the specified store.
* @param {Action} action - The Redux action SET_CONFIG which is being
* dispatched in the specified store.
* @private
* @returns {Object} The new state that is the result of the reduction of the
* specified action.
*/
function _setConfig({ dispatch }: IStore, next: Function, action: AnyAction) {
const result = next(action);
dispatch(setLoggingConfig(action.config?.logging));
return result;
}
/**
* Notifies the feature base/logging that the action {@link SET_LOGGING_CONFIG}
* is being dispatched within a specific Redux {@code store}.
*
* @param {Store} store - The Redux store in which the specified {@code action}
* is being dispatched.
* @param {Dispatch} next - The Redux {@code dispatch} function to dispatch the
* specified {@code action} to the specified {@code store}.
* @param {Action} action - The Redux action {@code SET_LOGGING_CONFIG} which is
* being dispatched in the specified {@code store}.
* @private
* @returns {Object} The new state that is the result of the reduction of the
* specified {@code action}.
*/
function _setLoggingConfig({ dispatch, getState }: IStore,
next: Function, action: AnyAction) {
const result = next(action);
const newValue = getState()['features/base/logging'].config;
const isTestingEnabled = isTestModeEnabled(getState());
// TODO Generally, we'll want to _setLogLevels and _initLogging only if the
// logging config values actually change.
// XXX Unfortunately, we don't currently have a (nice) way of determining
// whether _setLogLevels or _initLogging have been invoked so we have to
// invoke them unconditionally even if none of the values have actually
// changed.
_setLogLevels(Logger, newValue);
_setLogLevels(JitsiMeetJS, newValue);
_initLogging({
dispatch,
getState
}, newValue, isTestingEnabled);
return result;
}
/**
* Sets the log levels of {@link Logger} or {@link JitsiMeetJS} in accord with
* a specific configuration.
*
* @param {Object} logger - The object on which the log levels are to be set.
* @param {Object} config - The configuration specifying the log levels to be
* set on {@code Logger} or {@code JitsiMeetJS}.
* @private
* @returns {void}
*/
function _setLogLevels(logger: any, config: any) {
// XXX The loggers of the library lib-jitsi-meet and the application
// jitsi-meet are separate, so the log levels have to be set in both.
// First, set the default log level.
logger.setLogLevel(config.defaultLogLevel);
// Second, set the log level of each logger explicitly overridden by config.
for (const [ id, level ] of Object.entries(config.loggers)) {
logger.setLogLevelById(level, id);
}
}

View File

@@ -0,0 +1,115 @@
import { merge } from 'lodash-es';
import { AnyAction } from 'redux';
import ReducerRegistry from '../redux/ReducerRegistry';
import { equals, set } from '../redux/functions';
import { SET_LOGGING_CONFIG, SET_LOG_COLLECTOR } from './actionTypes';
import { ILoggingConfig, LogLevel } from './types';
const DEFAULT_LOGGING_CONFIG: ILoggingConfig = {
// default log level for the app and lib-jitsi-meet
defaultLogLevel: 'trace',
// Option to disable LogCollector (which stores the logs)
// disableLogCollector: true,
loggers: {
// The following are too verbose in their logging with the
// {@link #defaultLogLevel}:
'modules/RTC/TraceablePeerConnection': 'info',
'modules/xmpp/strophe.util': 'log'
}
};
/**
* The default/initial redux state of the feature base/logging.
*
* @type {{
* config: Object
* }}
*/
const DEFAULT_STATE = {
config: DEFAULT_LOGGING_CONFIG,
/**
* The log collector.
*/
logCollector: undefined
};
// Reduce default verbosity on mobile, it kills performance.
if (navigator.product === 'ReactNative') {
const RN_LOGGERS: { [key: string]: LogLevel; } = {
'modules/sdp/SDPUtil': 'info',
'modules/xmpp/ChatRoom': 'warn',
'modules/xmpp/JingleSessionPC': 'info',
'modules/xmpp/strophe.jingle': 'info'
};
DEFAULT_STATE.config.loggers = {
...DEFAULT_LOGGING_CONFIG.loggers,
...RN_LOGGERS
};
}
export interface ILoggingState {
config: ILoggingConfig;
logCollector?: {
flush: () => void;
start: () => void;
stop: () => void;
};
}
ReducerRegistry.register<ILoggingState>(
'features/base/logging',
(state = DEFAULT_STATE, action): ILoggingState => {
switch (action.type) {
case SET_LOGGING_CONFIG:
return _setLoggingConfig(state, action);
case SET_LOG_COLLECTOR: {
return _setLogCollector(state, action);
}
default:
return state;
}
});
/**
* Reduces a specific Redux action SET_LOGGING_CONFIG of the feature
* base/logging.
*
* @param {Object} state - The Redux state of the feature base/logging.
* @param {Action} action - The Redux action SET_LOGGING_CONFIG to reduce.
* @private
* @returns {Object} The new state of the feature base/logging after the
* reduction of the specified action.
*/
function _setLoggingConfig(state: ILoggingState, action: AnyAction) {
const newConfig = merge({}, DEFAULT_STATE.config, action.config);
if (equals(state.config, newConfig)) {
return state;
}
return {
...state,
config: newConfig
};
}
/**
* Reduces a specific Redux action SET_LOG_COLLECTOR of the feature
* base/logging.
*
* @param {Object} state - The Redux state of the feature base/logging.
* @param {Action} action - The Redux action SET_LOG_COLLECTOR to reduce.
* @private
* @returns {Object} The new state of the feature base/logging after the
* reduction of the specified action.
*/
function _setLogCollector(state: ILoggingState, action: AnyAction) {
return set(state, 'logCollector', action.logCollector);
}

View File

@@ -0,0 +1,9 @@
export type LogLevel = 'trace' | 'log' | 'info' | 'warn' | 'error';
export interface ILoggingConfig {
defaultLogLevel?: LogLevel;
disableLogCollector?: boolean;
loggers?: {
[key: string]: LogLevel;
};
}