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.

middleware.ts 11KB

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