From 00ecc68009146a6e4a9cb747f2ca19f8226ae9e0 Mon Sep 17 00:00:00 2001 From: Daniel Blanco Parla Date: Wed, 19 Apr 2023 15:57:18 +0200 Subject: [PATCH] feat: debugging logs --- README.md | 7 +++++++ package.json | 4 +++- src/EventEmitterP2P.ts | 10 ++++++++-- src/logger.ts | 14 ++++++++++++++ yarn.lock | 12 ++++++++++++ 5 files changed, 44 insertions(+), 3 deletions(-) create mode 100644 src/logger.ts diff --git a/README.md b/README.md index 0a765f7..6c89c3a 100644 --- a/README.md +++ b/README.md @@ -22,6 +22,13 @@ ev.emit('test', 'test-1') For further information, check it's [EventEmitter interface](src/IEventEmitter.ts) +For printing the debugging logs, the `DEBUG=p2p:*` env flag is required. + +```sh +$ DEBUG=p2p:* node index.js +$ DEBUG=p2p:* yarn test # to see an example of the outputs +``` + ## License This library is licensed under the MIT License. See the [LICENSE](LICENSE) file for more information. diff --git a/package.json b/package.json index d21c968..6f9cd73 100644 --- a/package.json +++ b/package.json @@ -17,6 +17,7 @@ "@commitlint/config-conventional": "^17.4.4", "@esbuild-plugins/node-globals-polyfill": "^0.2.3", "@esbuild-plugins/node-modules-polyfill": "^0.2.2", + "@types/debug": "^4.1.7", "@types/events": "^3.0.0", "@types/jest": "^29.5.0", "@types/node": "^18.15.11", @@ -38,10 +39,11 @@ "@libp2p/mplex": "^7.1.3", "@libp2p/pubsub-peer-discovery": "^8.0.2", "@libp2p/tcp": "^6.2.2", + "debug": "^4.3.4", "libp2p": "^0.44.0", "uint8arrays": "^4.0.3" }, "publishConfig": { "registry": "https://npm.pkg.github.com" } -} \ No newline at end of file +} diff --git a/src/EventEmitterP2P.ts b/src/EventEmitterP2P.ts index 5f9ea86..0ba46e2 100644 --- a/src/EventEmitterP2P.ts +++ b/src/EventEmitterP2P.ts @@ -2,6 +2,7 @@ import LibP2P, { Libp2p } from 'libp2p' import { Disposable, IEventEmitter, Listener } from './IEventEmitter' import { getNode } from './node' import { fromString as uint8ArrayFromString } from 'uint8arrays' +import { error, info } from './logger' type P2POptions = { overridedOptions: LibP2P.Libp2pOptions; bootstrapList?: string[] } @@ -20,6 +21,7 @@ export class EventEmitterP2P implements IEventEmitter { async initialize(p2pOpt?: P2POptions) { this.p2pnode = await getNode(p2pOpt?.overridedOptions, p2pOpt?.bootstrapList).then((node) => { node.pubsub.addEventListener('message', this._onMessage.bind(this)) + info('P2P node initialized') return node }) } @@ -51,10 +53,12 @@ export class EventEmitterP2P implements IEventEmitter { emit = async (topic: T, event: any) => { if (!this.p2pnode) { + error('P2P node not initialized') throw new Error('P2P node not initialized') } const data = uint8ArrayFromString(JSON.stringify(event)) - this.p2pnode.pubsub.publish(topic, data) + info('Publishing message on topic %s: %s', topic, JSON.stringify(event)) + return this.p2pnode.pubsub.publish(topic, data) } pipe = (topic: T, te: IEventEmitter): Disposable => { @@ -63,6 +67,7 @@ export class EventEmitterP2P implements IEventEmitter { _subscribe = (topic: string) => { if (!this.p2pnode) { + error('P2P node not initialized') throw new Error('P2P node not initialized') } const currentTopics = this.p2pnode.pubsub.getTopics() @@ -75,6 +80,7 @@ export class EventEmitterP2P implements IEventEmitter { try { const topic = msg.detail.topic as T const data = msg.detail.data.toString() + info('Received message on topic %s: %s', topic, data) if (this.listeners[topic]) { ;(this.listeners[topic] ?? []).forEach((listener) => listener(topic, data)) } @@ -85,7 +91,7 @@ export class EventEmitterP2P implements IEventEmitter { toCall.forEach((listener) => listener(topic, data)) } } catch (e) { - console.error('🚀 ~ file: EventEmitterP2P.ts:87 ~ EventEmitterP2P=13.7.0", "@types/node@^18.15.11": version "18.15.11" resolved "https://registry.yarnpkg.com/@types/node/-/node-18.15.11.tgz#b3b790f09cb1696cffcec605de025b088fa4225f"