diff --git a/apps/routerconsole/java/src/net/i2p/router/web/LogsHelper.java b/apps/routerconsole/java/src/net/i2p/router/web/LogsHelper.java index 651ace5728..01108f2467 100644 --- a/apps/routerconsole/java/src/net/i2p/router/web/LogsHelper.java +++ b/apps/routerconsole/java/src/net/i2p/router/web/LogsHelper.java @@ -1,6 +1,11 @@ package net.i2p.router.web; +import java.io.BufferedReader; import java.io.File; +import java.io.FileInputStream; +import java.io.InputStreamReader; +import java.io.IOException; +import java.util.ArrayList; import java.util.List; import net.i2p.I2PAppContext; @@ -71,10 +76,17 @@ public class LogsHelper extends HelperBase { public String getServiceLogs() { File f = wrapperLogFile(_context); - String str = FileUtil.readTextFile(f.getAbsolutePath(), 250, false); - if (str == null) + String str; + if (_context.hasWrapper()) { + // platform encoding + str = readTextFile(f, 250); + } else { + // UTF-8 + str = FileUtil.readTextFile(f.getAbsolutePath(), 250, false); + } + if (str == null) { return "
" + _("File not found") + ": " + f.getAbsolutePath() + "
" + _("File location") + ": " + f.getAbsolutePath() + "
" + str + ""; } @@ -136,4 +148,45 @@ public class LogsHelper extends HelperBase { return buf.toString(); } + + /** + * Read in the last few lines of a (newline delimited) textfile, or null if + * the file doesn't exist. + * + * Same as FileUtil.readTextFile but uses platform encoding, + * not UTF-8, since the wrapper log cannot be configured: + * http://stackoverflow.com/questions/14887690/how-do-i-get-the-tanuki-wrapper-log-files-to-be-utf-8-encoded + * + * Warning - this inefficiently allocates a StringBuilder of size maxNumLines*80, + * so don't make it too big. + * Warning - converts \r\n to \n + * + * @param maxNumLines max number of lines (greater than zero) + * @return string or null; does not throw IOException. + * @since 0.9.11 modded from FileUtil.readTextFile() + */ + private static String readTextFile(File f, int maxNumLines) { + if (!f.exists()) return null; + FileInputStream fis = null; + try { + fis = new FileInputStream(f); + BufferedReader in = new BufferedReader(new InputStreamReader(fis)); + List