diff --git a/lib/GlobalUdpSocket.js b/lib/GlobalUdpSocket.js index af45ead..590ee19 100644 --- a/lib/GlobalUdpSocket.js +++ b/lib/GlobalUdpSocket.js @@ -1,11 +1,13 @@ const dgram = require('dgram'), - HexUtil = require('./HexUtil'); + HexUtil = require('./HexUtil'), + Logger = require('./Logger'); class GlobalUdpSocket { constructor() { this.socket = null; this.callbacks = new Set(); this.debuggingCallbacks = new Set(); + this.logger = new Logger(); } _getSocket() { @@ -16,18 +18,16 @@ class GlobalUdpSocket { udpSocket.on('message', (buffer, rinfo) => { const fromAddress = rinfo.address; const fromPort = rinfo.port; - if (this.debuggingCallbacks.size) { - console.log(fromAddress + ':' + fromPort + " <--UDP"); - console.log(HexUtil.debugDump(buffer)); - } + this.logger.debug(log => { + log(fromAddress + ':' + fromPort + " <--UDP"); + log(HexUtil.debugDump(buffer)); + }); for (const cb of this.callbacks) { cb(fromAddress, fromPort, buffer); } }); - udpSocket.on('error', (e) => { - if (this.debuggingCallbacks.size) { - console.log("UDP ERROR: " + e); - } + udpSocket.on('error', e => { + this.logger.debug("UDP ERROR:", e); }); } return this.socket; @@ -41,11 +41,13 @@ class GlobalUdpSocket { this.callbacks.add(callback); if (debug) { this.debuggingCallbacks.add(callback); + this.logger.debugEnabled = true; } } removeCallback(callback) { this.callbacks.delete(callback); this.debuggingCallbacks.delete(callback); + this.logger.debugEnabled = this.debuggingCallbacks.size > 0; } } diff --git a/lib/Logger.js b/lib/Logger.js index 03bdea2..00141ae 100644 --- a/lib/Logger.js +++ b/lib/Logger.js @@ -13,7 +13,9 @@ class Logger { _print(...args) { try { const strings = this._convertArgsToStrings(...args); - console.log(...strings); + if (strings.length) { + console.log(...strings); + } } catch(e) { console.log("Error while logging: " + e); } @@ -25,9 +27,9 @@ class Logger { if (arg instanceof Error) { out.push(arg.stack); } else if (arg instanceof Buffer) { - out.push("\n" + HexUtil.debugDump(arg) + "\n"); + out.push(HexUtil.debugDump(arg)); } else if (typeof arg == 'function') { - const result = arg.call(undefined, (...args) => out.push(...this._convertArgsToStrings(...args))); + const result = arg.call(undefined, (...args) => this._print(...args)); if (result !== undefined) out.push(...this._convertArgsToStrings(result)); } else { out.push(arg); diff --git a/lib/QueryRunner.js b/lib/QueryRunner.js index fe4fb19..f4e152c 100644 --- a/lib/QueryRunner.js +++ b/lib/QueryRunner.js @@ -1,6 +1,7 @@ const GameResolver = require('./GameResolver'), ProtocolResolver = require('./ProtocolResolver'), - GlobalUdpSocket = require('./GlobalUdpSocket'); + GlobalUdpSocket = require('./GlobalUdpSocket'), + Logger = require('./Logger'); const defaultOptions = { socketTimeout: 2000, @@ -91,8 +92,10 @@ class QueryRunner { async _attempt(options) { if (options.debug) { - console.log("Running attempt with options:"); - console.log(options); + const logger = new Logger(); + logger.debugEnabled = true; + logger.debug("Running attempt with options:"); + logger.debug(options); } const core = this.protocolResolver.create(options.protocol); core.options = options; diff --git a/protocols/core.js b/protocols/core.js index c0e4708..ec52272 100644 --- a/protocols/core.js +++ b/protocols/core.js @@ -20,6 +20,7 @@ class Core extends EventEmitter { // Sent to us by QueryRunner this.options = null; + /** @type GlobalUdpSocket */ this.udpSocket = null; this.shortestRTT = 0; this.usedTcp = false; @@ -190,7 +191,7 @@ class Core extends EventEmitter { log(HexUtil.debugDump(args[0])); writeHook.apply(socket,args); }; - socket.on('error', e => log('TCP Error: ' + e)); + socket.on('error', e => log('TCP Error:', e)); socket.on('close', () => log('TCP Closed')); socket.on('data', (data) => { log(address+':'+port+" <--TCP");