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