-
Notifications
You must be signed in to change notification settings - Fork 5
/
Printer.scala
219 lines (203 loc) · 7.8 KB
/
Printer.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package com.github.johnreedlol.internal
import com.github.johnreedlol.Debug
/**
* Created by johnreed on 4/12/16 for https://github.com/JohnReedLOL/scala-trace-debug
*/
protected[johnreedlol] object Printer {
object Color {
val escape: Char = 27.toChar
val clear: String = escape + "[0m"
val red: String = "[31m"
val black: String = "[30m"
/**
* Uses ANSI color sequences to make the output yellow.
*/
def red(str: String): String = {
escape + red + str + clear
}
def black(str: String): String = {
escape + black + str + clear
}
}
private val mySystemProperty = "ENABLE_TRACE_DEBUG"
/**
* Disabling of traces, asserts, etc. can be specified through the system property
* "ENABLE_TRACE_DEBUG" or the environment variable
* "ENABLE_TRACE_DEBUG". The system property takes precedence over the
* environment variable. See: https://github.com/adamw/scala-macro-debug
*/
val debugDisabled_? : Boolean = {
// Holds true or false if debugging has been enabled or disabled through
// a system property
val systemProperty: Any = {
val tmp: String = System.getProperty(mySystemProperty)
if (tmp == null) null
else {
tmp.trim().toLowerCase() match {
case "true" => true
case "false" => false
case _ => null
}
}
}
// Holds true or false if debugging has been enabled or disabled through
// an environment variable
val environmentProperty: Any = {
val tmp: String = System.getenv(mySystemProperty)
if (tmp == null) null
else {
tmp.trim().toLowerCase() match {
case "true" => true
case "false" => false
case _ => null
}
}
}
// The resulting value
systemProperty match {
case b: Boolean => !b
case _ =>
environmentProperty match {
case b: Boolean => !b
case _ => false
}
}
}
/** The offset of the first line from the base of the stack trace
* The +1 is necessary because the method call traceInternal adds one to the offset of the stack trace
*/
protected[johnreedlol] val newStackOffset: Int = Debug.stackOffset + 1
/** Prints out the object with N lines of stack trace. Do not use with assertions
*
* @param toPrintOutNullable the object to print out. May be "null"
* @param numStackLinesIntended N, the number of lines of stack trace intended. Defaults to zero actual lines of stack trace for negative values
* @param usingStdOut Whether to use standard out for trace (as opposed to std error). Uses standard error by default
* @return The string that would have been printed out if printing were enabled and the string that was printed out because printing was enabled.
*/
protected[johnreedlol] final def traceInternal[A](toPrintOutNullable: A, numStackLinesIntended: Int
, usingStdOut: Boolean = false): String = {
if (debugDisabled_?) {
return ""
}
val toPrintOut: String = if (toPrintOutNullable == null) {
"null"
} else {
toPrintOutNullable.toString
}
var toPrint: String = "\n" + "\"" + toPrintOut + "\"" + " in thread " + Thread.currentThread().getName + ":"
if (numStackLinesIntended > 0) {
val stack: Array[StackTraceElement] = Thread.currentThread().getStackTrace
for (row <- 0 to Math.min(numStackLinesIntended - 1, stack.length - 1 - newStackOffset)) {
val lineNumber: Int = newStackOffset + row
val stackLine: StackTraceElement = stack(lineNumber)
val packageName: String = getPackageName(stackLine)
val myPackageName: String = if (packageName.equals("")) {
""
} else {
" [" + packageName + "]"
}
// The java stack traces use a tab character, not a space
val tab = "\t"
toPrint += "\n" + tab + "at " + stackLine + myPackageName
}
}
if ((!usingStdOut && !Debug.isTraceErrOn) || (usingStdOut && !Debug.isTraceOutOn)) {
// if we are using standard error and tracing to standard error is off, don't print anything
} else {
if (usingStdOut) {
PrintLocker.synchronized {
System.out.println(toPrint)
}
} else {
PrintLocker.synchronized {
System.err.println(toPrint)
}
}
}
toPrint
}
/**
* Gets the package name
*/
protected[internal] def getPackageName(stackLine: StackTraceElement): String = {
try {
val className: Class[_] = Class.forName(stackLine.getClassName)
val stringLocation: String = if (className != null) {
val packageName: String = PackagingDataCalculator.getCodeLocation(className)
if (packageName.endsWith(".jar")) {
packageName
} else {
""
}
} else {
""
}
stringLocation
} catch {
case _: java.lang.Exception => {
""
}
}
}
/** Prints out the object with N lines of stack trace. Meant to be used only for asserts
*
* @param toPrintOutNullable the object to print out. May be "null"
* @param numStackLinesIntended N, the number of lines of stack trace intended. Defaults to zero actual lines of stack trace for negative values
* @param usingStdOut Whether to use standard out for trace (as opposed to std error). Uses standard error by default
* @return The string that would have been printed out if printing were enabled and the string that was printed out because printing was enabled.
*/
protected[johnreedlol] final def internalAssert[A](toPrintOutNullable: A, numStackLinesIntended: Int
, usingStdOut: Boolean = false, assertionTrue_? : Boolean, isFatal_? : Boolean): String = {
if (debugDisabled_? || assertionTrue_?) {
return "" // If assertion is true, print nothing and return empty string.
}
val toPrintOut: String = if (toPrintOutNullable == null) {
"null"
} else {
toPrintOutNullable.toString // calling toString on null is bad
}
var toPrint: String = "\n" + "\"" + toPrintOut + "\"" + " in thread " + Thread.currentThread().getName + ":"
if (numStackLinesIntended > 0) {
// Only make call to Thread.currentThread().getStackTrace if there is a stack to print
val stack: Array[StackTraceElement] = Thread.currentThread().getStackTrace
for (row <- 0 to Math.min(numStackLinesIntended - 1, stack.length - 1 - newStackOffset)) {
val lineNumber: Int = newStackOffset + row
val stackLine: StackTraceElement = stack(lineNumber)
val packageName: String = getPackageName(stackLine)
val myPackageName: String = if (packageName.equals("")) {
""
} else {
" [" + packageName + "]"
}
// The java stack traces use a tab character, not a space
val tab = "\t"
toPrint += "\n" + tab + "at " + stackLine + myPackageName
}
toPrint += "\n" + "^ The above stack trace leads to an assertion failure. ^"
} else {
toPrint += "\n" + "^ An assertion failure has occured. ^"
}
toPrint = Color.red(toPrint)
if ((!isFatal_? && !Debug.isNonFatalAssertOn) || (isFatal_? && !Debug.isFatalAssertOn)) {
// If it is nonfatal and nonFatalAssert is off, don't print anything
} else {
if (usingStdOut) {
PrintLocker.synchronized {
System.out.println(toPrint)
}
} else {
PrintLocker.synchronized {
System.err.println(toPrint)
}
}
if (isFatal_? && Debug.isFatalAssertOn) {
System.exit(7) // if the assertion is fatal and fatal assert is on, exit with system code 7
}
}
toPrint
}
/**
* Ensures that no two threads can print at the same time
*/
private object PrintLocker
}