Skip to content

Commit

Permalink
More useful debug logging of received messages
Browse files Browse the repository at this point in the history
Signed-off-by: Marcus Better <marcus@better.se>
  • Loading branch information
marcusb committed Oct 15, 2021
1 parent 5ee45ef commit d1bd3a9
Showing 1 changed file with 19 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ private void processBuffer(final InputStream in, final int len) {
return;
}
if (logger.isDebugEnabled()) {
logger.debug("UPB Message: {}", HexUtils.bytesToHex(buf));
logger.debug("UPB Message: {}", formatMessage(buf));
}
final UPBMessage msg;
try {
Expand Down Expand Up @@ -244,6 +244,24 @@ public void terminate() {
}
}

// format a message for debug logging, include only printable characters
private static String formatMessage(byte[] buf) {
final int len;
// omit the final newline
if (buf[buf.length - 1] == '\r') {
len = buf.length - 1;
} else {
len = buf.length;
}
final String s = new String(buf, 0, len, US_ASCII);
if (s.chars().allMatch(c -> c >= 32 && c < 127)) {
return s;
} else {
// presence of non-printable characters is either noise or a misconfiguration, log it in hex
return HexUtils.bytesToHex(buf);
}
}

private class WriteRunnable implements Runnable {
private static final int MAX_RETRIES = 3;

Expand Down

0 comments on commit d1bd3a9

Please sign in to comment.