Skip to content

Commit 226053f

Browse files
committed
Refactor logging to take into account enableVerboseLogging
1 parent d6470c6 commit 226053f

File tree

1 file changed

+39
-44
lines changed

1 file changed

+39
-44
lines changed

test-app/app/src/main/java/com/tns/AndroidJsV8Inspector.java

Lines changed: 39 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@
2323
import fi.iki.elonen.NanoWSD;
2424

2525
class AndroidJsV8Inspector {
26-
private static boolean DEBUG_LOG_ENABLED = false;
27-
2826
private JsV8InspectorServer server;
2927
private static String ApplicationDir;
3028
private String packageName;
@@ -43,6 +41,8 @@ class AndroidJsV8Inspector {
4341

4442
private final Object debugBrkLock;
4543

44+
private Logger currentRuntimeLogger;
45+
4646
private static AtomicBoolean ReadyToProcessMessages = new AtomicBoolean(false);
4747

4848
private LinkedBlockingQueue<String> inspectorMessages = new LinkedBlockingQueue<String>();
@@ -60,10 +60,12 @@ public void start() throws IOException {
6060

6161
mainHandler = currentRuntime.getHandler();
6262

63-
this.server = new JsV8InspectorServer(this.packageName + "-inspectorServer");
63+
currentRuntimeLogger = currentRuntime.getLogger();
64+
65+
this.server = new JsV8InspectorServer(this.packageName + "-inspectorServer", currentRuntimeLogger);
6466
this.server.start(-1);
6567

66-
if (DEBUG_LOG_ENABLED) {
68+
if (currentRuntimeLogger.isEnabled()) {
6769
Log.d("V8Inspector", "start debugger ThreadId:" + Thread.currentThread().getId());
6870
}
6971

@@ -158,22 +160,22 @@ private static String getMimeType(String url) {
158160
// getMimeType may sometime return incorrect results in the context of NativeScript
159161
// e.g. `.ts` returns video/MP2TS
160162
switch (extension) {
161-
case "js":
162-
type = "text/javascript";
163-
break;
164-
case "json":
165-
type = "application/json";
166-
break;
167-
case "css":
168-
type = "text/css";
169-
break;
170-
case "ts":
171-
type = "text/typescript";
172-
break;
173-
// handle shared libraries so they are marked properly and don't appear in the sources tab
174-
case "so":
175-
type = "application/binary";
176-
break;
163+
case "js":
164+
type = "text/javascript";
165+
break;
166+
case "json":
167+
type = "application/json";
168+
break;
169+
case "css":
170+
type = "text/css";
171+
break;
172+
case "ts":
173+
type = "text/typescript";
174+
break;
175+
// handle shared libraries so they are marked properly and don't appear in the sources tab
176+
case "so":
177+
type = "application/binary";
178+
break;
177179
}
178180
}
179181

@@ -214,45 +216,38 @@ private void processDebugBreakMessages() {
214216
}
215217

216218
private class JsV8InspectorServer extends NanoWSD {
217-
JsV8InspectorServer(String name) {
219+
private Logger currentRuntimeLogger;
220+
221+
JsV8InspectorServer(String name, Logger runtimeLogger) {
218222
super(name);
223+
currentRuntimeLogger = runtimeLogger;
219224
}
220225

221-
private JsV8InspectorWebSocket webSocket;
222-
223226
@Override
224227
protected Response serveHttp(IHTTPSession session) {
225-
if (DEBUG_LOG_ENABLED) {
228+
if (currentRuntimeLogger.isEnabled()) {
226229
Log.d("{N}.v8-inspector", "http request for " + session.getUri());
227230
}
228231
return super.serveHttp(session);
229232
}
230233

231234
@Override
232235
protected WebSocket openWebSocket(IHTTPSession handshake) {
233-
// close the previous webSocket
234-
if(this.webSocket != null) {
235-
try {
236-
this.webSocket.close(WebSocketFrame.CloseCode.NormalClosure, "New browser connection is open", false);
237-
} catch (IOException ioException) {
238-
if(this.webSocket.getState() != State.CLOSED) {
239-
Log.e("{N}.v8-inspector", "Error closing previous connection", ioException);
240-
}
241-
}
242-
}
243-
this.webSocket = new JsV8InspectorWebSocket(handshake);
244-
return this.webSocket;
236+
return new JsV8InspectorWebSocket(handshake, currentRuntimeLogger);
245237
}
246238
}
247239

248240
private class JsV8InspectorWebSocket extends NanoWSD.WebSocket {
249-
JsV8InspectorWebSocket(NanoHTTPD.IHTTPSession handshakeRequest) {
241+
private Logger currentRuntimeLogger;
242+
243+
JsV8InspectorWebSocket(NanoHTTPD.IHTTPSession handshakeRequest, Logger runtimeLogger) {
250244
super(handshakeRequest);
245+
currentRuntimeLogger = runtimeLogger;
251246
}
252247

253248
@Override
254249
protected void onOpen() {
255-
if (DEBUG_LOG_ENABLED) {
250+
if (currentRuntimeLogger.isEnabled()) {
256251
Log.d("V8Inspector", "onOpen: ThreadID: " + Thread.currentThread().getId());
257252
}
258253

@@ -261,14 +256,14 @@ protected void onOpen() {
261256

262257
@Override
263258
protected void onClose(NanoWSD.WebSocketFrame.CloseCode code, String reason, boolean initiatedByRemote) {
264-
if (DEBUG_LOG_ENABLED) {
259+
if (currentRuntimeLogger.isEnabled()) {
265260
Log.d("V8Inspector", "onClose");
266261
}
267262

268263
mainHandler.post(new Runnable() {
269264
@Override
270265
public void run() {
271-
if (DEBUG_LOG_ENABLED) {
266+
if (currentRuntimeLogger.isEnabled()) {
272267
Log.d("V8Inspector", "Disconnecting");
273268
}
274269
disconnect();
@@ -278,7 +273,7 @@ public void run() {
278273

279274
@Override
280275
protected void onMessage(final NanoWSD.WebSocketFrame message) {
281-
if (DEBUG_LOG_ENABLED) {
276+
if (currentRuntimeLogger.isEnabled()) {
282277
Log.d("V8Inspector", "To dbg backend: " + message.getTextPayload() + " ThreadId:" + Thread.currentThread().getId());
283278
}
284279

@@ -312,7 +307,7 @@ public void run() {
312307

313308
@Override
314309
public void send(String payload) throws IOException {
315-
if (DEBUG_LOG_ENABLED) {
310+
if (currentRuntimeLogger.isEnabled()) {
316311
Log.d("V8Inspector", "To dbg client: " + payload);
317312
}
318313

@@ -335,8 +330,8 @@ protected void onPong(NanoWSD.WebSocketFrame pong) {
335330

336331
@Override
337332
protected void onException(IOException exception) {
338-
// when the chrome inspector is disconnected by closing the tab a "Broken pipe" exception is thrown which we don't need to log
339-
if(!exception.getMessage().equals("Broken pipe")) {
333+
// when the chrome inspector is disconnected by closing the tab a "Broken pipe" exception is thrown which we don't need to log, only in verbose logging mode
334+
if(!exception.getMessage().equals("Broken pipe") || currentRuntimeLogger.isEnabled()) {
340335
exception.printStackTrace();
341336
}
342337
disconnect();

0 commit comments

Comments
 (0)