From e81c1df19f6d7ea76b6c750632c382cd337a4550 Mon Sep 17 00:00:00 2001 From: jrandom Date: Tue, 24 Aug 2004 18:02:48 +0000 Subject: [PATCH] * helper to read the last few lines of a textfile * use that to render the last few lines of the wrapper log on /logs.jsp (for the on demand stack trace) * thread creation / finalization logging * support a hard restart (stop immediately and restart the JVM) - useful for rerunning clients.config (etc) * systray when not supported --- .../i2p/router/web/ConfigServiceHandler.java | 21 +++++++++--- .../src/net/i2p/router/web/LogsHelper.java | 9 ++++++ apps/routerconsole/jsp/configservice.jsp | 4 ++- apps/routerconsole/jsp/logs.jsp | 4 +++ core/java/src/net/i2p/data/DataHelper.java | 30 +++++++++++++++++ core/java/src/net/i2p/util/I2PThread.java | 32 ++++++++++++++++--- router/java/src/net/i2p/router/Router.java | 1 + 7 files changed, 92 insertions(+), 9 deletions(-) diff --git a/apps/routerconsole/java/src/net/i2p/router/web/ConfigServiceHandler.java b/apps/routerconsole/java/src/net/i2p/router/web/ConfigServiceHandler.java index 51f04d48c..83d354008 100644 --- a/apps/routerconsole/java/src/net/i2p/router/web/ConfigServiceHandler.java +++ b/apps/routerconsole/java/src/net/i2p/router/web/ConfigServiceHandler.java @@ -28,15 +28,28 @@ public class ConfigServiceHandler extends FormHandler { } else if ("Cancel graceful shutdown".equals(_action)) { _context.router().cancelGracefulShutdown(); addFormNotice("Graceful shutdown cancelled"); + } else if ("Hard restart".equals(_action)) { + _context.router().shutdown(Router.EXIT_HARD_RESTART); + addFormNotice("Hard restart requested"); } else if ("Dump threads".equals(_action)) { WrapperManager.requestThreadDump(); addFormNotice("Threads dumped to logs/wrapper.log"); } else if ("Show systray icon".equals(_action)) { - SysTray.getInstance().show(); - addFormNotice("Systray icon enabled (if possible)"); + SysTray tray = SysTray.getInstance(); + if (tray != null) { + tray.show(); + addFormNotice("Systray enabled"); + } else { + addFormNotice("Systray not supported on this platform"); + } } else if ("Hide systray icon".equals(_action)) { - SysTray.getInstance().hide(); - addFormNotice("Systray icon disabled"); + SysTray tray = SysTray.getInstance(); + if (tray != null) { + tray.hide(); + addFormNotice("Systray disabled"); + } else { + addFormNotice("Systray not supported on this platform"); + } } else { addFormNotice("Blah blah blah. whatever. I'm not going to " + _action); } 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 ab069ec76..ee8299bfb 100644 --- a/apps/routerconsole/java/src/net/i2p/router/web/LogsHelper.java +++ b/apps/routerconsole/java/src/net/i2p/router/web/LogsHelper.java @@ -4,6 +4,7 @@ import java.io.ByteArrayOutputStream; import java.io.IOException; import java.util.List; +import net.i2p.data.DataHelper; import net.i2p.router.RouterContext; public class LogsHelper { @@ -39,4 +40,12 @@ public class LogsHelper { return buf.toString(); } + + public String getServiceLogs() { + String str = DataHelper.readTextFile("logs/wrapper.log", 500); + if (str == null) + return ""; + else + return "
" + str + "
"; + } } diff --git a/apps/routerconsole/jsp/configservice.jsp b/apps/routerconsole/jsp/configservice.jsp index 7dd44a452..6728c1862 100644 --- a/apps/routerconsole/jsp/configservice.jsp +++ b/apps/routerconsole/jsp/configservice.jsp @@ -31,6 +31,7 @@ +

Systray integration

On the windows platform, there is a small application to sit in the system tray, allowing you to view the router's status (later on, I2P client applications @@ -50,7 +51,8 @@

Debugging

At times, it may be helpful to debug I2P by getting a thread dump. To do so, - please select the following option and review the thread dumped to logs/wrapper.log.
+ please select the following option and review the thread dumped to +logs/wrapper.log.
diff --git a/apps/routerconsole/jsp/logs.jsp b/apps/routerconsole/jsp/logs.jsp index f46f2c7d6..10b61c115 100644 --- a/apps/routerconsole/jsp/logs.jsp +++ b/apps/routerconsole/jsp/logs.jsp @@ -13,7 +13,11 @@
" /> +

Router logs:

+
+

Service logs:

+
diff --git a/core/java/src/net/i2p/data/DataHelper.java b/core/java/src/net/i2p/data/DataHelper.java index 738d84bc0..0c4f6e3bd 100644 --- a/core/java/src/net/i2p/data/DataHelper.java +++ b/core/java/src/net/i2p/data/DataHelper.java @@ -606,4 +606,34 @@ public class DataHelper { // * (((double) rv.length) / ((double) orig.length)) + "% savings)"); return rv; } + + /** + * Read in the last few lines of a (newline delimited) textfile, or null if + * the file doesn't exist. + * + */ + public static String readTextFile(String filename, int maxNumLines) { + File f = new File(filename); + if (!f.exists()) return null; + FileInputStream fis = null; + try { + fis = new FileInputStream(f); + BufferedReader in = new BufferedReader(new InputStreamReader(fis)); + List lines = new ArrayList(maxNumLines); + String line = null; + while ( (line = in.readLine()) != null) { + lines.add(line); + while (lines.size() > maxNumLines) + lines.remove(0); + } + StringBuffer buf = new StringBuffer(lines.size() * 80); + for (int i = 0; i < lines.size(); i++) + buf.append((String)lines.get(i)).append('\n'); + return buf.toString(); + } catch (IOException ioe) { + return null; + } finally { + if (fis != null) try { fis.close(); } catch (IOException ioe) {} + } + } } diff --git a/core/java/src/net/i2p/util/I2PThread.java b/core/java/src/net/i2p/util/I2PThread.java index 88d89da74..b4bd8d740 100644 --- a/core/java/src/net/i2p/util/I2PThread.java +++ b/core/java/src/net/i2p/util/I2PThread.java @@ -20,33 +20,51 @@ import java.util.Set; * */ public class I2PThread extends Thread { - private static Log _log; + private static volatile Log _log; private static Set _listeners = new HashSet(4); + private String _name; + private Exception _createdBy; public I2PThread() { super(); + if ( (_log == null) || (_log.shouldLog(Log.DEBUG)) ) + _createdBy = new Exception("Created by"); } public I2PThread(String name) { super(name); + if ( (_log == null) || (_log.shouldLog(Log.DEBUG)) ) + _createdBy = new Exception("Created by"); } public I2PThread(Runnable r) { super(r); + if ( (_log == null) || (_log.shouldLog(Log.DEBUG)) ) + _createdBy = new Exception("Created by"); } public I2PThread(Runnable r, String name) { super(r, name); + if ( (_log == null) || (_log.shouldLog(Log.DEBUG)) ) + _createdBy = new Exception("Created by"); + } + + private void log(int level, String msg) { log(level, msg, null); } + private void log(int level, String msg, Throwable t) { + // we cant assume log is created + if (_log == null) _log = new Log(I2PThread.class); + if (_log.shouldLog(level)) + _log.log(level, msg, t); } public void run() { + _name = Thread.currentThread().getName(); + log(Log.DEBUG, "New thread started: " + _name, _createdBy); try { super.run(); } catch (Throwable t) { try { - // we cant assume log is created - if (_log == null) _log = new Log(I2PThread.class); - _log.log(Log.CRIT, "Killing thread " + getName(), t); + log(Log.CRIT, "Killing thread " + getName(), t); } catch (Throwable woof) { System.err.println("Died within the OOM itself"); t.printStackTrace(); @@ -54,6 +72,12 @@ public class I2PThread extends Thread { if (t instanceof OutOfMemoryError) fireOOM((OutOfMemoryError)t); } + log(Log.DEBUG, "Thread finished gracefully: " + _name); + } + + protected void finalize() throws Throwable { + log(Log.DEBUG, "Thread finalized: " + _name); + super.finalize(); } private void fireOOM(OutOfMemoryError oom) { diff --git a/router/java/src/net/i2p/router/Router.java b/router/java/src/net/i2p/router/Router.java index 2459b715e..bf267b657 100644 --- a/router/java/src/net/i2p/router/Router.java +++ b/router/java/src/net/i2p/router/Router.java @@ -539,6 +539,7 @@ public class Router { public static final int EXIT_GRACEFUL = 2; public static final int EXIT_HARD = 3; public static final int EXIT_OOM = 10; + public static final int EXIT_HARD_RESTART = 4; public void shutdown(int exitCode) { _isAlive = false;