Minor debug logging improvements

This commit is contained in:
mmorrison 2019-01-20 04:21:40 -06:00
parent 9a536b160e
commit 2a87360a0e
4 changed files with 24 additions and 16 deletions

View file

@ -1,11 +1,13 @@
const dgram = require('dgram'), const dgram = require('dgram'),
HexUtil = require('./HexUtil'); HexUtil = require('./HexUtil'),
Logger = require('./Logger');
class GlobalUdpSocket { class GlobalUdpSocket {
constructor() { constructor() {
this.socket = null; this.socket = null;
this.callbacks = new Set(); this.callbacks = new Set();
this.debuggingCallbacks = new Set(); this.debuggingCallbacks = new Set();
this.logger = new Logger();
} }
_getSocket() { _getSocket() {
@ -16,18 +18,16 @@ class GlobalUdpSocket {
udpSocket.on('message', (buffer, rinfo) => { udpSocket.on('message', (buffer, rinfo) => {
const fromAddress = rinfo.address; const fromAddress = rinfo.address;
const fromPort = rinfo.port; const fromPort = rinfo.port;
if (this.debuggingCallbacks.size) { this.logger.debug(log => {
console.log(fromAddress + ':' + fromPort + " <--UDP"); log(fromAddress + ':' + fromPort + " <--UDP");
console.log(HexUtil.debugDump(buffer)); log(HexUtil.debugDump(buffer));
} });
for (const cb of this.callbacks) { for (const cb of this.callbacks) {
cb(fromAddress, fromPort, buffer); cb(fromAddress, fromPort, buffer);
} }
}); });
udpSocket.on('error', (e) => { udpSocket.on('error', e => {
if (this.debuggingCallbacks.size) { this.logger.debug("UDP ERROR:", e);
console.log("UDP ERROR: " + e);
}
}); });
} }
return this.socket; return this.socket;
@ -41,11 +41,13 @@ class GlobalUdpSocket {
this.callbacks.add(callback); this.callbacks.add(callback);
if (debug) { if (debug) {
this.debuggingCallbacks.add(callback); this.debuggingCallbacks.add(callback);
this.logger.debugEnabled = true;
} }
} }
removeCallback(callback) { removeCallback(callback) {
this.callbacks.delete(callback); this.callbacks.delete(callback);
this.debuggingCallbacks.delete(callback); this.debuggingCallbacks.delete(callback);
this.logger.debugEnabled = this.debuggingCallbacks.size > 0;
} }
} }

View file

@ -13,7 +13,9 @@ class Logger {
_print(...args) { _print(...args) {
try { try {
const strings = this._convertArgsToStrings(...args); const strings = this._convertArgsToStrings(...args);
console.log(...strings); if (strings.length) {
console.log(...strings);
}
} catch(e) { } catch(e) {
console.log("Error while logging: " + e); console.log("Error while logging: " + e);
} }
@ -25,9 +27,9 @@ class Logger {
if (arg instanceof Error) { if (arg instanceof Error) {
out.push(arg.stack); out.push(arg.stack);
} else if (arg instanceof Buffer) { } else if (arg instanceof Buffer) {
out.push("\n" + HexUtil.debugDump(arg) + "\n"); out.push(HexUtil.debugDump(arg));
} else if (typeof arg == 'function') { } 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)); if (result !== undefined) out.push(...this._convertArgsToStrings(result));
} else { } else {
out.push(arg); out.push(arg);

View file

@ -1,6 +1,7 @@
const GameResolver = require('./GameResolver'), const GameResolver = require('./GameResolver'),
ProtocolResolver = require('./ProtocolResolver'), ProtocolResolver = require('./ProtocolResolver'),
GlobalUdpSocket = require('./GlobalUdpSocket'); GlobalUdpSocket = require('./GlobalUdpSocket'),
Logger = require('./Logger');
const defaultOptions = { const defaultOptions = {
socketTimeout: 2000, socketTimeout: 2000,
@ -91,8 +92,10 @@ class QueryRunner {
async _attempt(options) { async _attempt(options) {
if (options.debug) { if (options.debug) {
console.log("Running attempt with options:"); const logger = new Logger();
console.log(options); logger.debugEnabled = true;
logger.debug("Running attempt with options:");
logger.debug(options);
} }
const core = this.protocolResolver.create(options.protocol); const core = this.protocolResolver.create(options.protocol);
core.options = options; core.options = options;

View file

@ -20,6 +20,7 @@ class Core extends EventEmitter {
// Sent to us by QueryRunner // Sent to us by QueryRunner
this.options = null; this.options = null;
/** @type GlobalUdpSocket */
this.udpSocket = null; this.udpSocket = null;
this.shortestRTT = 0; this.shortestRTT = 0;
this.usedTcp = false; this.usedTcp = false;
@ -190,7 +191,7 @@ class Core extends EventEmitter {
log(HexUtil.debugDump(args[0])); log(HexUtil.debugDump(args[0]));
writeHook.apply(socket,args); 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('close', () => log('TCP Closed'));
socket.on('data', (data) => { socket.on('data', (data) => {
log(address+':'+port+" <--TCP"); log(address+':'+port+" <--TCP");