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.js 9.2KB

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