Return-Path: X-Original-To: apmail-qpid-commits-archive@www.apache.org Delivered-To: apmail-qpid-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id BB188184A9 for ; Thu, 23 Jul 2015 12:06:17 +0000 (UTC) Received: (qmail 18873 invoked by uid 500); 23 Jul 2015 12:06:05 -0000 Delivered-To: apmail-qpid-commits-archive@qpid.apache.org Received: (qmail 18805 invoked by uid 500); 23 Jul 2015 12:06:05 -0000 Mailing-List: contact commits-help@qpid.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@qpid.apache.org Delivered-To: mailing list commits@qpid.apache.org Received: (qmail 18594 invoked by uid 99); 23 Jul 2015 12:06:04 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 23 Jul 2015 12:06:04 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id BAB2FE4424; Thu, 23 Jul 2015 12:06:04 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: robbie@apache.org To: commits@qpid.apache.org Date: Thu, 23 Jul 2015 12:06:11 -0000 Message-Id: In-Reply-To: <6687969c6328457790170664881fead9@git.apache.org> References: <6687969c6328457790170664881fead9@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: [08/10] qpid-proton git commit: PROTON-957: make transfer frame logging more like proton-c, report payload size rather than string length, do it regardless whether string is truncated, indicate whether string is truncated PROTON-957: make transfer frame logging more like proton-c, report payload size rather than string length, do it regardless whether string is truncated, indicate whether string is truncated Project: http://git-wip-us.apache.org/repos/asf/qpid-proton/repo Commit: http://git-wip-us.apache.org/repos/asf/qpid-proton/commit/de99c061 Tree: http://git-wip-us.apache.org/repos/asf/qpid-proton/tree/de99c061 Diff: http://git-wip-us.apache.org/repos/asf/qpid-proton/diff/de99c061 Branch: refs/heads/master Commit: de99c06137a5f51063cce9d997424988e4fb2e29 Parents: fff1061 Author: Robert Gemmell Authored: Thu Jul 23 12:57:27 2015 +0100 Committer: Robert Gemmell Committed: Thu Jul 23 12:57:27 2015 +0100 ---------------------------------------------------------------------- .../qpid/proton/engine/impl/StringUtils.java | 92 +++++++++++++ .../qpid/proton/engine/impl/TransportImpl.java | 13 +- .../proton/engine/impl/StringUtilsTest.java | 136 +++++++++++++++++++ 3 files changed, 234 insertions(+), 7 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/de99c061/proton-j/src/main/java/org/apache/qpid/proton/engine/impl/StringUtils.java ---------------------------------------------------------------------- diff --git a/proton-j/src/main/java/org/apache/qpid/proton/engine/impl/StringUtils.java b/proton-j/src/main/java/org/apache/qpid/proton/engine/impl/StringUtils.java new file mode 100644 index 0000000..f80cca3 --- /dev/null +++ b/proton-j/src/main/java/org/apache/qpid/proton/engine/impl/StringUtils.java @@ -0,0 +1,92 @@ +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.apache.qpid.proton.engine.impl; + +import org.apache.qpid.proton.amqp.Binary; + +public class StringUtils +{ + /** + * Converts the Binary to a quoted string. + * + * @param bin the Binary to convert + * @param stringLength the maximum length of stringified content (excluding the quotes, and truncated indicator) + * @param appendIfTruncated appends "...(truncated)" if not all of the payload is present in the string + * @return the converted string + */ + public static String toQuotedString(final Binary bin,final int stringLength,final boolean appendIfTruncated) + { + if(bin == null) + { + return "\"\""; + } + + final byte[] binData = bin.getArray(); + final int binLength = bin.getLength(); + final int offset = bin.getArrayOffset(); + + StringBuilder str = new StringBuilder(); + str.append("\""); + + int size = 0; + boolean truncated = false; + for (int i = 0; i < binLength; i++) + { + byte c = binData[offset + i]; + + if (c > 31 && c < 127 && c != '\\') + { + if (size + 1 <= stringLength) + { + size += 1; + str.append((char) c); + } + else + { + truncated = true; + break; + } + } + else + { + if (size + 4 <= stringLength) + { + size += 4; + str.append(String.format("\\x%02x", c)); + } + else + { + truncated = true; + break; + } + } + } + + str.append("\""); + + if (truncated && appendIfTruncated) + { + str.append("...(truncated)"); + } + + return str.toString(); + } +} http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/de99c061/proton-j/src/main/java/org/apache/qpid/proton/engine/impl/TransportImpl.java ---------------------------------------------------------------------- diff --git a/proton-j/src/main/java/org/apache/qpid/proton/engine/impl/TransportImpl.java b/proton-j/src/main/java/org/apache/qpid/proton/engine/impl/TransportImpl.java index 3c472b0..7faadc6 100644 --- a/proton-j/src/main/java/org/apache/qpid/proton/engine/impl/TransportImpl.java +++ b/proton-j/src/main/java/org/apache/qpid/proton/engine/impl/TransportImpl.java @@ -77,7 +77,7 @@ public class TransportImpl extends EndpointImpl } private static final boolean FRM_ENABLED = getBooleanEnv("PN_TRACE_FRM"); - private static final int TRACE_FRAME_PAYLOAD_LENGTH = Integer.getInteger("proton.trace_frame_payload_length", 80); + private static final int TRACE_FRAME_PAYLOAD_LENGTH = Integer.getInteger("proton.trace_frame_payload_length", 1024); // trace levels private int _levels = (FRM_ENABLED ? TRACE_FRM : 0); @@ -1594,12 +1594,11 @@ public class TransportImpl extends EndpointImpl msg.append("[").append(System.identityHashCode(this)).append(":") .append(frame.getChannel()).append("]"); msg.append(" ").append(event).append(" ").append(frame.getBody()); - if (frame.getPayload() != null) { - String payload = frame.getPayload().toString(); - if (payload.length() > TRACE_FRAME_PAYLOAD_LENGTH) { - payload = payload.substring(0, TRACE_FRAME_PAYLOAD_LENGTH) + "(" + payload.length() + ")"; - } - msg.append(" \"").append(payload).append("\""); + + Binary bin = frame.getPayload(); + if (bin != null) { + msg.append(" (").append(bin.getLength()).append(") "); + msg.append(StringUtils.toQuotedString(bin, TRACE_FRAME_PAYLOAD_LENGTH, true)); } System.out.println(msg.toString()); } http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/de99c061/proton-j/src/test/java/org/apache/qpid/proton/engine/impl/StringUtilsTest.java ---------------------------------------------------------------------- diff --git a/proton-j/src/test/java/org/apache/qpid/proton/engine/impl/StringUtilsTest.java b/proton-j/src/test/java/org/apache/qpid/proton/engine/impl/StringUtilsTest.java new file mode 100644 index 0000000..8711b9f --- /dev/null +++ b/proton-j/src/test/java/org/apache/qpid/proton/engine/impl/StringUtilsTest.java @@ -0,0 +1,136 @@ +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.apache.qpid.proton.engine.impl; + +import static org.junit.Assert.*; + +import org.apache.qpid.proton.amqp.Binary; +import org.junit.Test; + +public class StringUtilsTest +{ + @Test + public void testNullBinary() + { + assertEquals("unexpected result", "\"\"", StringUtils.toQuotedString(null, 10, true)); + } + + @Test + public void testEmptyBinaryEmptyArray() + { + Binary bin = new Binary(new byte[0]); + assertEquals("unexpected result", "\"\"", StringUtils.toQuotedString(bin, 10, true)); + } + + @Test + public void testEmptyBinaryNonEmptyArray() + { + byte[] bytes = new byte[] {(byte) 0, (byte) 0, (byte) 0}; + Binary bin = new Binary(bytes, 0, 0); + assertEquals("unexpected result", "\"\"", StringUtils.toQuotedString(bin, 10, true)); + } + + @Test + public void testEmptyBinaryNonEmptyArrayWithOffset() + { + byte[] bytes = new byte[] {(byte) 0, (byte) 0, (byte) 0}; + Binary bin = new Binary(bytes, 1, 0); + assertEquals("unexpected result", "\"\"", StringUtils.toQuotedString(bin, 10, true)); + } + + @Test + public void testBinaryStringifiedSmallerThanGivenMaxLength() + { + byte[] bytes = new byte[] {(byte) 0, (byte) 1, (byte) 3, (byte) 65, (byte) 152}; + String expected = "\"\\x00\\x01\\x03A\\x98\""; + Binary bin = new Binary(bytes); + assertEquals("unexpected result", expected, StringUtils.toQuotedString(bin, 100, true)); + assertEquals("unexpected result", expected, StringUtils.toQuotedString(bin, 100, false)); + } + + @Test + public void testBinaryStringifiedSmallerThanGivenMaxLengthWithOffset() + { + byte[] bytes = new byte[] {(byte) 0, (byte) 1, (byte) 3, (byte) 65, (byte) 152}; + String expected = "\"\\x01\\x03A\\x98\""; + Binary bin = new Binary(bytes, 1, 4); + assertEquals("unexpected result", expected, StringUtils.toQuotedString(bin, 100, true)); + assertEquals("unexpected result", expected, StringUtils.toQuotedString(bin, 100, false)); + } + + @Test + public void testBinaryStringifiedEqualToGivenMaxLength() + { + byte[] bytes = new byte[] {(byte) 0, (byte) 1, (byte) 3, (byte) 65}; + String expected = "\"\\x00\\x01\\x03A\""; + Binary bin = new Binary(bytes); + assertEquals("unexpected result", expected, StringUtils.toQuotedString(bin, 15, true)); + assertEquals("unexpected result", expected, StringUtils.toQuotedString(bin, 15, false)); + + bytes = new byte[] {(byte) 0, (byte) 1, (byte) 65, (byte) 3}; + expected = "\"\\x00\\x01A\\x03\""; + bin = new Binary(bytes); + assertEquals("unexpected result", expected, StringUtils.toQuotedString(bin, 15, true)); + assertEquals("unexpected result", expected, StringUtils.toQuotedString(bin, 15, false)); + } + + @Test + public void testBinaryStringifiedEqualToGivenMaxLengthWithOffset() + { + byte[] bytes = new byte[] {(byte) 0, (byte) 1, (byte) 3, (byte) 65}; + String expected = "\"\\x01\\x03A\""; + Binary bin = new Binary(bytes, 1, 3); + assertEquals("unexpected result", expected, StringUtils.toQuotedString(bin, 11, true)); + assertEquals("unexpected result", expected, StringUtils.toQuotedString(bin, 11, false)); + + bytes = new byte[] {(byte) 0, (byte) 1, (byte) 65, (byte) 3}; + expected = "\"\\x01A\\x03\""; + bin = new Binary(bytes, 1, 3); + assertEquals("unexpected result", expected, StringUtils.toQuotedString(bin, 11, true)); + assertEquals("unexpected result", expected, StringUtils.toQuotedString(bin, 11, false)); + } + + @Test + public void testBinaryStringifiedLargerThanGivenMaxLength() + { + byte[] bytes = new byte[] {(byte) 0, (byte) 1, (byte) 3}; + String expected1 = "\"\\x00\\x01\""; + Binary bin = new Binary(bytes); + + assertEquals("unexpected result", expected1, StringUtils.toQuotedString(bin, 10, false)); + + String expected2 = "\"\\x00\\x01\"...(truncated)"; + assertEquals("unexpected result", expected2, StringUtils.toQuotedString(bin, 10, true)); + } + + @Test + public void testBinaryStringifiedLargerThanGivenMaxLengthWithOffset() + { + byte[] bytes = new byte[] {(byte) 0, (byte) 1, (byte) 3}; + String expected1 = "\"\\x00\\x01\""; + Binary bin = new Binary(bytes); + + assertEquals("unexpected result", expected1, StringUtils.toQuotedString(bin, 10, false)); + + String expected2 = "\"\\x00\\x01\"...(truncated)"; + assertEquals("unexpected result", expected2, StringUtils.toQuotedString(bin, 10, true)); + } +} --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscribe@qpid.apache.org For additional commands, e-mail: commits-help@qpid.apache.org