Finding Errant exit in System.out in a large Java program

We have a large java code base [~ 1 M Lines].

Buried (somewhere) in the code base is some old debugging output in System.out that we want to delete (its cluttering things).

The problem is that due to the insufficient code base, we cannot easily find where the output comes from. What we want is a way to see where System.out.println is called from (for example, a stack trace from an exception, or some).

Not suitable for debugging - failure occurs from some strange thread somewhere, etc.

Any ideas on how to track the source of this erroneous output?

PS: 99.99% of calls in System.out are legal, and we have thousands of them, so just finding the code base for System.out calls is not a solution!

+3
source share
2 answers

There is a method System.setOut(). If you are really desperate, install this in some shell of a real stdout and do everything in your wrapper. For instance. compare the written string with something and discard if this erroneous conclusion.

+6
source

With AspectJ, you can easily print the signature for the class that calls System.out.

The following is a simple example that will be considered by TraceAspect. The important part is that the class is in the demo package and calls System.out. The aspect will also list all System.out calls from all classes to a subpackage of any depth in the demo package.

package demo;

public class DemoClass {

    public void demo() {
        System.out.println("inside demo method..");
    }

    public static void main(String[] args) {
        new DemoClass().demo();
    }
}

System.out, , :

@Aspect
public class TraceAspect {

    @Pointcut("call(* java.io.PrintStream.*(..))")
    public void sysoutPointcut() {
    }

    @Pointcut("within(demo..*)")
    public void packagePointcut() {
    }

    @Before("sysoutPointcut() && packagePointcut()")
public void beforeSysoutCallInsideDemoPackage(JoinPoint joinPoint) {
    System.out.print(joinPoint.getThis().getClass().getName() + ":"
            + joinPoint.getSourceLocation().getLine() + " - ");
}
}

DemoClass:

demo.DemoClass:6 - inside demo method..

Eclipse AspectJ " β†’ AspectJ". .

AspectJ @AspectJ .

+2

Source: https://habr.com/ru/post/1742019/


All Articles