Skip to content

Commit ed5d16a

Browse files
committed
improve logging
don't hexdump plaintext messages, and log them at DEBUG level so that you don't have to sift through all of the encrypted stuff to figure out what's going on at the HAP level
1 parent dc4cf16 commit ed5d16a

File tree

1 file changed

+10
-17
lines changed

1 file changed

+10
-17
lines changed

src/main/java/io/github/hapjava/impl/http/impl/BinaryHandler.java

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,9 @@
55
import io.netty.buffer.Unpooled;
66
import io.netty.channel.ChannelHandlerContext;
77
import io.netty.handler.codec.ByteToMessageCodec;
8-
import java.io.ByteArrayOutputStream;
98
import java.io.IOException;
109
import java.nio.charset.StandardCharsets;
1110
import java.util.List;
12-
import org.apache.commons.io.HexDump;
1311
import org.slf4j.Logger;
1412
import org.slf4j.LoggerFactory;
1513

@@ -27,9 +25,9 @@ public BinaryHandler(HomekitClientConnection connection) {
2725
@Override
2826
protected void encode(ChannelHandlerContext ctx, ByteBuf msg, ByteBuf out) throws Exception {
2927
if (started) {
28+
debugData("Sending data", msg, ctx);
3029
byte[] b = new byte[msg.readableBytes()];
3130
msg.readBytes(b);
32-
traceData("Sending data", b, ctx);
3331
out.writeBytes(connection.encryptResponse(b));
3432
} else {
3533
out.writeBytes(msg);
@@ -41,8 +39,9 @@ protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) t
4139
byte[] b = new byte[in.readableBytes()];
4240
in.readBytes(b);
4341
byte[] decrypted = connection.decryptRequest(b);
44-
traceData("Received data", decrypted, ctx);
45-
out.add(Unpooled.copiedBuffer(decrypted));
42+
ByteBuf outBuf = Unpooled.copiedBuffer(decrypted);
43+
debugData("Received data", outBuf, ctx);
44+
out.add(outBuf);
4645
started = true;
4746
}
4847

@@ -57,18 +56,12 @@ public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws E
5756
super.exceptionCaught(ctx, cause);
5857
}
5958

60-
private void traceData(String msg, byte[] b, ChannelHandlerContext ctx) throws Exception {
61-
if (logger.isTraceEnabled() && b.length > 0) {
62-
try (ByteArrayOutputStream stream = new ByteArrayOutputStream()) {
63-
HexDump.dump(b, 0, stream, 0);
64-
stream.flush();
65-
logger.trace(
66-
String.format(
67-
"%s [%s]:%n%s%n",
68-
msg,
69-
ctx.channel().remoteAddress().toString(),
70-
stream.toString(StandardCharsets.UTF_8.name())));
71-
}
59+
private void debugData(String msg, ByteBuf b, ChannelHandlerContext ctx) throws Exception {
60+
if (logger.isDebugEnabled()) {
61+
logger.debug(
62+
String.format(
63+
"%s [%s]:%n%s",
64+
msg, ctx.channel().remoteAddress().toString(), b.toString(StandardCharsets.UTF_8)));
7265
}
7366
}
7467
}

0 commit comments

Comments
 (0)