|
8 | 8 | import java.io.PrintStream; |
9 | 9 | import java.util.Locale; |
10 | 10 |
|
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 | + |
12 | 27 | private final Log.Level level; |
13 | | - private final PrintStream delegate; |
14 | | - private boolean trouble; |
| 28 | + private boolean shouldIndent; |
15 | 29 |
|
16 | 30 | DelegatePrintStream(Log.Level level, PrintStream delegate) { |
17 | | - super(System.err); |
| 31 | + super(delegate); |
18 | 32 | this.level = level; |
19 | | - this.delegate = delegate; |
20 | 33 | } |
21 | 34 |
|
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; |
24 | 37 | } |
25 | 38 |
|
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()); |
29 | 44 | } |
30 | 45 |
|
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; |
34 | 50 | } |
35 | 51 |
|
| 52 | + |
| 53 | + /* DELEGATE */ |
| 54 | + |
36 | 55 | @Override |
37 | | - public boolean checkError() { |
38 | | - return this.getDelegate().checkError() || this.trouble; |
| 56 | + public void flush() { |
| 57 | + if (!this.isEnabled()) return; |
| 58 | + super.flush(); |
39 | 59 | } |
40 | 60 |
|
| 61 | + @Override |
| 62 | + public void close() { } |
| 63 | + |
41 | 64 | @Override |
42 | 65 | protected void setError() { |
43 | | - this.trouble = true; |
| 66 | + if (!this.isEnabled()) return; |
| 67 | + super.setError(); |
44 | 68 | } |
45 | 69 |
|
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); |
49 | 74 | } |
50 | 75 |
|
51 | 76 | @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); |
54 | 80 | } |
55 | 81 |
|
56 | 82 | @Override |
57 | 83 | 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); |
59 | 86 | } |
60 | 87 |
|
61 | 88 | @Override |
62 | 89 | public void print(boolean b) { |
63 | | - this.getDelegate().print(b); |
| 90 | + if (!this.isEnabled()) return; |
| 91 | + this.printPre(); |
| 92 | + super.print(b); |
64 | 93 | } |
65 | 94 |
|
66 | 95 | @Override |
67 | 96 | public void print(char c) { |
68 | | - this.getDelegate().print(c); |
| 97 | + this.print(String.valueOf(c)); |
69 | 98 | } |
70 | 99 |
|
71 | 100 | @Override |
72 | 101 | public void print(int i) { |
73 | | - this.getDelegate().print(i); |
| 102 | + this.print(String.valueOf(i)); |
74 | 103 | } |
75 | 104 |
|
76 | 105 | @Override |
77 | 106 | public void print(long l) { |
78 | | - this.getDelegate().print(l); |
| 107 | + this.print(String.valueOf(l)); |
79 | 108 | } |
80 | 109 |
|
81 | 110 | @Override |
82 | 111 | public void print(float f) { |
83 | | - this.getDelegate().print(f); |
| 112 | + this.print(String.valueOf(f)); |
84 | 113 | } |
85 | 114 |
|
86 | 115 | @Override |
87 | 116 | public void print(double d) { |
88 | | - this.getDelegate().print(d); |
| 117 | + this.print(String.valueOf(d)); |
89 | 118 | } |
90 | 119 |
|
91 | 120 | @Override |
92 | 121 | 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()); |
94 | 127 | } |
95 | 128 |
|
96 | 129 | @Override |
97 | 130 | 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); |
99 | 135 | } |
100 | 136 |
|
101 | 137 | @Override |
102 | 138 | public void print(Object obj) { |
103 | | - this.getDelegate().print(obj); |
| 139 | + this.print(String.valueOf(obj)); |
104 | 140 | } |
105 | 141 |
|
106 | 142 | @Override |
107 | 143 | public void println() { |
108 | | - this.getDelegate().println(); |
| 144 | + if (!this.isEnabled()) return; |
| 145 | + this.printPre(); |
| 146 | + super.println(); |
| 147 | + this.printPost(true); |
109 | 148 | } |
110 | 149 |
|
111 | 150 | @Override |
112 | 151 | 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); |
114 | 156 | } |
115 | 157 |
|
116 | 158 | @Override |
117 | 159 | public void println(char x) { |
118 | | - this.getDelegate().println(x); |
| 160 | + this.println(String.valueOf(x)); |
119 | 161 | } |
120 | 162 |
|
121 | 163 | @Override |
122 | 164 | public void println(int x) { |
123 | | - this.getDelegate().println(x); |
| 165 | + this.println(String.valueOf(x)); |
124 | 166 | } |
125 | 167 |
|
126 | 168 | @Override |
127 | 169 | public void println(long x) { |
128 | | - this.getDelegate().println(x); |
| 170 | + this.println(String.valueOf(x)); |
129 | 171 | } |
130 | 172 |
|
131 | 173 | @Override |
132 | 174 | public void println(float x) { |
133 | | - this.getDelegate().println(x); |
| 175 | + this.println(String.valueOf(x)); |
134 | 176 | } |
135 | 177 |
|
136 | 178 | @Override |
137 | 179 | public void println(double x) { |
138 | | - this.getDelegate().println(x); |
| 180 | + this.println(String.valueOf(x)); |
139 | 181 | } |
140 | 182 |
|
141 | 183 | @Override |
142 | 184 | 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); |
144 | 189 | } |
145 | 190 |
|
146 | 191 | @Override |
147 | 192 | 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); |
149 | 197 | } |
150 | 198 |
|
151 | 199 | @Override |
152 | 200 | public void println(Object x) { |
153 | | - this.getDelegate().println(x); |
| 201 | + this.println(String.valueOf(x)); |
154 | 202 | } |
155 | 203 |
|
156 | 204 | @Override |
157 | 205 | public PrintStream printf(String format, Object... args) { |
158 | | - return this.getDelegate().printf(format, args); |
| 206 | + return this.isEnabled() ? super.printf(format, args) : this; |
159 | 207 | } |
160 | 208 |
|
161 | 209 | @Override |
162 | 210 | 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; |
164 | 212 | } |
165 | 213 |
|
166 | 214 | @Override |
167 | 215 | public PrintStream format(String format, Object... args) { |
168 | | - return this.getDelegate().format(format, args); |
| 216 | + return this.isEnabled() ? super.format(format, args) : this; |
169 | 217 | } |
170 | 218 |
|
171 | 219 | @Override |
172 | 220 | 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; |
174 | 222 | } |
175 | 223 |
|
176 | 224 | @Override |
177 | 225 | public PrintStream append(CharSequence csq) { |
178 | | - return this.getDelegate().append(csq); |
| 226 | + return this.isEnabled() ? super.append(csq) : this; |
179 | 227 | } |
180 | 228 |
|
181 | 229 | @Override |
182 | 230 | 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 | + } |
190 | 236 |
|
191 | | - @Override |
192 | | - public void write(byte[] b) throws IOException { |
193 | | - this.getDelegate().write(b); |
| 237 | + return this; |
194 | 238 | } |
195 | 239 |
|
196 | 240 | @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; |
210 | 243 | } |
211 | 244 | } |
0 commit comments