Skip to content

Commit

Permalink
wip - hook on debug messages
Browse files Browse the repository at this point in the history
  • Loading branch information
lkostrowski committed Mar 8, 2024
1 parent 3bba0f1 commit 265d2e2
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
17 changes: 17 additions & 0 deletions src/debug.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { describe, expect,it , vi} from "vitest";

import { createDebug, registerDebugLogger } from "./debug";

describe("Debug", () => {
it("Registers hook on log creation", () =>{
const mock = vi.fn()

registerDebugLogger((args) => mock(args))
const debug= createDebug("foo");

debug.log("message %s", "injected");

expect(mock).toHaveBeenCalledWith("")

})
})
22 changes: 21 additions & 1 deletion src/debug.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@
import debugPkg from "debug";

export const createDebug = (namespace: string) => debugPkg.debug(`app-sdk:${namespace}`);
const debugObservables = new Set<Function>();

export const registerDebugLogger = (loggerHook: (message: string) => void) => {
debugObservables.add(loggerHook);

return () => debugObservables.delete(loggerHook);
};

export const createDebug = (namespace: string) => {
const debugInstance = debugPkg.debug(`app-sdk:${namespace}`);
console.log(debugInstance)

const originalDebugLog = debugInstance.log.bind(debugInstance);

debugInstance.log = (args) => {
const log = originalDebugLog(args);
debugObservables.forEach((d) => d(log as string));
};

return debugInstance
};
1 change: 1 addition & 0 deletions src/get-app-id.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { createDebug } from "./debug";

const debug = createDebug("getAppId");


type GetIdResponseType = {
data?: {
app?: {
Expand Down

0 comments on commit 265d2e2

Please sign in to comment.