Made sure logger is enabled before logging errors/warnings. (PMD)
This commit is contained in:
parent
f6234f6db3
commit
7f06e8b348
1 changed files with 100 additions and 4 deletions
|
@ -251,8 +251,10 @@ public class Mobibot extends PircBot {
|
||||||
} catch (IOException ignore) {
|
} catch (IOException ignore) {
|
||||||
// Do nothing.
|
// Do nothing.
|
||||||
} catch (FeedException e) {
|
} catch (FeedException e) {
|
||||||
|
if (logger.isErrorEnabled()) {
|
||||||
logger.error("An error occurred while parsing the '" + EntriesMgr.CURRENT_XML + "' file.", e);
|
logger.error("An error occurred while parsing the '" + EntriesMgr.CURRENT_XML + "' file.", e);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Load the backlogs, if any.
|
// Load the backlogs, if any.
|
||||||
try {
|
try {
|
||||||
|
@ -260,8 +262,10 @@ public class Mobibot extends PircBot {
|
||||||
} catch (IOException ignore) {
|
} catch (IOException ignore) {
|
||||||
// Do nothing.
|
// Do nothing.
|
||||||
} catch (FeedException e) {
|
} catch (FeedException e) {
|
||||||
|
if (logger.isErrorEnabled()) {
|
||||||
logger.error("An error occurred while parsing the '" + EntriesMgr.NAV_XML + "' file.", e);
|
logger.error("An error occurred while parsing the '" + EntriesMgr.NAV_XML + "' file.", e);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Initialize the bot
|
// Initialize the bot
|
||||||
setVerbose(true);
|
setVerbose(true);
|
||||||
|
@ -939,13 +943,105 @@ public class Mobibot extends PircBot {
|
||||||
getName() + ReleaseInfo.VERSION + " has joined " + ircChannel,
|
getName() + ReleaseInfo.VERSION + " has joined " + ircChannel,
|
||||||
true);
|
true);
|
||||||
} catch (ModuleException e) {
|
} catch (ModuleException e) {
|
||||||
|
if (logger.isWarnEnabled()) {
|
||||||
logger.warn(
|
logger.warn(
|
||||||
"Failed to notify " + twitterHandle + " of joining " + ircChannel + " on Twitter.", e);
|
"Failed to notify " + twitterHandle + " of joining " + ircChannel + " on Twitter.", e);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}).start();
|
}).start();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The Truth Is Out There...
|
||||||
|
*
|
||||||
|
* @param args The command line arguments.
|
||||||
|
*/
|
||||||
|
@SuppressFBWarnings({"INFORMATION_EXPOSURE_THROUGH_AN_ERROR_MESSAGE", "DM_DEFAULT_ENCODING",
|
||||||
|
"IOI_USE_OF_FILE_STREAM_CONSTRUCTORS"})
|
||||||
|
@SuppressWarnings({"PMD.SystemPrintln", "PMD.AvoidFileStream"})
|
||||||
|
public static void main(final String[] args) {
|
||||||
|
// Setup the command line options
|
||||||
|
final Options options = new Options();
|
||||||
|
options.addOption(Commands.HELP_ARG.substring(0, 1), Commands.HELP_ARG, false, "print this help message");
|
||||||
|
options.addOption(Commands.DEBUG_ARG.substring(0, 1), Commands.DEBUG_ARG, false,
|
||||||
|
"print debug & logging data directly to the console");
|
||||||
|
options.addOption(Option.builder(
|
||||||
|
Commands.PROPS_ARG.substring(0, 1)).hasArg().argName("file").desc("use " + "alternate properties file")
|
||||||
|
.longOpt(Commands.PROPS_ARG).build());
|
||||||
|
options.addOption(Commands.VERSION_ARG.substring(0, 1), Commands.VERSION_ARG, false, "print version info");
|
||||||
|
|
||||||
|
// Parse the command line
|
||||||
|
final CommandLineParser parser = new DefaultParser();
|
||||||
|
CommandLine line = null;
|
||||||
|
|
||||||
|
try {
|
||||||
|
line = parser.parse(options, args);
|
||||||
|
} catch (ParseException e) {
|
||||||
|
System.err.println("CLI Parsing failed. Reason: " + e.getMessage());
|
||||||
|
e.printStackTrace(System.err);
|
||||||
|
System.exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (line.hasOption(Commands.HELP_ARG.charAt(0))) {
|
||||||
|
// Output the usage
|
||||||
|
new HelpFormatter().printHelp(Mobibot.class.getName(), options);
|
||||||
|
} else if (line.hasOption(Commands.VERSION_ARG.charAt(0))) {
|
||||||
|
for (final String s : INFO_STRS) {
|
||||||
|
System.out.println(s);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
final Properties p = new Properties();
|
||||||
|
|
||||||
|
try (final InputStream fis = Files.newInputStream(Paths.get(
|
||||||
|
line.getOptionValue(Commands.PROPS_ARG.charAt(0), "./mobibot.properties")))) {
|
||||||
|
// Load the properties files
|
||||||
|
p.load(fis);
|
||||||
|
} catch (FileNotFoundException e) {
|
||||||
|
System.err.println("Unable to find properties file.");
|
||||||
|
e.printStackTrace(System.err);
|
||||||
|
System.exit(1);
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.err.println("Unable to open properties file.");
|
||||||
|
e.printStackTrace(System.err);
|
||||||
|
System.exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
final String nickname = p.getProperty("nick", Mobibot.class.getName().toLowerCase(Constants.LOCALE));
|
||||||
|
final String channel = p.getProperty("channel");
|
||||||
|
final String logsDir = Utils.ensureDir(p.getProperty("logs", "."), false);
|
||||||
|
|
||||||
|
// Redirect the stdout and stderr
|
||||||
|
if (!line.hasOption(Commands.DEBUG_ARG.charAt(0))) {
|
||||||
|
try {
|
||||||
|
final PrintStream stdout = new PrintStream(new FileOutputStream(
|
||||||
|
logsDir + channel.substring(1) + '.' + Utils.today() + ".log", true));
|
||||||
|
System.setOut(stdout);
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.err.println("Unable to open output (stdout) log file.");
|
||||||
|
e.printStackTrace(System.err);
|
||||||
|
System.exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
final PrintStream stderr = new PrintStream(
|
||||||
|
new FileOutputStream(logsDir + nickname + ".err", true));
|
||||||
|
System.setErr(stderr);
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.err.println("Unable to open error (stderr) log file.");
|
||||||
|
e.printStackTrace(System.err);
|
||||||
|
System.exit(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create the bot
|
||||||
|
final Mobibot bot = new Mobibot(nickname, channel, logsDir, p);
|
||||||
|
|
||||||
|
// Connect
|
||||||
|
bot.connect();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@inheritDoc}
|
* {@inheritDoc}
|
||||||
*/
|
*/
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue