@@ -7,27 +7,39 @@ import config.Config
77import config .Printers
88import core .Mode
99
10+ object trace extends TraceSyntax :
11+ inline def isEnabled = Config .tracingEnabled
12+ protected val isForced = false
13+
14+ object force extends TraceSyntax :
15+ inline def isEnabled : true = true
16+ protected val isForced = true
17+ end trace
18+
1019/** This module is carefully optimized to give zero overhead if Config.tracingEnabled
1120 * is false. The `trace` operation is called in various hotspots, so every tiny bit
1221 * of overhead is unacceptable: boxing, closures, additional method calls are all out.
1322 */
14- object trace :
23+ trait TraceSyntax :
24+
25+ inline def isEnabled : Boolean
26+ protected val isForced : Boolean
1527
1628 inline def onDebug [TD ](inline question : String )(inline op : TD )(using Context ): TD =
1729 conditionally(ctx.settings.YdebugTrace .value, question, false )(op)
1830
1931 inline def conditionally [TC ](inline cond : Boolean , inline question : String , inline show : Boolean )(inline op : TC )(using Context ): TC =
20- if Config .tracingEnabled then
32+ inline if isEnabled then
2133 apply(question, if cond then Printers .default else Printers .noPrinter, show)(op)
2234 else op
2335
2436 inline def apply [T ](inline question : String , inline printer : Printers .Printer , inline showOp : Any => String )(inline op : T )(using Context ): T =
25- if Config .tracingEnabled then
37+ inline if isEnabled then
2638 doTrace[T ](question, printer, showOp)(op)
2739 else op
2840
2941 inline def apply [T ](inline question : String , inline printer : Printers .Printer , inline show : Boolean )(inline op : T )(using Context ): T =
30- if Config .tracingEnabled then
42+ inline if isEnabled then
3143 doTrace[T ](question, printer, if show then showShowable(_) else alwaysToString)(op)
3244 else op
3345
@@ -50,7 +62,7 @@ object trace:
5062 printer : Printers .Printer = Printers .default,
5163 showOp : Any => String = alwaysToString)
5264 (op : => T )(using Context ): T =
53- if ctx.mode.is(Mode .Printing ) || (printer eq Printers .noPrinter) then op
65+ if ctx.mode.is(Mode .Printing ) || ! isForced && (printer eq Printers .noPrinter) then op
5466 else
5567 // Avoid evaluating question multiple time, since each evaluation
5668 // may cause some extra logging output.
@@ -61,18 +73,19 @@ object trace:
6173 var logctx = ctx
6274 while logctx.reporter.isInstanceOf [StoreReporter ] do logctx = logctx.outer
6375 def margin = ctx.base.indentTab * ctx.base.indent
76+ def doLog (s : String ) = if isForced then println(s) else report.log(s)
6477 def finalize (result : Any , note : String ) =
6578 if ! finalized then
6679 ctx.base.indent -= 1
67- report.log (s " $margin${trailing(result)}$note" )
80+ doLog (s " $margin${trailing(result)}$note" )
6881 finalized = true
6982 try
70- report.log (s " $margin$leading" )
83+ doLog (s " $margin$leading" )
7184 ctx.base.indent += 1
7285 val res = op
7386 finalize(res, " " )
7487 res
7588 catch case ex : Throwable =>
7689 finalize(" <missing>" , s " (with exception $ex) " )
7790 throw ex
78- end trace
91+ end TraceSyntax
0 commit comments