Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restore the previous missing classes logging using DEBUG level #227

Merged
merged 3 commits into from
Mar 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/main/java/de/thetaphi/forbiddenapis/Checker.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
14 changes: 10 additions & 4 deletions src/main/java/de/thetaphi/forbiddenapis/StdIoLogger.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -40,7 +45,8 @@ public void info(String msg) {

@Override
public void debug(String msg) {
// no reporting of debug messages
if (debug) {
System.err.println("DEBUG: " + msg);
}
}

}
29 changes: 18 additions & 11 deletions src/main/java/de/thetaphi/forbiddenapis/cli/CliMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 (stderr)")
.longOpt("debug")
.build());
options.addOption(classpathOpt = Option.builder("c")
.desc("class search path of directories and zip/jar files")
.longOpt("classpath")
Expand Down Expand Up @@ -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);
Expand All @@ -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()
));
Expand Down Expand Up @@ -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<Checker.Option> 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,
Expand All @@ -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);
}
Expand Down Expand Up @@ -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;
}
}
Expand All @@ -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);
Expand Down