mirror of
https://github.com/tribufu/node-gamedig
synced 2026-05-18 09:35:50 +00:00
Minor debug logging improvements
This commit is contained in:
parent
9a536b160e
commit
2a87360a0e
4 changed files with 24 additions and 16 deletions
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue