Skip to content

Commit 2c09a25

Browse files
authored
An intermediate logging fix (#20)
1 parent b8a4991 commit 2c09a25

File tree

2 files changed

+67
-61
lines changed

2 files changed

+67
-61
lines changed

src/org.openjdk.nashorn/share/classes/org/openjdk/nashorn/internal/lookup/MethodHandleFactory.java

Lines changed: 59 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,6 @@
5858
*/
5959
public final class MethodHandleFactory {
6060

61-
private static final DebugLogger log = Context.getContext().getLogger(StandardMethodHandleFunctionality.class);
62-
6361
private static final MethodHandles.Lookup PUBLIC_LOOKUP = MethodHandles.publicLookup();
6462
private static final MethodHandles.Lookup LOOKUP = MethodHandles.lookup();
6563

@@ -128,6 +126,10 @@ public static MethodHandleFunctionality getFunctionality() {
128126

129127
private static final String VOID_TAG = "[VOID]";
130128

129+
private static void err(final String str) {
130+
Context.getContext().getErr().println(str);
131+
}
132+
131133
/**
132134
* Tracer that is applied before a value is returned from the traced function. It will output the return
133135
* value and its class
@@ -136,11 +138,16 @@ public static MethodHandleFunctionality getFunctionality() {
136138
* @return return value unmodified
137139
*/
138140
static Object traceReturn(final DebugLogger logger, final Object value) {
139-
if (logger.isLoggable(TRACE_LEVEL)) {
140-
final String str = " return" +
141+
if (logger != null && !logger.isLoggable(TRACE_LEVEL)) {
142+
return value;
143+
}
144+
final String str = " return" +
141145
(VOID_TAG.equals(value) ?
142146
";" :
143147
" " + stripName(value) + "; // [type=" + (value == null ? "null]" : stripName(value.getClass()) + ']'));
148+
if (logger == null) {
149+
err(str);
150+
} else {
144151
logger.log(TRACE_LEVEL, str);
145152
}
146153

@@ -159,44 +166,51 @@ static void traceReturnVoid(final DebugLogger logger) {
159166
* @param args arguments to the function
160167
*/
161168
static void traceArgs(final DebugLogger logger, final String tag, final int paramStart, final Object... args) {
162-
if (logger.isLoggable(TRACE_LEVEL)) {
163-
final StringBuilder sb = new StringBuilder();
169+
if (logger != null && !logger.isLoggable(TRACE_LEVEL)) {
170+
return;
171+
}
172+
final StringBuilder sb = new StringBuilder();
164173

165-
sb.append(tag);
174+
sb.append(tag);
166175

167-
for (int i = paramStart; i < args.length; i++) {
168-
if (i == paramStart) {
169-
sb.append(" => args: ");
170-
}
171-
172-
sb.append('\'').
173-
append(stripName(argString(args[i]))).
174-
append('\'').
175-
append(' ').
176-
append('[').
177-
append("type=").
178-
append(args[i] == null ? "null" : stripName(args[i].getClass())).
179-
append(']');
176+
for (int i = paramStart; i < args.length; i++) {
177+
if (i == paramStart) {
178+
sb.append(" => args: ");
179+
}
180180

181-
if (i + 1 < args.length) {
182-
sb.append(", ");
183-
}
181+
sb.append('\'').
182+
append(stripName(argString(args[i]))).
183+
append('\'').
184+
append(' ').
185+
append('[').
186+
append("type=").
187+
append(args[i] == null ? "null" : stripName(args[i].getClass())).
188+
append(']');
189+
190+
if (i + 1 < args.length) {
191+
sb.append(", ");
184192
}
193+
}
185194

195+
if (logger == null) {
196+
err(sb.toString());
197+
} else {
186198
logger.log(TRACE_LEVEL, sb);
187-
stacktrace(logger);
188199
}
200+
stacktrace(logger);
189201
}
190202

191203
private static void stacktrace(final DebugLogger logger) {
192-
if (!PRINT_STACKTRACE) {
204+
if (!PRINT_STACKTRACE || (logger != null && !logger.isLoggable(TRACE_LEVEL))) {
193205
return;
194206
}
195-
if (logger.isLoggable(TRACE_LEVEL)) {
196-
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
197-
final PrintStream ps = new PrintStream(baos);
198-
new Throwable().printStackTrace(ps);
199-
final String st = baos.toString();
207+
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
208+
final PrintStream ps = new PrintStream(baos);
209+
new Throwable().printStackTrace(ps);
210+
final String st = baos.toString();
211+
if (logger == null) {
212+
err(st);
213+
} else {
200214
logger.log(TRACE_LEVEL, st);
201215
}
202216
}
@@ -216,26 +230,14 @@ private static String argString(final Object arg) {
216230
}
217231

218232
if (arg instanceof ScriptObject) {
219-
return arg.toString() +
233+
return arg +
220234
" (map=" + Debug.id(((ScriptObject)arg).getMap()) +
221235
')';
222236
}
223237

224238
return arg.toString();
225239
}
226240

227-
/**
228-
* Add a debug printout to a method handle, tracing parameters and return values
229-
* Output will be unconditional to stderr
230-
*
231-
* @param mh method handle to trace
232-
* @param tag start of trace message
233-
* @return traced method handle
234-
*/
235-
public static MethodHandle addDebugPrintout(final MethodHandle mh, final Object tag) {
236-
return addDebugPrintout(null, Level.OFF, mh, 0, true, tag);
237-
}
238-
239241
/**
240242
* Add a debug printout to a method handle, tracing parameters and return values
241243
*
@@ -249,20 +251,6 @@ public static MethodHandle addDebugPrintout(final DebugLogger logger, final Leve
249251
return addDebugPrintout(logger, level, mh, 0, true, tag);
250252
}
251253

252-
/**
253-
* Add a debug printout to a method handle, tracing parameters and return values
254-
* Output will be unconditional to stderr
255-
*
256-
* @param mh method handle to trace
257-
* @param paramStart first param to print/trace
258-
* @param printReturnValue should we print/trace return value if available?
259-
* @param tag start of trace message
260-
* @return traced method handle
261-
*/
262-
public static MethodHandle addDebugPrintout(final MethodHandle mh, final int paramStart, final boolean printReturnValue, final Object tag) {
263-
return addDebugPrintout(null, Level.OFF, mh, paramStart, printReturnValue, tag);
264-
}
265-
266254
/**
267255
* Add a debug printout to a method handle, tracing parameters and return values
268256
*
@@ -317,12 +305,18 @@ public static MethodHandle addDebugPrintout(final DebugLogger logger, final Leve
317305
@Logger(name="methodhandles")
318306
private static class StandardMethodHandleFunctionality implements MethodHandleFunctionality, Loggable {
319307

308+
// For bootstrapping reasons, because a lot of static fields use MH for lookups, we
309+
// need to set the logger when the Global object is finished. This means that we don't
310+
// get instrumentation for public static final MethodHandle SOMETHING = MH... in the builtin
311+
// classes, but that doesn't matter, because this is usually not where we want it
312+
private DebugLogger log = DebugLogger.DISABLED_LOGGER;
313+
320314
public StandardMethodHandleFunctionality() {
321315
}
322316

323317
@Override
324318
public DebugLogger initLogger(final Context context) {
325-
return log;
319+
return this.log = context.getLogger(this.getClass());
326320
}
327321

328322
@Override
@@ -362,8 +356,13 @@ protected static String describe(final Object... data) {
362356
}
363357

364358
public MethodHandle debug(final MethodHandle master, final String str, final Object... args) {
365-
stacktrace(log);
366-
return addDebugPrintout(log, Level.INFO, master, Integer.MAX_VALUE, false, str + ' ' + describe(args));
359+
if (log.isEnabled()) {
360+
if (PRINT_STACKTRACE) {
361+
stacktrace(log);
362+
}
363+
return addDebugPrintout(log, Level.INFO, master, Integer.MAX_VALUE, false, str + ' ' + describe(args));
364+
}
365+
return master;
367366
}
368367

369368
@Override

src/org.openjdk.nashorn/share/classes/org/openjdk/nashorn/internal/runtime/logging/DebugLogger.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
public final class DebugLogger {
5353

5454
/** Disabled logger used for all loggers that need an instance, but shouldn't output anything */
55-
public static final DebugLogger DISABLED_LOGGER = new DebugLogger("disabled", Level.OFF, false);
55+
public static final DebugLogger DISABLED_LOGGER = new DebugLogger("disabled", Level.OFF, false, false);
5656

5757
private final Logger logger;
5858
private final boolean isEnabled;
@@ -80,6 +80,13 @@ public DebugLogger(final String loggerName, final Level loggerLevel, final boole
8080
this.isEnabled = getLevel() != Level.OFF;
8181
}
8282

83+
private DebugLogger(final String loggerName, final Level loggerLevel, final boolean isQuiet, final boolean isEnabled) {
84+
this.logger = instantiateLogger(loggerName, loggerLevel);
85+
this.isQuiet = isQuiet;
86+
assert logger != null;
87+
this.isEnabled = isEnabled;
88+
}
89+
8390
private static Logger instantiateLogger(final String name, final Level level) {
8491
final Logger logger = java.util.logging.Logger.getLogger(name);
8592
AccessController.doPrivileged((PrivilegedAction<Void>) () -> {

0 commit comments

Comments
 (0)