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

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