public int cmdFile(Dictionary opts, Reader in, PrintWriter out,
Session session) {
// Get log configuration service
LogConfig configuration = (LogConfig) LogCommands.logConfigTracker
.getService();
if (configuration == null) {
out.println("Unable to get a LogConfigService");
return 1;
}
if (!configuration.isDefaultConfig()) {
out.println(" This command is not persistent. "
+ "(No valid configuration has been received)");
}
if (configuration.getDir() != null) {
boolean optionFound = false;
// File logging on/off
if (opts.get("-on") != null) {
optionFound = true;
configuration.setFile(true);
} else if (opts.get("-off") != null) {
optionFound = true;
configuration.setFile(false);
}
// Flush
if (opts.get("-flush") != null) {
optionFound = true;
if (!configuration.getFile()) {
out
.println("Cannot activate flush (file logging disabled).");
} else {
configuration.setFlush(true);
}
} else if (opts.get("-noflush") != null) {
optionFound = true;
if (!configuration.getFile()) {
out
.println("Cannot deactivate flush (file logging disabled).");
} else {
configuration.setFlush(false);
}
}
// Log size
String value = (String) opts.get("-size");
if (value != null) {
optionFound = true;
if (!configuration.getFile()) {
out.println("Cannot set log size (file logging disabled).");
} else {
try {
configuration.setFileSize(Integer.parseInt(value));
} catch (NumberFormatException nfe1) {
out.println("Cannot set log size (" + nfe1 + ").");
}
}
}
// Log generations
value = (String) opts.get("-gen");
if (value != null) {
optionFound = true;
if (!configuration.getFile()) {
out
.println("Cannot set generation count (file logging disabled).");
} else {
try {
configuration.setMaxGen(Integer.parseInt(value));
} catch (NumberFormatException nfe2) {
out.println("Cannot set generation count (" + nfe2
+ ").");
}
}
}
// Update configuration only once
if (optionFound)
configuration.commit();
// Show current config
if (!optionFound) {
boolean isOn = configuration.getFile();
out.println(" file logging is " + (isOn ? "on" : "off") + ".");
if (isOn) {
out.println(" file size: "
+ configuration.getFileSize());
out.println(" generations: " + configuration.getMaxGen());
out.println(" flush: " + configuration.getFlush());
out.println(" log location: " + configuration.getDir());
}
}
return 0;
}