Skip to content

Commit

Permalink
Add simple facade for logging (#164)
Browse files Browse the repository at this point in the history
  • Loading branch information
bingenito authored May 29, 2018
1 parent b6cc60f commit 40a6f72
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 10 deletions.
4 changes: 2 additions & 2 deletions examples/electron/electron.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
{
Expand All @@ -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);
});
}

Expand Down
16 changes: 8 additions & 8 deletions examples/web/assets/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
6 changes: 6 additions & 0 deletions src/OpenFin/openfin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,12 @@ export class OpenFinContainer extends WebContainerBase {
}
}

public log(level: "debug" | "info" | "warn" | "error", message: string): Promise<void> {
return new Promise<void>((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());
Expand Down
33 changes: 33 additions & 0 deletions src/container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> {
return new Promise<void>(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();
});
}
}

/**
Expand Down
26 changes: 26 additions & 0 deletions tests/unit/OpenFin/openfin.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
26 changes: 26 additions & 0 deletions tests/unit/container.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand Down

0 comments on commit 40a6f72

Please sign in to comment.