diff --git a/apps/routerconsole/java/src/net/i2p/router/web/helpers/LogsHelper.java b/apps/routerconsole/java/src/net/i2p/router/web/helpers/LogsHelper.java index a59090b414..378ebec15d 100644 --- a/apps/routerconsole/java/src/net/i2p/router/web/helpers/LogsHelper.java +++ b/apps/routerconsole/java/src/net/i2p/router/web/helpers/LogsHelper.java @@ -6,12 +6,13 @@ import java.io.FileInputStream; import java.io.InputStreamReader; import java.io.IOException; import java.lang.reflect.Method; -import java.util.ArrayList; +import java.util.LinkedList; import java.util.List; import java.util.jar.Attributes; import net.i2p.I2PAppContext; import net.i2p.crypto.SigType; +import net.i2p.data.DataHelper; import net.i2p.router.web.ConfigServiceHandler; import net.i2p.router.web.CSSHelper; import net.i2p.router.web.HelperBase; @@ -24,6 +25,10 @@ public class LogsHelper extends HelperBase { private static final String _jstlVersion = jstlVersion(); + private static final int MAX_WRAPPER_LINES = 250; + private static final String PROP_LAST_WRAPPER = "routerconsole.lastWrapperLogEntry"; + + /** @since 0.8.12 */ public String getJettyVersion() { return RouterConsoleRunner.jettyVersion(); @@ -72,7 +77,8 @@ public class LogsHelper extends HelperBase { */ public String getLogs() { String str = formatMessages(_context.logManager().getBuffer().getMostRecentMessages()); - return "
" + _t("File location") + ": " + _context.logManager().currentFile() + "
" + str; + return "" + _t("File location") + ": " + + DataHelper.escapeHTML(_context.logManager().currentFile()) + "
" + str; } /** @@ -116,31 +122,82 @@ public class LogsHelper extends HelperBase { * @param consoleNonce must match * @since 0.9.46 */ - public void clearThrough(int n, int crit, String consoleNonce) { + public void clearThrough(int n, int crit, long wn, long wts, String wf, String consoleNonce) { if (!CSSHelper.getNonce().equals(consoleNonce)) return; if (n >= 0) _context.logManager().getBuffer().getUIMessages().clearThrough(n); if (crit >= 0) _context.logManager().getBuffer().getCriticalUIMessages().clearThrough(crit); + if (wn >= 0 && wts > 0 && wf != null) { + // timestamp, last line number, filename + String val = wts + "," + wn + "," + wf; + if (!val.equals(_context.getProperty(PROP_LAST_WRAPPER))) + _context.router().saveConfig(PROP_LAST_WRAPPER, val); + } } - public String getServiceLogs() { + /** + * last line number -1 on error + * @param buf out parameter + * @return Long timestamp, Long last line number, String filename (escaped) + */ + public Object[] getServiceLogs(StringBuilder obuf) { File f = ConfigServiceHandler.wrapperLogFile(_context); String str; - if (_context.hasWrapper()) { + long flastMod = f.lastModified(); + long lastMod = 0; + long toSkip = 0; + // timestamp, last line number, filename + String prop = _context.getProperty(PROP_LAST_WRAPPER); + if (prop != null) { + String[] vals = DataHelper.split(prop, ",", 3); + if (vals.length == 3) { + if (vals[2].equals(f.getName())) { + try { lastMod = Long.parseLong(vals[0]); } catch (NumberFormatException nfe) {} + try { toSkip = Long.parseLong(vals[1]); } catch (NumberFormatException nfe) {} + } else { + // file rotated + lastMod = 0; + } + } + } + if (lastMod > 0 && flastMod <= lastMod) { + str = ""; + toSkip = -1; + } else if (_context.hasWrapper()) { // platform encoding - str = readTextFile(f, 250); + StringBuilder buf = new StringBuilder(MAX_WRAPPER_LINES * 80); + toSkip = readTextFile(f, MAX_WRAPPER_LINES, toSkip, buf); + if (toSkip >= 0) + str = buf.toString(); + else + str = null; } else { // UTF-8 - str = FileUtil.readTextFile(f.getAbsolutePath(), 250, false); + // no skipping + str = FileUtil.readTextFile(f.getAbsolutePath(), MAX_WRAPPER_LINES, false); + toSkip = 0; } + String loc = DataHelper.escapeHTML(f.getAbsolutePath()); if (str == null) { - return "" + _t("File not found") + ": " + f.getAbsolutePath() + "
").append(_t("File not found")).append(": ").append(loc).append("
" + _t("File location") + ": " + f.getAbsolutePath() + "
\n" + str + ""; + obuf.append("
").append(_t("File location")).append(": ") + .append(loc).append("
").append(str).append(""); + } else { + obuf.append("
").append(_t("No log messages")).append("
- |