public static String grabConsoleOutput(Runnable f) {
        ByteArrayOutputStream buf = new ByteArrayOutputStream();
        PrintStream out = new PrintStream(buf);
        PrintStream out0 = System.out;

        System.setOut(out);
        try {
            f.run();
        } finally {
            System.setOut(out0);
        }
        out.flush();
        return buf.toString();
    }
Capture the console output during the execution of the provided function.
tools/src/test/java/org/apache/kafka/tools/ToolsTestUtils.java