Skip to content

Commit

Permalink
feat: tslog for packages/network, pulling the config from node packag…
Browse files Browse the repository at this point in the history
…e not working
  • Loading branch information
JanLewDev committed Aug 7, 2024
1 parent 3aff50c commit a5f76ba
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 41 deletions.
3 changes: 2 additions & 1 deletion packages/network/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"@libp2p/webtransport": "^4.1.2",
"@multiformats/multiaddr": "^12.3.0",
"it-pipe": "^3.0.1",
"libp2p": "^1.8.2"
"libp2p": "^1.8.2",
"tslog": "^4.9.3"
}
}
73 changes: 37 additions & 36 deletions packages/network/src/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,27 @@ import { webTransport } from "@libp2p/webtransport";
import { autoNAT } from "@libp2p/autonat";
import { fromString as uint8ArrayFromString } from "uint8arrays/from-string";

import { Logger } from "tslog";
// import * as fs from 'fs';
// import * as path from 'path';

// const readJson = (filePath: string) => {
// return JSON.parse(fs.readFileSync(filePath, 'utf-8'));
// };

// const nodePackagePath = path.resolve(__dirname, '../node/package.json');
// const nodePackageOptions = readJson(nodePackagePath);
// const { loggerOptions } = nodePackageOptions;

// const mainLogger = new Logger({ name: "topology:network", ...loggerOptions}); // 3 = INFO
const mainLogger = new Logger({ name: "topology:network", type: "pretty", minLevel: 3 }); // 3 = INFO
const startLogger = mainLogger.getSubLogger({ name: "start" });
const subscribeLogger = mainLogger.getSubLogger({ name: "subscribe" });
const unsubscribeLogger = mainLogger.getSubLogger({ name: "unsubscribe" });
const broadcastMessageLogger = mainLogger.getSubLogger({ name: "broadcastMessage" });
const sendMessageLogger = mainLogger.getSubLogger({ name: "sendMessage" });
const sendGroupMessageRandomPeerLogger = mainLogger.getSubLogger({ name: "sendGroupMessageRandomTopicPeer" });

// snake_casing to match the JSON config
export interface TopologyNetworkNodeConfig {
addresses?: string[];
Expand Down Expand Up @@ -115,60 +136,47 @@ export class TopologyNetworkNode {
this._pubsub = this._node.services.pubsub as PubSub<GossipsubEvents>;
this.peerId = this._node.peerId.toString();

console.log(
"topology::network::start: Successfuly started topology network w/ peer_id",
this.peerId,
);
startLogger.info("Successfuly started topology network w/ peer_id", this.peerId);

// TODO remove this or add better logger
// we need to keep it now for debugging
this._node.addEventListener("peer:connect", (e) =>
console.log("peer:connect", e.detail),
startLogger.info("peer:connect", e.detail)
);
this._node.addEventListener("peer:discovery", (e) =>
console.log("peer:discovery", e.detail),
startLogger.info("peer:discovery", e.detail)
);
this._node.addEventListener("peer:identify", (e) =>
console.log("peer:identify", e.detail),
startLogger.info("peer:identify", e.detail)
);
}

subscribe(topic: string) {
if (!this._node) {
console.error(
"topology::network::subscribe: Node not initialized, please run .start()",
);
subscribeLogger.error("Node not initialized, please run .start()");
return;
}

try {
this._pubsub?.subscribe(topic);
this._pubsub?.getPeers();
console.log(
"topology::network::subscribe: Successfuly subscribed the topic",
topic,
);
subscribeLogger.info("Successfuly subscribed the topic", topic);
} catch (e) {
console.error("topology::network::subscribe:", e);
subscribeLogger.error(e);
}
}

unsubscribe(topic: string) {
if (!this._node) {
console.error(
"topology::network::unsubscribe: Node not initialized, please run .start()",
);
unsubscribeLogger.error("Node not initialized, please run .start()");
return;
}

try {
this._pubsub?.unsubscribe(topic);
console.log(
"topology::network::unsubscribe: Successfuly unsubscribed the topic",
topic,
);
unsubscribeLogger.info("Successfuly unsubscribed the topic", topic);
} catch (e) {
console.error("topology::network::unsubscribe:", e);
unsubscribeLogger.error(e);
}
}

Expand All @@ -189,12 +197,9 @@ export class TopologyNetworkNode {
if (this._pubsub?.getSubscribers(topic)?.length === 0) return;
await this._pubsub?.publish(topic, message);

console.log(
"topology::network::broadcastMessage: Successfuly broadcasted message to topic",
topic,
);
broadcastMessageLogger.info("Successfuly broadcasted message to topic", topic);
} catch (e) {
console.error("topology::network::broadcastMessage:", e);
broadcastMessageLogger.error(e);
}
}

Expand All @@ -204,11 +209,9 @@ export class TopologyNetworkNode {
const stream = <Stream>await connection?.newStream(protocols);
stringToStream(stream, message);

console.log(
`topology::network::sendMessage: Successfuly sent message to peer: ${peerId} with message: ${message}`,
);
sendMessageLogger.info("Successfuly sent message to peer", peerId);
} catch (e) {
console.error("topology::network::sendMessage:", e);
sendMessageLogger.error(e);
}
}

Expand All @@ -226,11 +229,9 @@ export class TopologyNetworkNode {
const stream: Stream = (await connection?.newStream(protocols)) as Stream;
stringToStream(stream, message);

console.log(
`topology::network::sendMessageRandomTopicPeer: Successfuly sent message to peer: ${peerId} with message: ${message}`,
);
sendGroupMessageRandomPeerLogger.info(`Successfuly sent message to peer: ${peerId} with message: ${message}`);
} catch (e) {
console.error("topology::network::sendMessageRandomTopicPeer:", e);
sendGroupMessageRandomPeerLogger.error(e);
}
}

Expand Down
3 changes: 2 additions & 1 deletion packages/network/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"outDir": "dist"
"outDir": "dist",
"inlineSourceMap": true
},
"include": ["src/**/*.ts"]
}
7 changes: 6 additions & 1 deletion packages/node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@
"@topology-foundation/crdt": "0.0.23-5",
"@topology-foundation/network": "0.0.23-5",
"@topology-foundation/object": "0.0.23-5",
"commander": "^12.1.0"
"commander": "^12.1.0",
"tslog": "^4.9.3"
},
"loggerOptions": {
"minLevel": "info",
"type": "pretty"
}
}
2 changes: 1 addition & 1 deletion packages/node/src/version.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export const VERSION = "0.0.22";
export const VERSION = "0.0.23-5";
3 changes: 2 additions & 1 deletion packages/node/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"outDir": "dist"
"outDir": "dist",
"inlineSourceMap": true
},
"references": [
{
Expand Down

0 comments on commit a5f76ba

Please sign in to comment.