From f6e09ca687d9344d48c8beee48b1bda63612da2f Mon Sep 17 00:00:00 2001 From: hensm Date: Thu, 23 Jan 2020 01:14:57 +0000 Subject: [PATCH] Replace remaining console calls with logger calls --- ext/src/background/background.ts | 14 +++++----- .../NativeReceiverSelector.ts | 4 +-- .../ReceiverSelectorManager.ts | 10 +++---- ext/src/lib/bridge.ts | 6 ++--- ext/src/lib/logger.ts | 27 ++++++++++++++----- ext/src/lib/nativeMessaging.ts | 4 ++- .../senders/media/overlay/overlayContent.ts | 6 +++-- ext/src/ui/options/index.tsx | 2 +- 8 files changed, 46 insertions(+), 27 deletions(-) diff --git a/ext/src/background/background.ts b/ext/src/background/background.ts index b84eebf..1e7a60a 100755 --- a/ext/src/background/background.ts +++ b/ext/src/background/background.ts @@ -45,7 +45,7 @@ browser.runtime.onInstalled.addListener(async details => { function initBrowserAction () { - console.info("fx_cast (Debug): init (browser action)"); + logger.info("init (browser action)"); /*browser.browserAction.disable(); @@ -86,7 +86,7 @@ function initBrowserAction () { async function initMenus () { - console.info("fx_cast (Debug): init (menus)"); + logger.info("init (menus)"); const URL_PATTERN_HTTP = "http://*/*"; const URL_PATTERN_HTTPS = "https://*/*"; @@ -413,7 +413,7 @@ async function initMenus () { async function initRequestListener () { - console.info("fx_cast (Debug): init (request listener)"); + logger.info("init (request listener)"); type OnBeforeRequestDetails = Parameters[0]>[0]; @@ -457,7 +457,7 @@ async function initRequestListener () { function initWhitelist () { - console.info("fx_cast (Debug): init (whitelist)"); + logger.info("init (whitelist)"); type OnBeforeSendHeadersDetails = Parameters[0]>[0]; @@ -536,7 +536,7 @@ function initWhitelist () { async function initMediaOverlay () { - console.info("fx_cast (Debug): init (media overlay)"); + logger.info("init (media overlay)"); let contentScript: browser.contentScripts.RegisteredContentScript; @@ -553,7 +553,7 @@ async function initMediaOverlay () { , runAt: "document_start" }); } catch (err) { - console.error("fx_cast (Debug): Failed to register media overlay"); + logger.error("Failed to register media overlay") } } @@ -591,7 +591,7 @@ async function init () { return; } - console.info("fx_cast (Debug): init"); + logger.info("init"); isInitialized = true; diff --git a/ext/src/background/receiverSelector/NativeReceiverSelector.ts b/ext/src/background/receiverSelector/NativeReceiverSelector.ts index 821632c..0fa6da1 100644 --- a/ext/src/background/receiverSelector/NativeReceiverSelector.ts +++ b/ext/src/background/receiverSelector/NativeReceiverSelector.ts @@ -2,6 +2,7 @@ import bridge from "../../lib/bridge"; import knownApps from "../../lib/knownApps"; +import logger from "../../lib/logger"; import options from "../../lib/options"; import { TypedEventTarget } from "../../lib/typedEvents"; @@ -145,8 +146,7 @@ export default class NativeReceiverSelector private async onBridgePortMessageError ( message: NativeReceiverSelectorErrorMessage) { - console.error("fx_cast (Debug): Native receiver selector error" - , message.data); + logger.error("Native receiver selector error", message.data); this.dispatchEvent(new CustomEvent("error")); } diff --git a/ext/src/background/receiverSelector/ReceiverSelectorManager.ts b/ext/src/background/receiverSelector/ReceiverSelectorManager.ts index 2896557..4b87b25 100644 --- a/ext/src/background/receiverSelector/ReceiverSelectorManager.ts +++ b/ext/src/background/receiverSelector/ReceiverSelectorManager.ts @@ -90,7 +90,7 @@ async function getSelection ( availableMediaTypes = getMediaTypesForPageUrl(url); } catch (err) { - console.error("fx_cast (Debug): Failed to locate frame"); + logger.error("Failed to locate frame"); reject(); return; } @@ -136,25 +136,25 @@ async function getSelection ( } function onSelected (ev: any) { - console.info("fx_cast (Debug): Selected receiver", ev.detail); + logger.info("Selected receiver", ev.detail); resolve(ev.detail); removeListeners(); } function onCancelled () { - console.info("fx_cast (Debug): Cancelled receiver selection"); + logger.info("Cancelled receiver selection"); resolve(null); removeListeners(); } function onError () { - console.error("fx_cast (Debug): Failed to select receiver"); + logger.error("Failed to select receiver"); reject(); removeListeners(); } function onStop (ev: any) { - console.info("fx_cast (Debug): Stopped receiver app", ev.detail); + logger.info("Stopped receiver app", ev.detail); StatusManager.init().then(() => { StatusManager.stopReceiverApp(ev.detail.receiver); diff --git a/ext/src/lib/bridge.ts b/ext/src/lib/bridge.ts index c33294d..58086af 100644 --- a/ext/src/lib/bridge.ts +++ b/ext/src/lib/bridge.ts @@ -69,10 +69,10 @@ async function getInfo (): Promise { // Print compatibility info to console if (!isVersionCompatible) { - console.error(`Expecting ${applicationName} v${APPLICATION_VERSION}, found v${applicationVersion}.` - , isVersionOlder + logger.error(`Expecting ${applicationName} v${APPLICATION_VERSION}, found v${applicationVersion}. ${ + isVersionOlder ? "Try updating the native app to the latest version." - : "Try updating the extension to the latest version"); + : "Try updating the extension to the latest version"}`); } return { diff --git a/ext/src/lib/logger.ts b/ext/src/lib/logger.ts index bf2cdcd..98c13aa 100644 --- a/ext/src/lib/logger.ts +++ b/ext/src/lib/logger.ts @@ -3,15 +3,30 @@ export class Logger { constructor (private prefix: string) {} - log (message: string) { - console.log(`${this.prefix} (Log): ${message}`); + log (message: string, data?: any) { + const formattedMessage = `${this.prefix} (Log): ${message}`; + if (data) { + console.log(formattedMessage, data); + } else { + console.log(formattedMessage); + } } - debug (message: string) { - console.debug(`${this.prefix} (Debug): ${message}`); + info (message: string, data?: any) { + const formattedMessage = `${this.prefix} (Info): ${message}`; + if (data) { + console.info(formattedMessage, data); + } else { + console.info(formattedMessage); + } } - error (message: string) { + error (message: string, data?: any) { const formattedMessage = `${this.prefix} (Error): ${message}`; - console.error(formattedMessage); + if (data) { + console.error(formattedMessage, data); + } else { + console.error(formattedMessage); + } + return new Error(formattedMessage); } } diff --git a/ext/src/lib/nativeMessaging.ts b/ext/src/lib/nativeMessaging.ts index b8cc38b..ff26521 100644 --- a/ext/src/lib/nativeMessaging.ts +++ b/ext/src/lib/nativeMessaging.ts @@ -1,5 +1,7 @@ "use strict"; +import logger from "./logger"; + const WEBSOCKET_DAEMON_URL = "ws://localhost:9556"; @@ -164,7 +166,7 @@ async function sendNativeMessage ( }); ws.addEventListener("error", () => { - console.error("fx_cast (Debug): No bridge application found."); + logger.error("No bridge application found."); reject(); }); }); diff --git a/ext/src/senders/media/overlay/overlayContent.ts b/ext/src/senders/media/overlay/overlayContent.ts index 096256e..604ca05 100644 --- a/ext/src/senders/media/overlay/overlayContent.ts +++ b/ext/src/senders/media/overlay/overlayContent.ts @@ -1,5 +1,7 @@ "use strict"; +import logger from "../../../lib/logger"; + import { bindPropertyDescriptor , clonePropsDescriptor , getPropertyDescriptor @@ -299,7 +301,7 @@ function wrapMediaElement (mediaElement: HTMLMediaElement) { const shadowRoot = internalShadowRoots.get(wrappedMedia); if (!shadowRoot) { - console.error("err: Failed to fetch shadow root!"); + logger.error("Failed to fetch shadow root!"); return; } @@ -329,7 +331,7 @@ function wrapMediaElement (mediaElement: HTMLMediaElement) { for (const source of mediaElement.getElementsByTagName("source")) { const internalMedia = shadowRoot.querySelector("audio,video"); if (!internalMedia) { - console.error("err: Failed to fetch internal video element!"); + logger.error("Failed to fetch internal video element!"); return; } diff --git a/ext/src/ui/options/index.tsx b/ext/src/ui/options/index.tsx index 17019a6..e9248c3 100644 --- a/ext/src/ui/options/index.tsx +++ b/ext/src/ui/options/index.tsx @@ -422,7 +422,7 @@ class OptionsApp extends Component<{}, OptionsAppState> { }); } } catch (err) { - console.error("Failed to save options"); + logger.error("Failed to save options"); } }