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