Skip to content

Commit 3abeed8

Browse files
Add pprint.err.log (#74)
Logging to stderr rather than stdout.
1 parent a1ddeb7 commit 3abeed8

File tree

1 file changed

+34
-1
lines changed

1 file changed

+34
-1
lines changed

pprint/src/pprint/PPrinter.scala

+34-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package pprint
22

3+
import java.io.PrintStream
4+
35
/**
46
*
57
* @param defaultWidth How wide to allow a pretty-printed value to become
@@ -36,7 +38,38 @@ case class PPrinter(defaultWidth: Int = 100,
3638
showFieldNames: Boolean = defaultShowFieldNames)
3739
(implicit line: sourcecode.Line,
3840
fileName: sourcecode.FileName): T = {
41+
doLog(x, tag, width, height, indent, escapeUnicode, showFieldNames, Console.out)(line, fileName)
42+
}
43+
44+
object err {
45+
/**
46+
* Logs a given value to stderr with some metadata to identify where the log
47+
* message came from. Hard-coded and not very flexible, but you can easily
48+
* implement your own log method if you want to customize it further.
49+
*/
50+
def log[T](x: sourcecode.Text[T],
51+
tag: String = "",
52+
width: Int = defaultWidth,
53+
height: Int = defaultHeight,
54+
indent: Int = defaultIndent,
55+
escapeUnicode: Boolean = defaultEscapeUnicode,
56+
showFieldNames: Boolean = defaultShowFieldNames)
57+
(implicit line: sourcecode.Line,
58+
fileName: sourcecode.FileName): T = {
59+
doLog(x, tag, width, height, indent, escapeUnicode, showFieldNames, Console.err)(line, fileName)
60+
}
61+
}
3962

63+
private def doLog[T](x: sourcecode.Text[T],
64+
tag: String,
65+
width: Int,
66+
height: Int,
67+
indent: Int,
68+
escapeUnicode: Boolean,
69+
showFieldNames: Boolean,
70+
out: PrintStream)
71+
(implicit line: sourcecode.Line,
72+
fileName: sourcecode.FileName): T = {
4073
val tagStrs =
4174
if (tag.isEmpty) Seq()
4275
else Seq(fansi.Color.Cyan(tag), fansi.Str(" "))
@@ -61,7 +94,7 @@ case class PPrinter(defaultWidth: Int = 100,
6194
).toSeq
6295
)
6396

64-
println(str)
97+
out.println(str)
6598
x.value
6699
}
67100

0 commit comments

Comments
 (0)