comparison graal/com.oracle.graal.debug/src/com/oracle/graal/debug/LogStream.java @ 7530:5e3d1a68664e

applied mx eclipseformat to all Java files
author Doug Simon <doug.simon@oracle.com>
date Wed, 23 Jan 2013 16:34:57 +0100
parents 8fd4201ce98c
children 1cde96b96673
comparison
equal deleted inserted replaced
7529:4a11124a3563 7530:5e3d1a68664e
24 24
25 import java.io.*; 25 import java.io.*;
26 26
27 /** 27 /**
28 * A utility for printing compiler debug and informational output to an output stream. 28 * A utility for printing compiler debug and informational output to an output stream.
29 * 29 *
30 * A {@link LogStream} instance maintains an internal buffer that is flushed to the underlying 30 * A {@link LogStream} instance maintains an internal buffer that is flushed to the underlying
31 * output stream every time one of the {@code println} methods is invoked, or a newline character 31 * output stream every time one of the {@code println} methods is invoked, or a newline character (
32 * ({@code '\n'}) is written. 32 * {@code '\n'}) is written.
33 * 33 *
34 * All of the {@code print} and {@code println} methods return the {code LogStream} instance 34 * All of the {@code print} and {@code println} methods return the {code LogStream} instance on
35 * on which they were invoked. This allows chaining of these calls to mitigate use of String 35 * which they were invoked. This allows chaining of these calls to mitigate use of String
36 * concatenation by the caller. 36 * concatenation by the caller.
37 * 37 *
38 * A {@code LogStream} maintains a current {@linkplain #indentationLevel() indentation} level. 38 * A {@code LogStream} maintains a current {@linkplain #indentationLevel() indentation} level. Each
39 * Each line of output written to this stream has {@code n} spaces prefixed to it where 39 * line of output written to this stream has {@code n} spaces prefixed to it where {@code n} is the
40 * {@code n} is the value that would be returned by {@link #indentationLevel()} when the first 40 * value that would be returned by {@link #indentationLevel()} when the first character of a new
41 * character of a new line is written. 41 * line is written.
42 * 42 *
43 * A {@code LogStream} maintains a current {@linkplain #position() position} for the current 43 * A {@code LogStream} maintains a current {@linkplain #position() position} for the current line
44 * line being written. This position can be advanced to a specified position by 44 * being written. This position can be advanced to a specified position by
45 * {@linkplain #fillTo(int, char) filling} this stream with a given character. 45 * {@linkplain #fillTo(int, char) filling} this stream with a given character.
46 */ 46 */
47 public class LogStream { 47 public class LogStream {
48 48
49 /** 49 /**
50 * Null output stream that simply swallows any output sent to it. 50 * Null output stream that simply swallows any output sent to it.
51 */ 51 */
52 public static final LogStream SINK = new LogStream(); 52 public static final LogStream SINK = new LogStream();
53 53
54 private static final PrintStream SINK_PS = new PrintStream(new OutputStream() { 54 private static final PrintStream SINK_PS = new PrintStream(new OutputStream() {
55
55 @Override 56 @Override
56 public void write(int b) throws IOException { } 57 public void write(int b) throws IOException {
58 }
57 }); 59 });
58 60
59 private LogStream() { 61 private LogStream() {
60 this.ps = null; 62 this.ps = null;
61 this.lineBuffer = null; 63 this.lineBuffer = null;
83 */ 85 */
84 public static final String LINE_SEPARATOR = System.getProperty("line.separator"); 86 public static final String LINE_SEPARATOR = System.getProperty("line.separator");
85 87
86 /** 88 /**
87 * Creates a new log stream. 89 * Creates a new log stream.
88 * 90 *
89 * @param os the underlying output stream to which prints are sent 91 * @param os the underlying output stream to which prints are sent
90 */ 92 */
91 public LogStream(OutputStream os) { 93 public LogStream(OutputStream os) {
92 ps = os instanceof PrintStream ? (PrintStream) os : new PrintStream(os); 94 ps = os instanceof PrintStream ? (PrintStream) os : new PrintStream(os);
93 lineBuffer = new StringBuilder(100); 95 lineBuffer = new StringBuilder(100);
94 } 96 }
95 97
96 /** 98 /**
97 * Creates a new log stream that shares the same {@linkplain #ps output stream} as a given {@link LogStream}. 99 * Creates a new log stream that shares the same {@linkplain #ps output stream} as a given
98 * 100 * {@link LogStream}.
101 *
99 * @param log a LogStream whose output stream is shared with this one 102 * @param log a LogStream whose output stream is shared with this one
100 */ 103 */
101 public LogStream(LogStream log) { 104 public LogStream(LogStream log) {
102 ps = log.ps; 105 ps = log.ps;
103 lineBuffer = new StringBuilder(100); 106 lineBuffer = new StringBuilder(100);
104 } 107 }
105 108
106 /** 109 /**
107 * Prepends {@link #indentation} to the current output line until its write position is equal to the 110 * Prepends {@link #indentation} to the current output line until its write position is equal to
108 * current {@linkplain #indentationLevel()} level. 111 * the current {@linkplain #indentationLevel()} level.
109 */ 112 */
110 private void indent() { 113 private void indent() {
111 if (ps != null) { 114 if (ps != null) {
112 if (!indentationDisabled && indentationLevel != 0) { 115 if (!indentationDisabled && indentationLevel != 0) {
113 while (lineBuffer.length() < indentationLevel) { 116 while (lineBuffer.length() < indentationLevel) {
142 } 145 }
143 } 146 }
144 147
145 /** 148 /**
146 * Gets the current column position of this log stream. 149 * Gets the current column position of this log stream.
147 * 150 *
148 * @return the current column position of this log stream 151 * @return the current column position of this log stream
149 */ 152 */
150 public int position() { 153 public int position() {
151 return lineBuffer == null ? 0 : lineBuffer.length(); 154 return lineBuffer == null ? 0 : lineBuffer.length();
152 155
153 } 156 }
154 157
155 /** 158 /**
156 * Gets the current indentation level for this log stream. 159 * Gets the current indentation level for this log stream.
157 * 160 *
158 * @return the current indentation level for this log stream. 161 * @return the current indentation level for this log stream.
159 */ 162 */
160 public int indentationLevel() { 163 public int indentationLevel() {
161 return indentationLevel; 164 return indentationLevel;
162 } 165 }
163 166
164 /** 167 /**
165 * Adjusts the current indentation level of this log stream. 168 * Adjusts the current indentation level of this log stream.
166 * 169 *
167 * @param delta 170 * @param delta
168 */ 171 */
169 public void adjustIndentation(int delta) { 172 public void adjustIndentation(int delta) {
170 if (delta < 0) { 173 if (delta < 0) {
171 indentationLevel = Math.max(0, indentationLevel + delta); 174 indentationLevel = Math.max(0, indentationLevel + delta);
195 public void setIndentation(char c) { 198 public void setIndentation(char c) {
196 indentation = c; 199 indentation = c;
197 } 200 }
198 201
199 /** 202 /**
200 * Advances this stream's {@linkplain #position() position} to a given position by 203 * Advances this stream's {@linkplain #position() position} to a given position by repeatedly
201 * repeatedly appending a given character as necessary. 204 * appending a given character as necessary.
202 * 205 *
203 * @param position the position to which this stream's position will be advanced 206 * @param position the position to which this stream's position will be advanced
204 * @param filler the character used to pad the stream 207 * @param filler the character used to pad the stream
205 */ 208 */
206 public LogStream fillTo(int position, char filler) { 209 public LogStream fillTo(int position, char filler) {
207 if (ps != null) { 210 if (ps != null) {
213 return this; 216 return this;
214 } 217 }
215 218
216 /** 219 /**
217 * Writes a boolean value to this stream as {@code "true"} or {@code "false"}. 220 * Writes a boolean value to this stream as {@code "true"} or {@code "false"}.
218 * 221 *
219 * @param b the value to be printed 222 * @param b the value to be printed
220 * @return this {@link LogStream} instance 223 * @return this {@link LogStream} instance
221 */ 224 */
222 public LogStream print(boolean b) { 225 public LogStream print(boolean b) {
223 if (ps != null) { 226 if (ps != null) {
226 } 229 }
227 return this; 230 return this;
228 } 231 }
229 232
230 /** 233 /**
231 * Writes a boolean value to this stream followed by a {@linkplain #LINE_SEPARATOR line separator}. 234 * Writes a boolean value to this stream followed by a {@linkplain #LINE_SEPARATOR line
232 * 235 * separator}.
236 *
233 * @param b the value to be printed 237 * @param b the value to be printed
234 * @return this {@link LogStream} instance 238 * @return this {@link LogStream} instance
235 */ 239 */
236 public LogStream println(boolean b) { 240 public LogStream println(boolean b) {
237 if (ps != null) { 241 if (ps != null) {
242 return this; 246 return this;
243 } 247 }
244 248
245 /** 249 /**
246 * Writes a character value to this stream. 250 * Writes a character value to this stream.
247 * 251 *
248 * @param c the value to be printed 252 * @param c the value to be printed
249 * @return this {@link LogStream} instance 253 * @return this {@link LogStream} instance
250 */ 254 */
251 public LogStream print(char c) { 255 public LogStream print(char c) {
252 if (ps != null) { 256 if (ps != null) {
260 } 264 }
261 return this; 265 return this;
262 } 266 }
263 267
264 /** 268 /**
265 * Writes a character value to this stream followed by a {@linkplain #LINE_SEPARATOR line separator}. 269 * Writes a character value to this stream followed by a {@linkplain #LINE_SEPARATOR line
266 * 270 * separator}.
271 *
267 * @param c the value to be printed 272 * @param c the value to be printed
268 * @return this {@link LogStream} instance 273 * @return this {@link LogStream} instance
269 */ 274 */
270 public LogStream println(char c) { 275 public LogStream println(char c) {
271 if (ps != null) { 276 if (ps != null) {
276 return this; 281 return this;
277 } 282 }
278 283
279 /** 284 /**
280 * Prints an int value. 285 * Prints an int value.
281 * 286 *
282 * @param i the value to be printed 287 * @param i the value to be printed
283 * @return this {@link LogStream} instance 288 * @return this {@link LogStream} instance
284 */ 289 */
285 public LogStream print(int i) { 290 public LogStream print(int i) {
286 if (ps != null) { 291 if (ps != null) {
290 return this; 295 return this;
291 } 296 }
292 297
293 /** 298 /**
294 * Writes an int value to this stream followed by a {@linkplain #LINE_SEPARATOR line separator}. 299 * Writes an int value to this stream followed by a {@linkplain #LINE_SEPARATOR line separator}.
295 * 300 *
296 * @param i the value to be printed 301 * @param i the value to be printed
297 * @return this {@link LogStream} instance 302 * @return this {@link LogStream} instance
298 */ 303 */
299 public LogStream println(int i) { 304 public LogStream println(int i) {
300 if (ps != null) { 305 if (ps != null) {
305 return this; 310 return this;
306 } 311 }
307 312
308 /** 313 /**
309 * Writes a float value to this stream. 314 * Writes a float value to this stream.
310 * 315 *
311 * @param f the value to be printed 316 * @param f the value to be printed
312 * @return this {@link LogStream} instance 317 * @return this {@link LogStream} instance
313 */ 318 */
314 public LogStream print(float f) { 319 public LogStream print(float f) {
315 if (ps != null) { 320 if (ps != null) {
318 } 323 }
319 return this; 324 return this;
320 } 325 }
321 326
322 /** 327 /**
323 * Writes a float value to this stream followed by a {@linkplain #LINE_SEPARATOR line separator}. 328 * Writes a float value to this stream followed by a {@linkplain #LINE_SEPARATOR line separator}
324 * 329 * .
330 *
325 * @param f the value to be printed 331 * @param f the value to be printed
326 * @return this {@link LogStream} instance 332 * @return this {@link LogStream} instance
327 */ 333 */
328 public LogStream println(float f) { 334 public LogStream println(float f) {
329 if (ps != null) { 335 if (ps != null) {
334 return this; 340 return this;
335 } 341 }
336 342
337 /** 343 /**
338 * Writes a long value to this stream. 344 * Writes a long value to this stream.
339 * 345 *
340 * @param l the value to be printed 346 * @param l the value to be printed
341 * @return this {@link LogStream} instance 347 * @return this {@link LogStream} instance
342 */ 348 */
343 public LogStream print(long l) { 349 public LogStream print(long l) {
344 if (ps != null) { 350 if (ps != null) {
348 return this; 354 return this;
349 } 355 }
350 356
351 /** 357 /**
352 * Writes a long value to this stream followed by a {@linkplain #LINE_SEPARATOR line separator}. 358 * Writes a long value to this stream followed by a {@linkplain #LINE_SEPARATOR line separator}.
353 * 359 *
354 * @param l the value to be printed 360 * @param l the value to be printed
355 * @return this {@link LogStream} instance 361 * @return this {@link LogStream} instance
356 */ 362 */
357 public LogStream println(long l) { 363 public LogStream println(long l) {
358 if (ps != null) { 364 if (ps != null) {
363 return this; 369 return this;
364 } 370 }
365 371
366 /** 372 /**
367 * Writes a double value to this stream. 373 * Writes a double value to this stream.
368 * 374 *
369 * @param d the value to be printed 375 * @param d the value to be printed
370 * @return this {@link LogStream} instance 376 * @return this {@link LogStream} instance
371 */ 377 */
372 public LogStream print(double d) { 378 public LogStream print(double d) {
373 if (ps != null) { 379 if (ps != null) {
376 } 382 }
377 return this; 383 return this;
378 } 384 }
379 385
380 /** 386 /**
381 * Writes a double value to this stream followed by a {@linkplain #LINE_SEPARATOR line separator}. 387 * Writes a double value to this stream followed by a {@linkplain #LINE_SEPARATOR line
382 * 388 * separator}.
389 *
383 * @param d the value to be printed 390 * @param d the value to be printed
384 * @return this {@link LogStream} instance 391 * @return this {@link LogStream} instance
385 */ 392 */
386 public LogStream println(double d) { 393 public LogStream println(double d) {
387 if (ps != null) { 394 if (ps != null) {
391 } 398 }
392 return this; 399 return this;
393 } 400 }
394 401
395 /** 402 /**
396 * Writes a {@code String} value to this stream. This method ensures that the {@linkplain #position() position} 403 * Writes a {@code String} value to this stream. This method ensures that the
397 * of this stream is updated correctly with respect to any {@linkplain #LINE_SEPARATOR line separators} 404 * {@linkplain #position() position} of this stream is updated correctly with respect to any
398 * present in {@code s}. 405 * {@linkplain #LINE_SEPARATOR line separators} present in {@code s}.
399 * 406 *
400 * @param s the value to be printed 407 * @param s the value to be printed
401 * @return this {@link LogStream} instance 408 * @return this {@link LogStream} instance
402 */ 409 */
403 public LogStream print(String s) { 410 public LogStream print(String s) {
404 if (ps != null) { 411 if (ps != null) {
425 } 432 }
426 return this; 433 return this;
427 } 434 }
428 435
429 /** 436 /**
430 * Writes a {@code String} value to this stream followed by a {@linkplain #LINE_SEPARATOR line separator}. 437 * Writes a {@code String} value to this stream followed by a {@linkplain #LINE_SEPARATOR line
431 * 438 * separator}.
439 *
432 * @param s the value to be printed 440 * @param s the value to be printed
433 * @return this {@link LogStream} instance 441 * @return this {@link LogStream} instance
434 */ 442 */
435 public LogStream println(String s) { 443 public LogStream println(String s) {
436 if (ps != null) { 444 if (ps != null) {
440 return this; 448 return this;
441 } 449 }
442 450
443 /** 451 /**
444 * Writes a formatted string to this stream. 452 * Writes a formatted string to this stream.
445 * 453 *
446 * @param format a format string as described in {@link String#format(String, Object...)} 454 * @param format a format string as described in {@link String#format(String, Object...)}
447 * @param args the arguments to be formatted 455 * @param args the arguments to be formatted
448 * @return this {@link LogStream} instance 456 * @return this {@link LogStream} instance
449 */ 457 */
450 public LogStream printf(String format, Object... args) { 458 public LogStream printf(String format, Object... args) {
454 return this; 462 return this;
455 } 463 }
456 464
457 /** 465 /**
458 * Writes a {@linkplain #LINE_SEPARATOR line separator} to this stream. 466 * Writes a {@linkplain #LINE_SEPARATOR line separator} to this stream.
459 * 467 *
460 * @return this {@code LogStream} instance 468 * @return this {@code LogStream} instance
461 */ 469 */
462 public LogStream println() { 470 public LogStream println() {
463 if (ps != null) { 471 if (ps != null) {
464 indent(); 472 indent();