Skip to content

Commit

Permalink
Bsp: Log correct line and column numbers (#73)
Browse files Browse the repository at this point in the history
Also, deprecate the log level "detail". It was used for messages of
different severities which made it difficult to filter out errors in the
test cases.

Closes #70.
  • Loading branch information
tindzk authored Dec 17, 2019
1 parent e8f95f9 commit 5512473
Show file tree
Hide file tree
Showing 13 changed files with 116 additions and 71 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -671,7 +671,7 @@ In the `cli` section, you can find output-related configuration settings:
```toml
[cli]
# Log level
# Possible values: debug, detail, warn, info, error, silent
# Possible values: debug, warn, info, error, silent
level = "debug"

# Use Unicode characters to indicate log levels
Expand Down
56 changes: 32 additions & 24 deletions src/main/scala/seed/Log.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,54 +13,64 @@ class Log(
import Log._
import LogLevel._

val DetailPrefix = " " * (if (unicode) UnicodeLength else NonUnicodeLength)

def filter(f: String => Boolean): Log =
new Log(m => if (f(m)) this.f(m), map, level, unicode)

def prefix(text: String): Log = new Log(f, text + _, level, unicode)

def debug(message: String): Unit =
def debug(message: String, detail: Boolean = false): Unit =
if (level <= Debug)
f(
foreground(green2)(
bold(if (unicode) "" else "[debug] ") + map(message)
(if (detail) DetailPrefix
else bold(if (unicode) "" else "[debug] ")) +
map(message)
)
)

def detail(message: String, colour: Colour = blue3): Unit =
if (level <= Detail)
f(
(" " * (if (unicode) UnicodeLength else NonUnicodeLength)) +
foreground(colour)(map(message))
)

def info(message: String): Unit =
def info(
message: String,
detail: Boolean = false,
colour: Colour = blue2
): Unit =
if (level <= Info)
f(
foreground(blue2)(
bold(if (unicode) "" else " [info] ") + map(message)
foreground(colour)(
(if (detail) DetailPrefix
else bold(if (unicode) "" else " [info] ")) +
map(message)
)
)

def infoHighlight(message: String): Unit = info(message, detail = true, blue3)

def infoRetainColour(message: String): Unit =
if (level <= Info)
f(
foreground(blue2)(bold(if (unicode) "" else " [info] ")) + map(
message
)
foreground(blue2)(bold(if (unicode) "" else " [info] ")) +
map(message)
)

def warn(message: String): Unit =
def warn(message: String, detail: Boolean = false): Unit =
if (level <= Warn)
f(
foreground(yellow2)(
bold(if (unicode) "" else " [warn] ") + map(message)
(if (detail) DetailPrefix
else bold(if (unicode) "" else " [warn] ")) +
map(message)
)
)

def error(message: String): Unit =
def error(message: String, detail: Boolean = false): Unit =
if (level <= Error)
f(
foreground(red2)(bold(if (unicode) "" else "[error] ") + map(message))
foreground(red2)(
(if (detail) DetailPrefix
else bold(if (unicode) "" else "[error] ")) +
map(message)
)
)

def newLine(): Unit = f(" ")
Expand All @@ -73,14 +83,12 @@ sealed abstract class LogLevel(val index: Int) extends Ordered[LogLevel] {
object LogLevel {
case object Debug extends LogLevel(0)
case object Info extends LogLevel(1)
case object Detail extends LogLevel(2)
case object Warn extends LogLevel(3)
case object Error extends LogLevel(4)
case object Silent extends LogLevel(5)
case object Warn extends LogLevel(2)
case object Error extends LogLevel(3)
case object Silent extends LogLevel(4)

val All = Map(
"debug" -> Debug,
"detail" -> Detail,
"info" -> Info,
"warn" -> Warn,
"error" -> Error,
Expand Down
18 changes: 14 additions & 4 deletions src/main/scala/seed/artefact/ArtefactResolution.scala
Original file line number Diff line number Diff line change
Expand Up @@ -299,12 +299,22 @@ object ArtefactResolution {
val resolvedCachePath = cachePath.getOrElse(seedConfig.resolution.cachePath)

log.info("Configured resolvers:")
log.detail("- " + Ansi.italic(resolvedIvyPath.toString) + " (Ivy)")
log.detail("- " + Ansi.italic(resolvedCachePath.toString) + " (Coursier)")
log.info(
"- " + Ansi.italic(resolvedIvyPath.toString) + " (Ivy)",
detail = true
)
log.info(
"- " + Ansi.italic(resolvedCachePath.toString) + " (Coursier)",
detail = true
)
resolvers.ivy
.foreach(ivy => log.detail("- " + Ansi.italic(ivy.url) + " (Ivy)"))
.foreach(
ivy => log.info("- " + Ansi.italic(ivy.url) + " (Ivy)", detail = true)
)
resolvers.maven
.foreach(maven => log.detail("- " + Ansi.italic(maven) + " (Maven)"))
.foreach(
maven => log.info("- " + Ansi.italic(maven) + " (Maven)", detail = true)
)

def resolve(deps: Set[JavaDep]) =
Coursier.resolveAndDownload(
Expand Down
22 changes: 12 additions & 10 deletions src/main/scala/seed/build/Bloop.scala
Original file line number Diff line number Diff line change
Expand Up @@ -128,24 +128,26 @@ class BloopClient(
params.getDiagnostics.asScala.foreach { diag =>
val lines = diag.getMessage.linesIterator.toList
val lineInfo =
s"[${diag.getRange.getStart.getLine}:${diag.getRange.getStart.getCharacter}]: "
s"[${diag.getRange.getStart.getLine + 1}:${diag.getRange.getStart.getCharacter + 1}]: "
val message = lineInfo + lines.head

if (diag.getSeverity == DiagnosticSeverity.ERROR) {
if (filePath != lastDiagnosticFilePath) log.error(filePath + s" ($id)")
log.detail(message, ColourScheme.red2)
log.error(message, detail = true)
lines.tail.foreach(
l => log.detail((" " * lineInfo.length) + l, ColourScheme.red2)
l => log.error((" " * lineInfo.length) + l, detail = true)
)
} else if (diag.getSeverity == DiagnosticSeverity.INFORMATION || diag.getSeverity == DiagnosticSeverity.HINT) {
if (filePath != lastDiagnosticFilePath) log.info(filePath + s" ($id)")
log.detail(message, ColourScheme.blue2)
lines.tail.foreach(l => log.detail((" " * lineInfo.length) + l))
log.info(message, detail = true)
lines.tail.foreach(
l => log.info((" " * lineInfo.length) + l, detail = true)
)
} else if (diag.getSeverity == DiagnosticSeverity.WARNING) {
if (filePath != lastDiagnosticFilePath) log.warn(filePath + s" ($id)")
log.detail(message, ColourScheme.yellow2)
log.warn(message, detail = true)
lines.tail.foreach(
l => log.detail((" " * lineInfo.length) + l, ColourScheme.yellow2)
l => log.warn((" " * lineInfo.length) + l, detail = true)
)
}
}
Expand Down Expand Up @@ -470,14 +472,14 @@ object Bsp {
val (bspSocketPath, bspProcess) = runBspServer(
projectPath,
new Log(log.f, log.map, log.level, log.unicode) {
override def error(message: String): Unit =
override def error(message: String, detail: Boolean = false): Unit =
// This message is printed to stderr, but it is not an error, therefore
// change log level to 'debug'
if (message.contains("BSP server cancelled, closing socket..."))
// Remove "[E] " from message
debug(message.dropWhile(_ != ' ').tail)
else
super.error(message)
super.error(message, detail)
},
message => log.debug(Ansi.bold("[BSP] ") + message)
)
Expand Down Expand Up @@ -616,7 +618,7 @@ object Bsp {
.foreach { _ =>
for {
_ <- current match {
case None => UIO(())
case None => UIO(())
case Some(c) => if (wait) c.join else c.interrupt.map(_ => ())
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/scala/seed/cli/Build.scala
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ object Build {
_ => ()
) match {
case Left(errors) =>
errors.foreach(log.error)
errors.foreach(log.error(_))
sys.exit(1)
case Right(uio) =>
val result = RTS.unsafeRunSync(uio)
Expand Down
6 changes: 3 additions & 3 deletions src/main/scala/seed/cli/BuildTarget.scala
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ object BuildTarget {
val process = ProcessHelper.runBloop(
projectPath,
customLog,
customLog.info,
customLog.info(_),
Some(modulePath.toAbsolutePath.toString),
Some(buildPath.toAbsolutePath.toString)
)(args: _*)
Expand All @@ -85,7 +85,7 @@ object BuildTarget {
cmd,
buildPath.toAbsolutePath.toString,
customLog,
customLog.info
customLog.info(_)
)
)
)
Expand All @@ -98,7 +98,7 @@ object BuildTarget {
cmd,
buildPath.toAbsolutePath.toString,
customLog,
customLog.info
customLog.info(_)
)

List(if (target.await) Left(process) else Right(process))
Expand Down
2 changes: 1 addition & 1 deletion src/main/scala/seed/cli/Link.scala
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ object Link {
_ => ()
) match {
case Left(errors) =>
errors.foreach(log.error)
errors.foreach(log.error(_))
sys.exit(1)
case Right(uio) =>
val result = RTS.unsafeRunSync(uio)
Expand Down
2 changes: 1 addition & 1 deletion src/main/scala/seed/cli/Run.scala
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ object Run {
_ => ()
) match {
case Left(errors) =>
errors.foreach(log.error)
errors.foreach(log.error(_))
sys.exit(1)
case Right(uio) =>
val result = RTS.unsafeRunSync(uio)
Expand Down
16 changes: 9 additions & 7 deletions src/main/scala/seed/cli/Scaffold.scala
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,11 @@ class Scaffold(log: Log, silent: Boolean = false) {
log.info(
s"${Ansi.italic("Which platform(s) do you want to support?")} [default: ${Ansi.underlined("1, 2")}]"
)
log.detail(s"${Ansi.bold("1.")} JVM")
log.detail(s"${Ansi.bold("2.")} JavaScript")
log.detail(s"${Ansi.bold("3.")} Native (${Ansi.italic("experimental")})")
log.infoHighlight(s"${Ansi.bold("1.")} JVM")
log.infoHighlight(s"${Ansi.bold("2.")} JavaScript")
log.infoHighlight(
s"${Ansi.bold("3.")} Native (${Ansi.italic("experimental")})"
)

readInput[Set[Platform]](
Set(JVM, JavaScript),
Expand All @@ -102,10 +104,10 @@ class Scaffold(log: Log, silent: Boolean = false) {
log.info(
s"${Ansi.italic("Which test framework(s) do you need?")} [default: ${Ansi.underlined("none")}]"
)
log.detail(s"${Ansi.bold("1.")} minitest")
log.detail(s"${Ansi.bold("2.")} ScalaTest")
log.detail(s"${Ansi.bold("3.")} ScalaCheck")
log.detail(s"${Ansi.bold("4.")} µTest")
log.infoHighlight(s"${Ansi.bold("1.")} minitest")
log.infoHighlight(s"${Ansi.bold("2.")} ScalaTest")
log.infoHighlight(s"${Ansi.bold("3.")} ScalaCheck")
log.infoHighlight(s"${Ansi.bold("4.")} µTest")

import TestFramework._
readInput[Set[TestFramework]](
Expand Down
6 changes: 3 additions & 3 deletions src/main/scala/seed/cli/Server.scala
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ object Server {
onBroadcastBuildEvent(wsServer, serverLog)
) match {
case Left(errors) =>
errors.foreach(clientLog.error)
errors.foreach(clientLog.error(_))
wsClient.close()
case Right(uio) => runCommandUio(uio, wsClient)
}
Expand All @@ -187,7 +187,7 @@ object Server {
onBroadcastBuildEvent(wsServer, serverLog)
) match {
case Left(errors) =>
errors.foreach(clientLog.error)
errors.foreach(clientLog.error(_))
wsClient.close()
case Right(uio) => runCommandUio(uio, wsClient)
}
Expand All @@ -204,7 +204,7 @@ object Server {
onBroadcastBuildEvent(wsServer, serverLog)
) match {
case Left(errors) =>
errors.foreach(clientLog.error)
errors.foreach(clientLog.error(_))
wsClient.close()
case Right(uio) => runCommandUio(uio, wsClient)
}
Expand Down
9 changes: 7 additions & 2 deletions src/main/scala/seed/config/util/TomlUtils.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ import scala.util.Try
object TomlUtils {
object Codecs {
implicit val logLevelCodec: Codec[LogLevel] = Codec {
case (Value.Str(level), _, _) if level == "detail" =>
Log.urgent.error(
"The log level 'detail' was deprecated. Using 'info' instead..."
)
Right(LogLevel.Info)
case (Value.Str(level), _, _) =>
LogLevel.All.get(level) match {
case None =>
Expand Down Expand Up @@ -115,11 +120,11 @@ object TomlUtils {
log.error(
s"The $description ${Ansi.italic(path.toString)} is malformed"
)
log.detail(s"${Ansi.bold("Message:")} $message")
log.error(s"${Ansi.bold("Message:")} $message", detail = true)

if (address.nonEmpty) {
val trace = address.map(Ansi.italic).mkString("")
log.detail(s"${Ansi.bold("Trace:")} $trace")
log.error(s"${Ansi.bold("Trace:")} $trace", detail = true)
}

None
Expand Down
11 changes: 6 additions & 5 deletions src/main/scala/seed/process/ProcessHelper.scala
Original file line number Diff line number Diff line change
Expand Up @@ -72,20 +72,21 @@ object ProcessHelper {
): Process =
UIO.effectAsyncInterrupt { termination =>
log.info(s"Running command '${Ansi.italic(cmd.mkString(" "))}'...")
log.detail(
s"Working directory: ${Ansi.italic(cwd.toAbsolutePath.toString)}"
log.debug(
s"Working directory: ${Ansi.italic(cwd.toAbsolutePath.toString)}",
detail = true
)

val pb = new NuProcessBuilder(cmd.asJava)

modulePath.foreach { mp =>
pb.environment().put("MODULE_PATH", mp)
log.detail(s"Module path: ${Ansi.italic(mp)}")
log.debug(s"Module path: ${Ansi.italic(mp)}", detail = true)
}

buildPath.foreach { bp =>
pb.environment().put("BUILD_PATH", bp)
log.detail(s"Build path: ${Ansi.italic(bp)}")
log.debug(s"Build path: ${Ansi.italic(bp)}", detail = true)
}

var destroyed = false
Expand All @@ -96,7 +97,7 @@ object ProcessHelper {
case ProcessOutput.StdOut(output) => onStdOut(output)
case ProcessOutput.StdErr(output) => log.error(output)
},
pid => if (verbose) log.debug("PID: " + pid),
pid => if (verbose) log.debug("PID: " + pid, detail = true),
code =>
if (!destroyed) {
if (code == 0) {
Expand Down
Loading

0 comments on commit 5512473

Please sign in to comment.