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

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