Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find source of print call

Tags:

java

debugging

I'm debugging a Java application that helpfully prints this to the console about a dozen times during startup:

java.awt.Dimension[width=140,height=122]

I want to shut it up but I have no idea where it's coming from. The application is huge and I don't know much about it. I can probably find the culprit by stepping through the code as it runs but I'm wondering is there a cleverer way?

like image 623
Boann Avatar asked Dec 26 '22 18:12

Boann


1 Answers

I found my offending print statement (inside a getPreferredSize() method), but I also found a more general solution. By replacing System.out using System.setOut it's possible, in theory, to catch a print statement when it happens.

This isn't fully reliable as: (1) class PrintStream has many printing methods for different data types and there isn't a good single method to override. The methods that do the real output are private. (2) If it wanted to, code could split a message up into individual characters, so there would be no easy way to do the String.contains() check.

Still, as a quick debugging hack, this seems to work nicely:

System.setOut(new java.io.PrintStream(
        new java.io.FileOutputStream(java.io.FileDescriptor.out)) {
    @Override
    public void print(String s) {
        super.print(s);
        if (s.contains("java.awt.Dimension")) {
            throw new RuntimeException("Found you!");
        }
    }
});
like image 182
Boann Avatar answered Dec 28 '22 09:12

Boann