Skip to content

Commit 5b71e38

Browse files
committed
update debug stuff
1 parent 55cac78 commit 5b71e38

File tree

4 files changed

+244
-11
lines changed

4 files changed

+244
-11
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ repositories {
1717
}
1818

1919
dependencies {
20-
implementation 'io.nats:jnats:2.20.5'
20+
implementation 'io.nats:jnats:2.22.0'
2121
implementation 'com.fasterxml.jackson.core:jackson-databind:2.13.4.2'
2222
implementation 'com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.13.4.2'
2323
testImplementation 'org.junit.jupiter:junit-jupiter:5.8.2'

src/main/java/scottf/Debug.java

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -66,24 +66,34 @@ public static void stackTrace(String label, Throwable t) {
6666
if (PAUSE) { return; }
6767
String m = t.getMessage();
6868
if (m == null) {
69-
info(label);
69+
info(label, "Stack Trace");
7070
}
7171
else {
72-
info(label, t.getMessage());
72+
info(label, "Stack Trace", t.getMessage());
7373
}
74+
boolean compress = false;
7475
StackTraceElement[] elements = t.getStackTrace();
7576
for (int i = 0; i < elements.length; i++) {
7677
String ts = elements[i].toString();
77-
if (i == 0) {
78-
info(label, ts);
79-
}
80-
else {
81-
info(label, "> " + ts);
78+
if (i > 0) {
79+
if (ts.startsWith("io.nats")) {
80+
if (compress) {
81+
info(label, "> ...");
82+
}
83+
info(label, "> " + ts);
84+
compress = false;
85+
}
86+
else {
87+
compress = true;
88+
}
8289
}
8390
if (ts.startsWith("java")) {
8491
break;
8592
}
8693
}
94+
if (compress) {
95+
info(label, "> ...");
96+
}
8797
}
8898

8999
public static void info(String label, Object... extras) {
@@ -199,8 +209,7 @@ public static String replyToString(Message msg) {
199209
}
200210

201211
public static String time() {
202-
String t = "" + System.currentTimeMillis();
203-
return t.substring(t.length() - 10);
212+
return "" + System.currentTimeMillis();
204213
}
205214

206215
public static String dataString(Message msg) {
@@ -323,7 +332,7 @@ public static String getString(Object o) {
323332
}
324333
return sb.toString();
325334
}
326-
String s = o.toString().trim();
335+
String s = o.toString();
327336
return s.isEmpty() ? "<empty>" : s;
328337
}
329338

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright 2021 The NATS Authors
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at:
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
package scottf;
15+
16+
import io.nats.client.Connection;
17+
import io.nats.client.ConnectionListener;
18+
import io.nats.client.api.ServerInfo;
19+
20+
public class DebugConnectionListener implements ConnectionListener {
21+
@Override
22+
public void connectionEvent(Connection conn, Events type) {
23+
if (type == Events.CONNECTED) {
24+
ServerInfo si = conn.getServerInfo();
25+
Debug.info("Connected", si.getServerId(), "Port: " + si.getPort());
26+
}
27+
else if (type.isConnectionEvent()) {
28+
Debug.info("Connection", type.getEvent());
29+
}
30+
else {
31+
Debug.info("Listener", type.getEvent());
32+
}
33+
}
34+
}
Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
// Copyright 2021 The NATS Authors
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at:
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
package scottf;
15+
16+
import io.nats.client.*;
17+
import io.nats.client.support.Status;
18+
19+
public class DebugListener implements ErrorListener, ConnectionListener, ReadListener {
20+
String rlLabel;
21+
String clLabel;
22+
String elLabel;
23+
boolean printStackTrace;
24+
25+
public DebugListener() {
26+
this("DbgRL", "DbgCL", "DbgEL");
27+
}
28+
29+
public DebugListener(String rlLabel, String clLabel, String elLabel) {
30+
this.rlLabel = rlLabel;
31+
this.clLabel = clLabel;
32+
this.elLabel = elLabel;
33+
}
34+
35+
public DebugListener rlLabel(String rlLabel) {
36+
this.rlLabel = rlLabel;
37+
return this;
38+
}
39+
40+
public DebugListener clLabel(String clLabel) {
41+
this.clLabel = clLabel;
42+
return this;
43+
}
44+
45+
public DebugListener elLabel(String elLabel) {
46+
this.elLabel = elLabel;
47+
return this;
48+
}
49+
50+
public DebugListener printStackTrace(boolean printStackTrace) {
51+
this.printStackTrace = printStackTrace;
52+
return this;
53+
}
54+
55+
private String string(Connection conn) {
56+
return "Connection(" + conn.hashCode() + ") " + conn.getStatus();
57+
}
58+
59+
@Override
60+
public void protocol(String op, String string) {
61+
if (rlLabel != null) {
62+
Debug.info(rlLabel + "-P", op, string);
63+
}
64+
}
65+
66+
@Override
67+
public void message(String op, Message message) {
68+
if (rlLabel != null) {
69+
Debug.msg(rlLabel + "-M", message);
70+
}
71+
}
72+
73+
@Override
74+
public void connectionEvent(Connection conn, Events type) {
75+
if (clLabel != null) {
76+
Debug.info(clLabel, string(conn), "Event: {}", type.getEvent());
77+
}
78+
}
79+
80+
/**
81+
* {@inheritDoc}
82+
*/
83+
@Override
84+
public void errorOccurred(final Connection conn, final String error) {
85+
if (elLabel != null) {
86+
Debug.info(elLabel, "errorOccurred", string(conn), "Error: " + error);
87+
}
88+
}
89+
90+
/**
91+
* {@inheritDoc}
92+
*/
93+
@SuppressWarnings("CallToPrintStackTrace")
94+
@Override
95+
public void exceptionOccurred(final Connection conn, final Exception exp) {
96+
if (elLabel != null) {
97+
Debug.info(elLabel, "exceptionOccurred:", string(conn), exp);
98+
if (exp.getCause() != null) {
99+
Debug.info(elLabel, " cause:", exp.getCause());
100+
if (printStackTrace) {
101+
exp.getCause().printStackTrace();
102+
}
103+
}
104+
else if (printStackTrace) {
105+
exp.printStackTrace();
106+
}
107+
}
108+
}
109+
110+
/**
111+
* {@inheritDoc}
112+
*/
113+
@Override
114+
public void slowConsumerDetected(final Connection conn, final Consumer consumer) {
115+
if (elLabel != null) {
116+
Debug.info(elLabel, "slowConsumerDetected", string(conn), consumer);
117+
}
118+
}
119+
120+
/**
121+
* {@inheritDoc}
122+
*/
123+
@Override
124+
public void messageDiscarded(final Connection conn, final Message msg) {
125+
if (elLabel != null) {
126+
Debug.info(elLabel, "messageDiscarded", string(conn), "Message: " + msg);
127+
}
128+
}
129+
130+
/**
131+
* {@inheritDoc}
132+
*/
133+
@Override
134+
public void heartbeatAlarm(final Connection conn, final JetStreamSubscription sub,
135+
final long lastStreamSequence, final long lastConsumerSequence) {
136+
if (elLabel != null) {
137+
Debug.info(elLabel, "heartbeatAlarm", string(conn), sub, "lastStreamSequence: " + lastStreamSequence, "lastConsumerSequence: " + lastConsumerSequence);
138+
}
139+
}
140+
141+
/**
142+
* {@inheritDoc}
143+
*/
144+
@Override
145+
public void unhandledStatus(final Connection conn, final JetStreamSubscription sub, final Status status) {
146+
if (elLabel != null) {
147+
Debug.info(elLabel, "unhandledStatus", string(conn), sub, "Status: " + status);
148+
}
149+
}
150+
151+
/**
152+
* {@inheritDoc}
153+
*/
154+
@Override
155+
public void pullStatusWarning(Connection conn, JetStreamSubscription sub, Status status) {
156+
if (elLabel != null) {
157+
Debug.info(elLabel, "pullStatusWarning", string(conn), sub, "Status: " + status);
158+
}
159+
}
160+
161+
/**
162+
* {@inheritDoc}
163+
*/
164+
@Override
165+
public void pullStatusError(Connection conn, JetStreamSubscription sub, Status status) {
166+
if (elLabel != null) {
167+
Debug.info(elLabel, "pullStatusError", string(conn), sub, "Status: " + status);
168+
}
169+
}
170+
171+
/**
172+
* {@inheritDoc}
173+
*/
174+
@Override
175+
public void flowControlProcessed(Connection conn, JetStreamSubscription sub, String id, FlowControlSource source) {
176+
if (elLabel != null) {
177+
Debug.info(elLabel, "flowControlProcessed", string(conn), sub, "FlowControlSource: " + source);
178+
}
179+
}
180+
181+
/**
182+
* {@inheritDoc}
183+
*/
184+
@Override
185+
public void socketWriteTimeout(Connection conn) {
186+
if (elLabel != null) {
187+
Debug.info(elLabel, "socketWriteTimeout", string(conn));
188+
}
189+
}
190+
}

0 commit comments

Comments
 (0)