Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

middleware.ts 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. // @ts-expect-error
  2. import Logger from '@jitsi/logger';
  3. import { AnyAction } from 'redux';
  4. import { IStore } from '../../app/types';
  5. import { APP_WILL_MOUNT } from '../app/actionTypes';
  6. import { CONFERENCE_JOINED } from '../conference/actionTypes';
  7. import { getCurrentConference } from '../conference/functions';
  8. import { SET_CONFIG } from '../config/actionTypes';
  9. import JitsiMeetJS, {
  10. JitsiConferenceEvents
  11. } from '../lib-jitsi-meet';
  12. import { LIB_WILL_INIT } from '../lib-jitsi-meet/actionTypes';
  13. import MiddlewareRegistry from '../redux/MiddlewareRegistry';
  14. import { isTestModeEnabled } from '../testing/functions';
  15. import buildExternalApiLogTransport from './ExternalApiLogTransport';
  16. import JitsiMeetInMemoryLogStorage from './JitsiMeetInMemoryLogStorage';
  17. import JitsiMeetLogStorage from './JitsiMeetLogStorage';
  18. import { SET_LOGGING_CONFIG } from './actionTypes';
  19. import { setLogCollector, setLoggingConfig } from './actions';
  20. /**
  21. * The Redux middleware of the feature base/logging.
  22. *
  23. * @param {Store} store - The Redux store.
  24. * @returns {Function}
  25. * @private
  26. */
  27. MiddlewareRegistry.register(store => next => action => {
  28. switch (action.type) {
  29. case APP_WILL_MOUNT:
  30. return _appWillMount(store, next, action);
  31. case CONFERENCE_JOINED:
  32. return _conferenceJoined(store, next, action);
  33. case LIB_WILL_INIT:
  34. return _libWillInit(store, next, action);
  35. case SET_CONFIG:
  36. return _setConfig(store, next, action);
  37. case SET_LOGGING_CONFIG:
  38. return _setLoggingConfig(store, next, action);
  39. }
  40. return next(action);
  41. });
  42. /**
  43. * Notifies the feature base/logging that the action {@link APP_WILL_MOUNT} is
  44. * being dispatched within a specific Redux {@code store}.
  45. *
  46. * @param {Store} store - The Redux store in which the specified {@code action}
  47. * is being dispatched.
  48. * @param {Dispatch} next - The Redux {@code dispatch} function to dispatch the
  49. * specified {@code action} to the specified {@code store}.
  50. * @param {Action} action - The Redux action {@code APP_WILL_MOUNT} which is
  51. * being dispatched in the specified {@code store}.
  52. * @private
  53. * @returns {Object} The new state that is the result of the reduction of the
  54. * specified {@code action}.
  55. */
  56. function _appWillMount({ getState }: IStore, next: Function, action: AnyAction) {
  57. const { config } = getState()['features/base/logging'];
  58. _setLogLevels(Logger, config);
  59. // FIXME Until the logic of conference.js is rewritten into the React
  60. // app we, JitsiMeetJS.init is to not be used for the React app.
  61. // Consequently, LIB_WILL_INIT will not be dispatched. In the meantime, do
  62. // the following:
  63. typeof APP === 'undefined' || _setLogLevels(JitsiMeetJS, config);
  64. return next(action);
  65. }
  66. /**
  67. * Starts the log collector, after {@link CONFERENCE_JOINED} action is reduced.
  68. *
  69. * @param {Store} store - The Redux store in which the specified {@code action}
  70. * is being dispatched.
  71. * @param {Dispatch} next - The Redux {@code dispatch} function to dispatch the
  72. * specified {@code action} to the specified {@code store}.
  73. * @param {Action} action - The Redux action {@code CONFERENCE_JOINED} which is
  74. * being dispatched in the specified {@code store}.
  75. * @private
  76. * @returns {*}
  77. */
  78. function _conferenceJoined({ getState }: IStore, next: Function, action: AnyAction) {
  79. // Wait until the joined event is processed, so that the JitsiMeetLogStorage
  80. // will be ready.
  81. const result = next(action);
  82. const { conference } = action;
  83. const { logCollector } = getState()['features/base/logging'];
  84. if (logCollector && conference === getCurrentConference(getState())) {
  85. // Start the LogCollector's periodic "store logs" task
  86. logCollector.start();
  87. // Make an attempt to flush in case a lot of logs have been cached,
  88. // before the collector was started.
  89. logCollector.flush();
  90. // This event listener will flush the logs, before the statistics module
  91. // is stopped.
  92. //
  93. // NOTE The LogCollector is not stopped, because this event can be
  94. // triggered multiple times during single conference (whenever
  95. // statistics module is stopped). That includes the case when Jicofo
  96. // terminates a single person conference (one person left in the room
  97. // waiting for someone to join). It will then restart the media session
  98. // when someone eventually joins the room which will start the stats
  99. // again.
  100. conference.on(
  101. JitsiConferenceEvents.BEFORE_STATISTICS_DISPOSED,
  102. () => logCollector.flush()
  103. );
  104. }
  105. return result;
  106. }
  107. /**
  108. * Initializes logging in the app.
  109. *
  110. * @param {Store} store - The Redux store in which context the logging is to be
  111. * initialized.
  112. * @param {Object} loggingConfig - The configuration with which logging is to be
  113. * initialized.
  114. * @param {boolean} isTestingEnabled - Is debug logging enabled.
  115. * @private
  116. * @returns {void}
  117. */
  118. function _initLogging({ dispatch, getState }: IStore,
  119. loggingConfig: any, isTestingEnabled: boolean) {
  120. const { logCollector } = getState()['features/base/logging'];
  121. // Create the LogCollector and register it as the global log transport. It
  122. // is done early to capture as much logs as possible. Captured logs will be
  123. // cached, before the JitsiMeetLogStorage gets ready (statistics module is
  124. // initialized).
  125. if (!logCollector && !loggingConfig.disableLogCollector) {
  126. const _logCollector = new Logger.LogCollector(new JitsiMeetLogStorage(getState));
  127. const { apiLogLevels } = getState()['features/base/config'];
  128. if (apiLogLevels && Array.isArray(apiLogLevels) && typeof APP === 'object') {
  129. const transport = buildExternalApiLogTransport(apiLogLevels);
  130. Logger.addGlobalTransport(transport);
  131. JitsiMeetJS.addGlobalLogTransport(transport);
  132. }
  133. Logger.addGlobalTransport(_logCollector);
  134. JitsiMeetJS.addGlobalLogTransport(_logCollector);
  135. dispatch(setLogCollector(_logCollector));
  136. // The JitsiMeetInMemoryLogStorage can not be accessed on mobile through
  137. // the 'executeScript' method like it's done in torture tests for WEB.
  138. if (isTestingEnabled && typeof APP === 'object') {
  139. APP.debugLogs = new JitsiMeetInMemoryLogStorage();
  140. const debugLogCollector = new Logger.LogCollector(
  141. APP.debugLogs, { storeInterval: 1000 });
  142. Logger.addGlobalTransport(debugLogCollector);
  143. JitsiMeetJS.addGlobalLogTransport(debugLogCollector);
  144. debugLogCollector.start();
  145. }
  146. } else if (logCollector && loggingConfig.disableLogCollector) {
  147. Logger.removeGlobalTransport(logCollector);
  148. JitsiMeetJS.removeGlobalLogTransport(logCollector);
  149. logCollector.stop();
  150. dispatch(setLogCollector(undefined));
  151. }
  152. }
  153. /**
  154. * Notifies the feature base/logging that the action {@link LIB_WILL_INIT} is
  155. * being dispatched within a specific Redux {@code store}.
  156. *
  157. * @param {Store} store - The Redux store in which the specified {@code action}
  158. * is being dispatched.
  159. * @param {Dispatch} next - The Redux {@code dispatch} function to dispatch the
  160. * specified {@code action} to the specified {@code store}.
  161. * @param {Action} action - The Redux action {@code LIB_WILL_INIT} which is
  162. * being dispatched in the specified {@code store}.
  163. * @private
  164. * @returns {Object} The new state that is the result of the reduction of the
  165. * specified {@code action}.
  166. */
  167. function _libWillInit({ getState }: IStore, next: Function, action: AnyAction) {
  168. // Adding the if in order to preserve the logic for web after enabling
  169. // LIB_WILL_INIT action for web in initLib action.
  170. if (typeof APP === 'undefined') {
  171. _setLogLevels(JitsiMeetJS, getState()['features/base/logging'].config);
  172. }
  173. return next(action);
  174. }
  175. /**
  176. * This feature that the action SET_CONFIG is being
  177. * dispatched within a specific Redux store.
  178. *
  179. * @param {Store} store - The Redux store in which the specified action is being
  180. * dispatched.
  181. * @param {Dispatch} next - The Redux dispatch function to dispatch the
  182. * specified action to the specified store.
  183. * @param {Action} action - The Redux action SET_CONFIG which is being
  184. * dispatched in the specified store.
  185. * @private
  186. * @returns {Object} The new state that is the result of the reduction of the
  187. * specified action.
  188. */
  189. function _setConfig({ dispatch }: IStore, next: Function, action: AnyAction) {
  190. const result = next(action);
  191. dispatch(setLoggingConfig(action.config?.logging));
  192. return result;
  193. }
  194. /**
  195. * Notifies the feature base/logging that the action {@link SET_LOGGING_CONFIG}
  196. * is being dispatched within a specific Redux {@code store}.
  197. *
  198. * @param {Store} store - The Redux store in which the specified {@code action}
  199. * is being dispatched.
  200. * @param {Dispatch} next - The Redux {@code dispatch} function to dispatch the
  201. * specified {@code action} to the specified {@code store}.
  202. * @param {Action} action - The Redux action {@code SET_LOGGING_CONFIG} which is
  203. * being dispatched in the specified {@code store}.
  204. * @private
  205. * @returns {Object} The new state that is the result of the reduction of the
  206. * specified {@code action}.
  207. */
  208. function _setLoggingConfig({ dispatch, getState }: IStore,
  209. next: Function, action: AnyAction) {
  210. const result = next(action);
  211. const newValue = getState()['features/base/logging'].config;
  212. const isTestingEnabled = isTestModeEnabled(getState());
  213. // TODO Generally, we'll want to _setLogLevels and _initLogging only if the
  214. // logging config values actually change.
  215. // XXX Unfortunately, we don't currently have a (nice) way of determining
  216. // whether _setLogLevels or _initLogging have been invoked so we have to
  217. // invoke them unconditionally even if none of the values have actually
  218. // changed.
  219. _setLogLevels(Logger, newValue);
  220. _setLogLevels(JitsiMeetJS, newValue);
  221. _initLogging({
  222. dispatch,
  223. getState
  224. }, newValue, isTestingEnabled);
  225. return result;
  226. }
  227. /**
  228. * Sets the log levels of {@link Logger} or {@link JitsiMeetJS} in accord with
  229. * a specific configuration.
  230. *
  231. * @param {Object} logger - The object on which the log levels are to be set.
  232. * @param {Object} config - The configuration specifying the log levels to be
  233. * set on {@code Logger} or {@code JitsiMeetJS}.
  234. * @private
  235. * @returns {void}
  236. */
  237. function _setLogLevels(logger: any, config: any) {
  238. // XXX The loggers of the library lib-jitsi-meet and the application
  239. // jitsi-meet are separate, so the log levels have to be set in both.
  240. // First, set the default log level.
  241. logger.setLogLevel(config.defaultLogLevel);
  242. // Second, set the log level of each logger explicitly overridden by config.
  243. for (const [ id, level ] of Object.entries(config.loggers)) {
  244. logger.setLogLevelById(level, id);
  245. }
  246. }