52
52
*/
53
53
public final class CliMain implements Constants {
54
54
55
+ private final Logger logger ;
55
56
private final Option classpathOpt , dirOpt , includesOpt , excludesOpt , signaturesfileOpt , bundledsignaturesOpt , suppressannotationsOpt ,
56
- allowmissingclassesOpt , ignoresignaturesofmissingclassesOpt , allowunresolvablesignaturesOpt , versionOpt , helpOpt ;
57
+ allowmissingclassesOpt , ignoresignaturesofmissingclassesOpt , allowunresolvablesignaturesOpt , versionOpt , helpOpt , debugOpt ;
57
58
private final CommandLine cmd ;
58
59
59
- private static final Logger LOG = StdIoLogger .INSTANCE ;
60
-
61
60
public static final int EXIT_SUCCESS = 0 ;
62
61
public static final int EXIT_VIOLATION = 1 ;
63
62
public static final int EXIT_ERR_CMDLINE = 2 ;
@@ -84,6 +83,10 @@ public CliMain(String... args) throws ExitException {
84
83
85
84
final Options options = new Options ();
86
85
options .addOptionGroup (required );
86
+ options .addOption (debugOpt = Option .builder ()
87
+ .desc ("enable debug logging" )
88
+ .longOpt ("debug" )
89
+ .build ());
87
90
options .addOption (classpathOpt = Option .builder ("c" )
88
91
.desc ("class search path of directories and zip/jar files" )
89
92
.longOpt ("classpath" )
@@ -140,6 +143,8 @@ public CliMain(String... args) throws ExitException {
140
143
141
144
try {
142
145
this .cmd = new DefaultParser ().parse (options , args );
146
+ final boolean debugLogging = cmd .hasOption (debugOpt .getLongOpt ());
147
+ this .logger = debugLogging ? StdIoLogger .INSTANCE_DEBUG : StdIoLogger .INSTANCE ;
143
148
if (cmd .hasOption (helpOpt .getLongOpt ())) {
144
149
printHelp (options );
145
150
throw new ExitException (EXIT_SUCCESS );
@@ -156,7 +161,7 @@ public CliMain(String... args) throws ExitException {
156
161
157
162
private void printVersion () {
158
163
final Package pkg = this .getClass ().getPackage ();
159
- LOG .info (String .format (Locale .ENGLISH ,
164
+ logger .info (String .format (Locale .ENGLISH ,
160
165
"%s %s" ,
161
166
pkg .getImplementationTitle (), pkg .getImplementationVersion ()
162
167
));
@@ -211,18 +216,20 @@ public void run() throws ExitException {
211
216
} catch (MalformedURLException mfue ) {
212
217
throw new ExitException (EXIT_ERR_OTHER , "The given classpath is invalid: " + mfue );
213
218
}
214
- // System.err.println ("Classpath: " + Arrays.toString(urls));
219
+ logger . debug ("Classpath: " + Arrays .toString (urls ));
215
220
216
221
try (final URLClassLoader loader = URLClassLoader .newInstance (urls , ClassLoader .getSystemClassLoader ())) {
217
222
final EnumSet <Checker .Option > options = EnumSet .of (FAIL_ON_VIOLATION );
218
223
if (!cmd .hasOption (allowmissingclassesOpt .getLongOpt ())) options .add (FAIL_ON_MISSING_CLASSES );
219
224
if (cmd .hasOption (allowunresolvablesignaturesOpt .getLongOpt ())) {
220
- LOG .warn (DEPRECATED_WARN_FAIL_ON_UNRESOLVABLE_SIGNATURES );
225
+ logger .warn (DEPRECATED_WARN_FAIL_ON_UNRESOLVABLE_SIGNATURES );
221
226
} else {
222
227
options .add (FAIL_ON_UNRESOLVABLE_SIGNATURES );
223
228
}
224
- if (cmd .hasOption (ignoresignaturesofmissingclassesOpt .getLongOpt ())) options .add (IGNORE_SIGNATURES_OF_MISSING_CLASSES );
225
- final Checker checker = new Checker (LOG , loader , options );
229
+ if (cmd .hasOption (ignoresignaturesofmissingclassesOpt .getLongOpt ())) {
230
+ options .add (IGNORE_SIGNATURES_OF_MISSING_CLASSES );
231
+ }
232
+ final Checker checker = new Checker (logger , loader , options );
226
233
227
234
if (!checker .isSupportedJDK ) {
228
235
throw new ExitException (EXIT_UNSUPPORTED_JDK , String .format (Locale .ENGLISH ,
@@ -235,7 +242,7 @@ public void run() throws ExitException {
235
242
checker .addSuppressAnnotation (a );
236
243
}
237
244
238
- LOG .info ("Scanning for classes to check..." );
245
+ logger .info ("Scanning for classes to check..." );
239
246
if (!classesDirectory .exists ()) {
240
247
throw new ExitException (EXIT_ERR_OTHER , "Directory with class files does not exist: " + classesDirectory );
241
248
}
@@ -282,7 +289,7 @@ public void run() throws ExitException {
282
289
bundledsignaturesOpt .getLongOpt (), signaturesfileOpt .getLongOpt ()
283
290
));
284
291
} else {
285
- LOG .info ("Skipping execution because no API signatures are available." );
292
+ logger .info ("Skipping execution because no API signatures are available." );
286
293
return ;
287
294
}
288
295
}
@@ -308,7 +315,7 @@ public static void main(String... args) {
308
315
new CliMain (args ).run ();
309
316
} catch (ExitException e ) {
310
317
if (e .getMessage () != null ) {
311
- LOG .error (e .getMessage ());
318
+ StdIoLogger . INSTANCE .error (e .getMessage ());
312
319
}
313
320
if (e .exitCode != 0 ) {
314
321
System .exit (e .exitCode );
0 commit comments