Skip to content

Commit f6c1df1

Browse files
committed
Startup logger info dump optimization
- remove reflective JDK module info query code - print standard ISO date/time to avoid date formatter bootstrap misc: - removed a test cases testing ThreadDeath as part of the cleanup
1 parent 1fa4b2b commit f6c1df1

2 files changed

Lines changed: 26 additions & 80 deletions

File tree

platform/core.startup/src/org/netbeans/core/startup/TopLogging.java

Lines changed: 25 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -25,26 +25,21 @@
2525
import java.io.ByteArrayInputStream;
2626
import java.io.ByteArrayOutputStream;
2727
import java.io.File;
28-
import java.io.FileReader;
2928
import java.io.IOException;
3029
import java.io.PrintStream;
3130
import java.io.PrintWriter;
32-
import java.io.Reader;
33-
import java.io.UnsupportedEncodingException;
3431
import java.lang.Thread.UncaughtExceptionHandler;
35-
import java.lang.reflect.Method;
3632
import java.net.URL;
3733
import java.net.URLClassLoader;
38-
import java.text.DateFormat;
34+
import java.nio.charset.StandardCharsets;
35+
import java.nio.file.Files;
36+
import java.time.Instant;
3937
import java.util.ArrayList;
4038
import java.util.Collection;
41-
import java.util.Collections;
42-
import java.util.Date;
4339
import java.util.LinkedList;
4440
import java.util.List;
4541
import java.util.Locale;
4642
import java.util.Properties;
47-
import java.util.Set;
4843
import java.util.StringTokenizer;
4944
import java.util.logging.Handler;
5045
import java.util.logging.Level;
@@ -54,7 +49,6 @@
5449
import java.util.logging.StreamHandler;
5550
import java.util.regex.Matcher;
5651
import java.util.regex.Pattern;
57-
import java.util.stream.Collectors;
5852
import org.netbeans.NbExit;
5953
import org.netbeans.core.startup.logging.NbLogging;
6054
import org.openide.filesystems.FileUtil;
@@ -181,12 +175,8 @@ private static void initialize(boolean verbose) {
181175
try (PrintStream ps = new PrintStream(os)) {
182176
logging.printSystemInfo(ps);
183177
}
184-
try {
185-
Logger logger = Logger.getLogger(TopLogging.class.getName()); // NOI18N
186-
logger.log(Level.INFO, os.toString("utf-8"));
187-
} catch (UnsupportedEncodingException ex) {
188-
assert false;
189-
}
178+
Logger logger = Logger.getLogger(TopLogging.class.getName()); // NOI18N
179+
logger.log(Level.INFO, os.toString(StandardCharsets.UTF_8));
190180
}
191181
if (!Boolean.getBoolean("netbeans.logger.noSystem")) {
192182
if (!PrintStreamLogger.isLogger(System.err)) {
@@ -202,11 +192,9 @@ private static void initialize(boolean verbose) {
202192

203193

204194
private void printSystemInfo(PrintStream ps) {
205-
DateFormat df = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL, Locale.US);
206-
Date date = new Date();
207195

208196
ps.println("-------------------------------------------------------------------------------"); // NOI18N
209-
ps.println(">Log Session: "+df.format (date)); // NOI18N
197+
ps.println(">Log Session: " + Instant.now()); // NOI18N
210198
ps.println(">System Info: "); // NOI18N
211199

212200
List<File> clusters = new ArrayList<>();
@@ -228,20 +216,17 @@ private void printSystemInfo(PrintStream ps) {
228216
for (File cluster : clusters) { // also print Hg ID if available; more precise
229217
File buildInfo = new File(cluster, "build_info"); // NOI18N
230218
if (buildInfo.isFile()) {
231-
try {
232-
try (Reader r = new FileReader(buildInfo)) {
233-
BufferedReader b = new BufferedReader(r);
234-
Pattern p = Pattern.compile("Hg ID: ([0-9a-f]{12})"); // NOI18N
235-
for (;;) {
236-
String line = b.readLine();
237-
if (line == null) {
238-
break;
239-
}
240-
Matcher m = p.matcher(line);
241-
if (m.matches()) {
242-
ps.print(" (#" + m.group(1) + ")"); // NOI18N
243-
break;
244-
}
219+
Pattern p = Pattern.compile("Hg ID: ([0-9a-f]{12})"); // NOI18N
220+
try (BufferedReader br = Files.newBufferedReader(buildInfo.toPath())) {
221+
for (;;) {
222+
String line = br.readLine();
223+
if (line == null) {
224+
break;
225+
}
226+
Matcher m = p.matcher(line);
227+
if (m.matches()) {
228+
ps.print(" (#" + m.group(1) + ")"); // NOI18N
229+
break;
245230
}
246231
}
247232
} catch (IOException x) {
@@ -276,7 +261,8 @@ private void printSystemInfo(PrintStream ps) {
276261
ps.println(" Cache Directory = " + Places.getCacheDirectory()); // NOI18N
277262
ps.print( " Installation = "); // NOI18N
278263
for (File cluster : clusters) {
279-
ps.print(cluster + "\n "); // NOI18N
264+
ps.println(cluster);
265+
ps.print(" ");// NOI18N
280266
}
281267
ps.println(CLIOptions.getHomeDir()); // platform cluster is separate
282268
ps.println(" Boot & Ext. Classpath = " + createBootClassPath()); // NOI18N
@@ -286,9 +272,9 @@ private void printSystemInfo(PrintStream ps) {
286272
cp = System.getProperty("java.class.path", "unknown"); // NOI18N
287273
} else {
288274
StringBuilder sb = new StringBuilder("loaded by "); // NOI18N
289-
if (l instanceof URLClassLoader) {
275+
if (l instanceof URLClassLoader urlCL) {
290276
sb.append("URLClassLoader"); // NOI18N
291-
for (URL u : ((URLClassLoader)l).getURLs()) {
277+
for (URL u : urlCL.getURLs()) {
292278
sb.append(' ').append(u);
293279
}
294280
} else {
@@ -315,29 +301,10 @@ private static String createBootClassPath() {
315301
}
316302

317303
private List<String> createJavaBootModuleList() {
318-
// TODO JDK 11 equivalent
319-
// return ModuleLayer.boot().modules().stream()
320-
// .map(Module::getName)
321-
// .sorted()
322-
// .collect(Collectors.toList());
323-
try {
324-
Class<?> ml_class = Class.forName("java.lang.ModuleLayer");
325-
Method mod_getName = Class.forName("java.lang.Module").getMethod("getName");
326-
@SuppressWarnings("unchecked")
327-
Set<?> mods = (Set<?>)ml_class.getDeclaredMethod("modules").invoke(
328-
ml_class.getDeclaredMethod("boot").invoke(null)
329-
);
330-
return mods.stream().map(mod -> {
331-
try {
332-
return (String) mod_getName.invoke(mod);
333-
} catch (ReflectiveOperationException ex) {
334-
return "unknown"; // outer try would fail first
335-
}
336-
})
337-
.sorted().collect(Collectors.toList());
338-
} catch (ReflectiveOperationException ex) {
339-
return Collections.emptyList();
340-
}
304+
return ModuleLayer.boot().modules().stream()
305+
.map(Module::getName)
306+
.sorted()
307+
.toList();
341308
}
342309

343310
/** Scans path list for something that can be added to classpath.
@@ -501,9 +468,6 @@ public void uncaughtException(Thread t, Throwable e) {
501468
if (e.getClass().getName().endsWith(".ExitSecurityException")) { // NOI18N
502469
return;
503470
}
504-
if (e instanceof ThreadDeath) {
505-
return;
506-
}
507471
g.log(Level.SEVERE, null, e);
508472
}
509473
} // end of AWTHandler

platform/core.startup/test/unit/src/org/netbeans/core/startup/TopLoggingTest.java

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,9 @@
2929
import java.util.logging.Handler;
3030
import java.util.logging.Level;
3131
import java.util.logging.LogManager;
32-
import java.util.logging.LogRecord;
3332
import java.util.logging.Logger;
3433
import java.util.regex.Matcher;
3534
import java.util.regex.Pattern;
36-
import javax.swing.SwingUtilities;
3735
import org.netbeans.junit.NbTestCase;
3836
import org.openide.util.Exceptions;
3937
import org.openide.util.RequestProcessor;
@@ -360,11 +358,7 @@ public void testSystemErrPrintLnIsSentToLog() throws Exception {
360358
public void testLoggingFromRequestProcessor() throws Exception {
361359
Logger.getLogger("org.openide.util.RequestProcessor").setLevel(Level.ALL);
362360

363-
RequestProcessor.getDefault().post(new Runnable() {
364-
public void run() {
365-
366-
}
367-
}).waitFinished();
361+
RequestProcessor.getDefault().post(() -> {}).waitFinished();
368362

369363
}
370364

@@ -399,16 +393,4 @@ public void testAttachMessage() throws Exception { // #158906
399393
assertTrue(disk, disk.contains("me please"));
400394
}
401395

402-
public void testThreadDeath() throws Exception { // #203171
403-
Thread t = new Thread(new Runnable() {
404-
@Override public void run() {
405-
throw new ThreadDeath();
406-
}
407-
});
408-
t.start();
409-
t.join();
410-
String disk = readLog(true);
411-
assertFalse(disk, disk.contains("java.lang.ThreadDeath"));
412-
}
413-
414396
}

0 commit comments

Comments
 (0)