mirror of https://github.com/sbt/sbt.git
substitute println with logger
This commit is contained in:
parent
0c1c380f71
commit
4fe0d08ab0
|
|
@ -16,9 +16,10 @@ import scala.util.{ Success, Failure }
|
||||||
import sbt.io.syntax._
|
import sbt.io.syntax._
|
||||||
import sbt.io.Hash
|
import sbt.io.Hash
|
||||||
import sbt.internal.server._
|
import sbt.internal.server._
|
||||||
import sbt.internal.util.{ StringEvent, ObjectEvent }
|
import sbt.internal.util.{ StringEvent, ObjectEvent, ConsoleOut, MainAppender }
|
||||||
import sbt.internal.util.codec.JValueFormats
|
import sbt.internal.util.codec.JValueFormats
|
||||||
import sbt.protocol.{ EventMessage, Serialization, ChannelAcceptedEvent }
|
import sbt.protocol.{ EventMessage, Serialization, ChannelAcceptedEvent }
|
||||||
|
import sbt.util.{ Level, Logger, LogExchange }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The command exchange merges multiple command channels (e.g. network and console),
|
* The command exchange merges multiple command channels (e.g. network and console),
|
||||||
|
|
@ -92,9 +93,19 @@ private[sbt] final class CommandExchange {
|
||||||
case Some(xs) => xs
|
case Some(xs) => xs
|
||||||
case None => Set(ServerAuthentication.Token)
|
case None => Set(ServerAuthentication.Token)
|
||||||
}
|
}
|
||||||
|
val serverLogLevel: Level.Value = Level.Debug
|
||||||
def onIncomingSocket(socket: Socket, instance: ServerInstance): Unit = {
|
def onIncomingSocket(socket: Socket, instance: ServerInstance): Unit = {
|
||||||
s.log.info(s"new client connected from: ${socket.getPort}")
|
s.log.info(s"new client connected from: ${socket.getPort}")
|
||||||
val channel = new NetworkChannel(newChannelName, socket, Project structure s, auth, instance)
|
val logger: Logger = {
|
||||||
|
val loggerName = s"network-${socket.getPort}"
|
||||||
|
val log = LogExchange.logger(loggerName, None, None)
|
||||||
|
LogExchange.unbindLoggerAppenders(loggerName)
|
||||||
|
val appender = MainAppender.defaultScreen(s.globalLogging.console)
|
||||||
|
LogExchange.bindLoggerAppenders(loggerName, List(appender -> serverLogLevel))
|
||||||
|
log
|
||||||
|
}
|
||||||
|
val channel =
|
||||||
|
new NetworkChannel(newChannelName, socket, Project structure s, auth, instance, logger)
|
||||||
subscribe(channel)
|
subscribe(channel)
|
||||||
}
|
}
|
||||||
server match {
|
server match {
|
||||||
|
|
|
||||||
|
|
@ -9,6 +9,7 @@ import sbt.internal.protocol._
|
||||||
import sbt.internal.protocol.codec._
|
import sbt.internal.protocol.codec._
|
||||||
import sbt.internal.langserver._
|
import sbt.internal.langserver._
|
||||||
import sbt.internal.util.ObjectEvent
|
import sbt.internal.util.ObjectEvent
|
||||||
|
import sbt.util.Logger
|
||||||
|
|
||||||
private[sbt] case class LangServerError(code: Long, message: String) extends Throwable(message)
|
private[sbt] case class LangServerError(code: Long, message: String) extends Throwable(message)
|
||||||
|
|
||||||
|
|
@ -22,13 +23,14 @@ private[sbt] trait LanguageServerProtocol extends CommandChannel {
|
||||||
protected def authenticate(token: String): Boolean
|
protected def authenticate(token: String): Boolean
|
||||||
protected def authOptions: Set[ServerAuthentication]
|
protected def authOptions: Set[ServerAuthentication]
|
||||||
protected def setInitialized(value: Boolean): Unit
|
protected def setInitialized(value: Boolean): Unit
|
||||||
|
protected def log: Logger
|
||||||
|
|
||||||
protected def onRequestMessage(request: JsonRpcRequestMessage): Unit = {
|
protected def onRequestMessage(request: JsonRpcRequestMessage): Unit = {
|
||||||
|
|
||||||
import sbt.internal.langserver.codec.JsonProtocol._
|
import sbt.internal.langserver.codec.JsonProtocol._
|
||||||
import internalJsonProtocol._
|
import internalJsonProtocol._
|
||||||
|
|
||||||
println(s"onRequestMessage: $request")
|
log.debug(s"onRequestMessage: $request")
|
||||||
request.method match {
|
request.method match {
|
||||||
case "initialize" =>
|
case "initialize" =>
|
||||||
if (authOptions(ServerAuthentication.Token)) {
|
if (authOptions(ServerAuthentication.Token)) {
|
||||||
|
|
@ -70,7 +72,7 @@ private[sbt] trait LanguageServerProtocol extends CommandChannel {
|
||||||
case "xsbti.Problem" =>
|
case "xsbti.Problem" =>
|
||||||
() // ignore
|
() // ignore
|
||||||
case _ =>
|
case _ =>
|
||||||
// println(event)
|
// log.debug(event)
|
||||||
()
|
()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -114,7 +116,7 @@ private[sbt] trait LanguageServerProtocol extends CommandChannel {
|
||||||
private[sbt] def langNotify[A: JsonFormat](method: String, params: A): Unit = {
|
private[sbt] def langNotify[A: JsonFormat](method: String, params: A): Unit = {
|
||||||
val m =
|
val m =
|
||||||
JsonRpcNotificationMessage("2.0", method, Option(Converter.toJson[A](params).get))
|
JsonRpcNotificationMessage("2.0", method, Option(Converter.toJson[A](params).get))
|
||||||
println(s"langNotify: $m")
|
log.debug(s"langNotify: $m")
|
||||||
val bytes = Serialization.serializeNotificationMessage(m)
|
val bytes = Serialization.serializeNotificationMessage(m)
|
||||||
publishBytes(bytes)
|
publishBytes(bytes)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -16,12 +16,14 @@ import sbt.internal.langserver.ErrorCodes
|
||||||
import sbt.internal.protocol.JsonRpcResponseMessage
|
import sbt.internal.protocol.JsonRpcResponseMessage
|
||||||
import sbt.internal.util.ObjectEvent
|
import sbt.internal.util.ObjectEvent
|
||||||
import sbt.internal.util.codec.JValueFormats
|
import sbt.internal.util.codec.JValueFormats
|
||||||
|
import sbt.util.Logger
|
||||||
|
|
||||||
final class NetworkChannel(val name: String,
|
final class NetworkChannel(val name: String,
|
||||||
connection: Socket,
|
connection: Socket,
|
||||||
structure: BuildStructure,
|
structure: BuildStructure,
|
||||||
auth: Set[ServerAuthentication],
|
auth: Set[ServerAuthentication],
|
||||||
instance: ServerInstance)
|
instance: ServerInstance,
|
||||||
|
val log: Logger)
|
||||||
extends CommandChannel
|
extends CommandChannel
|
||||||
with LanguageServerProtocol {
|
with LanguageServerProtocol {
|
||||||
import NetworkChannel._
|
import NetworkChannel._
|
||||||
|
|
@ -106,7 +108,7 @@ final class NetworkChannel(val name: String,
|
||||||
.deserializeCommand(chunk)
|
.deserializeCommand(chunk)
|
||||||
.fold(
|
.fold(
|
||||||
errorDesc =>
|
errorDesc =>
|
||||||
println(
|
log.error(
|
||||||
s"Got invalid chunk from client (${new String(chunk.toArray, "UTF-8")}): " + errorDesc),
|
s"Got invalid chunk from client (${new String(chunk.toArray, "UTF-8")}): " + errorDesc),
|
||||||
onCommand
|
onCommand
|
||||||
)
|
)
|
||||||
|
|
@ -117,7 +119,7 @@ final class NetworkChannel(val name: String,
|
||||||
case Some(_) =>
|
case Some(_) =>
|
||||||
state = InHeader
|
state = InHeader
|
||||||
process()
|
process()
|
||||||
case _ => println("Got invalid chunk from client: " + str)
|
case _ => log.error("Got invalid chunk from client: " + str)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
case _ => ()
|
case _ => ()
|
||||||
|
|
@ -133,7 +135,7 @@ final class NetworkChannel(val name: String,
|
||||||
handleHeader(str) match {
|
handleHeader(str) match {
|
||||||
case Some(_) => process()
|
case Some(_) => process()
|
||||||
case _ =>
|
case _ =>
|
||||||
println("Got invalid header from client: " + str)
|
log.error("Got invalid header from client: " + str)
|
||||||
resetChannelState()
|
resetChannelState()
|
||||||
}
|
}
|
||||||
case _ => ()
|
case _ => ()
|
||||||
|
|
@ -152,7 +154,7 @@ final class NetworkChannel(val name: String,
|
||||||
while (bytesRead != -1 && running.get) {
|
while (bytesRead != -1 && running.get) {
|
||||||
try {
|
try {
|
||||||
bytesRead = in.read(readBuffer)
|
bytesRead = in.read(readBuffer)
|
||||||
println(s"bytesRead: $bytesRead")
|
// log.debug(s"bytesRead: $bytesRead")
|
||||||
if (bytesRead > 0) {
|
if (bytesRead > 0) {
|
||||||
buffer = buffer ++ readBuffer.toVector.take(bytesRead)
|
buffer = buffer ++ readBuffer.toVector.take(bytesRead)
|
||||||
}
|
}
|
||||||
|
|
@ -174,7 +176,7 @@ final class NetworkChannel(val name: String,
|
||||||
onRequestMessage(req)
|
onRequestMessage(req)
|
||||||
} catch {
|
} catch {
|
||||||
case LangServerError(code, message) =>
|
case LangServerError(code, message) =>
|
||||||
println(s"sending error: $code: $message")
|
log.debug(s"sending error: $code: $message")
|
||||||
langError(Option(req.id), code, message)
|
langError(Option(req.id), code, message)
|
||||||
}
|
}
|
||||||
case Left(errorDesc) =>
|
case Left(errorDesc) =>
|
||||||
|
|
@ -188,12 +190,12 @@ final class NetworkChannel(val name: String,
|
||||||
.deserializeCommand(chunk)
|
.deserializeCommand(chunk)
|
||||||
.fold(
|
.fold(
|
||||||
errorDesc =>
|
errorDesc =>
|
||||||
println(
|
log.error(
|
||||||
s"Got invalid chunk from client (${new String(chunk.toArray, "UTF-8")}): " + errorDesc),
|
s"Got invalid chunk from client (${new String(chunk.toArray, "UTF-8")}): " + errorDesc),
|
||||||
onCommand
|
onCommand
|
||||||
)
|
)
|
||||||
case _ =>
|
case _ =>
|
||||||
println(s"Unknown Content-Type: $contentType")
|
log.error(s"Unknown Content-Type: $contentType")
|
||||||
}
|
}
|
||||||
} // if-else
|
} // if-else
|
||||||
}
|
}
|
||||||
|
|
@ -312,7 +314,7 @@ final class NetworkChannel(val name: String,
|
||||||
append(
|
append(
|
||||||
Exec(cmd.commandLine, cmd.execId orElse Some(Exec.newExecId), Some(CommandSource(name))))
|
Exec(cmd.commandLine, cmd.execId orElse Some(Exec.newExecId), Some(CommandSource(name))))
|
||||||
} else {
|
} else {
|
||||||
println(s"ignoring command $cmd before initialization")
|
log.warn(s"ignoring command $cmd before initialization")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -320,12 +322,12 @@ final class NetworkChannel(val name: String,
|
||||||
if (initialized) {
|
if (initialized) {
|
||||||
StandardMain.exchange publishEventMessage SettingQuery.handleSettingQuery(req, structure)
|
StandardMain.exchange publishEventMessage SettingQuery.handleSettingQuery(req, structure)
|
||||||
} else {
|
} else {
|
||||||
println(s"ignoring query $req before initialization")
|
log.warn(s"ignoring query $req before initialization")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
def shutdown(): Unit = {
|
def shutdown(): Unit = {
|
||||||
println("Shutting down client connection")
|
log.info("Shutting down client connection")
|
||||||
running.set(false)
|
running.set(false)
|
||||||
out.close()
|
out.close()
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue