Skip to content

Commit fcf1667

Browse files
committed
Handle indentations within the delegate print stream
1 parent 5fab10b commit fcf1667

File tree

6 files changed

+238
-157
lines changed

6 files changed

+238
-157
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
/**/.project
44
/**/.settings/
55
/**/bin/
6-
/**/test/
76
/**/build/
87
/repo/
98
/cache/

log-utils/build.gradle

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ jar {
2828
}
2929
}
3030

31+
compileJava {
32+
// Java 6 exclusive javac argument that ignores "internal and proprietary usage" warnings for UnsafeHacks
33+
options.forkOptions.jvmArgs += '-XDignore.symbol.file'
34+
}
35+
3136
publishing {
3237
publications.register('mavenJava', MavenPublication).configure {
3338
artifactId = project.name

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

Lines changed: 102 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -8,204 +8,237 @@
88
import java.io.PrintStream;
99
import java.util.Locale;
1010

11-
final class DelegatePrintStream extends PrintStream {
11+
class DelegatePrintStream extends PrintStream {
12+
static final PrintStream EMPTY = new DelegatePrintStream(null, System.err) {
13+
@Override
14+
boolean isEnabled() {
15+
return false;
16+
}
17+
18+
@Override
19+
public boolean checkError() {
20+
return false;
21+
}
22+
23+
@SuppressWarnings({"unused", "ProtectedMemberInFinalClass", "override", "RedundantSuppression"})
24+
protected void clearError() { }
25+
};
26+
1227
private final Log.Level level;
13-
private final PrintStream delegate;
14-
private boolean trouble;
28+
private boolean shouldIndent;
1529

1630
DelegatePrintStream(Log.Level level, PrintStream delegate) {
17-
super(System.err);
31+
super(delegate);
1832
this.level = level;
19-
this.delegate = delegate;
2033
}
2134

22-
PrintStream getDelegate() {
23-
return Log.enabled != null && this.level.compareTo(Log.enabled) >= 0 ? this.delegate : EmptyPrintStream.INSTANCE;
35+
boolean isEnabled() {
36+
return this.level != null && Log.enabled != null && this.level.compareTo(Log.enabled) >= 0;
2437
}
2538

26-
@Override
27-
public void flush() {
28-
this.getDelegate().flush();
39+
private void printPre() {
40+
if (!this.shouldIndent) return;
41+
42+
this.shouldIndent = false;
43+
super.print(Log.getIndentation());
2944
}
3045

31-
@Override
32-
public void close() {
33-
this.getDelegate().close();
46+
private void printPost(boolean newline) {
47+
if (!newline) return;
48+
49+
this.shouldIndent = true;
3450
}
3551

52+
53+
/* DELEGATE */
54+
3655
@Override
37-
public boolean checkError() {
38-
return this.getDelegate().checkError() || this.trouble;
56+
public void flush() {
57+
if (!this.isEnabled()) return;
58+
super.flush();
3959
}
4060

61+
@Override
62+
public void close() { }
63+
4164
@Override
4265
protected void setError() {
43-
this.trouble = true;
66+
if (!this.isEnabled()) return;
67+
super.setError();
4468
}
4569

46-
@SuppressWarnings({"unused", "ProtectedMemberInFinalClass", "override", "RedundantSuppression"})
47-
protected void clearError() {
48-
this.trouble = false;
70+
@Override
71+
public void write(int b) {
72+
if (!this.isEnabled()) return;
73+
super.write(b);
4974
}
5075

5176
@Override
52-
public void write(int b) {
53-
this.getDelegate().write(b);
77+
public void write(byte[] b) throws IOException {
78+
if (!this.isEnabled()) return;
79+
super.write(b);
5480
}
5581

5682
@Override
5783
public void write(byte[] buf, int off, int len) {
58-
this.getDelegate().write(buf, off, len);
84+
if (!this.isEnabled()) return;
85+
super.write(buf, off, len);
5986
}
6087

6188
@Override
6289
public void print(boolean b) {
63-
this.getDelegate().print(b);
90+
if (!this.isEnabled()) return;
91+
this.printPre();
92+
super.print(b);
6493
}
6594

6695
@Override
6796
public void print(char c) {
68-
this.getDelegate().print(c);
97+
this.print(String.valueOf(c));
6998
}
7099

71100
@Override
72101
public void print(int i) {
73-
this.getDelegate().print(i);
102+
this.print(String.valueOf(i));
74103
}
75104

76105
@Override
77106
public void print(long l) {
78-
this.getDelegate().print(l);
107+
this.print(String.valueOf(l));
79108
}
80109

81110
@Override
82111
public void print(float f) {
83-
this.getDelegate().print(f);
112+
this.print(String.valueOf(f));
84113
}
85114

86115
@Override
87116
public void print(double d) {
88-
this.getDelegate().print(d);
117+
this.print(String.valueOf(d));
89118
}
90119

91120
@Override
92121
public void print(char[] s) {
93-
this.getDelegate().print(s);
122+
if (!this.isEnabled()) return;
123+
super.print(s);
124+
125+
if (s[s.length - 1] == '\n')
126+
super.print(Log.getIndentation());
94127
}
95128

96129
@Override
97130
public void print(String s) {
98-
this.getDelegate().print(s);
131+
if (!this.isEnabled()) return;
132+
this.printPre();
133+
super.print(s);
134+
this.printPost(s.indexOf('\n') == s.length() - 1);
99135
}
100136

101137
@Override
102138
public void print(Object obj) {
103-
this.getDelegate().print(obj);
139+
this.print(String.valueOf(obj));
104140
}
105141

106142
@Override
107143
public void println() {
108-
this.getDelegate().println();
144+
if (!this.isEnabled()) return;
145+
this.printPre();
146+
super.println();
147+
this.printPost(true);
109148
}
110149

111150
@Override
112151
public void println(boolean x) {
113-
this.getDelegate().println(x);
152+
if (!this.isEnabled()) return;
153+
this.printPre();
154+
super.println(x);
155+
this.printPost(true);
114156
}
115157

116158
@Override
117159
public void println(char x) {
118-
this.getDelegate().println(x);
160+
this.println(String.valueOf(x));
119161
}
120162

121163
@Override
122164
public void println(int x) {
123-
this.getDelegate().println(x);
165+
this.println(String.valueOf(x));
124166
}
125167

126168
@Override
127169
public void println(long x) {
128-
this.getDelegate().println(x);
170+
this.println(String.valueOf(x));
129171
}
130172

131173
@Override
132174
public void println(float x) {
133-
this.getDelegate().println(x);
175+
this.println(String.valueOf(x));
134176
}
135177

136178
@Override
137179
public void println(double x) {
138-
this.getDelegate().println(x);
180+
this.println(String.valueOf(x));
139181
}
140182

141183
@Override
142184
public void println(char[] x) {
143-
this.getDelegate().println(x);
185+
if (!this.isEnabled()) return;
186+
this.printPre();
187+
super.println(x);
188+
this.printPost(true);
144189
}
145190

146191
@Override
147192
public void println(String x) {
148-
this.getDelegate().println(x);
193+
if (!this.isEnabled()) return;
194+
this.printPre();
195+
super.println(x);
196+
this.printPost(true);
149197
}
150198

151199
@Override
152200
public void println(Object x) {
153-
this.getDelegate().println(x);
201+
this.println(String.valueOf(x));
154202
}
155203

156204
@Override
157205
public PrintStream printf(String format, Object... args) {
158-
return this.getDelegate().printf(format, args);
206+
return this.isEnabled() ? super.printf(format, args) : this;
159207
}
160208

161209
@Override
162210
public PrintStream printf(Locale l, String format, Object... args) {
163-
return this.getDelegate().printf(l, format, args);
211+
return this.isEnabled() ? super.printf(l, format, args) : this;
164212
}
165213

166214
@Override
167215
public PrintStream format(String format, Object... args) {
168-
return this.getDelegate().format(format, args);
216+
return this.isEnabled() ? super.format(format, args) : this;
169217
}
170218

171219
@Override
172220
public PrintStream format(Locale l, String format, Object... args) {
173-
return this.getDelegate().format(l, format, args);
221+
return this.isEnabled() ? super.format(l, format, args) : this;
174222
}
175223

176224
@Override
177225
public PrintStream append(CharSequence csq) {
178-
return this.getDelegate().append(csq);
226+
return this.isEnabled() ? super.append(csq) : this;
179227
}
180228

181229
@Override
182230
public PrintStream append(CharSequence csq, int start, int end) {
183-
return this.getDelegate().append(csq, start, end);
184-
}
185-
186-
@Override
187-
public PrintStream append(char c) {
188-
return this.getDelegate().append(c);
189-
}
231+
if (this.isEnabled()) {
232+
super.append(csq, start, end);
233+
if (csq.charAt(end) == '\n')
234+
super.print(Log.getIndentation());
235+
}
190236

191-
@Override
192-
public void write(byte[] b) throws IOException {
193-
this.getDelegate().write(b);
237+
return this;
194238
}
195239

196240
@Override
197-
public int hashCode() {
198-
return this.getDelegate().hashCode();
199-
}
200-
201-
@SuppressWarnings("EqualsDoesntCheckParameterClass")
202-
@Override
203-
public boolean equals(Object obj) {
204-
return this.getDelegate().equals(obj);
205-
}
206-
207-
@Override
208-
public String toString() {
209-
return this.getDelegate().toString();
241+
public PrintStream append(char c) {
242+
return this.isEnabled() ? super.append(c) : this;
210243
}
211244
}

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

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

0 commit comments

Comments
 (0)