@@ -7,34 +7,50 @@ import config.Config
7
7
import config .Printers
8
8
import core .Mode
9
9
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
11
27
12
28
@ forceInline
13
29
def onDebug [TD ](question : => String )(op : => TD )(implicit ctx : Context ): TD =
14
30
conditionally(ctx.settings.YdebugTrace .value, question, false )(op)
15
31
16
32
@ forceInline
17
33
def conditionally [TC ](cond : Boolean , question : => String , show : Boolean )(op : => TC )(implicit ctx : Context ): TC =
18
- if (Config .tracingEnabled) {
34
+ if (isForced || Config .tracingEnabled) {
19
35
def op1 = op
20
36
if (cond) apply[TC ](question, Printers .default, show)(op1)
21
37
else op1
22
38
} else op
23
39
24
40
@ forceInline
25
41
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) {
27
43
def op1 = op
28
- if (printer.eq(config.Printers .noPrinter)) op1
44
+ if (! isForced && printer.eq(config.Printers .noPrinter)) op1
29
45
else doTrace[T ](question, printer, showOp)(op1)
30
46
}
31
47
else op
32
48
33
49
@ forceInline
34
50
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) {
36
52
def op1 = op
37
- if (printer.eq(config.Printers .noPrinter)) op1
53
+ if (! isForced && printer.eq(config.Printers .noPrinter)) op1
38
54
else doTrace[T ](question, printer, if (show) showShowable(_) else alwaysToString)(op1)
39
55
}
40
56
else op
@@ -68,20 +84,27 @@ object trace {
68
84
apply[T ](s " ==> $q? " , (res : Any ) => s " <== $q = ${showOp(res)}" )(op)
69
85
}
70
86
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 =
72
97
if (ctx.mode.is(Mode .Printing )) op
73
98
else {
74
99
var finalized = false
75
- var logctx = ctx
76
- while (logctx.reporter.isInstanceOf [StoreReporter ]) logctx = logctx.outer
77
100
def finalize (result : Any , note : String ) =
78
101
if (! finalized) {
79
102
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" )
81
104
finalized = true
82
105
}
83
106
try {
84
- logctx. log(s " ${ctx.base.indentTab * ctx.base.indent}$leading" )
107
+ log(s " ${ctx.base.indentTab * ctx.base.indent}$leading" )
85
108
ctx.base.indent += 1
86
109
val res = op
87
110
finalize(res, " " )
@@ -92,4 +115,4 @@ object trace {
92
115
throw ex
93
116
}
94
117
}
95
- }
118
+ }
0 commit comments