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.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import net.i2p.I2PAppContext;
import net.i2p.crypto.SigType;
import net.i2p.util.FileUtil;
import net.i2p.util.VersionComparator;
import org.eclipse.jetty.server.Server;
import org.tanukisoftware.wrapper.WrapperManager;
public class LogsHelper extends HelperBase {
private static final String LOCATION_AVAILABLE = "3.3.7";
private static final String _jstlVersion = jstlVersion();
/** @since 0.8.12 */
public String getJettyVersion() {
return Server.getVersion();
}
/** @since 0.8.13 */
public static String jettyVersion() {
return Server.getVersion();
}
/** @since 0.9.15 */
public String getUnavailableCrypto() {
StringBuilder buf = new StringBuilder(128);
for (SigType t : SigType.values()) {
if (!t.isAvailable()) {
buf.append("Crypto: ").append(t.toString()).append(" unavailable
");
}
}
return buf.toString();
}
/**
* @return non-null, "n/a" on failure
* @since 0.9.26
*/
public String getJstlVersion() {
return _jstlVersion;
}
/**
* @return non-null, "n/a" on failure
* @since 0.9.26
*/
private static String jstlVersion() {
String rv = "n/a";
try {
Class> cls = Class.forName("org.apache.taglibs.standard.Version", true, ClassLoader.getSystemClassLoader());
Method getVersion = cls.getMethod("getVersion");
// returns "standard-taglib 1.2.0"
Object version = getVersion.invoke(null, (Object[]) null);
rv = (String) version;
//int sp = rv.indexOf(' ');
//if (sp >= 0 && rv.length() > sp + 1)
// rv = rv.substring(sp + 1);
} catch (Exception e) {}
return rv;
}
/**
* Does not call logManager.flush(); call getCriticalLogs() first to flush
*/
public String getLogs() {
String str = formatMessages(_context.logManager().getBuffer().getMostRecentMessages());
return "
" + _t("File location") + ": " + _context.logManager().currentFile() + "
" + str; } /** * Side effect - calls logManager.flush() */ public String getCriticalLogs() { _context.logManager().flush(); return formatMessages(_context.logManager().getBuffer().getMostRecentCriticalMessages()); } /** * Does not necessarily exist. * * @return non-null, doesn't necessarily exist * @since 0.9.1, public since 0.9.27 */ public static File wrapperLogFile(I2PAppContext ctx) { File f = null; if (ctx.hasWrapper()) { String wv = System.getProperty("wrapper.version"); if (wv != null && VersionComparator.comp(wv, LOCATION_AVAILABLE) >= 0) { try { f = WrapperManager.getWrapperLogFile(); } catch (Throwable t) {} } } if (f == null || !f.exists()) { // RouterLaunch puts the location here if no wrapper String path = System.getProperty("wrapper.logfile"); if (path != null) { f = new File(path); } else { // look in new and old places f = new File(System.getProperty("java.io.tmpdir"), "wrapper.log"); if (!f.exists()) f = new File(ctx.getBaseDir(), "wrapper.log"); } } return f; } public String getServiceLogs() { File f = wrapperLogFile(_context); 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 "" + _t("File not found") + ": " + f.getAbsolutePath() + "
" + _t("File location") + ": " + f.getAbsolutePath() + "
\n" + str + ""; } } /***** unused public String getConnectionLogs() { return formatMessages(_context.commSystem().getMostRecentErrorMessages()); } ******/ private final static String NL = System.getProperty("line.separator"); /** formats in reverse order */ private String formatMessages(List
" + _t("No log messages") + "
"; boolean colorize = _context.getBooleanPropertyDefaultTrue("routerconsole.logs.color"); StringBuilder buf = new StringBuilder(16*1024); buf.append("