Skip to content

Commit 1924f99

Browse files
committed
Fix captured logs printing with extra indent
1 parent 964dd9b commit 1924f99

File tree

4 files changed

+85
-84
lines changed

4 files changed

+85
-84
lines changed

log-utils/src/main/java/net/minecraftforge/util/logging/CapturingPrintStream.java

Lines changed: 0 additions & 43 deletions
This file was deleted.
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* Copyright (c) Forge Development LLC
3+
* SPDX-License-Identifier: LGPL-2.1-only
4+
*/
5+
package net.minecraftforge.util.logging;
6+
7+
import java.io.OutputStream;
8+
import java.io.PrintStream;
9+
import java.util.function.Consumer;
10+
11+
interface DelegatePrintStream {
12+
PrintStream EMPTY = new Empty();
13+
14+
PrintStream getDelegate();
15+
16+
final class Capturing extends PrintStream implements DelegatePrintStream {
17+
private final PrintStream raw;
18+
19+
public Capturing(Log.Level level, PrintStream stream) {
20+
super(new OutputStream() {
21+
private final Consumer<String> logger = new LogConsumer(level, stream::println);
22+
private StringBuffer buffer = new StringBuffer(512);
23+
24+
@Override
25+
public void write(int b) {
26+
this.write((char) b);
27+
}
28+
29+
private void write(char c) {
30+
if (c == '\n' || c == '\r') {
31+
if (this.buffer.length() != 0) {
32+
logger.accept(this.buffer.insert(0, Log.getIndentation()).toString());
33+
this.buffer = new StringBuffer(512);
34+
}
35+
} else {
36+
this.buffer.append(c);
37+
}
38+
}
39+
});
40+
41+
this.raw = stream;
42+
}
43+
44+
@Override
45+
public PrintStream getDelegate() {
46+
return this.raw;
47+
}
48+
}
49+
50+
final class Empty extends PrintStream implements DelegatePrintStream {
51+
private Empty() {
52+
super(new OutputStream() {
53+
@Override
54+
public void write(int b) { }
55+
});
56+
}
57+
58+
@Override
59+
public boolean checkError() {
60+
return false;
61+
}
62+
63+
@Override
64+
protected void clearError() { }
65+
66+
@Override
67+
public PrintStream getDelegate() {
68+
return this;
69+
}
70+
}
71+
}

log-utils/src/main/java/net/minecraftforge/util/logging/EmptyPrintStream.java

Lines changed: 0 additions & 27 deletions
This file was deleted.

log-utils/src/main/java/net/minecraftforge/util/logging/Log.java

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,19 @@ public final class Log {
3737
/** The lowest level that should be logged. If {@code null}, all logging is completely disabled. */
3838
public static @Nullable Level enabled = Level.INFO;
3939

40-
public static final PrintStream EMPTY = EmptyPrintStream.INSTANCE;
40+
public static final PrintStream EMPTY = DelegatePrintStream.EMPTY;
4141
/** The stream used for {@link Level#DEBUG}. */
42-
public static final PrintStream DEBUG = CapturingPrintStream.of(Level.DEBUG, System.out::println);
42+
public static final PrintStream DEBUG = new DelegatePrintStream.Capturing(Level.DEBUG, System.out);
4343
/** The stream used for {@link Level#QUIET}. */
44-
public static final PrintStream QUIET = CapturingPrintStream.of(Level.QUIET, System.out::println);
44+
public static final PrintStream QUIET = new DelegatePrintStream.Capturing(Level.QUIET, System.out);
4545
/** The stream used for {@link Level#INFO}. */
46-
public static final PrintStream INFO = CapturingPrintStream.of(Level.INFO, System.out::println);
46+
public static final PrintStream INFO = new DelegatePrintStream.Capturing(Level.INFO, System.out);
4747
/** The stream used for {@link Level#WARN}. */
48-
public static final PrintStream WARN = CapturingPrintStream.of(Level.WARN, System.out::println);
48+
public static final PrintStream WARN = new DelegatePrintStream.Capturing(Level.WARN, System.out);
4949
/** The stream used for {@link Level#ERROR}. */
50-
public static final PrintStream ERROR = CapturingPrintStream.of(Level.ERROR, System.err::println);
50+
public static final PrintStream ERROR = new DelegatePrintStream.Capturing(Level.ERROR, System.err);
5151
/** The stream used for {@link Level#FATAL}. */
52-
public static final PrintStream FATAL = CapturingPrintStream.of(Level.FATAL, System.err::println);
52+
public static final PrintStream FATAL = new DelegatePrintStream.Capturing(Level.FATAL, System.err);
5353

5454

5555
/* INDENTATIONS */
@@ -104,10 +104,10 @@ private static String getIndentation(byte indent) {
104104
static @UnknownNullability List<CapturedMessage> CAPTURED;
105105

106106
private static final class CapturedMessage {
107-
private final Log.Level level;
107+
private final Level level;
108108
private final String message;
109109

110-
private CapturedMessage(Log.Level level, String message) {
110+
private CapturedMessage(Level level, String message) {
111111
this.level = level;
112112
this.message = message;
113113
}
@@ -134,7 +134,7 @@ public static void capture() {
134134
CAPTURED = new ArrayList<>(128);
135135
}
136136

137-
static void tryCapture(Consumer<String> logger, Log.Level level, String message) {
137+
static void tryCapture(Consumer<String> logger, Level level, String message) {
138138
if (CAPTURED != null)
139139
CAPTURED.add(new CapturedMessage(level, message));
140140
else
@@ -157,7 +157,7 @@ public static void drop() {
157157
* @see #release(BiConsumer)
158158
*/
159159
public static void release() {
160-
release(Log::logInternal);
160+
release(Log::logCaptured);
161161
}
162162

163163
/**
@@ -166,7 +166,7 @@ public static void release() {
166166
* @param consumer The consumer to release the captured log messages to
167167
* @see #capture()
168168
*/
169-
public static void release(BiConsumer<Log.Level, String> consumer) {
169+
public static void release(BiConsumer<Level, String> consumer) {
170170
if (CAPTURED == null) return;
171171

172172
Iterator<CapturedMessage> itor = CAPTURED.iterator();
@@ -178,8 +178,8 @@ public static void release(BiConsumer<Log.Level, String> consumer) {
178178
}
179179

180180
// so we can use a method reference instead of allocate a lambda
181-
private static void logInternal(Level level, String message) {
182-
Log.log(level, message);
181+
private static void logCaptured(Level level, String message) {
182+
((DelegatePrintStream) Log.getLog(level)).getDelegate().println(message);
183183
}
184184

185185

0 commit comments

Comments
 (0)