Skip to content

Commit 5e6d103

Browse files
committed
Added more trace logging to log pre-pairing requests
1 parent dac8c97 commit 5e6d103

File tree

2 files changed

+33
-19
lines changed

2 files changed

+33
-19
lines changed
Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,51 @@
11
package com.beowulfe.hap.impl.http.impl;
22

33
import io.netty.buffer.ByteBuf;
4+
import io.netty.channel.ChannelDuplexHandler;
45
import io.netty.channel.ChannelHandlerContext;
5-
import io.netty.channel.ChannelInboundHandlerAdapter;
6+
import io.netty.channel.ChannelPromise;
67

7-
import java.io.File;
8-
import java.io.FileOutputStream;
8+
import java.io.ByteArrayOutputStream;
99
import java.io.IOException;
1010

11-
public class LoggingHandler extends ChannelInboundHandlerAdapter {
12-
13-
private final FileOutputStream fos;
11+
import org.apache.commons.io.HexDump;
12+
import org.slf4j.Logger;
13+
import org.slf4j.LoggerFactory;
14+
15+
public class LoggingHandler extends ChannelDuplexHandler {
1416

15-
public LoggingHandler() throws IOException {
16-
File file = File.createTempFile("hap", "log");
17-
fos = new FileOutputStream(file);
18-
System.out.println("Logging to "+file.getCanonicalPath());
19-
}
17+
private final static Logger logger = LoggerFactory.getLogger(NettyHomekitHttpService.class);
2018

2119
@Override
2220
public void channelRead(ChannelHandlerContext ctx, Object msg)
2321
throws Exception {
24-
if (msg instanceof ByteBuf) {
25-
ByteBuf buf = (ByteBuf) msg;
26-
buf.getBytes(0, fos, buf.readableBytes());
27-
fos.flush();
22+
if (logger.isTraceEnabled()) {
23+
if (msg instanceof ByteBuf) {
24+
logBytes("READ", (ByteBuf) msg, ctx);
25+
}
2826
}
2927
super.channelRead(ctx, msg);
3028
}
3129

3230
@Override
33-
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
34-
fos.close();
31+
public void write(ChannelHandlerContext ctx, Object msg,
32+
ChannelPromise promise) throws Exception {
33+
if (logger.isTraceEnabled()) {
34+
if (msg instanceof ByteBuf) {
35+
logBytes("WRITE", (ByteBuf) msg, ctx);
36+
}
37+
}
38+
super.write(ctx, msg, promise);
39+
}
40+
41+
private void logBytes(String type, ByteBuf buf, ChannelHandlerContext ctx) throws IOException {
42+
try (ByteArrayOutputStream stream = new ByteArrayOutputStream()) {
43+
byte[] bytes = new byte[buf.readableBytes()];
44+
buf.getBytes(0, bytes, 0, bytes.length);
45+
HexDump.dump(bytes, 0, stream, 0);
46+
stream.flush();
47+
logger.trace(String.format("%s %s [%s]:\n%s\n", type, buf, ctx.channel().remoteAddress().toString(),
48+
stream.toString()));
49+
}
3550
}
3651
}

src/main/java/com/beowulfe/hap/impl/http/impl/ServerInitializer.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@ public ServerInitializer(HomekitClientConnectionFactory homekit, ChannelGroup al
3030
@Override
3131
protected void initChannel(SocketChannel ch) throws Exception {
3232
ChannelPipeline pipeline = ch.pipeline();
33-
// Useful for debugging:
34-
//pipeline.addLast(new LoggingHandler());
33+
pipeline.addLast(new LoggingHandler());
3534
pipeline.addLast(HTTP_HANDLER_NAME, new HttpResponseEncoderAggregate());
3635
pipeline.addLast(new HttpRequestDecoder());
3736
pipeline.addLast(new HttpObjectAggregator(MAX_POST));

0 commit comments

Comments
 (0)