Skip to content

Commit 11736b6

Browse files
committed
Add mill.api.Debug helper (#6311)
Easier to use than constantly writing `mill.constants.DebugLog.println("foo " + pprint.apply(foo))` Also add a check that `DebugLog.println` isn't run in CI, so we don't accidentally forget to remove some calls before publishing
1 parent 5d6e4b1 commit 11736b6

File tree

7 files changed

+44
-7
lines changed

7 files changed

+44
-7
lines changed

core/api/src/mill/api/Debug.scala

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package mill.api
2+
3+
import mill.api.daemon.experimental
4+
5+
import java.io.{ByteArrayOutputStream, PrintStream}
6+
7+
@experimental
8+
object Debug {
9+
def apply[T](
10+
x: sourcecode.Text[T],
11+
tag: String = "",
12+
width: Int = 100,
13+
height: Int = 500,
14+
indent: Int = 2,
15+
escapeUnicode: Boolean = true,
16+
showFieldNames: Boolean = true
17+
)(implicit line: sourcecode.Line, fileName: sourcecode.FileName): T = {
18+
19+
val baos = new ByteArrayOutputStream()
20+
pprint.logTo(
21+
x,
22+
tag,
23+
width,
24+
height,
25+
indent,
26+
escapeUnicode,
27+
showFieldNames,
28+
new PrintStream(baos)
29+
)
30+
mill.constants.DebugLog.println(baos.toString)
31+
x.value
32+
}
33+
}

core/constants/src/mill/constants/DebugLog.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ public static synchronized void apply(String s) {
1414
}
1515

1616
public static synchronized void println(String s) {
17+
if (System.getenv("CI") != null) {
18+
throw new RuntimeException("DebugLog cannot be run in CI");
19+
}
20+
1721
Path path = Paths.get(System.getProperty("user.home"), "mill-debug-log.txt");
1822
try {
1923
if (!Files.exists(path)) Files.createFile(path);

developer.adoc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ The following table contains the main ways you can test the code in
3333

3434
In general, `println` or `pprint.log` should work in most places and be sufficient for
3535
instrumenting and debugging the Mill codebase. In the occasional spot where `println`
36-
doesn't work you can use `mill.constants.DebugLog.println` which writes to a file
37-
`~/mill-debug-log.txt` in your home folder. `DebugLog` is useful for scenarios like
38-
debugging Mill's terminal UI (where `println` would mess things up) or subprocesses
36+
doesn't work you can use `mill.constants.DebugLog.println`/`mill.api.Debug` which writes
37+
to a file `~/mill-debug-log.txt` in your home folder. `DebugLog` is useful for scenarios
38+
like debugging Mill's terminal UI (where `println` would mess things up) or subprocesses
3939
(where stdout/stderr may get captured or used and cannot be used to display your own
4040
debug statements).
4141

integration/ide/gen-idea/resources/extended/idea/mill_modules/mill-build.iml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@
9696
<orderEntry type="library" scope="RUNTIME" name="plexus-container-default-2.1.1.jar" level="project"/>
9797
<orderEntry type="library" scope="RUNTIME" name="plexus-io-3.5.1.jar" level="project"/>
9898
<orderEntry type="library" scope="RUNTIME" name="plexus-utils-4.0.2.jar" level="project"/>
99-
<orderEntry type="library" name="pprint_3-0.9.5.jar" level="project"/>
99+
<orderEntry type="library" name="pprint_3-0.9.6.jar" level="project"/>
100100
<orderEntry type="library" name="requests_3-0.9.0.jar" level="project"/>
101101
<orderEntry type="library" scope="RUNTIME" name="scala-collection-compat_2.13-2.13.0.jar" level="project"/>
102102
<orderEntry type="library" name="scala-collection-compat_3-2.12.0.jar" level="project"/>

integration/ide/gen-idea/resources/extended/idea/mill_modules/mill-build.mill-build.iml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@
9898
<orderEntry type="library" scope="RUNTIME" name="plexus-container-default-2.1.1.jar" level="project"/>
9999
<orderEntry type="library" scope="RUNTIME" name="plexus-io-3.5.1.jar" level="project"/>
100100
<orderEntry type="library" scope="RUNTIME" name="plexus-utils-4.0.2.jar" level="project"/>
101-
<orderEntry type="library" name="pprint_3-0.9.5.jar" level="project"/>
101+
<orderEntry type="library" name="pprint_3-0.9.6.jar" level="project"/>
102102
<orderEntry type="library" name="requests_3-0.9.0.jar" level="project"/>
103103
<orderEntry type="library" scope="RUNTIME" name="scala-collection-compat_2.13-2.13.0.jar" level="project"/>
104104
<orderEntry type="library" name="scala-collection-compat_3-2.12.0.jar" level="project"/>

integration/ide/gen-idea/resources/hello-idea/idea/mill_modules/mill-build.iml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@
9292
<orderEntry type="library" scope="RUNTIME" name="plexus-container-default-2.1.1.jar" level="project"/>
9393
<orderEntry type="library" scope="RUNTIME" name="plexus-io-3.5.1.jar" level="project"/>
9494
<orderEntry type="library" scope="RUNTIME" name="plexus-utils-4.0.2.jar" level="project"/>
95-
<orderEntry type="library" name="pprint_3-0.9.5.jar" level="project"/>
95+
<orderEntry type="library" name="pprint_3-0.9.6.jar" level="project"/>
9696
<orderEntry type="library" name="requests_3-0.9.0.jar" level="project"/>
9797
<orderEntry type="library" scope="RUNTIME" name="scala-collection-compat_2.13-2.13.0.jar" level="project"/>
9898
<orderEntry type="library" name="scala-collection-compat_3-2.12.0.jar" level="project"/>

mill-build/src/millbuild/Deps.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ object Deps {
140140
val osLibVersion = "0.11.5"
141141
val osLib = mvn"com.lihaoyi::os-lib:$osLibVersion"
142142
val osLibWatch = mvn"com.lihaoyi::os-lib-watch:$osLibVersion"
143-
val pprint = mvn"com.lihaoyi::pprint:0.9.5"
143+
val pprint = mvn"com.lihaoyi::pprint:0.9.6"
144144
val mainargs = mvn"com.lihaoyi::mainargs:0.7.7"
145145
val millModuledefsVersion = "0.12.3"
146146
val millModuledefsString = s"com.lihaoyi::mill-moduledefs:${millModuledefsVersion}"

0 commit comments

Comments
 (0)