New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
trace the debug adapter protocol messages #1023
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
metals/src/main/scala/scala/meta/internal/metals/debug/EndpointLogger.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package scala.meta.internal.metals.debug | ||
|
||
import java.io.PrintWriter | ||
import java.time.Clock | ||
import java.time.format.DateTimeFormatter | ||
import java.util.Collections | ||
import java.util.function.Consumer | ||
import com.google.gson.GsonBuilder | ||
import org.eclipse.lsp4j.jsonrpc.MessageConsumer | ||
import org.eclipse.lsp4j.jsonrpc.debug.json.DebugMessageJsonHandler | ||
import org.eclipse.lsp4j.jsonrpc.json.MessageJsonHandler | ||
import org.eclipse.lsp4j.jsonrpc.messages.Message | ||
import org.eclipse.lsp4j.jsonrpc.messages.NotificationMessage | ||
import org.eclipse.lsp4j.jsonrpc.messages.RequestMessage | ||
import org.eclipse.lsp4j.jsonrpc.messages.ResponseMessage | ||
import scala.meta.internal.metals.debug.EndpointLogger.Direction | ||
import scala.meta.internal.metals.debug.EndpointLogger.Received | ||
import scala.meta.internal.metals.debug.EndpointLogger.Sent | ||
import scala.meta.internal.metals.debug.EndpointLogger.time | ||
|
||
final class EndpointLogger(endpoint: RemoteEndpoint, logger: PrintWriter) | ||
extends RemoteEndpoint { | ||
private val writer: MessageJsonHandler = EndpointLogger.serializer | ||
|
||
override def consume(message: Message): Unit = { | ||
try log(Sent, message) | ||
finally endpoint.consume(message) | ||
} | ||
|
||
override def listen(messageConsumer: MessageConsumer): Unit = { | ||
endpoint.listen { message => | ||
try log(Received, message) | ||
finally messageConsumer.consume(message) | ||
} | ||
} | ||
|
||
override def cancel(): Unit = endpoint.cancel() | ||
|
||
private def log(direction: Direction, message: Message): Unit = { | ||
logger.println(s"[Trace][$time] $direction ${typeOf(message)}:") | ||
writer.serialize(message, logger) | ||
logger.println() | ||
logger.flush() | ||
} | ||
|
||
private def typeOf(message: Message): String = { | ||
message match { | ||
case _: RequestMessage => "request" | ||
case _: ResponseMessage => "response" | ||
case _: NotificationMessage => "notification" | ||
} | ||
} | ||
} | ||
|
||
object EndpointLogger { | ||
sealed trait Direction | ||
final case object Received extends Direction | ||
final case object Sent extends Direction | ||
|
||
private def serializer: MessageJsonHandler = { | ||
val configure: Consumer[GsonBuilder] = { gson => | ||
gson.setPrettyPrinting() | ||
} | ||
new DebugMessageJsonHandler(Collections.emptyMap(), configure) | ||
} | ||
|
||
private val clock = Clock.systemDefaultZone() | ||
private val timeFormat = | ||
DateTimeFormatter.ofPattern("KK:mm:ss a").withZone(clock.getZone) | ||
|
||
def time: String = timeFormat.format(clock.instant()) | ||
} |
39 changes: 2 additions & 37 deletions
39
metals/src/main/scala/scala/meta/internal/metals/debug/RemoteEndpoint.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,10 @@ | ||
package scala.meta.internal.metals.debug | ||
|
||
import java.net.Socket | ||
import java.util.Collections | ||
import org.eclipse.lsp4j.jsonrpc.MessageConsumer | ||
import org.eclipse.lsp4j.jsonrpc.MessageProducer | ||
import org.eclipse.lsp4j.jsonrpc.debug.json.DebugMessageJsonHandler | ||
import org.eclipse.lsp4j.jsonrpc.json.StreamMessageConsumer | ||
import org.eclipse.lsp4j.jsonrpc.json.StreamMessageProducer | ||
import org.eclipse.lsp4j.jsonrpc.messages.Message | ||
import scala.meta.internal.metals.Cancelable | ||
import scala.meta.internal.metals.debug.RemoteEndpoint._ | ||
|
||
private[debug] final class RemoteEndpoint(socket: Socket) | ||
trait RemoteEndpoint | ||
extends MessageConsumer | ||
with MessageProducer | ||
with Cancelable { | ||
private val source = messageSource(socket) | ||
private val target = messageTarget(socket) | ||
|
||
override def consume(message: Message): Unit = { | ||
target.consume(message) | ||
} | ||
|
||
override def listen(consumer: MessageConsumer): Unit = { | ||
source.listen(consumer) | ||
} | ||
|
||
override def cancel(): Unit = { | ||
source.close() | ||
socket.close() | ||
} | ||
} | ||
|
||
private[debug] object RemoteEndpoint { | ||
private val handler = new DebugMessageJsonHandler(Collections.emptyMap()) | ||
|
||
private def messageSource(socket: Socket): StreamMessageProducer = { | ||
new StreamMessageProducer(socket.getInputStream, handler) | ||
} | ||
|
||
private def messageTarget(socket: Socket): MessageConsumer = { | ||
new StreamMessageConsumer(socket.getOutputStream, handler) | ||
} | ||
} | ||
with Cancelable |
41 changes: 41 additions & 0 deletions
41
metals/src/main/scala/scala/meta/internal/metals/debug/SocketEndpoint.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package scala.meta.internal.metals.debug | ||
|
||
import java.net.Socket | ||
import java.util.Collections | ||
import org.eclipse.lsp4j.jsonrpc.MessageConsumer | ||
import org.eclipse.lsp4j.jsonrpc.debug.json.DebugMessageJsonHandler | ||
import org.eclipse.lsp4j.jsonrpc.json.StreamMessageConsumer | ||
import org.eclipse.lsp4j.jsonrpc.json.StreamMessageProducer | ||
import org.eclipse.lsp4j.jsonrpc.messages.Message | ||
import scala.meta.internal.metals.debug.SocketEndpoint._ | ||
|
||
private[debug] final class SocketEndpoint(socket: Socket) | ||
extends RemoteEndpoint { | ||
private val source = messageSource(socket) | ||
private val target = messageTarget(socket) | ||
|
||
override def consume(message: Message): Unit = { | ||
target.consume(message) | ||
} | ||
|
||
override def listen(consumer: MessageConsumer): Unit = { | ||
source.listen(consumer) | ||
} | ||
|
||
override def cancel(): Unit = { | ||
source.close() | ||
socket.close() | ||
} | ||
} | ||
|
||
private[debug] object SocketEndpoint { | ||
private val handler = new DebugMessageJsonHandler(Collections.emptyMap()) | ||
|
||
private def messageSource(socket: Socket): StreamMessageProducer = { | ||
new StreamMessageProducer(socket.getInputStream, handler) | ||
} | ||
|
||
private def messageTarget(socket: Socket): MessageConsumer = { | ||
new StreamMessageConsumer(socket.getOutputStream, handler) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does it work with the inspector tool? It would be nice to keep it compatible.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sadly, no - the format is too much of a hassle to implement it manually. Reusing the implementation present in the
lsp4j.jsonrpc
library is also not viable because it is tightly coupled with their launcher implementation.At one point we might want to change the format to accomodate the inspector but let's delay it for now as it is better to have some logs than none at all.