comparison graal/GraalCompiler/src/com/sun/c1x/debug/TTY.java @ 2509:16b9a8b5ad39

Renamings Runtime=>GraalRuntime and Compiler=>GraalCompiler
author Thomas Wuerthinger <thomas@wuerthinger.net>
date Wed, 27 Apr 2011 11:50:44 +0200
parents graal/Compiler/src/com/sun/c1x/debug/TTY.java@9ec15d6914ca
children
comparison
equal deleted inserted replaced
2508:fea94949e0a2 2509:16b9a8b5ad39
1 /*
2 * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23 package com.sun.c1x.debug;
24
25 import java.io.*;
26 import java.util.regex.*;
27
28 import com.sun.c1x.util.*;
29
30
31 /**
32 * A collection of static methods for printing debug and informational output to a global {@link LogStream}.
33 * The output can be (temporarily) suppressed per thread through use of a {@linkplain Filter filter}.
34 *
35 * @author Doug Simon
36 */
37 public class TTY {
38
39 /**
40 * Support for thread-local suppression of {@link TTY}.
41 *
42 * @author Doug Simon
43 */
44 public static class Filter {
45 private LogStream previous;
46 private final Thread thread = Thread.currentThread();
47
48 /**
49 * Creates an object that will suppress {@link TTY} for the current thread if the given filter does not
50 * {@linkplain #matches(String, Object) match} the given object. To revert the suppression state to how it was
51 * before this call, the {@link #remove()} method must be called on the suppression object.
52 *
53 * @param filter the pattern for matching. If {@code null}, then the match is successful. If it starts with "~",
54 * then a regular expression {@linkplain Pattern#matches(String, CharSequence) match} is performed
55 * where the regular expression is specified by {@code filter} without the "~" prefix. Otherwise, a
56 * simple {@linkplain String#contains(CharSequence) substring} match is performed where {@code
57 * filter} is the substring used.
58 * @param object an object whose {@linkplain Object#toString() string} value is matched against {@code filter}
59 */
60 public Filter(String filter, Object object) {
61 boolean suppressed = false;
62 if (filter != null) {
63 String input = object.toString();
64 if (filter.startsWith("~")) {
65 suppressed = !Pattern.matches(filter.substring(1), input);
66 } else {
67 suppressed = !input.contains(filter);
68 }
69 if (suppressed) {
70 previous = out();
71 out.set(LogStream.SINK);
72 }
73 }
74 }
75
76 /**
77 * Reverts the suppression state of {@link TTY} to how it was before this object was constructed.
78 */
79 public void remove() {
80 assert thread == Thread.currentThread();
81 if (previous != null) {
82 out.set(previous);
83 }
84 }
85 }
86
87 public static final String C1X_TTY_LOG_FILE_PROPERTY = "c1x.tty.file";
88
89 private static final LogStream log;
90 static {
91 PrintStream out = System.out;
92 String value = System.getProperty(C1X_TTY_LOG_FILE_PROPERTY);
93 if (value != null) {
94 try {
95 out = new PrintStream(new FileOutputStream(value));
96 } catch (FileNotFoundException e) {
97 Util.warning("Could not open log file " + value + ": " + e);
98 }
99 }
100 log = new LogStream(out);
101 }
102
103 private static final ThreadLocal<LogStream> out = new ThreadLocal<LogStream>() {
104 @Override
105 protected LogStream initialValue() {
106 return log;
107 };
108 };
109
110 public static boolean isSuppressed() {
111 return out.get() == LogStream.SINK;
112 }
113
114 /**
115 * Gets the thread-local log stream to which the static methods of this class send their output.
116 * This will either be a global log stream or the global {@linkplain LogStream#SINK sink} depending
117 * on whether any suppression {@linkplain Filter filters} are in effect for the current thread.
118 */
119 public static LogStream out() {
120 return out.get();
121 }
122
123 /**
124 * @see LogStream#print(String)
125 */
126 public static void print(String s) {
127 out().print(s);
128 }
129
130 /**
131 * @see LogStream#print(int)
132 */
133 public static void print(int i) {
134 out().print(i);
135 }
136
137 /**
138 * @see LogStream#print(long)
139 */
140 public static void print(long i) {
141 out().print(i);
142 }
143
144 /**
145 * @see LogStream#print(char)
146 */
147 public static void print(char c) {
148 out().print(c);
149 }
150
151 /**
152 * @see LogStream#print(boolean)
153 */
154 public static void print(boolean b) {
155 out().print(b);
156 }
157
158 /**
159 * @see LogStream#print(double)
160 */
161 public static void print(double d) {
162 out().print(d);
163 }
164
165 /**
166 * @see LogStream#print(float)
167 */
168 public static void print(float f) {
169 out().print(f);
170 }
171
172 /**
173 * @see LogStream#println(String)
174 */
175 public static void println(String s) {
176 out().println(s);
177 }
178
179 /**
180 * @see LogStream#println()
181 */
182 public static void println() {
183 out().println();
184 }
185
186 /**
187 * @see LogStream#println(int)
188 */
189 public static void println(int i) {
190 out().println(i);
191 }
192
193 /**
194 * @see LogStream#println(long)
195 */
196 public static void println(long l) {
197 out().println(l);
198 }
199
200 /**
201 * @see LogStream#println(char)
202 */
203 public static void println(char c) {
204 out().println(c);
205 }
206
207 /**
208 * @see LogStream#println(boolean)
209 */
210 public static void println(boolean b) {
211 out().println(b);
212 }
213
214 /**
215 * @see LogStream#println(double)
216 */
217 public static void println(double d) {
218 out().println(d);
219 }
220
221 /**
222 * @see LogStream#println(float)
223 */
224 public static void println(float f) {
225 out().println(f);
226 }
227
228 public static void print(String format, Object... args) {
229 out().printf(format, args);
230 }
231
232 public static void println(String format, Object... args) {
233 out().printf(format + "%n", args);
234 }
235
236 public static void fillTo(int i) {
237 out().fillTo(i, ' ');
238 }
239 }