From fcb109f46da9ec049d21528ccd6f924f93cb2e9b Mon Sep 17 00:00:00 2001 From: jrandom Date: Sat, 31 Jul 2004 23:19:23 +0000 Subject: [PATCH] made the last of the config pages support dynamic updates (w3wt) --- .../i2p/router/web/ConfigLoggingHandler.java | 153 ++++++++++++++++++ .../i2p/router/web/ConfigLoggingHelper.java | 95 +++++------ apps/routerconsole/jsp/configlogging.jsp | 12 +- 3 files changed, 204 insertions(+), 56 deletions(-) create mode 100644 apps/routerconsole/java/src/net/i2p/router/web/ConfigLoggingHandler.java diff --git a/apps/routerconsole/java/src/net/i2p/router/web/ConfigLoggingHandler.java b/apps/routerconsole/java/src/net/i2p/router/web/ConfigLoggingHandler.java new file mode 100644 index 000000000..4be78b2e8 --- /dev/null +++ b/apps/routerconsole/java/src/net/i2p/router/web/ConfigLoggingHandler.java @@ -0,0 +1,153 @@ +package net.i2p.router.web; + +import java.io.ByteArrayInputStream; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.Properties; +import net.i2p.util.Log; + +/** + * Handler to deal with form submissions from the logging config form and act + * upon the values. + * + */ +public class ConfigLoggingHandler extends FormHandler { + private boolean _shouldSave; + private String _levels; + private String _defaultLevel; + private String _filename; + private String _recordFormat; + private String _dateFormat; + private String _fileSize; + + public void ConfigNetHandler() { + _shouldSave = false; + } + + protected void processForm() { + if (_shouldSave) { + saveChanges(); + } else { + // noop + } + } + + public void setShouldsave(String moo) { _shouldSave = true; } + + public void setLevels(String levels) { + _levels = (levels != null ? levels.trim() : null); + } + public void setDefaultloglevel(String level) { + _defaultLevel = (level != null ? level.trim() : null); + } + public void setFilename(String filename) { + _filename = (filename != null ? filename.trim() : null); + } + public void setRecordformat(String format) { + _recordFormat = (format != null ? format.trim() : null); + } + public void setDateformat(String format) { + _dateFormat = (format != null ? format.trim() : null); + } + public void setFilesize(String size) { + _fileSize = (size != null ? size.trim() : null); + } + + /** + * The user made changes to the config and wants to save them, so + * lets go ahead and do so. + * + */ + private void saveChanges() { + boolean shouldSave = false; + + if (_levels != null) { + try { + Properties props = new Properties(); + props.load(new ByteArrayInputStream(_levels.getBytes())); + _context.logManager().setLimits(props); + shouldSave = true; + addFormNotice("Log limits updated"); + } catch (IOException ioe) { + _context.logManager().getLog(ConfigLoggingHandler.class).error("Error reading from the props?", ioe); + addFormError("Error updating the log limits - levels not valid"); + } + } else { + _context.logManager().setLimits(null); + addFormNotice("Log limits cleared"); + } + + if (_defaultLevel != null) { + String oldDefault = _context.logManager().getDefaultLimit(); + if (_defaultLevel.equals(oldDefault)) { + // noop + } else { + shouldSave = true; + _context.logManager().setDefaultLimit(_defaultLevel); + addFormNotice("Default log level updated from " + oldDefault + " to " + _defaultLevel); + } + } + + if (_dateFormat != null) { + boolean valid = _context.logManager().setDateFormat(_dateFormat); + if (valid) { + shouldSave = true; + addFormNotice("Date format updated"); + } else { + addFormError("Specified date format is not valid (" + _dateFormat + ") - not updated"); + } + } + + if (_fileSize != null) { + int newBytes = _context.logManager().getFileSize(_fileSize); + int oldBytes = _context.logManager().getFileSize(); + if (newBytes > 0) { + if (oldBytes != newBytes) { + _context.logManager().setFileSize(newBytes); + shouldSave = true; + addFormNotice("File size updated"); + } + } else { + addFormError("Specified file size limit is not valid (" + _fileSize + ") - not updated"); + } + } + + if ( (_filename != null) && (_filename.trim().length() > 0) ) { + _filename = _filename.trim(); + String old = _context.logManager().getBaseLogfilename(); + if ( (old != null) && (_filename.equals(old)) ) { + // noop - don't update since its the same + } else { + shouldSave = true; + _context.logManager().setBaseLogfilename(_filename); + addFormNotice("Log file name pattern updated to " + _filename + + " (note: will not take effect until next rotation)"); + } + } + + if ( (_recordFormat != null) && (_recordFormat.trim().length() > 0) ) { + _recordFormat = _recordFormat.trim(); + String old = new String(_context.logManager().getFormat()); + if (_recordFormat.equalsIgnoreCase(old)) { + // noop - no change + } else { + char fmt[] = new char[_recordFormat.length()]; + for (int i = 0; i < fmt.length; i++) + fmt[i] = _recordFormat.charAt(i); + _context.logManager().setFormat(fmt); + shouldSave = true; + addFormNotice("Log record format updated"); + } + } + + if (shouldSave) { + boolean saved = _context.logManager().saveConfig(); + + if (saved) + addFormNotice("Log configuration saved and applied successfully"); + else + addFormNotice("Error saving the configuration (applied but not saved) - please see the error logs"); + } + } +} diff --git a/apps/routerconsole/java/src/net/i2p/router/web/ConfigLoggingHelper.java b/apps/routerconsole/java/src/net/i2p/router/web/ConfigLoggingHelper.java index 9230d2166..d526f5b0d 100644 --- a/apps/routerconsole/java/src/net/i2p/router/web/ConfigLoggingHelper.java +++ b/apps/routerconsole/java/src/net/i2p/router/web/ConfigLoggingHelper.java @@ -2,9 +2,10 @@ package net.i2p.router.web; import java.io.ByteArrayOutputStream; import java.io.IOException; -import java.util.List; import java.util.Iterator; -import java.util.TreeMap; +import java.util.List; +import java.util.Properties; +import java.util.TreeSet; import net.i2p.util.Log; @@ -49,65 +50,49 @@ public class ConfigLoggingHelper { } public String getLogLevelTable() { StringBuffer buf = new StringBuffer(32*1024); - buf.append("
\n"); buf.append("Valid levels are DEBUG, INFO, WARN, ERROR, CRIT\n"); return buf.toString(); } - public String getLogLevelTableDetail() { - StringBuffer buf = new StringBuffer(8*1024); - buf.append("\n"); - buf.append("\n"); - List logs = _context.logManager().getLogs(); - TreeMap sortedLogs = new TreeMap(); - for (int i = 0; i < logs.size(); i++) { - Log l = (Log)logs.get(i); - sortedLogs.put(l.getName(), l); - } - int i = 0; - for (Iterator iter = sortedLogs.values().iterator(); iter.hasNext(); i++) { - Log l = (Log)iter.next(); - buf.append("\n \n"); - buf.append("\n\n"); - } - buf.append("
Package/classLevel
\n"); + public String getDefaultLogLevelBox() { + String cur = _context.logManager().getDefaultLimit(); + StringBuffer buf = new StringBuffer(128); + buf.append("\n"); return buf.toString(); } } diff --git a/apps/routerconsole/jsp/configlogging.jsp b/apps/routerconsole/jsp/configlogging.jsp index 0c8fa2055..9a2041e2f 100644 --- a/apps/routerconsole/jsp/configlogging.jsp +++ b/apps/routerconsole/jsp/configlogging.jsp @@ -14,6 +14,13 @@
<%@include file="confignav.jsp" %> + + + + " /> + + +
Logging filename: " />
@@ -28,9 +35,12 @@ " />

Log levels:
+ Default log level: +

- + +