Skip to content

Commit 334da74

Browse files
committed
feat: improve logging
1 parent 9f7c835 commit 334da74

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

app/src/main/java/io/pslab/communication/SocketClient.java

+7-3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ private SocketClient() {
2222
}
2323

2424
public void openConnection(String ip, int port) throws IOException {
25+
Log.v(TAG, "Connecting to " + ip + ":" + port);
2526
socket = new Socket(ip, port);
2627
outputStream = socket.getOutputStream();
2728
inputStream = socket.getInputStream();
@@ -54,11 +55,14 @@ public synchronized void write(byte[] data) throws IOException {
5455
public synchronized int read(int bytesToBeRead) throws IOException {
5556
int numBytesRead = 0;
5657
int readNow;
57-
Log.v(TAG, "To read : " + bytesToBeRead);
58+
final long start = System.currentTimeMillis();
59+
Log.v(TAG, "Bytes to read : " + bytesToBeRead);
5860
int bytesToBeReadTemp = bytesToBeRead;
5961
receivedData = new byte[bytesToBeRead];
6062
while (numBytesRead < bytesToBeRead) {
63+
final long start2 = System.currentTimeMillis();
6164
readNow = inputStream.read(receivedData, numBytesRead, bytesToBeReadTemp);
65+
Log.v(TAG, "Bytes read: " + readNow + " in " + (System.currentTimeMillis() - start2) + " ms");
6266
if (readNow <= 0) {
6367
Log.e(TAG, "Read Error: " + bytesToBeReadTemp);
6468
return numBytesRead;
@@ -67,7 +71,7 @@ public synchronized int read(int bytesToBeRead) throws IOException {
6771
bytesToBeReadTemp -= readNow;
6872
}
6973
}
70-
Log.v("Bytes Read", "" + numBytesRead);
74+
Log.v(TAG, "Total bytes read: " + numBytesRead + " in " + (System.currentTimeMillis() - start) + " ms");
7175
return numBytesRead;
7276
}
7377

@@ -84,7 +88,7 @@ public void closeConnection() {
8488
isConnected = false;
8589
}
8690
} catch (Exception e) {
87-
e.printStackTrace();
91+
Log.e(TAG, "Error closing connection", e);
8892
}
8993
}
9094
}

0 commit comments

Comments
 (0)