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.6KB

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