From 80855eceb2e96a3b3531c56b7ca65053aff709f1 Mon Sep 17 00:00:00 2001 From: bingenito Date: Tue, 29 May 2018 13:10:32 -0400 Subject: [PATCH] Add simple facade for logging --- examples/electron/electron.js | 4 ++-- examples/web/assets/js/app.js | 16 +++++++-------- src/OpenFin/openfin.ts | 6 ++++++ src/container.ts | 33 ++++++++++++++++++++++++++++++ tests/unit/OpenFin/openfin.spec.ts | 26 +++++++++++++++++++++++ tests/unit/container.spec.ts | 26 +++++++++++++++++++++++ 6 files changed, 101 insertions(+), 10 deletions(-) diff --git a/examples/electron/electron.js b/examples/electron/electron.js index ad2e83cb..34d40061 100644 --- a/examples/electron/electron.js +++ b/examples/electron/electron.js @@ -8,7 +8,7 @@ let snapAssist; function createWindow() { let container = desktopJS.resolveContainer(); - desktopJS.ContainerWindow.addListener("window-created", (e) => console.log("Window created - static (ContainerWindow): " + e.windowId + ", " + e.windowName)); + desktopJS.ContainerWindow.addListener("window-created", (e) => container.log("info", "Window created - static (ContainerWindow): " + e.windowId + ", " + e.windowName)); snapAssist = new desktopJS.SnapAssistWindowManager(container, { @@ -29,7 +29,7 @@ function createWindow() { }, [{ label: "Exit", click: (menuItem) => app.quit() }]); container.ipc.subscribe("stock.selected", function (event, message) { - console.log("Message received: " + message.symbol); + container.log("info", "Message received: " + message.symbol); }); } diff --git a/examples/web/assets/js/app.js b/examples/web/assets/js/app.js index 177cb8fa..7ec586d8 100644 --- a/examples/web/assets/js/app.js +++ b/examples/web/assets/js/app.js @@ -64,14 +64,14 @@ document.addEventListener("DOMContentLoaded", function (event) { $("#button-joingroup").prop("disabled", !container.getCurrentWindow().allowGrouping); $("#button-leavegroup").prop("disabled", !container.getCurrentWindow().allowGrouping); - container.addListener("window-created", (e) => console.log("Window created: " + e.window + ", " + e.windowId + ", " + e.windowName)); - container.addListener("layout-loaded", (e) => console.log("Layout loaded")); - container.addListener("layout-saved", (e) => console.log("Layout saved")); - - desktopJS.Container.addListener("window-created", (e) => console.log("Window created - static (Container): " + e.windowId + ", " + e.windowName)); - desktopJS.ContainerWindow.addListener("window-created", (e) => console.log("Window created - static (ContainerWindow): " + e.windowId + ", " + e.windowName)); - desktopJS.Container.addListener("layout-saved", (e) => console.log("Layout saved - static: " + e.layoutName)); - desktopJS.Container.addListener("layout-loaded", (e) => console.log("Layout loaded - static: " + e.layoutName)); + container.addListener("window-created", (e) => container.log("info", "Window created: " + e.window + ", " + e.windowId + ", " + e.windowName)); + container.addListener("layout-loaded", (e) => container.log("info", "Layout loaded")); + container.addListener("layout-saved", (e) => container.log("info", "Layout saved")); + + desktopJS.Container.addListener("window-created", (e) => container.log("info", "Window created - static (Container): " + e.windowId + ", " + e.windowName)); + desktopJS.ContainerWindow.addListener("window-created", (e) => container.log("info", "Window created - static (ContainerWindow): " + e.windowId + ", " + e.windowName)); + desktopJS.Container.addListener("layout-saved", (e) => container.log("info", "Layout saved - static: " + e.layoutName)); + desktopJS.Container.addListener("layout-loaded", (e) => container.log("info", "Layout loaded - static: " + e.layoutName)); subscribe(); diff --git a/src/OpenFin/openfin.ts b/src/OpenFin/openfin.ts index 0c2e3f80..75ecebbb 100644 --- a/src/OpenFin/openfin.ts +++ b/src/OpenFin/openfin.ts @@ -367,6 +367,12 @@ export class OpenFinContainer extends WebContainerBase { } } + public log(level: "debug" | "info" | "warn" | "error", message: string): Promise { + return new Promise((resolve, reject) => { + this.desktop.System.log(level, message, resolve, reject); + }); + } + public getMainWindow(): ContainerWindow { if (!this.mainWindow) { this.mainWindow = this.wrapWindow(this.desktop.Application.getCurrent().getWindow()); diff --git a/src/container.ts b/src/container.ts index 4f734d5c..5ad54db9 100644 --- a/src/container.ts +++ b/src/container.ts @@ -238,6 +238,39 @@ export abstract class ContainerBase extends Container { resolve(undefined); }); } + + /** + * Write a log message + * @param {"debug" | "info" | "warn" | "error"} level The log level for the entry + * @param {string} message The log message text + */ + public log(level: "debug" | "info" | "warn" | "error", message: string): Promise { + return new Promise(resolve => { + let logger; + switch (level) { + case "debug": { + logger = console.debug; + break; + } + case "warn": { + logger = console.warn; + break; + } + case "error": { + logger = console.error; + break; + } + default: { + logger = console.log; + } + } + + if (logger) { + logger(message); + } + resolve(); + }); + } } /** diff --git a/tests/unit/OpenFin/openfin.spec.ts b/tests/unit/OpenFin/openfin.spec.ts index ae443997..9b281f07 100644 --- a/tests/unit/OpenFin/openfin.spec.ts +++ b/tests/unit/OpenFin/openfin.spec.ts @@ -419,6 +419,32 @@ describe("OpenFinContainer", () => { }); }); + describe("log", () => { + beforeEach(() => { + Object.defineProperty(desktop, "System", { value: jasmine.createSpyObj("System", ["log"]) }); + }); + + it("debug", () => { + container.log("debug", "message"); + expect(desktop.System.log).toHaveBeenCalledWith("debug", "message", jasmine.any(Function), jasmine.any(Function)); + }); + + it("info", () => { + container.log("info", "message"); + expect(desktop.System.log).toHaveBeenCalledWith("info", "message", jasmine.any(Function), jasmine.any(Function)); + }); + + it("warn", () => { + container.log("warn", "message"); + expect(desktop.System.log).toHaveBeenCalledWith("warn", "message", jasmine.any(Function), jasmine.any(Function)); + }); + + it("error", () => { + container.log("error", "message"); + expect(desktop.System.log).toHaveBeenCalledWith("error", "message", jasmine.any(Function), jasmine.any(Function)); + }); + }); + it("getMainWindow returns wrapped inner window", () => { const win: OpenFinContainerWindow = container.getMainWindow(); expect(win).toBeDefined(); diff --git a/tests/unit/container.spec.ts b/tests/unit/container.spec.ts index df486056..df706bbb 100644 --- a/tests/unit/container.spec.ts +++ b/tests/unit/container.spec.ts @@ -107,6 +107,32 @@ describe("container", () => { }); }); + describe("logging", () => { + it("debug", () => { + spyOn(console, "debug").and.stub(); + container.log("debug", "message"); + expect(console.debug).toHaveBeenCalledWith("message"); + }); + + it("warn", () => { + spyOn(console, "warn").and.stub(); + container.log("warn", "message"); + expect(console.warn).toHaveBeenCalledWith("message"); + }); + + it("error", () => { + spyOn(console, "error").and.stub(); + container.log("error", "message"); + expect(console.error).toHaveBeenCalledWith("message"); + }); + + it("info", () => { + spyOn(console, "log").and.stub(); + container.log("info", "message"); + expect(console.log).toHaveBeenCalledWith("message"); + }); + }); + describe("ContainerBase", () => { describe("addTrayIcon", () => { it("Throws Not implemented", () => {