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'),
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;
}
}

View file

@ -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);

View file

@ -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;

View file

@ -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");