Skip to content

Commit 1541cdd

Browse files
committed
debug verbose
1 parent f93514d commit 1541cdd

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

Sources/TCP/Streams/TCPSocketSink.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ public final class TCPSocketSink: Async.InputStream {
5656

5757
/// See InputStream.input
5858
public func input(_ event: InputEvent<ByteBuffer>) {
59+
DEBUG("TCPSocketSink.(\(event))")
5960
// update variables
6061
switch event {
6162
case .next(let input, let ready):
@@ -79,6 +80,7 @@ public final class TCPSocketSink: Async.InputStream {
7980

8081
/// Cancels reading
8182
public func close() {
83+
DEBUG("TCPSocketSink.close()")
8284
guard !isClosed else {
8385
return
8486
}
@@ -94,12 +96,14 @@ public final class TCPSocketSink: Async.InputStream {
9496

9597
/// Writes the buffered data to the socket.
9698
private func writeData(ready: Promise<Void>) {
99+
DEBUG("TCPSocketSink.writeData(\(ready))")
97100
do {
98101
guard let buffer = self.inputBuffer else {
99102
ERROR("Unexpected nil SocketSink inputBuffer during writeData")
100103
return
101104
}
102105

106+
DEBUG("TCPSocketSink.buffer = \(String(bytes: buffer, encoding: .ascii) ?? "nil")")
103107
let write = try socket.write(from: buffer)
104108
switch write {
105109
case .success(let count):
@@ -130,6 +134,7 @@ public final class TCPSocketSink: Async.InputStream {
130134

131135
/// Called when the write source signals.
132136
private func writeSourceSignal(isCancelled: Bool) {
137+
DEBUG("TCPSocketSink.writeSourceSignal(\(isCancelled))")
133138
guard !isCancelled else {
134139
// source is cancelled, we will never receive signals again
135140
close()
@@ -144,6 +149,7 @@ public final class TCPSocketSink: Async.InputStream {
144149
ERROR("SocketSink writeSource illegally nil during signal.")
145150
return
146151
}
152+
DEBUG("TCPSocketSink [suspending write]")
147153
writeSource.suspend()
148154
sourceIsSuspended = true
149155
}
@@ -159,6 +165,7 @@ public final class TCPSocketSink: Async.InputStream {
159165
}
160166

161167
private func resumeIfSuspended() {
168+
DEBUG("TCPSocketSink.resumeIfSuspended()")
162169
guard sourceIsSuspended else {
163170
return
164171
}

Sources/TCP/Streams/TCPSocketSource.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ public final class TCPSocketSource: Async.OutputStream {
4545

4646
/// Creates a new `SocketSource`
4747
internal init(socket: TCPSocket, on worker: Worker, bufferSize: Int) {
48+
DEBUG("TCPSocketSource.init(bufferSize: \(bufferSize))")
4849
self.socket = socket
4950
self.eventLoop = worker.eventLoop
5051
self.isClosed = false
@@ -59,12 +60,14 @@ public final class TCPSocketSource: Async.OutputStream {
5960

6061
/// See OutputStream.output
6162
public func output<S>(to inputStream: S) where S: Async.InputStream, S.Input == Output {
63+
DEBUG("TCPSocketSource.output<\(S.self)>(to: \(inputStream))")
6264
downstream = AnyInputStream(inputStream)
6365
resumeIfSuspended()
6466
}
6567

6668
/// Cancels reading
6769
public func close() {
70+
DEBUG("TCPSocketSource.close()")
6871
guard !isClosed else {
6972
return
7073
}
@@ -84,12 +87,14 @@ public final class TCPSocketSource: Async.OutputStream {
8487
/// important: the socket _must_ be ready to read data
8588
/// as indicated by a read source.
8689
private func readData() {
90+
DEBUG("TCPSocketSource.readData()")
8791
guard let downstream = self.downstream else {
8892
ERROR("Unexpected nil downstream on SocketSource during readData.")
8993
return
9094
}
9195
do {
9296
let read = try socket.read(into: buffer)
97+
DEBUG("TCPSocketSource.socket.read() -> \(read)")
9398
switch read {
9499
case .success(let count):
95100
guard count > 0 else {
@@ -98,10 +103,12 @@ public final class TCPSocketSource: Async.OutputStream {
98103
}
99104

100105
let view = ByteBuffer(start: buffer.baseAddress, count: count)
106+
DEBUG("TCPSocketSource.view = \(String(bytes: view, encoding: .ascii) ?? "nil")")
101107
downstreamIsReady = false
102108
let promise = Promise(Void.self)
103109
downstream.input(.next(view, promise))
104110
promise.future.addAwaiter { result in
111+
DEBUG("TCPSocketSource.downstream.input.future.complete(\(result)) [cancelIsPending: \(self.cancelIsPending)]")
105112
switch result {
106113
case .error(let e): downstream.error(e)
107114
case .expectation:
@@ -129,6 +136,7 @@ public final class TCPSocketSource: Async.OutputStream {
129136

130137
/// Called when the read source signals.
131138
private func readSourceSignal(isCancelled: Bool) {
139+
DEBUG("TCPSocketSource.readSourceSignal(\(isCancelled))")
132140
guard !isCancelled else {
133141
// source is cancelled, we will never receive signals again
134142
cancelIsPending = true
@@ -146,6 +154,7 @@ public final class TCPSocketSource: Async.OutputStream {
146154
ERROR("SocketSource readSource illegally nil during signal.")
147155
return
148156
}
157+
DEBUG("TCPSocketSource.resumeIfSuspended() [sourceIsSuspended: \(sourceIsSuspended)]")
149158
readSource.suspend()
150159
sourceIsSuspended = true
151160
}
@@ -159,6 +168,7 @@ public final class TCPSocketSource: Async.OutputStream {
159168

160169
/// Resumes the readSource if it was currently suspended.
161170
private func resumeIfSuspended() {
171+
DEBUG("TCPSocketSource.resumeIfSuspended() [sourceIsSuspended: \(sourceIsSuspended)]")
162172
guard sourceIsSuspended else {
163173
return
164174
}

Sources/TCP/Utilties/TCPError.swift

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,5 +63,12 @@ public struct TCPError: Traceable, Debuggable, Helpable, Swift.Error, Encodable
6363
}
6464

6565
func ERROR(_ message: String, file: StaticString = #file, line: Int = #line) {
66-
print("[TCP] \(message) [\(file):\(line)]")
66+
print("[TCP] \(message) [\(file.description.split(separator: "/").last!):\(line)]")
67+
}
68+
69+
/// For printing debug info.
70+
func DEBUG(_ string: @autoclosure () -> String, file: StaticString = #file, line: Int = #line) {
71+
#if VERBOSE
72+
print("[VERBOSE] \(string()) [\(file.description.split(separator: "/").last!):\(line)]")
73+
#endif
6774
}

0 commit comments

Comments
 (0)