added debug() method for NODE_DEBUG=mc-proto

This commit is contained in:
Xabier de Zuazo 2013-04-06 05:02:36 +02:00
parent 0ff49078ff
commit b699d5506a
2 changed files with 17 additions and 0 deletions

View file

@ -4,6 +4,7 @@ var net = require('net')
, protocol = require('./protocol')
, createPacketBuffer = protocol.createPacketBuffer
, parsePacket = protocol.parsePacket
, debug = protocol.debug
module.exports = Client;

View file

@ -1,4 +1,5 @@
var assert = require('assert');
var util = require('util');
var STRING_MAX_LENGTH = 240;
@ -500,6 +501,20 @@ var types = {
'stringArray': [readStringArray, writeStringArray, sizeOfStringArray],
};
var debug;
if (process.env.NODE_DEBUG && /(minecraft-protocol|mc-proto)/.test(process.env.NODE_DEBUG)) {
var pid = process.pid;
debug = function(x) {
// if console is not set up yet, then skip this.
if (!console.error)
return;
console.error('MC-PROTO: %d', pid,
util.format.apply(util, arguments).slice(0, 500));
};
} else {
debug = function() { };
}
function sizeOfByteArray32(value) {
return 4 + value.length;
}
@ -1273,4 +1288,5 @@ module.exports = {
STRING_MAX_LENGTH: STRING_MAX_LENGTH,
packets: packets,
get: get,
debug: debug,
};