-
Notifications
You must be signed in to change notification settings - Fork 119
/
CliLogger.scala
239 lines (218 loc) · 7.53 KB
/
CliLogger.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
package scala.cli.internal
import bloop.rifle.BloopRifleLogger
import ch.epfl.scala.bsp4j.Location
import ch.epfl.scala.bsp4j as b
import coursier.cache.CacheLogger
import coursier.cache.loggers.{FallbackRefreshDisplay, RefreshLogger}
import org.scalajs.logging.{ScalaConsoleLogger, Level as ScalaJsLevel, Logger as ScalaJsLogger}
import java.io.PrintStream
import scala.build.bsp.protocol.TextEdit
import scala.build.errors.{BuildException, CompositeBuildException, Diagnostic, Severity}
import scala.build.internal.CustomProgressBarRefreshDisplay
import scala.build.internal.util.WarningMessages
import scala.build.internals.FeatureType
import scala.build.options.ShadowingSeq
import scala.build.{ConsoleBloopBuildClient, Logger, Position}
import scala.collection.mutable
import scala.jdk.CollectionConverters.*
import scala.scalanative.build as sn
class CliLogger(
val verbosity: Int,
quiet: Boolean,
progress: Option[Boolean],
out: PrintStream
) extends Logger { logger =>
override def log(diagnostics: Seq[Diagnostic]): Unit = {
val hashMap = new mutable.HashMap[os.Path, Seq[String]]
diagnostics.foreach { d =>
printDiagnostic(
d.positions,
d.severity,
d.message,
hashMap,
d.textEdit
)
}
}
def error(message: String) =
out.println(message)
def message(message: => String) =
if (verbosity >= 0)
out.println(message)
def log(message: => String) =
if (verbosity >= 1)
out.println(message)
def log(message: => String, debugMessage: => String) =
if (verbosity >= 2)
out.println(debugMessage)
else if (verbosity >= 1)
out.println(message)
def debug(message: => String) =
if (verbosity >= 2)
out.println(message)
def printDiagnostic(
positions: Seq[Position],
severity: Severity,
message: String,
contentCache: mutable.Map[os.Path, Seq[String]],
textEditOpt: Option[Diagnostic.TextEdit]
) =
if (positions.isEmpty)
out.println(
s"${ConsoleBloopBuildClient.diagnosticPrefix(severity)} $message"
)
else {
val positions0 = positions.distinct
val filePositions = positions0.collect {
case f: Position.File => f
}
val otherPositions = positions0.filter {
case _: Position.File => false
case _ => true
}
for (f <- filePositions) {
val startPos = new b.Position(f.startPos._1, f.startPos._2)
val endPos = new b.Position(f.endPos._1, f.endPos._2)
val range = new b.Range(startPos, endPos)
val diag = new b.Diagnostic(range, message)
diag.setSeverity(severity.toBsp4j)
diag.setSource("scala-cli")
for (textEdit <- textEditOpt) {
val bTextEdit = TextEdit(range, textEdit.newText)
diag.setData(bTextEdit.toJsonTree())
}
for (file <- f.path) {
val lines = contentCache.getOrElseUpdate(file, os.read(file).linesIterator.toVector)
if (f.startPos._1 < lines.length)
diag.setCode(lines(f.startPos._1))
}
ConsoleBloopBuildClient.printFileDiagnostic(
this,
f.path,
diag
)
}
if (otherPositions.nonEmpty)
ConsoleBloopBuildClient.printOtherDiagnostic(
this,
message,
severity,
otherPositions
)
}
private def printEx(
ex: BuildException,
contentCache: mutable.Map[os.Path, Seq[String]]
): Unit =
ex match {
case c: CompositeBuildException =>
// FIXME We might want to order things here… Or maybe just collect all b.Diagnostics
// below, and order them before printing them.
for (ex <- c.exceptions)
printEx(ex, contentCache)
case _ =>
printDiagnostic(ex.positions, Severity.Error, ex.getMessage(), contentCache, None)
}
def log(ex: BuildException): Unit =
if (verbosity >= 0)
printEx(ex, new mutable.HashMap)
def debug(ex: BuildException): Unit =
if (verbosity >= 2)
printEx(ex, new mutable.HashMap)
def exit(ex: BuildException): Nothing =
flushExperimentalWarnings
if (verbosity < 0)
sys.exit(1)
else if (verbosity == 0) {
printEx(ex, new mutable.HashMap)
sys.exit(1)
}
else
throw new Exception(ex)
def coursierLogger(printBefore: String) =
if (quiet)
CacheLogger.nop
else if (progress.getOrElse(coursier.paths.Util.useAnsiOutput()))
RefreshLogger.create(
CustomProgressBarRefreshDisplay.create(
keepOnScreen = verbosity >= 1,
if (printBefore.nonEmpty) System.err.println(printBefore),
()
)
)
else
RefreshLogger.create(new FallbackRefreshDisplay)
def bloopRifleLogger =
new BloopRifleLogger {
def info(msg: => String) = logger.message(msg)
def debug(msg: => String, ex: Throwable) =
if (verbosity >= 3) {
logger.debug(msg)
if (ex != null)
ex.printStackTrace(out)
}
def error(msg: => String, ex: Throwable) = {
logger.error(s"Error: $msg ($ex)")
if (verbosity >= 1 && ex != null)
ex.printStackTrace(out)
}
def error(msg: => String) = logger.error(msg)
def bloopBspStdout =
if (verbosity >= 2) Some(out)
else None
def bloopBspStderr =
if (verbosity >= 2) Some(out)
else None
def bloopCliInheritStdout = verbosity >= 3
def bloopCliInheritStderr = verbosity >= 3
}
def scalaJsLogger: ScalaJsLogger =
// FIXME Doesn't use 'out'
new ScalaConsoleLogger(
minLevel =
if (verbosity >= 2) ScalaJsLevel.Debug
else if (verbosity >= 1) ScalaJsLevel.Info
else if (verbosity >= 0) ScalaJsLevel.Warn
else ScalaJsLevel.Error
)
def scalaNativeTestLogger: sn.Logger =
new sn.Logger {
def trace(msg: Throwable) = ()
def debug(msg: String) = logger.debug(msg)
def info(msg: String) = logger.log(msg)
def warn(msg: String) = logger.log(msg)
def error(msg: String) = logger.message(msg)
}
val scalaNativeCliInternalLoggerOptions: List[String] =
if (verbosity >= 1) List("-v", "-v", "-v") // debug
else if (verbosity >= 0) List("-v", "-v") // info
else List() // error
// Allow to disable that?
def compilerOutputStream = out
private var experimentalWarnings: Map[FeatureType, Set[String]] = Map.empty
private var reported: Map[FeatureType, Set[String]] = Map.empty
def experimentalWarning(featureName: String, featureType: FeatureType): Unit =
if (!reported.get(featureType).exists(_.contains(featureName)))
experimentalWarnings ++= experimentalWarnings.updatedWith(featureType) {
case None => Some(Set(featureName))
case Some(namesSet) => Some(namesSet + featureName)
}
def flushExperimentalWarnings: Unit = if (experimentalWarnings.nonEmpty) {
val messageStr: String = {
val namesAndTypes = for {
(featureType, names) <- experimentalWarnings.toSeq.sortBy(_._1) // by feature type
name <- names
} yield name -> featureType
WarningMessages.experimentalFeaturesUsed(namesAndTypes)
}
message(messageStr)
reported = for {
(featureType, names) <- experimentalWarnings
reportedNames = reported.getOrElse(featureType, Set.empty[String])
} yield featureType -> (names ++ reportedNames)
experimentalWarnings = Map.empty
}
}
object CliLogger {
def default: CliLogger = new CliLogger(0, false, None, System.err)
}