Mercurial > hg > graal-compiler
comparison graal/com.oracle.jvmci.hotspot/src/com/oracle/jvmci/hotspot/logging/Logger.java @ 21551:5324104ac4f3
moved com.oracle.graal.hotspot.jvmci classes to com.oracle.jvmci.hotspot module (JBS:GRAAL-53)
author | Doug Simon <doug.simon@oracle.com> |
---|---|
date | Tue, 26 May 2015 17:13:37 +0200 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
21550:f48a6cea31eb | 21551:5324104ac4f3 |
---|---|
1 /* | |
2 * Copyright (c) 2011, 2015, 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.oracle.jvmci.hotspot.logging; | |
24 | |
25 import java.io.*; | |
26 import java.lang.reflect.*; | |
27 import java.util.*; | |
28 | |
29 import com.oracle.graal.debug.*; | |
30 import com.oracle.jvmci.hotspot.*; | |
31 | |
32 /** | |
33 * Scoped logging class used to display the call hierarchy of {@link CompilerToVM} calls. | |
34 */ | |
35 public class Logger { | |
36 | |
37 public static final boolean ENABLED = Boolean.valueOf(System.getProperty("graal.debug")); | |
38 private static final int SPACING = 4; | |
39 private static final ThreadLocal<Logger> loggerTL; | |
40 | |
41 private Deque<Boolean> openStack = new LinkedList<>(); | |
42 private boolean open = false; | |
43 private int level = 0; | |
44 | |
45 private static final PrintStream out; | |
46 | |
47 static { | |
48 if (ENABLED) { | |
49 loggerTL = new ThreadLocal<Logger>() { | |
50 | |
51 @Override | |
52 protected Logger initialValue() { | |
53 return new Logger(); | |
54 } | |
55 }; | |
56 } else { | |
57 loggerTL = null; | |
58 } | |
59 | |
60 PrintStream ps = null; | |
61 String filename = System.getProperty("graal.info_file"); | |
62 if (filename != null && !"".equals(filename)) { | |
63 try { | |
64 ps = new PrintStream(new FileOutputStream(filename)); | |
65 } catch (FileNotFoundException e) { | |
66 e.printStackTrace(); | |
67 ps = null; | |
68 } | |
69 } | |
70 out = ps; | |
71 if (out != null) { | |
72 out.println("start: " + new Date()); | |
73 } | |
74 } | |
75 | |
76 public static void info(String message) { | |
77 if (ENABLED) { | |
78 log(message); | |
79 } else { | |
80 TTY.println(message); | |
81 } | |
82 if (out != null) { | |
83 out.println(message); | |
84 out.flush(); | |
85 } | |
86 } | |
87 | |
88 public static void log(String message) { | |
89 if (ENABLED) { | |
90 Logger logger = loggerTL.get(); | |
91 for (String line : message.split("\n")) { | |
92 if (logger.open) { | |
93 TTY.println("..."); | |
94 logger.open = false; | |
95 } | |
96 TTY.print(space(logger.level)); | |
97 TTY.println(line); | |
98 } | |
99 } | |
100 } | |
101 | |
102 public static void startScope(String message) { | |
103 if (ENABLED) { | |
104 Logger logger = loggerTL.get(); | |
105 if (logger.open) { | |
106 TTY.println("..."); | |
107 logger.open = false; | |
108 } | |
109 TTY.print(space(logger.level)); | |
110 TTY.print(message); | |
111 logger.openStack.push(logger.open); | |
112 logger.open = true; | |
113 logger.level++; | |
114 } | |
115 } | |
116 | |
117 public static void endScope(String message) { | |
118 if (ENABLED) { | |
119 Logger logger = loggerTL.get(); | |
120 logger.level--; | |
121 if (logger.open) { | |
122 TTY.println(message); | |
123 } else { | |
124 TTY.println(space(logger.level) + "..." + message); | |
125 } | |
126 logger.open = logger.openStack.pop(); | |
127 } | |
128 } | |
129 | |
130 private static String[] spaces = new String[50]; | |
131 | |
132 private static String space(int count) { | |
133 assert count >= 0; | |
134 String result; | |
135 if (count >= spaces.length || spaces[count] == null) { | |
136 StringBuilder str = new StringBuilder(); | |
137 for (int i = 0; i < count * SPACING; i++) { | |
138 str.append(' '); | |
139 } | |
140 result = str.toString(); | |
141 if (count < spaces.length) { | |
142 spaces[count] = result; | |
143 } | |
144 } else { | |
145 result = spaces[count]; | |
146 } | |
147 return result; | |
148 } | |
149 | |
150 public static String pretty(Object value) { | |
151 if (value == null) { | |
152 return "null"; | |
153 } | |
154 | |
155 Class<?> klass = value.getClass(); | |
156 if (value instanceof Void) { | |
157 return "void"; | |
158 } else if (value instanceof String) { | |
159 return "\"" + value + "\""; | |
160 } else if (value instanceof Method) { | |
161 return "method \"" + ((Method) value).getName() + "\""; | |
162 } else if (value instanceof Class<?>) { | |
163 return "class \"" + ((Class<?>) value).getSimpleName() + "\""; | |
164 } else if (value instanceof Integer) { | |
165 if ((Integer) value < 10) { | |
166 return value.toString(); | |
167 } | |
168 return value + " (0x" + Integer.toHexString((Integer) value) + ")"; | |
169 } else if (value instanceof Long) { | |
170 if ((Long) value < 10 && (Long) value > -10) { | |
171 return value + "l"; | |
172 } | |
173 return value + "l (0x" + Long.toHexString((Long) value) + "l)"; | |
174 } else if (klass.isArray()) { | |
175 StringBuilder str = new StringBuilder(); | |
176 int dimensions = 0; | |
177 while (klass.isArray()) { | |
178 dimensions++; | |
179 klass = klass.getComponentType(); | |
180 } | |
181 int length = Array.getLength(value); | |
182 str.append(klass.getSimpleName()).append('[').append(length).append(']'); | |
183 for (int i = 1; i < dimensions; i++) { | |
184 str.append("[]"); | |
185 } | |
186 str.append(" {"); | |
187 for (int i = 0; i < length; i++) { | |
188 str.append(pretty(Array.get(value, i))); | |
189 if (i < length - 1) { | |
190 str.append(", "); | |
191 } | |
192 } | |
193 str.append('}'); | |
194 return str.toString(); | |
195 } | |
196 | |
197 return value.toString(); | |
198 } | |
199 } |