From b699d5506a3eccdf49ca399f681922dffb23e973 Mon Sep 17 00:00:00 2001
From: Xabier de Zuazo <xabier@onddo.com>
Date: Sat, 6 Apr 2013 05:02:36 +0200
Subject: [PATCH] added debug() method for NODE_DEBUG=mc-proto

---
 lib/client.js   |  1 +
 lib/protocol.js | 16 ++++++++++++++++
 2 files changed, 17 insertions(+)

diff --git a/lib/client.js b/lib/client.js
index fb41c85..a89ba5b 100644
--- a/lib/client.js
+++ b/lib/client.js
@@ -4,6 +4,7 @@ var net = require('net')
   , protocol = require('./protocol')
   , createPacketBuffer = protocol.createPacketBuffer
   , parsePacket = protocol.parsePacket
+  , debug = protocol.debug
 
 module.exports = Client;
 
diff --git a/lib/protocol.js b/lib/protocol.js
index e994a2c..38459c6 100644
--- a/lib/protocol.js
+++ b/lib/protocol.js
@@ -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,
 };