diff options
author | Fabio Berger <me@fabioberger.com> | 2018-12-17 09:46:28 +0800 |
---|---|---|
committer | Fabio Berger <me@fabioberger.com> | 2018-12-17 09:46:28 +0800 |
commit | 5d0e715d9ac9f358c1cdf23c9c96d622e0f1060c (patch) | |
tree | 9b7c5dc2983d40e6332105280ef5182e94609867 | |
parent | 896c8d17c16c4f1e9670ab0747ae8934ce5400a5 (diff) | |
download | dexon-sol-tools-5d0e715d9ac9f358c1cdf23c9c96d622e0f1060c.tar dexon-sol-tools-5d0e715d9ac9f358c1cdf23c9c96d622e0f1060c.tar.gz dexon-sol-tools-5d0e715d9ac9f358c1cdf23c9c96d622e0f1060c.tar.bz2 dexon-sol-tools-5d0e715d9ac9f358c1cdf23c9c96d622e0f1060c.tar.lz dexon-sol-tools-5d0e715d9ac9f358c1cdf23c9c96d622e0f1060c.tar.xz dexon-sol-tools-5d0e715d9ac9f358c1cdf23c9c96d622e0f1060c.tar.zst dexon-sol-tools-5d0e715d9ac9f358c1cdf23c9c96d622e0f1060c.zip |
Add isVerbose option to enable/disable logging
-rw-r--r-- | packages/order-watcher/src/order_watcher/order_watcher_websocket_server.ts | 20 | ||||
-rw-r--r-- | packages/order-watcher/test/order_watcher_websocket_server_test.ts | 9 |
2 files changed, 23 insertions, 6 deletions
diff --git a/packages/order-watcher/src/order_watcher/order_watcher_websocket_server.ts b/packages/order-watcher/src/order_watcher/order_watcher_websocket_server.ts index da5667db3..2e29e775a 100644 --- a/packages/order-watcher/src/order_watcher/order_watcher_websocket_server.ts +++ b/packages/order-watcher/src/order_watcher/order_watcher_websocket_server.ts @@ -22,6 +22,7 @@ export class OrderWatcherWebSocketServer { private readonly _httpServer: http.Server; private readonly _connectionStore: Set<WebSocket.connection>; private readonly _wsServer: WebSocket.server; + private readonly _isVerbose: boolean; /** * Recover types lost when the payload is stringified. */ @@ -47,13 +48,16 @@ export class OrderWatcherWebSocketServer { * @param contractAddresses Optional contract addresses. Defaults to known * addresses based on networkId. * @param partialConfig Optional configurations. + * @param isVerbose Whether to enable verbose logging. Defaults to true. */ constructor( provider: Provider, networkId: number, contractAddresses?: ContractAddresses, + isVerbose: boolean = true, partialConfig?: Partial<OrderWatcherConfig>, ) { + this._isVerbose = isVerbose; this._orderWatcher = new OrderWatcher(provider, networkId, contractAddresses, partialConfig); this._connectionStore = new Set(); this._httpServer = http.createServer(); @@ -72,7 +76,7 @@ export class OrderWatcherWebSocketServer { // Designed for usage pattern where client and server are run on the same // machine by the same user. As such, no security checks are in place. const connection: WebSocket.connection = request.accept(null, request.origin); - logUtils.log(`${new Date()} [Server] Accepted connection from origin ${request.origin}.`); + this._log(`${new Date()} [Server] Accepted connection from origin ${request.origin}.`); connection.on('message', this._onMessageCallbackAsync.bind(this, connection)); connection.on('close', this._onCloseCallback.bind(this, connection)); this._connectionStore.add(connection); @@ -90,7 +94,7 @@ export class OrderWatcherWebSocketServer { const port = process.env.ORDER_WATCHER_HTTP_PORT || DEFAULT_HTTP_PORT; this._httpServer.listen(port, () => { - logUtils.log(`${new Date()} [Server] Listening on port ${port}`); + this._log(`${new Date()} [Server] Listening on port ${port}`); }); } @@ -103,6 +107,12 @@ export class OrderWatcherWebSocketServer { this._orderWatcher.unsubscribe(); } + private _log(...args: any[]): void { + if (this._isVerbose) { + logUtils.log(...args); + } + } + private async _onMessageCallbackAsync(connection: WebSocket.connection, message: any): Promise<void> { let response: WebSocketResponse; let id: number | null = null; @@ -126,17 +136,17 @@ export class OrderWatcherWebSocketServer { error: err.toString(), }; } - logUtils.log(`${new Date()} [Server] OrderWatcher output: ${JSON.stringify(response)}`); + this._log(`${new Date()} [Server] OrderWatcher output: ${JSON.stringify(response)}`); connection.sendUTF(JSON.stringify(response)); } private _onCloseCallback(connection: WebSocket.connection): void { this._connectionStore.delete(connection); - logUtils.log(`${new Date()} [Server] Client ${connection.remoteAddress} disconnected.`); + this._log(`${new Date()} [Server] Client ${connection.remoteAddress} disconnected.`); } private async _routeRequestAsync(request: WebSocketRequest): Promise<GetStatsResult | undefined> { - logUtils.log(`${new Date()} [Server] Request received: ${request.method}`); + this._log(`${new Date()} [Server] Request received: ${request.method}`); switch (request.method) { case OrderWatcherMethod.AddOrder: { const signedOrder: SignedOrder = OrderWatcherWebSocketServer._parseSignedOrder( diff --git a/packages/order-watcher/test/order_watcher_websocket_server_test.ts b/packages/order-watcher/test/order_watcher_websocket_server_test.ts index d21c676fc..d1a947105 100644 --- a/packages/order-watcher/test/order_watcher_websocket_server_test.ts +++ b/packages/order-watcher/test/order_watcher_websocket_server_test.ts @@ -105,7 +105,14 @@ describe.only('OrderWatcherWebSocketServer', async () => { // Prepare OrderWatcher WebSocket server const orderWatcherConfig = {}; - wsServer = new OrderWatcherWebSocketServer(provider, networkId, contractAddresses, orderWatcherConfig); + const isVerbose = true; + wsServer = new OrderWatcherWebSocketServer( + provider, + networkId, + contractAddresses, + isVerbose, + orderWatcherConfig, + ); wsServer.start(); }); after(async () => { |