@@ -7,34 +7,50 @@ import config.Config
77import config .Printers
88import core .Mode
99
10- object trace {
10+ /** Exposes the {{{ trace("question") { op } }}} syntax.
11+ *
12+ * Traced operations will print indented messages if enabled.
13+ * Tracing depends on [[Config.tracingEnabled ]] and [[dotty.tools.dotc.config.ScalaSettings.Ylog ]].
14+ * Tracing can be forced by replacing [[trace ]] with [[trace.force ]] (see below).
15+ */
16+ object trace extends TraceSyntax {
17+ final val isForced = false
18+
19+ /** Forces a particular trace to be printed out regardless of tracing being enabled. */
20+ object force extends TraceSyntax {
21+ final val isForced = true
22+ }
23+ }
24+
25+ abstract class TraceSyntax {
26+ val isForced : Boolean
1127
1228 @ forceInline
1329 def onDebug [TD ](question : => String )(op : => TD )(implicit ctx : Context ): TD =
1430 conditionally(ctx.settings.YdebugTrace .value, question, false )(op)
1531
1632 @ forceInline
1733 def conditionally [TC ](cond : Boolean , question : => String , show : Boolean )(op : => TC )(implicit ctx : Context ): TC =
18- if (Config .tracingEnabled) {
34+ if (isForced || Config .tracingEnabled) {
1935 def op1 = op
2036 if (cond) apply[TC ](question, Printers .default, show)(op1)
2137 else op1
2238 } else op
2339
2440 @ forceInline
2541 def apply [T ](question : => String , printer : Printers .Printer , showOp : Any => String )(op : => T )(implicit ctx : Context ): T =
26- if (Config .tracingEnabled) {
42+ if (isForced || Config .tracingEnabled) {
2743 def op1 = op
28- if (printer.eq(config.Printers .noPrinter)) op1
44+ if (! isForced && printer.eq(config.Printers .noPrinter)) op1
2945 else doTrace[T ](question, printer, showOp)(op1)
3046 }
3147 else op
3248
3349 @ forceInline
3450 def apply [T ](question : => String , printer : Printers .Printer , show : Boolean )(op : => T )(implicit ctx : Context ): T =
35- if (Config .tracingEnabled) {
51+ if (isForced || Config .tracingEnabled) {
3652 def op1 = op
37- if (printer.eq(config.Printers .noPrinter)) op1
53+ if (! isForced && printer.eq(config.Printers .noPrinter)) op1
3854 else doTrace[T ](question, printer, if (show) showShowable(_) else alwaysToString)(op1)
3955 }
4056 else op
@@ -68,20 +84,27 @@ object trace {
6884 apply[T ](s " ==> $q? " , (res : Any ) => s " <== $q = ${showOp(res)}" )(op)
6985 }
7086
71- def apply [T ](leading : => String , trailing : Any => String )(op : => T )(implicit ctx : Context ): T =
87+ def apply [T ](leading : => String , trailing : Any => String )(op : => T )(implicit ctx : Context ): T = {
88+ val log : String => Unit = if (isForced) Console .println else {
89+ var logctx = ctx
90+ while (logctx.reporter.isInstanceOf [StoreReporter ]) logctx = logctx.outer
91+ logctx.log(_)
92+ }
93+ doApply(leading, trailing, log)(op)
94+ }
95+
96+ def doApply [T ](leading : => String , trailing : Any => String , log : String => Unit )(op : => T )(implicit ctx : Context ): T =
7297 if (ctx.mode.is(Mode .Printing )) op
7398 else {
7499 var finalized = false
75- var logctx = ctx
76- while (logctx.reporter.isInstanceOf [StoreReporter ]) logctx = logctx.outer
77100 def finalize (result : Any , note : String ) =
78101 if (! finalized) {
79102 ctx.base.indent -= 1
80- logctx. log(s " ${ctx.base.indentTab * ctx.base.indent}${trailing(result)}$note" )
103+ log(s " ${ctx.base.indentTab * ctx.base.indent}${trailing(result)}$note" )
81104 finalized = true
82105 }
83106 try {
84- logctx. log(s " ${ctx.base.indentTab * ctx.base.indent}$leading" )
107+ log(s " ${ctx.base.indentTab * ctx.base.indent}$leading" )
85108 ctx.base.indent += 1
86109 val res = op
87110 finalize(res, " " )
@@ -92,4 +115,4 @@ object trace {
92115 throw ex
93116 }
94117 }
95- }
118+ }
0 commit comments