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 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. _setLogLevels(JitsiMeetJS, getState()['features/base/logging'].config);
  100. return next(action);
  101. }
  102. /**
  103. * Notifies the feature base/logging that the action {@link SET_LOGGING_CONFIG}
  104. * is being dispatched within a specific Redux {@code store}.
  105. *
  106. * @param {Store} store - The Redux store in which the specified {@code action}
  107. * is being dispatched.
  108. * @param {Dispatch} next - The Redux {@code dispatch} function to dispatch the
  109. * specified {@code action} to the specified {@code store}.
  110. * @param {Action} action - The Redux action {@code SET_LOGGING_CONFIG} which is
  111. * being dispatched in the specified {@code store}.
  112. * @private
  113. * @returns {Object} The new state that is the result of the reduction of the
  114. * specified {@code action}.
  115. */
  116. function _setLoggingConfig({ getState }, next, action) {
  117. const result = next(action);
  118. const newValue = getState()['features/base/logging'].config;
  119. const isTestingEnabled = isTestModeEnabled(getState());
  120. // TODO Generally, we'll want to _setLogLevels and _initLogging only if the
  121. // logging config values actually change.
  122. // XXX Unfortunately, we don't currently have a (nice) way of determining
  123. // whether _setLogLevels or _initLogging have been invoked so we have to
  124. // invoke them unconditionally even if none of the values have actually
  125. // changed.
  126. _setLogLevels(Logger, newValue);
  127. _setLogLevels(JitsiMeetJS, newValue);
  128. _initLogging(newValue, isTestingEnabled);
  129. return result;
  130. }
  131. /**
  132. * Sets the log levels of {@link Logger} or {@link JitsiMeetJS} in accord with
  133. * a specific configuration.
  134. *
  135. * @param {Object} logger - The object on which the log levels are to be set.
  136. * @param {Object} config - The configuration specifying the log levels to be
  137. * set on {@code Logger} or {@code JitsiMeetJS}.
  138. * @private
  139. * @returns {void}
  140. */
  141. function _setLogLevels(logger, config) {
  142. // XXX The loggers of the library lib-jitsi-meet and the application
  143. // jitsi-meet are separate, so the log levels have to be set in both.
  144. // First, set the default log level.
  145. logger.setLogLevel(config.defaultLogLevel);
  146. // Second, set the log level of each logger explictly overriden by config.
  147. Object.keys(config).forEach(
  148. id =>
  149. id === 'defaultLogLevel' || logger.setLogLevelById(config[id], id));
  150. }