From f295b4be22e95dd1a9f536c5a3659678615f109b Mon Sep 17 00:00:00 2001 From: Uwe Schindler Date: Wed, 29 Mar 2023 16:41:57 +0200 Subject: [PATCH] Restore the previous logging using DEBUG level after #207/#210; add `--debug` option to CLI --- .../de/thetaphi/forbiddenapis/Checker.java | 3 ++ .../thetaphi/forbiddenapis/StdIoLogger.java | 14 ++++++--- .../thetaphi/forbiddenapis/cli/CliMain.java | 29 ++++++++++++------- 3 files changed, 31 insertions(+), 15 deletions(-) diff --git a/src/main/java/de/thetaphi/forbiddenapis/Checker.java b/src/main/java/de/thetaphi/forbiddenapis/Checker.java index 29a3399..7efbf5b 100644 --- a/src/main/java/de/thetaphi/forbiddenapis/Checker.java +++ b/src/main/java/de/thetaphi/forbiddenapis/Checker.java @@ -309,6 +309,9 @@ public ClassMetadata lookupRelatedClass(String internalName, String internalName if (options.contains(Option.FAIL_ON_MISSING_CLASSES)) { throw new RelatedClassLoadingException(cnfe, origClassName); } else { + logger.debug(String.format(Locale.ENGLISH, + "Class '%s' cannot be loaded (while looking up details about referenced class '%s').", + type.getClassName(), origClassName)); missingClasses.add(type.getClassName()); return null; } diff --git a/src/main/java/de/thetaphi/forbiddenapis/StdIoLogger.java b/src/main/java/de/thetaphi/forbiddenapis/StdIoLogger.java index f98d6c8..e73d3fa 100644 --- a/src/main/java/de/thetaphi/forbiddenapis/StdIoLogger.java +++ b/src/main/java/de/thetaphi/forbiddenapis/StdIoLogger.java @@ -19,9 +19,14 @@ @SuppressForbidden public final class StdIoLogger implements Logger { - public static final Logger INSTANCE = new StdIoLogger(); + public static final Logger INSTANCE = new StdIoLogger(false); + public static final Logger INSTANCE_DEBUG = new StdIoLogger(true); - private StdIoLogger() {} + private final boolean debug; + + private StdIoLogger(boolean debug) { + this.debug = debug; + } @Override public void error(String msg) { @@ -40,7 +45,8 @@ public void info(String msg) { @Override public void debug(String msg) { - // no reporting of debug messages + if (debug) { + System.out.println("DEBUG: " + msg); + } } - } diff --git a/src/main/java/de/thetaphi/forbiddenapis/cli/CliMain.java b/src/main/java/de/thetaphi/forbiddenapis/cli/CliMain.java index f4b0f0c..8ce6e4b 100644 --- a/src/main/java/de/thetaphi/forbiddenapis/cli/CliMain.java +++ b/src/main/java/de/thetaphi/forbiddenapis/cli/CliMain.java @@ -52,12 +52,11 @@ */ public final class CliMain implements Constants { + private final Logger logger; private final Option classpathOpt, dirOpt, includesOpt, excludesOpt, signaturesfileOpt, bundledsignaturesOpt, suppressannotationsOpt, - allowmissingclassesOpt, ignoresignaturesofmissingclassesOpt, allowunresolvablesignaturesOpt, versionOpt, helpOpt; + allowmissingclassesOpt, ignoresignaturesofmissingclassesOpt, allowunresolvablesignaturesOpt, versionOpt, helpOpt, debugOpt; private final CommandLine cmd; - private static final Logger LOG = StdIoLogger.INSTANCE; - public static final int EXIT_SUCCESS = 0; public static final int EXIT_VIOLATION = 1; public static final int EXIT_ERR_CMDLINE = 2; @@ -84,6 +83,10 @@ public CliMain(String... args) throws ExitException { final Options options = new Options(); options.addOptionGroup(required); + options.addOption(debugOpt = Option.builder() + .desc("enable debug logging") + .longOpt("debug") + .build()); options.addOption(classpathOpt = Option.builder("c") .desc("class search path of directories and zip/jar files") .longOpt("classpath") @@ -140,6 +143,8 @@ public CliMain(String... args) throws ExitException { try { this.cmd = new DefaultParser().parse(options, args); + final boolean debugLogging = cmd.hasOption(debugOpt.getLongOpt()); + this.logger = debugLogging ? StdIoLogger.INSTANCE_DEBUG : StdIoLogger.INSTANCE; if (cmd.hasOption(helpOpt.getLongOpt())) { printHelp(options); throw new ExitException(EXIT_SUCCESS); @@ -156,7 +161,7 @@ public CliMain(String... args) throws ExitException { private void printVersion() { final Package pkg = this.getClass().getPackage(); - LOG.info(String.format(Locale.ENGLISH, + logger.info(String.format(Locale.ENGLISH, "%s %s", pkg.getImplementationTitle(), pkg.getImplementationVersion() )); @@ -211,18 +216,20 @@ public void run() throws ExitException { } catch (MalformedURLException mfue) { throw new ExitException(EXIT_ERR_OTHER, "The given classpath is invalid: " + mfue); } - // System.err.println("Classpath: " + Arrays.toString(urls)); + logger.debug("Classpath: " + Arrays.toString(urls)); try (final URLClassLoader loader = URLClassLoader.newInstance(urls, ClassLoader.getSystemClassLoader())) { final EnumSet options = EnumSet.of(FAIL_ON_VIOLATION); if (!cmd.hasOption(allowmissingclassesOpt.getLongOpt())) options.add(FAIL_ON_MISSING_CLASSES); if (cmd.hasOption(allowunresolvablesignaturesOpt.getLongOpt())) { - LOG.warn(DEPRECATED_WARN_FAIL_ON_UNRESOLVABLE_SIGNATURES); + logger.warn(DEPRECATED_WARN_FAIL_ON_UNRESOLVABLE_SIGNATURES); } else { options.add(FAIL_ON_UNRESOLVABLE_SIGNATURES); } - if (cmd.hasOption(ignoresignaturesofmissingclassesOpt.getLongOpt())) options.add(IGNORE_SIGNATURES_OF_MISSING_CLASSES); - final Checker checker = new Checker(LOG, loader, options); + if (cmd.hasOption(ignoresignaturesofmissingclassesOpt.getLongOpt())) { + options.add(IGNORE_SIGNATURES_OF_MISSING_CLASSES); + } + final Checker checker = new Checker(logger, loader, options); if (!checker.isSupportedJDK) { throw new ExitException(EXIT_UNSUPPORTED_JDK, String.format(Locale.ENGLISH, @@ -235,7 +242,7 @@ public void run() throws ExitException { checker.addSuppressAnnotation(a); } - LOG.info("Scanning for classes to check..."); + logger.info("Scanning for classes to check..."); if (!classesDirectory.exists()) { throw new ExitException(EXIT_ERR_OTHER, "Directory with class files does not exist: " + classesDirectory); } @@ -282,7 +289,7 @@ public void run() throws ExitException { bundledsignaturesOpt.getLongOpt(), signaturesfileOpt.getLongOpt() )); } else { - LOG.info("Skipping execution because no API signatures are available."); + logger.info("Skipping execution because no API signatures are available."); return; } } @@ -308,7 +315,7 @@ public static void main(String... args) { new CliMain(args).run(); } catch (ExitException e) { if (e.getMessage() != null) { - LOG.error(e.getMessage()); + StdIoLogger.INSTANCE.error(e.getMessage()); } if (e.exitCode != 0) { System.exit(e.exitCode);