forked from JohnReedLOL/scala-trace-debug
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.scala
199 lines (145 loc) · 6.23 KB
/
Main.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package my.pkg
/**
* Created by johnreed on 3/23/16. Run with sbt test:run
*/
object Main {
def main(args: Array[String]) {
def sleep() = Thread.sleep(60) // to prevent output mangling
import scala.trace.Pos
Pos.err(null)
Pos.err("Standard error") ; sleep()
Pos.out("Hello World")
Pos.out(null)
println("an error message" + Pos()) /*position*/ ; sleep()
import scala.trace.Debug
// debug traces are clickable
Debug.err("trace to standard error")
Debug.err("trace to standard error", numLines = 2)
Debug.arrayErr(Array(1,2,3))
Debug.arrayErr(Array(1,2,3), numElements = 2, numLines = 2) ; sleep()
Debug.out("trace to standard out")
Debug.out("trace to standard out", numLines = 2)
Debug.arrayOut(Array(1,2,3))
Debug.arrayOut(Array(1,2,3), start = 1, numLines = 2) ; sleep()
// assertions are totally fatal (full stop)
Debug.assert(1 == 1, "one must equal one")
Debug.assertOut(1 == 1, "one must equal one") ; sleep()
// turn them off like this
Debug.fatalAssertOff()
Debug.assert(1 == 2, "one must equal two") // nothing happens
// checks are non-fatal (no thread death)
Debug.check("one" == 2, "one must equal one") ; sleep()
// output is colored bright red for visibility
Debug.checkOut("one" == 2, "one must equal one", numLines = 1) ; sleep()
import scala.trace.Macro
// Macro methods use advanced Scala features to print code and types
Macro.contentsOut(List(1, 2, 3))
Macro.contentsOut(List(1, 2, 3), numLines = 2) ; sleep()
Macro.contentsErr(List(1, 2, 3))
Macro.contentsErr(List(1, 2, 3), numLines = 2) ; sleep()
Macro.checkCode("one" == 2)
Macro.assertCode("one" == "one") ; sleep()
val (one, two, three) = (1, 2, 3)
// desugaring includes all the parenthesis
Macro.desugarOut(one + two / three)
Macro.codeOut(one + two / three) ; sleep()
Macro.codeOut(one + two / three, 2) ; sleep()
Macro.desugarErr(one + two / three)
Macro.codeErr(one + two / three) ; sleep()
import scala.trace.implicitlyTraceable
// you can easily remove calls to ".out" and ".err" from the source by pressing Ctr-R (find-replace)
"foo bar baz".out
"foo bar baz".err ; sleep()
println("")
import scala.trace.implicitlyAssertable
"foo bar".assertEq("foo bar", "foo bar must equal foo bar")
2.check(_ + 3 == 5, "two plus three is five")
import scala.trace.Format
// Formatting makes the string more readable
println(Format.text("This is a really really really really really really" +
"really really really really really really really really really really" +
"really really really really really really really really" +
"long string that needs to be formatted because it is longer than 100 chars default \n"))
import scala.trace.implicitlyFormatable
// Instead of using Format you can use the .wrap method
println(("This is a really,really,really,really,really,really,really,really,really,really,really," +
",really,really,really,really,really," +
",really,really,really,really,really,," +
"long,string,that,needs,to,be,formatted,because,it,is,longer,than," + Format.getLineLength
+ ",chars").wrap(delimiter = ","))
// output
/*
Standard error - my.pkg.Main.main(Main.scala:12)
Hello World - my.pkg.Main.main(Main.scala:13)
an error message - my.pkg.Main.main(Main.scala:14)
"trace to standard error" in thread main:
at my.pkg.Main$.main(Main.scala:20)
"trace to standard error" in thread main:
at my.pkg.Main$.main(Main.scala:21)
at my.pkg.Main.main(Main.scala)
" 1, 2, 3
" in thread main:
at my.pkg.Main$.main(Main.scala:22)
" 1, 2
" in thread main:
at my.pkg.Main$.main(Main.scala:23)
at my.pkg.Main.main(Main.scala)
"trace to standard out" in thread main:
at my.pkg.Main$.main(Main.scala:25)
"trace to standard out" in thread main:
at my.pkg.Main$.main(Main.scala:26)
at my.pkg.Main.main(Main.scala)
" 1, 2, 3
" in thread main:
at my.pkg.Main$.main(Main.scala:27)
" 2, 3
" in thread main:
at my.pkg.Main$.main(Main.scala:28)
at my.pkg.Main.main(Main.scala)
"one must equal one" in thread main:
at my.pkg.Main$.main(Main.scala:39)
at my.pkg.Main.main(Main.scala)
^ The above stack trace leads to an assertion failure. ^
"one must equal one" in thread main:
at my.pkg.Main$.main(Main.scala:41)
^ The above stack trace leads to an assertion failure. ^
"Contains: Int 1, 2, 3
" in thread main:
at my.pkg.Main$.main(Main.scala:47)
"Contains: Int 1, 2, 3
" in thread main:
at my.pkg.Main$.main(Main.scala:48)
at my.pkg.Main.main(Main.scala)
"Contains: Int 1, 2, 3
" in thread main:
at my.pkg.Main$.main(Main.scala:50)
"Contains: Int 1, 2, 3
" in thread main:
at my.pkg.Main$.main(Main.scala:51)
at my.pkg.Main.main(Main.scala)
"("one" == 2) -> false" in thread main:
at my.pkg.Main$.main(Main.scala:53)
at my.pkg.Main.main(Main.scala)
^ The above stack trace leads to an assertion failure. ^
"one.+(two./(three)) -> 1" in thread main:
at my.pkg.Main$.main(Main.scala:60)
"(one + two / three) -> 1" in thread main:
at my.pkg.Main$.main(Main.scala:61)
"one.+(two./(three)) -> 1" in thread main:
at my.pkg.Main$.main(Main.scala:63)
"(one + two / three) -> 1" in thread main:
at my.pkg.Main$.main(Main.scala:64)
"foo bar baz" in thread main:
at my.pkg.Main$.main(Main.scala:71)
"foo bar baz" in thread main:
at my.pkg.Main$.main(Main.scala:70)
Disconnected from the target VM, address: '127.0.0.1:46176', transport: 'socket'
This is a really really really really really reallyreally really really really really really really really
really reallyreally really really really really really really reallylong string that needs to be formatted
because it is longer than 100 chars default
This is a really,really,really,really,really,really,really,really,really,really,really,,really,really,
really,really,really,,really,really,really,really,really,,long,string,that,needs,to,be,formatted,because,
it,is,longer,than,100,chars,
*/
}
}