From 36bcc6831b5eb2acdac3d56c6594d803fb523bc0 Mon Sep 17 00:00:00 2001 From: paweldomas Date: Tue, 22 Nov 2016 14:51:25 -0600 Subject: [PATCH] feat: use LogCollector to capture JS console logs --- app.js | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++- conference.js | 9 +++++++ 2 files changed, 74 insertions(+), 1 deletion(-) diff --git a/app.js b/app.js index 7996f208e..7b48bddce 100644 --- a/app.js +++ b/app.js @@ -20,6 +20,7 @@ import 'aui-experimental-css'; window.toastr = require("toastr"); const Logger = require("jitsi-meet-logger"); +const LogCollector = Logger.LogCollector; import URLProcessor from "./modules/config/URLProcessor"; import RoomnameGenerator from './modules/util/RoomnameGenerator'; @@ -34,6 +35,7 @@ import UIEvents from './service/UI/UIEvents'; import getTokenData from "./modules/tokendata/TokenData"; import translation from "./modules/translation/translation"; +const ConferenceEvents = JitsiMeetJS.events.conference; /** * Tries to push history state with the following parameters: @@ -131,6 +133,16 @@ const APP = { settings, conference, translation, + /** + * The log collector which captures JS console logs for this app. + * @type {LogCollector} + */ + logCollector: null, + /** + * Indicates if the log collector has been started (it will not be started + * if the welcome page is displayed). + */ + logCollectorStarted : false, /** * After the APP has been initialized provides utility methods for dealing * with the conference room URL(address). @@ -140,11 +152,40 @@ const APP = { connection: null, API, init () { - configureLoggingLevels(); + this.initLogging(); this.keyboardshortcut = require("./modules/keyboardshortcut/keyboardshortcut"); this.configFetch = require("./modules/config/HttpConfigFetch"); this.tokenData = getTokenData(); + }, + initLogging () { + // Adjust logging level + configureLoggingLevels(); + // Start the LogCollector and register it as the global log transport + if (!this.logCollector) { + this.logCollector = new LogCollector({ + storeLogs: (logJSON) => { + // Try catch was used, because there are many variables + // on the way that could be uninitialized if the storeLogs + // attempt would be made very early (which is unlikely) + try { + // Currently it makes sense to store the log only + // if CallStats is enabled + if (APP.logCollectorStarted + && APP.conference + && APP.conference.isCallstatsEnabled()) { + APP.conference.logJSON(logJSON); + } + } catch (error) { + // NOTE console is intentional here + console.error( + "Failed to store the logs: ", logJSON, error); + } + } + }); + Logger.addGlobalTransport(this.logCollector); + JitsiMeetJS.addGlobalLogTransport(this.logCollector); + } } }; @@ -168,7 +209,25 @@ function init() { replaceHistoryState(APP.ConferenceUrl.getInviteUrl()); var isUIReady = APP.UI.start(); if (isUIReady) { + // Start the LogCollector's periodic "store logs" task only if we're in + // the conference and not on the welcome page. + APP.logCollector.start(); + APP.logCollectorStarted = true; + APP.conference.init({roomName: buildRoomName()}).then(function () { + + // Will flush the logs, before the stats are disposed + if (APP.logCollector) { + APP.conference.addConferenceListener( + ConferenceEvents.BEFORE_STATISTICS_DISPOSED, + () => { + if (APP.logCollector) { + APP.logCollector.flush(); + } + } + ); + } + APP.UI.initConference(); APP.UI.addListener(UIEvents.LANG_CHANGED, function (language) { @@ -237,6 +296,11 @@ $(document).ready(function () { }); $(window).bind('beforeunload', function () { + // Stop the LogCollector + if (APP.logCollectorStarted) { + APP.logCollector.stop(); + APP.logCollectorStarted = false; + } APP.API.dispose(); }); diff --git a/conference.js b/conference.js index 992d0ef35..648bcb9ec 100644 --- a/conference.js +++ b/conference.js @@ -1743,6 +1743,15 @@ export default { room.sendApplicationLog(JSON.stringify({name, value})); } }, + /** + * Methods logs an application event given in the JSON format. + * @param {string} logJSON an event to be logged in JSON format + */ + logJSON(logJSON) { + if (room) { + room.sendApplicationLog(logJSON); + } + }, /** * Disconnect from the conference and optionally request user feedback. * @param {boolean} [requestFeedback=false] if user feedback should be