Skip to content

Commit

Permalink
camel-jbang - Trace should show size of collection/array
Browse files Browse the repository at this point in the history
  • Loading branch information
davsclaus committed Oct 4, 2023
1 parent 7ca9cf1 commit 3d2b79b
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import java.io.Reader;
import java.io.StringWriter;
import java.io.Writer;
import java.lang.reflect.Array;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
Expand Down Expand Up @@ -588,6 +590,14 @@ public static String dumpAsXml(
if (type != null) {
sb.append(" type=\"").append(type).append("\"");
}
if (body instanceof Collection) {
long size = ((Collection<?>) body).size();
sb.append(" size=\"").append(size).append("\"");
}
if (body != null && body.getClass().isArray()) {
int size = Array.getLength(body);
sb.append(" size=\"").append(size).append("\"");
}
if (body instanceof StreamCache) {
long pos = ((StreamCache) body).position();
if (pos != -1) {
Expand Down Expand Up @@ -996,6 +1006,14 @@ public static JsonObject dumpAsJSonObject(
if (type != null) {
jb.put("type", type);
}
if (body instanceof Collection) {
long size = ((Collection<?>) body).size();
jb.put("size", size);
}
if (body != null && body.getClass().isArray()) {
int size = Array.getLength(body);
jb.put("size", size);
}
if (body instanceof StreamCache) {
long pos = ((StreamCache) body).position();
if (pos != -1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,8 @@ public String getDataAsTable(
// body and type
JsonObject jo = root.getMap("body");
if (jo != null) {
TableRow bodyRow = new TableRow("Body", jo.getString("type"), null, jo.get("value"), jo.getLong("position"));
TableRow bodyRow = new TableRow(
"Body", jo.getString("type"), null, jo.get("value"), jo.getLong("size"), jo.getLong("position"));
tab5 = AsciiTable.getTable(AsciiTable.NO_BORDERS, List.of(bodyRow), Arrays.asList(
new Column().dataAlign(HorizontalAlign.LEFT)
.minWidth(showExchangeProperties ? 12 : 10).with(TableRow::kindAsString),
Expand Down Expand Up @@ -239,17 +240,19 @@ private class TableRow {
String key;
Object value;
Long position;
Long size;

TableRow(String kind, String type, String key, Object value) {
this(kind, type, key, value, null);
this(kind, type, key, value, null, null);
}

TableRow(String kind, String type, String key, Object value, Long position) {
TableRow(String kind, String type, String key, Object value, Long size, Long position) {
this.kind = kind;
this.type = type;
this.key = key;
this.value = value;
this.position = position;
this.size = size;
}

String valueAsString() {
Expand Down Expand Up @@ -378,13 +381,20 @@ String typeAndLengthAsString() {
}
s = "(" + s + ")";
int l = valueLength();
long sz = size != null ? size : -1;
long p = position != null ? position : -1;
if (l != -1 & p != -1) {
s = s + " (pos: " + p + " length: " + l + ")";
} else if (l != -1) {
s = s + " (length: " + l + ")";
} else if (p != -1) {
s = s + " (pos: " + p + ")";
StringBuilder sb = new StringBuilder();
if (sz != -1) {
sb.append(" size: ").append(sz);
}
if (p != -1) {
sb.append(" pos: ").append(p);
}
if (l != -1) {
sb.append(" bytes: ").append(l);
}
if (!sb.isEmpty()) {
s = s + " (" + sb.toString().trim() + ")";
}
if (loggingColor) {
s = Ansi.ansi().fgBrightDefault().a(Ansi.Attribute.INTENSITY_FAINT).a(s).reset().toString();
Expand Down

0 comments on commit 3d2b79b

Please sign in to comment.