您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

middleware.js 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /* @flow */
  2. import Logger from 'jitsi-meet-logger';
  3. import { APP_WILL_MOUNT } from '../app';
  4. import JitsiMeetJS, { LIB_WILL_INIT } from '../lib-jitsi-meet';
  5. import { MiddlewareRegistry } from '../redux';
  6. import JitsiMeetInMemoryLogStorage
  7. from '../../../../modules/util/JitsiMeetInMemoryLogStorage';
  8. import JitsiMeetLogStorage from '../../../../modules/util/JitsiMeetLogStorage';
  9. import { isTestModeEnabled } from '../testing';
  10. import { SET_LOGGING_CONFIG } from './actionTypes';
  11. declare var APP: Object;
  12. /**
  13. * The Redux middleware of the feature base/logging.
  14. *
  15. * @param {Store} store - The Redux store.
  16. * @returns {Function}
  17. * @private
  18. */
  19. MiddlewareRegistry.register(store => next => action => {
  20. switch (action.type) {
  21. case APP_WILL_MOUNT:
  22. return _appWillMount(store, next, action);
  23. case LIB_WILL_INIT:
  24. return _libWillInit(store, next, action);
  25. case SET_LOGGING_CONFIG:
  26. return _setLoggingConfig(store, next, action);
  27. }
  28. return next(action);
  29. });
  30. /**
  31. * Notifies the feature base/logging that the action {@link APP_WILL_MOUNT} is
  32. * being dispatched within a specific Redux {@code store}.
  33. *
  34. * @param {Store} store - The Redux store in which the specified {@code action}
  35. * is being dispatched.
  36. * @param {Dispatch} next - The Redux {@code dispatch} function to dispatch the
  37. * specified {@code action} to the specified {@code store}.
  38. * @param {Action} action - The Redux action {@code APP_WILL_MOUNT} which is
  39. * being dispatched in the specified {@code store}.
  40. * @private
  41. * @returns {Object} The new state that is the result of the reduction of the
  42. * specified {@code action}.
  43. */
  44. function _appWillMount({ getState }, next, action) {
  45. const { config } = getState()['features/base/logging'];
  46. _setLogLevels(Logger, config);
  47. // FIXME Until the logic of conference.js is rewritten into the React
  48. // app we, JitsiMeetJS.init is to not be used for the React app.
  49. // Consequently, LIB_WILL_INIT will not be dispatched. In the meantime, do
  50. // the following:
  51. typeof APP === 'undefined' || _setLogLevels(JitsiMeetJS, config);
  52. return next(action);
  53. }
  54. /**
  55. * Initializes logging in the app.
  56. *
  57. * @param {Object} loggingConfig - The configuration with which logging is to be
  58. * initialized.
  59. * @param {boolean} isTestingEnabled - Is debug logging enabled.
  60. * @private
  61. * @returns {void}
  62. */
  63. function _initLogging(loggingConfig, isTestingEnabled) {
  64. // Create the LogCollector and register it as the global log transport. It
  65. // is done early to capture as much logs as possible. Captured logs will be
  66. // cached, before the JitsiMeetLogStorage gets ready (statistics module is
  67. // initialized).
  68. if (typeof APP === 'object'
  69. && !APP.logCollector
  70. && !loggingConfig.disableLogCollector) {
  71. APP.logCollector = new Logger.LogCollector(new JitsiMeetLogStorage());
  72. Logger.addGlobalTransport(APP.logCollector);
  73. JitsiMeetJS.addGlobalLogTransport(APP.logCollector);
  74. if (isTestingEnabled) {
  75. APP.debugLogs = new JitsiMeetInMemoryLogStorage();
  76. const debugLogCollector = new Logger.LogCollector(
  77. APP.debugLogs, { storeInterval: 1000 });
  78. Logger.addGlobalTransport(debugLogCollector);
  79. JitsiMeetJS.addGlobalLogTransport(debugLogCollector);
  80. debugLogCollector.start();
  81. }
  82. }
  83. }
  84. /**
  85. * Notifies the feature base/logging that the action {@link LIB_WILL_INIT} is
  86. * being dispatched within a specific Redux {@code store}.
  87. *
  88. * @param {Store} store - The Redux store in which the specified {@code action}
  89. * is being dispatched.
  90. * @param {Dispatch} next - The Redux {@code dispatch} function to dispatch the
  91. * specified {@code action} to the specified {@code store}.
  92. * @param {Action} action - The Redux action {@code LIB_WILL_INIT} which is
  93. * being dispatched in the specified {@code store}.
  94. * @private
  95. * @returns {Object} The new state that is the result of the reduction of the
  96. * specified {@code action}.
  97. */
  98. function _libWillInit({ getState }, next, action) {
  99. // Adding the if in order to preserve the logic for web after enabling
  100. // LIB_WILL_INIT action for web in initLib action.
  101. if (typeof APP === 'undefined') {
  102. _setLogLevels(JitsiMeetJS, getState()['features/base/logging'].config);
  103. }
  104. return next(action);
  105. }
  106. /**
  107. * Notifies the feature base/logging that the action {@link SET_LOGGING_CONFIG}
  108. * is being dispatched within a specific Redux {@code store}.
  109. *
  110. * @param {Store} store - The Redux store in which the specified {@code action}
  111. * is being dispatched.
  112. * @param {Dispatch} next - The Redux {@code dispatch} function to dispatch the
  113. * specified {@code action} to the specified {@code store}.
  114. * @param {Action} action - The Redux action {@code SET_LOGGING_CONFIG} which is
  115. * being dispatched in the specified {@code store}.
  116. * @private
  117. * @returns {Object} The new state that is the result of the reduction of the
  118. * specified {@code action}.
  119. */
  120. function _setLoggingConfig({ getState }, next, action) {
  121. const result = next(action);
  122. const newValue = getState()['features/base/logging'].config;
  123. const isTestingEnabled = isTestModeEnabled(getState());
  124. // TODO Generally, we'll want to _setLogLevels and _initLogging only if the
  125. // logging config values actually change.
  126. // XXX Unfortunately, we don't currently have a (nice) way of determining
  127. // whether _setLogLevels or _initLogging have been invoked so we have to
  128. // invoke them unconditionally even if none of the values have actually
  129. // changed.
  130. _setLogLevels(Logger, newValue);
  131. _setLogLevels(JitsiMeetJS, newValue);
  132. _initLogging(newValue, isTestingEnabled);
  133. return result;
  134. }
  135. /**
  136. * Sets the log levels of {@link Logger} or {@link JitsiMeetJS} in accord with
  137. * a specific configuration.
  138. *
  139. * @param {Object} logger - The object on which the log levels are to be set.
  140. * @param {Object} config - The configuration specifying the log levels to be
  141. * set on {@code Logger} or {@code JitsiMeetJS}.
  142. * @private
  143. * @returns {void}
  144. */
  145. function _setLogLevels(logger, config) {
  146. // XXX The loggers of the library lib-jitsi-meet and the application
  147. // jitsi-meet are separate, so the log levels have to be set in both.
  148. // First, set the default log level.
  149. logger.setLogLevel(config.defaultLogLevel);
  150. // Second, set the log level of each logger explictly overriden by config.
  151. Object.keys(config).forEach(
  152. id =>
  153. id === 'defaultLogLevel' || logger.setLogLevelById(config[id], id));
  154. }