"Fossies" - the Fresh Open Source Software Archive 
Member "jitsi-meet-7313/react/features/base/logging/middleware.ts" (2 Jun 2023, 10997 Bytes) of package /linux/misc/jitsi-meet-7313.tar.gz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) TypeScript source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
See also the last
Fossies "Diffs" side-by-side code changes report for "middleware.ts":
7283_vs_7286.
1 // @ts-expect-error
2 import Logger from '@jitsi/logger';
3 import { AnyAction } from 'redux';
4
5 import { IStore } from '../../app/types';
6 import { APP_WILL_MOUNT } from '../app/actionTypes';
7 import { CONFERENCE_JOINED } from '../conference/actionTypes';
8 import { getCurrentConference } from '../conference/functions';
9 import { SET_CONFIG } from '../config/actionTypes';
10 import JitsiMeetJS, {
11 JitsiConferenceEvents
12 } from '../lib-jitsi-meet';
13 import { LIB_WILL_INIT } from '../lib-jitsi-meet/actionTypes';
14 import MiddlewareRegistry from '../redux/MiddlewareRegistry';
15 import { isTestModeEnabled } from '../testing/functions';
16
17 import buildExternalApiLogTransport from './ExternalApiLogTransport';
18 import JitsiMeetInMemoryLogStorage from './JitsiMeetInMemoryLogStorage';
19 import JitsiMeetLogStorage from './JitsiMeetLogStorage';
20 import { SET_LOGGING_CONFIG } from './actionTypes';
21 import { setLogCollector, setLoggingConfig } from './actions';
22
23 /**
24 * The Redux middleware of the feature base/logging.
25 *
26 * @param {Store} store - The Redux store.
27 * @returns {Function}
28 * @private
29 */
30 MiddlewareRegistry.register(store => next => action => {
31 switch (action.type) {
32 case APP_WILL_MOUNT:
33 return _appWillMount(store, next, action);
34
35 case CONFERENCE_JOINED:
36 return _conferenceJoined(store, next, action);
37
38 case LIB_WILL_INIT:
39 return _libWillInit(store, next, action);
40
41 case SET_CONFIG:
42 return _setConfig(store, next, action);
43
44 case SET_LOGGING_CONFIG:
45 return _setLoggingConfig(store, next, action);
46 }
47
48 return next(action);
49 });
50
51 /**
52 * Notifies the feature base/logging that the action {@link APP_WILL_MOUNT} is
53 * being dispatched within a specific Redux {@code store}.
54 *
55 * @param {Store} store - The Redux store in which the specified {@code action}
56 * is being dispatched.
57 * @param {Dispatch} next - The Redux {@code dispatch} function to dispatch the
58 * specified {@code action} to the specified {@code store}.
59 * @param {Action} action - The Redux action {@code APP_WILL_MOUNT} which is
60 * being dispatched in the specified {@code store}.
61 * @private
62 * @returns {Object} The new state that is the result of the reduction of the
63 * specified {@code action}.
64 */
65 function _appWillMount({ getState }: IStore, next: Function, action: AnyAction) {
66 const { config } = getState()['features/base/logging'];
67
68 _setLogLevels(Logger, config);
69
70 // FIXME Until the logic of conference.js is rewritten into the React
71 // app we, JitsiMeetJS.init is to not be used for the React app.
72 // Consequently, LIB_WILL_INIT will not be dispatched. In the meantime, do
73 // the following:
74 typeof APP === 'undefined' || _setLogLevels(JitsiMeetJS, config);
75
76 return next(action);
77 }
78
79 /**
80 * Starts the log collector, after {@link CONFERENCE_JOINED} action is reduced.
81 *
82 * @param {Store} store - The Redux store in which the specified {@code action}
83 * is being dispatched.
84 * @param {Dispatch} next - The Redux {@code dispatch} function to dispatch the
85 * specified {@code action} to the specified {@code store}.
86 * @param {Action} action - The Redux action {@code CONFERENCE_JOINED} which is
87 * being dispatched in the specified {@code store}.
88 * @private
89 * @returns {*}
90 */
91 function _conferenceJoined({ getState }: IStore, next: Function, action: AnyAction) {
92
93 // Wait until the joined event is processed, so that the JitsiMeetLogStorage
94 // will be ready.
95 const result = next(action);
96
97 const { conference } = action;
98 const { logCollector } = getState()['features/base/logging'];
99
100 if (logCollector && conference === getCurrentConference(getState())) {
101 // Start the LogCollector's periodic "store logs" task
102 logCollector.start();
103
104 // Make an attempt to flush in case a lot of logs have been cached,
105 // before the collector was started.
106 logCollector.flush();
107
108 // This event listener will flush the logs, before the statistics module
109 // (CallStats) is stopped.
110 //
111 // NOTE The LogCollector is not stopped, because this event can be
112 // triggered multiple times during single conference (whenever
113 // statistics module is stopped). That includes the case when Jicofo
114 // terminates a single person conference (one person left in the room
115 // waiting for someone to join). It will then restart the media session
116 // when someone eventually joins the room which will start the stats
117 // again.
118 conference.on(
119 JitsiConferenceEvents.BEFORE_STATISTICS_DISPOSED,
120 () => logCollector.flush()
121 );
122 }
123
124 return result;
125 }
126
127 /**
128 * Initializes logging in the app.
129 *
130 * @param {Store} store - The Redux store in which context the logging is to be
131 * initialized.
132 * @param {Object} loggingConfig - The configuration with which logging is to be
133 * initialized.
134 * @param {boolean} isTestingEnabled - Is debug logging enabled.
135 * @private
136 * @returns {void}
137 */
138 function _initLogging({ dispatch, getState }: IStore,
139 loggingConfig: any, isTestingEnabled: boolean) {
140 const { logCollector } = getState()['features/base/logging'];
141
142 // Create the LogCollector and register it as the global log transport. It
143 // is done early to capture as much logs as possible. Captured logs will be
144 // cached, before the JitsiMeetLogStorage gets ready (statistics module is
145 // initialized).
146 if (!logCollector && !loggingConfig.disableLogCollector) {
147 const _logCollector = new Logger.LogCollector(new JitsiMeetLogStorage(getState));
148
149 const { apiLogLevels } = getState()['features/base/config'];
150
151 if (apiLogLevels && Array.isArray(apiLogLevels) && typeof APP === 'object') {
152 const transport = buildExternalApiLogTransport(apiLogLevels);
153
154 Logger.addGlobalTransport(transport);
155 JitsiMeetJS.addGlobalLogTransport(transport);
156 }
157
158 Logger.addGlobalTransport(_logCollector);
159 JitsiMeetJS.addGlobalLogTransport(_logCollector);
160 dispatch(setLogCollector(_logCollector));
161
162 // The JitsiMeetInMemoryLogStorage can not be accessed on mobile through
163 // the 'executeScript' method like it's done in torture tests for WEB.
164 if (isTestingEnabled && typeof APP === 'object') {
165 APP.debugLogs = new JitsiMeetInMemoryLogStorage();
166 const debugLogCollector = new Logger.LogCollector(
167 APP.debugLogs, { storeInterval: 1000 });
168
169 Logger.addGlobalTransport(debugLogCollector);
170 JitsiMeetJS.addGlobalLogTransport(debugLogCollector);
171 debugLogCollector.start();
172 }
173 } else if (logCollector && loggingConfig.disableLogCollector) {
174 Logger.removeGlobalTransport(logCollector);
175 JitsiMeetJS.removeGlobalLogTransport(logCollector);
176 logCollector.stop();
177 dispatch(setLogCollector(undefined));
178 }
179 }
180
181 /**
182 * Notifies the feature base/logging that the action {@link LIB_WILL_INIT} is
183 * being dispatched within a specific Redux {@code store}.
184 *
185 * @param {Store} store - The Redux store in which the specified {@code action}
186 * is being dispatched.
187 * @param {Dispatch} next - The Redux {@code dispatch} function to dispatch the
188 * specified {@code action} to the specified {@code store}.
189 * @param {Action} action - The Redux action {@code LIB_WILL_INIT} which is
190 * being dispatched in the specified {@code store}.
191 * @private
192 * @returns {Object} The new state that is the result of the reduction of the
193 * specified {@code action}.
194 */
195 function _libWillInit({ getState }: IStore, next: Function, action: AnyAction) {
196 // Adding the if in order to preserve the logic for web after enabling
197 // LIB_WILL_INIT action for web in initLib action.
198 if (typeof APP === 'undefined') {
199 _setLogLevels(JitsiMeetJS, getState()['features/base/logging'].config);
200 }
201
202 return next(action);
203 }
204
205 /**
206 * This feature that the action SET_CONFIG is being
207 * dispatched within a specific Redux store.
208 *
209 * @param {Store} store - The Redux store in which the specified action is being
210 * dispatched.
211 * @param {Dispatch} next - The Redux dispatch function to dispatch the
212 * specified action to the specified store.
213 * @param {Action} action - The Redux action SET_CONFIG which is being
214 * dispatched in the specified store.
215 * @private
216 * @returns {Object} The new state that is the result of the reduction of the
217 * specified action.
218 */
219 function _setConfig({ dispatch }: IStore, next: Function, action: AnyAction) {
220 const result = next(action);
221
222 dispatch(setLoggingConfig(action.config?.logging));
223
224 return result;
225 }
226
227 /**
228 * Notifies the feature base/logging that the action {@link SET_LOGGING_CONFIG}
229 * is being dispatched within a specific Redux {@code store}.
230 *
231 * @param {Store} store - The Redux store in which the specified {@code action}
232 * is being dispatched.
233 * @param {Dispatch} next - The Redux {@code dispatch} function to dispatch the
234 * specified {@code action} to the specified {@code store}.
235 * @param {Action} action - The Redux action {@code SET_LOGGING_CONFIG} which is
236 * being dispatched in the specified {@code store}.
237 * @private
238 * @returns {Object} The new state that is the result of the reduction of the
239 * specified {@code action}.
240 */
241 function _setLoggingConfig({ dispatch, getState }: IStore,
242 next: Function, action: AnyAction) {
243 const result = next(action);
244 const newValue = getState()['features/base/logging'].config;
245 const isTestingEnabled = isTestModeEnabled(getState());
246
247 // TODO Generally, we'll want to _setLogLevels and _initLogging only if the
248 // logging config values actually change.
249 // XXX Unfortunately, we don't currently have a (nice) way of determining
250 // whether _setLogLevels or _initLogging have been invoked so we have to
251 // invoke them unconditionally even if none of the values have actually
252 // changed.
253 _setLogLevels(Logger, newValue);
254 _setLogLevels(JitsiMeetJS, newValue);
255
256 _initLogging({
257 dispatch,
258 getState
259 }, newValue, isTestingEnabled);
260
261 return result;
262 }
263
264 /**
265 * Sets the log levels of {@link Logger} or {@link JitsiMeetJS} in accord with
266 * a specific configuration.
267 *
268 * @param {Object} logger - The object on which the log levels are to be set.
269 * @param {Object} config - The configuration specifying the log levels to be
270 * set on {@code Logger} or {@code JitsiMeetJS}.
271 * @private
272 * @returns {void}
273 */
274 function _setLogLevels(logger: any, config: any) {
275 // XXX The loggers of the library lib-jitsi-meet and the application
276 // jitsi-meet are separate, so the log levels have to be set in both.
277
278 // First, set the default log level.
279 logger.setLogLevel(config.defaultLogLevel);
280
281 // Second, set the log level of each logger explicitly overridden by config.
282 for (const [ id, level ] of Object.entries(config.loggers)) {
283 logger.setLogLevelById(level, id);
284 }
285 }