From b9bc254fd320e7345c2565399bbc0e9aa3aca00e Mon Sep 17 00:00:00 2001 From: zzz Date: Mon, 16 Apr 2018 11:06:18 +0000 Subject: [PATCH] SusiMail: Move logging to router logs (ticket #2191) --- .../src/src/i2p/susi/debug/Debug.java | 62 --------- .../src/src/i2p/susi/util/Config.java | 11 +- .../src/src/i2p/susi/webmail/Mail.java | 12 +- .../src/src/i2p/susi/webmail/MailCache.java | 14 +- .../src/src/i2p/susi/webmail/MailPart.java | 39 +++--- .../i2p/susi/webmail/PersistentMailCache.java | 18 +-- .../src/src/i2p/susi/webmail/WebMail.java | 127 ++++++++++-------- .../i2p/susi/webmail/encoding/Encoding.java | 8 ++ .../webmail/encoding/EncodingFactory.java | 8 +- .../i2p/susi/webmail/encoding/HeaderLine.java | 36 ++--- .../susi/webmail/pop3/BackgroundChecker.java | 16 ++- .../i2p/susi/webmail/pop3/DelayedDeleter.java | 13 +- .../src/i2p/susi/webmail/pop3/IdleCloser.java | 9 +- .../i2p/susi/webmail/pop3/POP3MailBox.java | 119 ++++++++-------- .../src/i2p/susi/webmail/smtp/SMTPClient.java | 21 +-- 15 files changed, 249 insertions(+), 264 deletions(-) delete mode 100644 apps/susimail/src/src/i2p/susi/debug/Debug.java diff --git a/apps/susimail/src/src/i2p/susi/debug/Debug.java b/apps/susimail/src/src/i2p/susi/debug/Debug.java deleted file mode 100644 index b3f280642..000000000 --- a/apps/susimail/src/src/i2p/susi/debug/Debug.java +++ /dev/null @@ -1,62 +0,0 @@ -/* - * Created on Nov 4, 2004 - * - * This file is part of susimail project, see http://susi.i2p/ - * - * Copyright (C) 2004-2005 - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * $Revision: 1.2 $ - */ -package i2p.susi.debug; - -import net.i2p.I2PAppContext; - -/** - * @author susi23 - */ -public class Debug { - - public static final int ERROR = 1; - public static final int DEBUG = 2; - private static int level = ERROR; - - public static void setLevel( int newLevel ) - { - level = newLevel; - } - - /** @since 0.9.13 */ - public static int getLevel() { - return level; - } - - public static void debug( int msgLevel, String msg ) { - debug(msgLevel, msg, null); - } - - /** @since 0.9.34 */ - public static void debug(int msgLevel, String msg, Throwable t) - { - if( msgLevel <= level ) { - System.err.println("SusiMail: " + msg); - if (t != null) - t.printStackTrace(); - } - if (msgLevel <= ERROR) - I2PAppContext.getGlobalContext().logManager().getLog(Debug.class).error(msg, t); - } -} diff --git a/apps/susimail/src/src/i2p/susi/util/Config.java b/apps/susimail/src/src/i2p/susi/util/Config.java index 26171e87f..488001479 100644 --- a/apps/susimail/src/src/i2p/susi/util/Config.java +++ b/apps/susimail/src/src/i2p/susi/util/Config.java @@ -23,8 +23,6 @@ */ package i2p.susi.util; -import i2p.susi.debug.Debug; - import java.io.File; import java.io.FileInputStream; import java.io.InputStream; @@ -34,6 +32,7 @@ import java.util.Properties; import net.i2p.I2PAppContext; import net.i2p.data.DataHelper; +import net.i2p.util.Log; import net.i2p.util.OrderedProperties; /** @@ -92,14 +91,15 @@ public class Config { */ public synchronized static void reloadConfiguration() { - // DEBUG level logging won't work here since we haven't loaded the config yet... + I2PAppContext ctx = I2PAppContext.getGlobalContext(); properties = new Properties(); InputStream iv = null; try { iv = Config.class.getResourceAsStream("/susimail.properties"); properties.load(iv); } catch (IOException e) { - Debug.debug(Debug.ERROR, "Could not open WEB-INF/classes/susimail.properties (possibly in jar), reason: " + e); + Log log = ctx.logManager().getLog(Config.class); + log.error("Could not open WEB-INF/classes/susimail.properties (possibly in jar)", e); } finally { if(iv != null) try { iv.close(); } catch(IOException ioe) {} } @@ -110,7 +110,8 @@ public class Config { DataHelper.loadProps(config, cfg); } } catch (IOException e) { - Debug.debug(Debug.ERROR, "Could not open susimail.config, reason: " + e); + Log log = ctx.logManager().getLog(Config.class); + log.error("Could not open susimail.config", e); } } diff --git a/apps/susimail/src/src/i2p/susi/webmail/Mail.java b/apps/susimail/src/src/i2p/susi/webmail/Mail.java index 51dece0e2..84660e0b4 100644 --- a/apps/susimail/src/src/i2p/susi/webmail/Mail.java +++ b/apps/susimail/src/src/i2p/susi/webmail/Mail.java @@ -23,7 +23,6 @@ */ package i2p.susi.webmail; -import i2p.susi.debug.Debug; import i2p.susi.util.Buffer; import i2p.susi.util.Config; import i2p.susi.util.CountingInputStream; @@ -51,6 +50,7 @@ import java.util.regex.Pattern; import net.i2p.I2PAppContext; import net.i2p.data.DataHelper; import net.i2p.servlet.util.ServletUtil; +import net.i2p.util.Log; import net.i2p.util.RFC822Date; import net.i2p.util.SystemVersion; @@ -96,6 +96,7 @@ class Mail { public String error; public boolean markForDeletion; + private final Log _log; public Mail(String uidl) { this.uidl = uidl; @@ -108,6 +109,7 @@ class Mail { shortSubject = ""; quotedDate = unknown; error = ""; + _log = I2PAppContext.getGlobalContext().logManager().getLog(Mail.class); } /** @@ -186,9 +188,9 @@ class Mail { size = rb.getLength(); success = true; } catch (IOException de) { - Debug.debug(Debug.ERROR, "Decode error", de); + _log.error("Decode error", de); } catch (RuntimeException e) { - Debug.debug(Debug.ERROR, "Parse error", e); + _log.error("Parse error", e); } finally { if (in != null) try { in.close(); } catch (IOException ioe) {} rb.readComplete(success); @@ -410,7 +412,7 @@ class Mail { MemoryBuffer decoded = new MemoryBuffer(4096); hl.decode(eofin, decoded); if (!eofin.wasFound()) - Debug.debug(Debug.DEBUG, "EOF hit before \\r\\n\\r\\n in Mail"); + if (_log.shouldDebug()) _log.debug("EOF hit before \\r\\n\\r\\n in Mail"); // Fixme UTF-8 to bytes to UTF-8 headerLines = DataHelper.split(new String(decoded.getContent(), decoded.getOffset(), decoded.getLength()), "\r\n"); for (int j = 0; j < headerLines.length; j++) { @@ -501,7 +503,7 @@ class Mail { } catch( Exception e ) { error += "Error parsing mail header: " + e.getClass().getName() + '\n'; - Debug.debug(Debug.ERROR, "Parse error", e); + _log.error("Parse error", e); } } } diff --git a/apps/susimail/src/src/i2p/susi/webmail/MailCache.java b/apps/susimail/src/src/i2p/susi/webmail/MailCache.java index 8fbb0eff2..3a238dc22 100644 --- a/apps/susimail/src/src/i2p/susi/webmail/MailCache.java +++ b/apps/susimail/src/src/i2p/susi/webmail/MailCache.java @@ -23,7 +23,6 @@ */ package i2p.susi.webmail; -import i2p.susi.debug.Debug; import i2p.susi.util.Config; import i2p.susi.util.Buffer; import i2p.susi.util.FileBuffer; @@ -48,6 +47,7 @@ import java.util.Set; import net.i2p.I2PAppContext; import net.i2p.util.FileUtil; import net.i2p.util.I2PAppThread; +import net.i2p.util.Log; /** * There's one of these for each Folder. @@ -70,6 +70,7 @@ class MailCache { private NewMailListener _loadInProgress; private boolean _isLoaded; private final boolean _isDrafts; + private final Log _log; /** Includes header, headers are generally 1KB to 1.5 KB, * and bodies will compress well. @@ -84,6 +85,7 @@ class MailCache { */ MailCache(I2PAppContext ctx, POP3MailBox mailbox, String folderName, String host, int port, String user, String pass) throws IOException { + _log = ctx.logManager().getLog(MailCache.class); this.mailbox = mailbox; mails = new Hashtable(); disk = new PersistentMailCache(ctx, host, port, user, pass, folderName); @@ -242,7 +244,7 @@ class MailCache { public synchronized boolean loadFromDisk(NewMailListener nml) { if (_isLoaded || _loadInProgress != null) return false; - Debug.debug(Debug.DEBUG, "Loading folder " + folderName); + if (_log.shouldDebug()) _log.debug("Loading folder " + folderName); Thread t = new I2PAppThread(new LoadMailRunner(nml), "Email loader"); _loadInProgress = nml; try { @@ -268,7 +270,7 @@ class MailCache { blockingLoadFromDisk(); if(!mails.isEmpty()) result = true; - Debug.debug(Debug.DEBUG, "Folder loaded: " + folderName); + if (_log.shouldDebug()) _log.debug("Folder loaded: " + folderName); } finally { synchronized(MailCache.this) { if (_loadInProgress == _nml) @@ -401,7 +403,7 @@ class MailCache { if (mode == FetchMode.CACHE_ONLY) throw new IllegalArgumentException(); if (mailbox == null) { - Debug.debug(Debug.DEBUG, "getMail() mode " + mode + " called on wrong folder " + getFolderName(), new Exception()); + if (_log.shouldDebug()) _log.debug("getMail() mode " + mode + " called on wrong folder " + getFolderName(), new Exception()); return false; } boolean hOnly = mode == FetchMode.HEADER; @@ -439,7 +441,7 @@ class MailCache { if(!mail.hasHeader()) { if (disk != null) { if (disk.getMail(mail, true)) { - Debug.debug(Debug.DEBUG, "Loaded header from disk cache: " + uidl); + if (_log.shouldDebug()) _log.debug("Loaded header from disk cache: " + uidl); // note that disk loaded the full body if it had it if (mail.hasBody() && !Boolean.parseBoolean(Config.getProperty(WebMail.CONFIG_LEAVE_ON_SERVER))) { @@ -462,7 +464,7 @@ class MailCache { if(!mail.hasBody()) { if (disk != null) { if (disk.getMail(mail, false)) { - Debug.debug(Debug.DEBUG, "Loaded body from disk cache: " + uidl); + if (_log.shouldDebug()) _log.debug("Loaded body from disk cache: " + uidl); // note that disk loaded the full body if it had it if (!Boolean.parseBoolean(Config.getProperty(WebMail.CONFIG_LEAVE_ON_SERVER))) { // we already have it, send delete diff --git a/apps/susimail/src/src/i2p/susi/webmail/MailPart.java b/apps/susimail/src/src/i2p/susi/webmail/MailPart.java index ec71dc339..73d0a9e1c 100644 --- a/apps/susimail/src/src/i2p/susi/webmail/MailPart.java +++ b/apps/susimail/src/src/i2p/susi/webmail/MailPart.java @@ -23,7 +23,6 @@ */ package i2p.susi.webmail; -import i2p.susi.debug.Debug; import i2p.susi.util.Buffer; import i2p.susi.util.CountingOutputStream; import i2p.susi.util.DummyOutputStream; @@ -46,7 +45,9 @@ import java.util.List; import java.util.Locale; import java.util.concurrent.atomic.AtomicInteger; +import net.i2p.I2PAppContext; import net.i2p.data.DataHelper; +import net.i2p.util.Log; /** * @author susi23 @@ -68,6 +69,7 @@ class MailPart { public final List parts; public final boolean multipart, message; public final Buffer buffer; + private final Log _log; /** * the decoded length if known, else -1 @@ -92,6 +94,7 @@ class MailPart { */ public MailPart(String uidl, AtomicInteger id, Buffer readBuffer, InputStream in, ReadCounter counter, String[] hdrlines) throws IOException { + _log = I2PAppContext.getGlobalContext().logManager().getLog(MailPart.class); this.uidl = uidl; intID = id.getAndIncrement(); buffer = readBuffer; @@ -113,7 +116,7 @@ class MailPart { MemoryBuffer decodedHeaders = new MemoryBuffer(4096); EncodingFactory.getEncoding("HEADERLINE").decode(eofin, decodedHeaders); if (!eofin.wasFound()) - Debug.debug(Debug.DEBUG, "EOF hit before \\r\\n\\r\\n in MailPart"); + if (_log.shouldDebug()) _log.debug("EOF hit before \\r\\n\\r\\n in MailPart"); // Fixme UTF-8 to bytes to UTF-8 headerLines = DataHelper.split(new String(decodedHeaders.getContent(), decodedHeaders.getOffset(), decodedHeaders.getLength()), "\r\n"); } @@ -193,7 +196,7 @@ class MailPart { // swallow the \n int c = in.read(); if (c != '\n') - Debug.debug(Debug.DEBUG, "wasn't a \\n, it was " + c); + if (_log.shouldDebug()) _log.debug("wasn't a \\n, it was " + c); beginBody = (int) counter.getRead(); } @@ -211,10 +214,10 @@ class MailPart { OutputStream dummy = new DummyOutputStream(); DataHelper.copy(eofin, dummy); if (!eofin.wasFound()) - Debug.debug(Debug.DEBUG, "EOF hit before first boundary " + boundary + " UIDL: " + uidl); + if (_log.shouldDebug()) _log.debug("EOF hit before first boundary " + boundary + " UIDL: " + uidl); if (readBoundaryTrailer(in)) { if (!eofin.wasFound()) - Debug.debug(Debug.DEBUG, "EOF hit before first part body " + boundary + " UIDL: " + uidl); + if (_log.shouldDebug()) _log.debug("EOF hit before first part body " + boundary + " UIDL: " + uidl); tmpEnd = (int) eofin.getRead(); break; } @@ -229,7 +232,7 @@ class MailPart { // if MailPart contains a MailPart, we may not have drained to the end DataHelper.copy(eofin, DUMMY_OUTPUT); if (!eofin.wasFound()) - Debug.debug(Debug.DEBUG, "EOF hit before end of body " + i + " boundary: " + boundary + " UIDL: " + uidl); + if (_log.shouldDebug()) _log.debug("EOF hit before end of body " + i + " boundary: " + boundary + " UIDL: " + uidl); } if (readBoundaryTrailer(in)) break; @@ -251,7 +254,7 @@ class MailPart { decodedLength = end - beginBody; } //if (Debug.getLevel() >= Debug.DEBUG) - // Debug.debug(Debug.DEBUG, "New " + this); + // if (_log.shouldDebug()) _log.debug("New " + this); } /** @@ -269,13 +272,13 @@ class MailPart { * * @return true if end of input */ - private static boolean readBoundaryTrailer(InputStream in) throws IOException { + private boolean readBoundaryTrailer(InputStream in) throws IOException { int c = in.read(); if (c == '-') { // end of parts with this boundary c = in.read(); if (c != '-') { - Debug.debug(Debug.DEBUG, "Unexpected char after boundary-: " + c); + if (_log.shouldDebug()) _log.debug("Unexpected char after boundary-: " + c); return true; } c = in.read(); @@ -283,19 +286,19 @@ class MailPart { return true; } if (c != '\r') { - Debug.debug(Debug.DEBUG, "Unexpected char after boundary--: " + c); + if (_log.shouldDebug()) _log.debug("Unexpected char after boundary--: " + c); return true; } c = in.read(); if (c != '\n') - Debug.debug(Debug.DEBUG, "Unexpected char after boundary--\\r: " + c); + if (_log.shouldDebug()) _log.debug("Unexpected char after boundary--\\r: " + c); return true; } else if (c == '\r') { c = in.read(); if (c != '\n') - Debug.debug(Debug.DEBUG, "Unexpected char after boundary\\r: " + c); + if (_log.shouldDebug()) _log.debug("Unexpected char after boundary\\r: " + c); } else { - Debug.debug(Debug.DEBUG, "Unexpected char after boundary: " + c); + if (_log.shouldDebug()) _log.debug("Unexpected char after boundary: " + c); } return c == -1; } @@ -310,7 +313,7 @@ class MailPart { String encg = encoding; if (encg == null) { //throw new DecodingException("No encoding specified"); - Debug.debug(Debug.DEBUG, "Warning: no transfer encoding found, fallback to 7bit."); + if (_log.shouldDebug()) _log.debug("Warning: no transfer encoding found, fallback to 7bit."); encg = "7bit"; } Encoding enc = EncodingFactory.getEncoding(encg); @@ -332,13 +335,13 @@ class MailPart { //dout.getOutputStream().flush(); } catch (IOException ioe) { if (lin != null) - Debug.debug(Debug.DEBUG, "Decode IOE at in position " + lin.getRead() + if (_log.shouldDebug()) _log.debug("Decode IOE at in position " + lin.getRead() + " offset " + offset, ioe); else if (cos != null) - Debug.debug(Debug.DEBUG, "Decode IOE at out position " + cos.getWritten() + if (_log.shouldDebug()) _log.debug("Decode IOE at out position " + cos.getWritten() + " offset " + offset, ioe); else - Debug.debug(Debug.DEBUG, "Decode IOE", ioe); + if (_log.shouldDebug()) _log.debug("Decode IOE", ioe); throw ioe; } finally { if (lin != null) try { lin.close(); } catch (IOException ioe) {}; @@ -365,7 +368,7 @@ class MailPart { lin = getRawInputStream(0); DataHelper.copy(lin, out); } catch (IOException ioe) { - Debug.debug(Debug.DEBUG, "Decode IOE", ioe); + if (_log.shouldDebug()) _log.debug("Decode IOE", ioe); throw ioe; } finally { if (lin != null) try { lin.close(); } catch (IOException ioe) {}; diff --git a/apps/susimail/src/src/i2p/susi/webmail/PersistentMailCache.java b/apps/susimail/src/src/i2p/susi/webmail/PersistentMailCache.java index ce6b63a2d..a002bfc66 100644 --- a/apps/susimail/src/src/i2p/susi/webmail/PersistentMailCache.java +++ b/apps/susimail/src/src/i2p/susi/webmail/PersistentMailCache.java @@ -1,6 +1,5 @@ package i2p.susi.webmail; -import i2p.susi.debug.Debug; import i2p.susi.webmail.Messages; import i2p.susi.util.Buffer; import i2p.susi.util.FileBuffer; @@ -33,6 +32,7 @@ import net.i2p.data.Base64; import net.i2p.data.DataHelper; import net.i2p.util.FileSuffixFilter; import net.i2p.util.I2PAppThread; +import net.i2p.util.Log; import net.i2p.util.PasswordManager; import net.i2p.util.SecureDirectory; import net.i2p.util.SecureFile; @@ -73,6 +73,7 @@ class PersistentMailCache { // non-null only for Drafts private final File _attachmentDir; private final I2PAppContext _context; + private final Log _log; private final boolean _isDrafts; private static final String DIR_SUSI = "susimail"; @@ -96,6 +97,7 @@ class PersistentMailCache { */ public PersistentMailCache(I2PAppContext ctx, String host, int port, String user, String pass, String folder) throws IOException { _context = ctx; + _log = ctx.logManager().getLog(PersistentMailCache.class); _isDrafts = folder.equals(WebMail.DIR_DRAFTS); _lock = getLock(host, port, user, pass); synchronized(_lock) { @@ -163,7 +165,7 @@ class PersistentMailCache { } } long end = _context.clock().now(); - Debug.debug(Debug.DEBUG, "Loaded " + sz + " emails with " + tcnt + " threads in " + DataHelper.formatDuration(end - begin)); + if (_log.shouldDebug()) _log.debug("Loaded " + sz + " emails with " + tcnt + " threads in " + DataHelper.formatDuration(end - begin)); return rv; } @@ -347,7 +349,7 @@ class PersistentMailCache { * * @return success */ - private static boolean write(Buffer rb, File f) { + private boolean write(Buffer rb, File f) { InputStream in = null; OutputStream out = null; try { @@ -357,7 +359,7 @@ class PersistentMailCache { DataHelper.copy(in, out); return true; } catch (IOException ioe) { - Debug.debug(Debug.ERROR, "Error writing: " + f + ": " + ioe); + _log.error("Error writing: " + f + ": " + ioe); return false; } finally { if (in != null) @@ -437,7 +439,7 @@ class PersistentMailCache { } } } catch (IOException ioe) { - Debug.debug(Debug.ERROR, "Import failed " + f, ioe); + _log.error("Import failed " + f, ioe); continue; } finally { if (in != null) @@ -447,7 +449,7 @@ class PersistentMailCache { uidl = Long.toString(_context.random().nextLong()); File to = getFullFile(uidl); if (to.exists()) { - Debug.debug(Debug.DEBUG, "Already have " + f + " as UIDL " + uidl); + if (_log.shouldDebug()) _log.debug("Already have " + f + " as UIDL " + uidl); f.delete(); continue; } @@ -460,7 +462,7 @@ class PersistentMailCache { out = new FixCRLFOutputStream(gb.getOutputStream()); DataHelper.copy(in, out); } catch (IOException ioe) { - Debug.debug(Debug.ERROR, "Import failed " + f, ioe); + _log.error("Import failed " + f, ioe); continue; } finally { if (in != null) @@ -469,7 +471,7 @@ class PersistentMailCache { try { out.close(); } catch (IOException ioe) {} } f.delete(); - Debug.debug(Debug.DEBUG, "Imported " + f + " as UIDL " + uidl); + if (_log.shouldDebug()) _log.debug("Imported " + f + " as UIDL " + uidl); } } } diff --git a/apps/susimail/src/src/i2p/susi/webmail/WebMail.java b/apps/susimail/src/src/i2p/susi/webmail/WebMail.java index 4548e5f6f..cd6b4147f 100644 --- a/apps/susimail/src/src/i2p/susi/webmail/WebMail.java +++ b/apps/susimail/src/src/i2p/susi/webmail/WebMail.java @@ -23,7 +23,6 @@ */ package i2p.susi.webmail; -import i2p.susi.debug.Debug; import i2p.susi.util.Buffer; import i2p.susi.util.Config; import i2p.susi.util.DecodingOutputStream; @@ -89,6 +88,7 @@ import net.i2p.servlet.RequestWrapper; import net.i2p.servlet.util.ServletUtil; import net.i2p.servlet.util.WriterOutputStream; import net.i2p.util.I2PAppThread; +import net.i2p.util.Log; import net.i2p.util.RFC822Date; import net.i2p.util.SecureFileOutputStream; import net.i2p.util.Translate; @@ -98,10 +98,8 @@ import net.i2p.util.Translate; */ public class WebMail extends HttpServlet { - /* - * set to true, if its a release build - */ - private static final boolean RELEASE; + private final Log _log = I2PAppContext.getGlobalContext().logManager().getLog(WebMail.class); + /* * increase version number for every release */ @@ -268,8 +266,6 @@ public class WebMail extends HttpServlet static { Config.setPrefix( "susimail" ); - RELEASE = !Boolean.parseBoolean(Config.getProperty(CONFIG_DEBUG)); - Debug.setLevel( RELEASE ? Debug.ERROR : Debug.DEBUG ); } @@ -299,12 +295,19 @@ public class WebMail extends HttpServlet boolean bccToSelf; private final List nonces; private static final int MAX_NONCES = 15; + public final Log log; - SessionObject() + SessionObject(Log log) { bccToSelf = Boolean.parseBoolean(Config.getProperty( CONFIG_BCC_TO_SELF, "true" )); nonces = new ArrayList(MAX_NONCES + 1); caches = new HashMap(8); + this.log = log; + String dbg = Config.getProperty(CONFIG_DEBUG); + if (dbg != null) { + boolean release = !Boolean.parseBoolean(dbg); + log.setMinimumPriority(release ? Log.ERROR : Log.DEBUG); + } } /** @since 0.9.13 */ @@ -315,7 +318,7 @@ public class WebMail extends HttpServlet * @since 0.9.13 */ public void valueUnbound(HttpSessionBindingEvent event) { - Debug.debug(Debug.DEBUG, "Session unbound: " + event.getSession().getId()); + if (log.shouldDebug()) log.debug("Session unbound: " + event.getSession().getId()); POP3MailBox mbox = mailbox; if (mbox != null) { mbox.destroy(); @@ -780,6 +783,7 @@ public class WebMail extends HttpServlet String host, int pop3PortNo, String user, String pass) { POP3MailBox mailbox = new POP3MailBox(host, pop3PortNo, user, pass); I2PAppContext ctx = I2PAppContext.getGlobalContext(); + Log log = sessionObject.log; MailCache mc; try { mc = new MailCache(ctx, mailbox, DIR_FOLDER, @@ -798,7 +802,7 @@ public class WebMail extends HttpServlet host, pop3PortNo, user, pass); sessionObject.caches.put(DIR_SPAM, mc2); } catch (IOException ioe) { - Debug.debug(Debug.ERROR, "Error creating disk cache", ioe); + log.error("Error creating disk cache", ioe); sessionObject.error += ioe.toString() + '\n'; return State.AUTH; } @@ -820,7 +824,7 @@ public class WebMail extends HttpServlet // thread 2: mailbox.connectToServer() if (offline) { - Debug.debug(Debug.DEBUG, "OFFLINE MODE"); + if (log.shouldDebug()) log.debug("OFFLINE MODE"); } else { sessionObject.isFetching = true; if (!mailbox.connectToServer(new ConnectWaiter(sessionObject))) { @@ -834,7 +838,7 @@ public class WebMail extends HttpServlet try { sessionObject.wait(5000); } catch (InterruptedException ie) { - Debug.debug(Debug.DEBUG, "Interrupted waiting for load", ie); + if (log.shouldDebug()) log.debug("Interrupted waiting for load", ie); } } state = mc.isLoading() ? State.LOADING : State.LIST; @@ -890,8 +894,9 @@ public class WebMail extends HttpServlet MailCache mc = null; Folder f = null; boolean found = false; + Log log = _so.log; if (connected) { - Debug.debug(Debug.DEBUG, "CONNECTED, YAY"); + if (log.shouldDebug()) log.debug("CONNECTED, YAY"); // we do this whether new mail was found or not, // because we may already have UIDLs in the MailCache to fetch synchronized(_so) { @@ -900,19 +905,19 @@ public class WebMail extends HttpServlet try { _so.wait(5000); } catch (InterruptedException ie) { - Debug.debug(Debug.DEBUG, "Interrupted waiting for load", ie); + if (log.shouldDebug()) log.debug("Interrupted waiting for load", ie); return; } } } - Debug.debug(Debug.DEBUG, "Done waiting for folder load"); + if (log.shouldDebug()) log.debug("Done waiting for folder load"); // fetch the mail outside the lock // TODO, would be better to add each email as we get it if (mc != null) { found = mc.getMail(MailCache.FetchMode.HEADER); } } else { - Debug.debug(Debug.DEBUG, "NOT CONNECTED, BOO"); + if (log.shouldDebug()) log.debug("NOT CONNECTED, BOO"); } synchronized(_so) { if (!connected) { @@ -922,7 +927,7 @@ public class WebMail extends HttpServlet else _so.connectError = _t("Error connecting to server"); } else if (!found) { - Debug.debug(Debug.DEBUG, "No new emails"); + if (log.shouldDebug()) log.debug("No new emails"); _so.newMails = 0; _so.connectError = null; } else if (mc != null && f != null) { @@ -932,9 +937,9 @@ public class WebMail extends HttpServlet _so.pageChanged = true; _so.newMails = added; _so.connectError = null; - Debug.debug(Debug.DEBUG, "Added " + added + " new emails"); + if (log.shouldDebug()) log.debug("Added " + added + " new emails"); } else { - Debug.debug(Debug.DEBUG, "MailCache/folder vanished?"); + if (log.shouldDebug()) log.debug("MailCache/folder vanished?"); } _mb.setNewMailListener(_so); _so.isFetching = false; @@ -952,8 +957,9 @@ public class WebMail extends HttpServlet */ private static State processLogout(SessionObject sessionObject, RequestWrapper request, boolean isPOST, State state) { + Log log = sessionObject.log; if( buttonPressed( request, LOGOUT ) && isPOST) { - Debug.debug(Debug.DEBUG, "LOGOUT, REMOVING SESSION"); + if (log.shouldDebug()) log.debug("LOGOUT, REMOVING SESSION"); HttpSession session = request.getSession(); session.removeAttribute( "sessionObject" ); session.invalidate(); @@ -969,7 +975,7 @@ public class WebMail extends HttpServlet !buttonPressed(request, SAVE)) { // AUTH will be passed in if mailbox is null // Check previous state - Debug.debug(Debug.DEBUG, "Lost conn, prev. state was " + request.getParameter(DEBUG_STATE)); + if (log.shouldDebug()) log.debug("Lost conn, prev. state was " + request.getParameter(DEBUG_STATE)); sessionObject.error += _t("Internal error, lost connection.") + '\n' + _t("User logged out.") + '\n'; } @@ -1002,6 +1008,7 @@ public class WebMail extends HttpServlet if (mc.isLoading()) return State.LOADING; } + Log log = sessionObject.log; /* * compose dialog @@ -1015,7 +1022,7 @@ public class WebMail extends HttpServlet String uidl = Base64.decodeToString(request.getParameter(NEW_UIDL)); if (uidl == null) uidl = I2PAppContext.getGlobalContext().random().nextLong() + "drft"; - Debug.debug(Debug.DEBUG, "Save as draft: " + uidl); + if (log.shouldDebug()) log.debug("Save as draft: " + uidl); MailCache toMC = sessionObject.caches.get(DIR_DRAFTS); Writer wout = null; boolean ok = false; @@ -1030,11 +1037,11 @@ public class WebMail extends HttpServlet buffer = toMC.getFullWriteBuffer(uidl); wout = new BufferedWriter(new OutputStreamWriter(buffer.getOutputStream(), "ISO-8859-1")); SMTPClient.writeMail(wout, draft, null, null); - Debug.debug(Debug.DEBUG, "Saved as draft: " + uidl); + if (log.shouldDebug()) log.debug("Saved as draft: " + uidl); ok = true; } catch (IOException ioe) { sessionObject.error += _t("Unable to save mail.") + ' ' + ioe.getMessage() + '\n'; - Debug.debug(Debug.DEBUG, "Unable to save as draft: " + uidl, ioe); + if (log.shouldDebug()) log.debug("Unable to save as draft: " + uidl, ioe); } finally { if (wout != null) try { wout.close(); } catch (IOException ioe) {} if (buffer != null) @@ -1052,13 +1059,13 @@ public class WebMail extends HttpServlet } else if (ok && buttonPressed(request, SEND)) { Draft mail = (Draft) toMC.getMail(uidl, MailCache.FetchMode.CACHE_ONLY); if (mail != null) { - Debug.debug(Debug.DEBUG, "Send mail: " + uidl); + if (log.shouldDebug()) log.debug("Send mail: " + uidl); ok = sendMail(sessionObject, mail); } else { // couldn't read it back in? ok = false; sessionObject.error += _t("Unable to save mail.") + '\n'; - Debug.debug(Debug.DEBUG, "Draft readback fail: " + uidl); + if (log.shouldDebug()) log.debug("Draft readback fail: " + uidl); } } if (ok) { @@ -1070,7 +1077,7 @@ public class WebMail extends HttpServlet } else { state = State.NEW; } - Debug.debug(Debug.DEBUG, "State after save as draft: " + state); + if (log.shouldDebug()) log.debug("State after save as draft: " + state); } else if (buttonPressed(request, CANCEL)) { // If we have a reference UIDL, go back to that if (request.getParameter(B64UIDL) != null) @@ -1360,11 +1367,12 @@ public class WebMail extends HttpServlet // shouldn't happen, button disabled return state; } + Log log = sessionObject.log; sessionObject.isFetching = true; ConnectWaiter cw = new ConnectWaiter(sessionObject); if (mailbox.connectToServer(cw)) { // Start a thread to wait for results - Debug.debug(Debug.DEBUG, "Already connected, running CW"); + if (log.shouldDebug()) log.debug("Already connected, running CW"); Thread t = new I2PAppThread(cw, "Email fetcher"); t.start(); } else { @@ -1375,7 +1383,7 @@ public class WebMail extends HttpServlet try { sessionObject.wait(3000); } catch (InterruptedException ie) { - Debug.debug(Debug.DEBUG, "Interrupted waiting for connect", ie); + if (log.shouldDebug()) log.debug("Interrupted waiting for connect", ie); } } return state; @@ -1771,8 +1779,12 @@ public class WebMail extends HttpServlet } catch( NumberFormatException nfe ) {} } Config.saveConfiguration(props); - boolean release = !Boolean.parseBoolean(props.getProperty(CONFIG_DEBUG)); - Debug.setLevel( release ? Debug.ERROR : Debug.DEBUG ); + String dbg = props.getProperty(CONFIG_DEBUG); + if (dbg != null) { + boolean release = !Boolean.parseBoolean(dbg); + Log log = sessionObject.log; + log.setMinimumPriority(release ? Log.ERROR : Log.DEBUG); + } state = folder != null ? State.LIST : State.AUTH; sessionObject.info = _t("Configuration saved"); } catch (IOException ioe) { @@ -1816,11 +1828,11 @@ public class WebMail extends HttpServlet SessionObject sessionObject = (SessionObject)httpSession.getAttribute( "sessionObject" ); if( sessionObject == null ) { - sessionObject = new SessionObject(); + sessionObject = new SessionObject(_log); httpSession.setAttribute( "sessionObject", sessionObject ); - Debug.debug(Debug.DEBUG, "NEW session " + httpSession.getId()); + if (_log.shouldDebug()) _log.debug("NEW session " + httpSession.getId()); } else { - Debug.debug(Debug.DEBUG, "Existing session " + httpSession.getId() + + if (_log.shouldDebug()) _log.debug("Existing session " + httpSession.getId() + " created " + new Date(httpSession.getCreationTime())); } return sessionObject; @@ -1895,7 +1907,8 @@ public class WebMail extends HttpServlet try { sessionObject.wait(5000); } catch (InterruptedException ie) { - Debug.debug(Debug.DEBUG, "Interrupted waiting for load", ie); + Log log = sessionObject.log; + if (log.shouldDebug()) log.debug("Interrupted waiting for load", ie); } } } @@ -2001,7 +2014,7 @@ public class WebMail extends HttpServlet } state = processStateChangeButtons(sessionObject, request, isPOST, state); state = processConfigButtons(sessionObject, request, isPOST, state); - Debug.debug(Debug.DEBUG, "Prelim. state is " + state); + if (_log.shouldDebug()) _log.debug("Prelim. state is " + state); if (state == State.CONFIG) { if (isPOST) { // P-R-G @@ -2034,7 +2047,7 @@ public class WebMail extends HttpServlet // int oldIdle = httpSession.getMaxInactiveInterval(); // httpSession.setMaxInactiveInterval(60*60*24); // seconds // int newIdle = httpSession.getMaxInactiveInterval(); - // Debug.debug(Debug.DEBUG, "Changed idle from " + oldIdle + " to " + newIdle); + // if (_log.shouldDebug()) _log.debug("Changed idle from " + oldIdle + " to " + newIdle); //} if( state != State.AUTH ) { @@ -2160,7 +2173,7 @@ public class WebMail extends HttpServlet try { sessionObject.wait(5000); } catch (InterruptedException ie) { - Debug.debug(Debug.DEBUG, "Interrupted waiting for load", ie); + if (_log.shouldDebug()) _log.debug("Interrupted waiting for load", ie); } } if ((state == State.LIST || state == State.SHOW) && mc.isLoading()) @@ -2170,7 +2183,7 @@ public class WebMail extends HttpServlet Folder folder = mc != null ? mc.getFolder() : null; //// End state determination, state will not change after here - Debug.debug(Debug.DEBUG, "Final state is " + state); + if (_log.shouldDebug()) _log.debug("Final state is " + state); if (state == State.LIST || state == State.SHOW) { // mc non-null @@ -2393,7 +2406,7 @@ public class WebMail extends HttpServlet resp.setHeader("Location", buf.toString()); resp.setStatus(303); resp.getOutputStream().close(); - Debug.debug(Debug.DEBUG, "P-R-G to " + q); + //if (_log.shouldDebug()) _log.debug("P-R-G to " + q); } /** @@ -2415,6 +2428,7 @@ public class WebMail extends HttpServlet { boolean shown = false; if(part != null) { + Log log = sessionObject.log; String name = part.filename; if (name == null) { name = part.name; @@ -2435,11 +2449,11 @@ public class WebMail extends HttpServlet response.setContentType(part.type); if (part.decodedLength >= 0) response.setContentLength(part.decodedLength); - Debug.debug(Debug.DEBUG, "Sending raw attachment " + name + " length " + part.decodedLength); + if (log.shouldDebug()) log.debug("Sending raw attachment " + name + " length " + part.decodedLength); part.decode(0, new OutputStreamBuffer(response.getOutputStream())); shown = true; } catch (IOException e) { - Debug.debug(Debug.ERROR, "Error sending raw attachment " + name + " length " + part.decodedLength, e); + log.error("Error sending raw attachment " + name + " length " + part.decodedLength, e); } } else { ZipOutputStream zip = null; @@ -2456,7 +2470,7 @@ public class WebMail extends HttpServlet zip.finish(); shown = true; } catch (IOException e) { - Debug.debug(Debug.ERROR, "Error sending zip attachment " + name + " length " + part.decodedLength, e); + log.error("Error sending zip attachment " + name + " length " + part.decodedLength, e); } finally { if ( zip != null) try { zip.close(); } catch (IOException ioe) {} @@ -2500,7 +2514,8 @@ public class WebMail extends HttpServlet DataHelper.copy(in, response.getOutputStream()); return true; } catch (IOException e) { - Debug.debug(Debug.DEBUG, "Save-As", e); + Log log = sessionObject.log; + if (log.shouldDebug()) log.debug("Save-As", e); return false; } finally { if (in != null) try { in.close(); } catch (IOException ioe) {} @@ -2582,12 +2597,13 @@ public class WebMail extends HttpServlet Mail.appendRecipients( body, ccList, "Cc: " ); // only for draft Mail.appendRecipients(body, bccList, Draft.HDR_BCC); + Log log = sessionObject.log; try { body.append(hl.encode("Subject: " + subject)); } catch (EncodingException e) { ok = false; sessionObject.error += e.getMessage() + '\n'; - Debug.debug(Debug.DEBUG, "Draft subj", e); + if (log.shouldDebug()) log.debug("Draft subj", e); } body.append("MIME-Version: 1.0\r\nContent-type: text/plain; charset=\"utf-8\"\r\nContent-Transfer-Encoding: quoted-printable\r\n\r\n"); try { @@ -2598,7 +2614,7 @@ public class WebMail extends HttpServlet } catch (EncodingException e) { ok = false; sessionObject.error += e.getMessage() + '\n'; - Debug.debug(Debug.DEBUG, "Draft body", e); + if (log.shouldDebug()) log.debug("Draft body", e); } } return ok ? body : null; @@ -2739,11 +2755,12 @@ public class WebMail extends HttpServlet } public void run() { - Debug.debug(Debug.DEBUG, "Email send start"); + Log log = sessionObject.log; + if (log.shouldDebug()) log.debug("Email send start"); SMTPClient relay = new SMTPClient(); boolean ok = relay.sendMail(host, port, user, pass, sender, recipients, body, attachments, boundary); - Debug.debug(Debug.DEBUG, "Email send complete, success? " + ok); + if (log.shouldDebug()) log.debug("Email send complete, success? " + ok); synchronized(sessionObject) { if (ok) { sessionObject.info += _t("Mail sent.") + '\n'; @@ -2754,7 +2771,7 @@ public class WebMail extends HttpServlet waitForLoad(sessionObject, mc); mc.delete(draft.uidl); mc.getFolder().removeElement(draft.uidl); - Debug.debug(Debug.DEBUG, "Sent email deleted from drafts"); + if (log.shouldDebug()) log.debug("Sent email deleted from drafts"); } // now store to sent mc = sessionObject.caches.get(DIR_SENT); @@ -2770,11 +2787,11 @@ public class WebMail extends HttpServlet wout = new BufferedWriter(new OutputStreamWriter(buffer.getOutputStream(), "ISO-8859-1")); SMTPClient.writeMail(wout, body, attachments, boundary); - Debug.debug(Debug.DEBUG, "Sent email saved to Sent"); + if (log.shouldDebug()) log.debug("Sent email saved to Sent"); copyOK = true; } catch (IOException ioe) { sessionObject.error += _t("Unable to save mail.") + ' ' + ioe.getMessage() + '\n'; - Debug.debug(Debug.DEBUG, "Sent email saved error", ioe); + if (log.shouldDebug()) log.debug("Sent email saved error", ioe); } finally { if (wout != null) try { wout.close(); } catch (IOException ioe) {} if (buffer != null) @@ -2863,7 +2880,8 @@ public class WebMail extends HttpServlet return; } String newUIDL = Base64.decodeToString(b64UIDL); - Debug.debug(Debug.DEBUG, "Show draft: " + newUIDL); + Log log = sessionObject.log; + if (log.shouldDebug()) log.debug("Show draft: " + newUIDL); if (newUIDL != null) draft = (Draft) drafts.getMail(newUIDL, MailCache.FetchMode.CACHE_ONLY); if (draft != null) { @@ -3101,7 +3119,7 @@ public class WebMail extends HttpServlet if( sessionObject.clear ) idChecked = false; - //Debug.debug( Debug.DEBUG, "check" + i + ": checkId=" + checkId + ", idChecked=" + idChecked + ", pageChanged=" + sessionObject.pageChanged + + //if (_log.shouldDebug()) _log.debug("check" + i + ": checkId=" + checkId + ", idChecked=" + idChecked + ", pageChanged=" + sessionObject.pageChanged + // ", markAll=" + sessionObject.markAll + // ", invert=" + sessionObject.invert + // ", clear=" + sessionObject.clear ); @@ -3243,7 +3261,8 @@ public class WebMail extends HttpServlet "

"); } Mail mail = mc.getMail(showUIDL, MailCache.FetchMode.ALL); - if(!RELEASE && mail != null && mail.hasBody() && mail.getSize() < 16384) { + boolean debug = Boolean.parseBoolean(Config.getProperty(CONFIG_DEBUG)); + if(debug && mail != null && mail.hasBody() && mail.getSize() < 16384) { out.println( "