Skip to content

Commit

Permalink
Add pprint.err.log (#74)
Browse files Browse the repository at this point in the history
Logging to stderr rather than stdout.
  • Loading branch information
alexarchambault authored Dec 21, 2021
1 parent a1ddeb7 commit 3abeed8
Showing 1 changed file with 34 additions and 1 deletion.
35 changes: 34 additions & 1 deletion pprint/src/pprint/PPrinter.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package pprint

import java.io.PrintStream

/**
*
* @param defaultWidth How wide to allow a pretty-printed value to become
Expand Down Expand Up @@ -36,7 +38,38 @@ case class PPrinter(defaultWidth: Int = 100,
showFieldNames: Boolean = defaultShowFieldNames)
(implicit line: sourcecode.Line,
fileName: sourcecode.FileName): T = {
doLog(x, tag, width, height, indent, escapeUnicode, showFieldNames, Console.out)(line, fileName)
}

object err {
/**
* Logs a given value to stderr with some metadata to identify where the log
* message came from. Hard-coded and not very flexible, but you can easily
* implement your own log method if you want to customize it further.
*/
def log[T](x: sourcecode.Text[T],
tag: String = "",
width: Int = defaultWidth,
height: Int = defaultHeight,
indent: Int = defaultIndent,
escapeUnicode: Boolean = defaultEscapeUnicode,
showFieldNames: Boolean = defaultShowFieldNames)
(implicit line: sourcecode.Line,
fileName: sourcecode.FileName): T = {
doLog(x, tag, width, height, indent, escapeUnicode, showFieldNames, Console.err)(line, fileName)
}
}

private def doLog[T](x: sourcecode.Text[T],
tag: String,
width: Int,
height: Int,
indent: Int,
escapeUnicode: Boolean,
showFieldNames: Boolean,
out: PrintStream)
(implicit line: sourcecode.Line,
fileName: sourcecode.FileName): T = {
val tagStrs =
if (tag.isEmpty) Seq()
else Seq(fansi.Color.Cyan(tag), fansi.Str(" "))
Expand All @@ -61,7 +94,7 @@ case class PPrinter(defaultWidth: Int = 100,
).toSeq
)

println(str)
out.println(str)
x.value
}

Expand Down

0 comments on commit 3abeed8

Please sign in to comment.