Skip to content

Commit 3bfb9b8

Browse files
committed
trace
1 parent fb0e3bc commit 3bfb9b8

File tree

1 file changed

+21
-11
lines changed

1 file changed

+21
-11
lines changed

compiler/src/dotty/tools/dotc/reporting/trace.scala

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,22 @@ import config.Config
77
import config.Printers
88
import core.Mode
99

10-
object trace {
11-
10+
class TraceSyntax(val isForced: Boolean) {
1211
@forceInline
1312
def onDebug[TD](question: => String)(op: => TD)(implicit ctx: Context): TD =
1413
conditionally(ctx.settings.YdebugTrace.value, question, false)(op)
1514

1615
@forceInline
1716
def conditionally[TC](cond: Boolean, question: => String, show: Boolean)(op: => TC)(implicit ctx: Context): TC =
18-
if (Config.tracingEnabled) {
17+
if (isForced || Config.tracingEnabled) {
1918
def op1 = op
2019
if (cond) apply[TC](question, Printers.default, show)(op1)
2120
else op1
2221
} else op
2322

2423
@forceInline
2524
def apply[T](question: => String, printer: Printers.Printer, showOp: Any => String)(op: => T)(implicit ctx: Context): T =
26-
if (Config.tracingEnabled) {
25+
if (isForced || Config.tracingEnabled) {
2726
def op1 = op
2827
if (printer.eq(config.Printers.noPrinter)) op1
2928
else doTrace[T](question, printer, showOp)(op1)
@@ -32,9 +31,9 @@ object trace {
3231

3332
@forceInline
3433
def apply[T](question: => String, printer: Printers.Printer, show: Boolean)(op: => T)(implicit ctx: Context): T =
35-
if (Config.tracingEnabled) {
34+
if (isForced || Config.tracingEnabled) {
3635
def op1 = op
37-
if (printer.eq(config.Printers.noPrinter)) op1
36+
if (!isForced && printer.eq(config.Printers.noPrinter)) op1
3837
else doTrace[T](question, printer, if (show) showShowable(_) else alwaysToString)(op1)
3938
}
4039
else op
@@ -68,20 +67,27 @@ object trace {
6867
apply[T](s"==> $q?", (res: Any) => s"<== $q = ${showOp(res)}")(op)
6968
}
7069

71-
def apply[T](leading: => String, trailing: Any => String)(op: => T)(implicit ctx: Context): T =
70+
def apply[T](leading: => String, trailing: Any => String)(op: => T)(implicit ctx: Context): T = {
71+
val log: String => Unit = if (isForced) Console.println else {
72+
var logctx = ctx
73+
while (logctx.reporter.isInstanceOf[StoreReporter]) logctx = logctx.outer
74+
logctx.log(_)
75+
}
76+
apply(leading, trailing, log)(op)
77+
}
78+
79+
def apply[T](leading: => String, trailing: Any => String, log: String => Unit)(op: => T)(implicit ctx: Context): T =
7280
if (ctx.mode.is(Mode.Printing)) op
7381
else {
7482
var finalized = false
75-
var logctx = ctx
76-
while (logctx.reporter.isInstanceOf[StoreReporter]) logctx = logctx.outer
7783
def finalize(result: Any, note: String) =
7884
if (!finalized) {
7985
ctx.base.indent -= 1
80-
logctx.log(s"${ctx.base.indentTab * ctx.base.indent}${trailing(result)}$note")
86+
log(s"${ctx.base.indentTab * ctx.base.indent}${trailing(result)}$note")
8187
finalized = true
8288
}
8389
try {
84-
logctx.log(s"${ctx.base.indentTab * ctx.base.indent}$leading")
90+
log(s"${ctx.base.indentTab * ctx.base.indent}$leading")
8591
ctx.base.indent += 1
8692
val res = op
8793
finalize(res, "")
@@ -92,4 +98,8 @@ object trace {
9298
throw ex
9399
}
94100
}
101+
}
102+
103+
object trace extends TraceSyntax(isForced = false) {
104+
object force extends TraceSyntax(isForced = true)
95105
}

0 commit comments

Comments
 (0)