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

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