diff --git a/app/src/main/java/eu/faircode/email/EmailService.java b/app/src/main/java/eu/faircode/email/EmailService.java index 2c167bfb00..f849e61ebc 100644 --- a/app/src/main/java/eu/faircode/email/EmailService.java +++ b/app/src/main/java/eu/faircode/email/EmailService.java @@ -45,8 +45,6 @@ import com.sun.mail.util.MailConnectException; import com.sun.mail.util.SocketConnectException; import com.sun.mail.util.TraceOutputStream; -import org.json.JSONException; - import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.OutputStream; @@ -80,7 +78,6 @@ import java.util.concurrent.ExecutorService; import java.util.regex.Pattern; import javax.mail.AuthenticationFailedException; -import javax.mail.Authenticator; import javax.mail.Folder; import javax.mail.MessagingException; import javax.mail.NoSuchProviderException; @@ -117,6 +114,7 @@ public class EmailService implements AutoCloseable { private Service iservice; private StoreListener listener; private ServiceAuthenticator authenticator; + private RingBuffer breadcrumbs; private ExecutorService executor = Helper.getBackgroundExecutor(0, "mail"); @@ -136,6 +134,7 @@ public class EmailService implements AutoCloseable { private final static int POOL_SIZE = 1; // connections private final static int POOL_TIMEOUT = 60 * 1000; // milliseconds, default 45 sec private final static long PROTOCOL_LOG_DURATION = 12 * 3600 * 1000L; + private final static int BREADCRUMBS_SIZE = 100; private final static int MAX_IPV4 = 2; private final static int MAX_IPV6 = 1; @@ -679,6 +678,8 @@ public class EmailService implements AutoCloseable { SSLSocketFactoryService factory) throws MessagingException { isession = Session.getInstance(properties, authenticator); + breadcrumbs = new RingBuffer<>(BREADCRUMBS_SIZE); + isession.setDebug(debug || log); if (debug || log) isession.setDebugOut(new PrintStream(new OutputStream() { @@ -693,6 +694,7 @@ public class EmailService implements AutoCloseable { if (log) EntityLog.log(context, EntityLog.Type.Protocol, user + " " + line); else { + breadcrumbs.push(line); if (BuildConfig.DEBUG) Log.i("javamail", user + " " + line); } @@ -876,6 +878,11 @@ public class EmailService implements AutoCloseable { } } + public void dump() { + while (breadcrumbs != null && !breadcrumbs.isEmpty()) + EntityLog.log(context, "Crumb " + breadcrumbs.pop()); + } + private static class SocketFactoryService extends SocketFactory { private SocketFactory factory = SocketFactory.getDefault(); diff --git a/app/src/main/java/eu/faircode/email/RingBuffer.java b/app/src/main/java/eu/faircode/email/RingBuffer.java new file mode 100644 index 0000000000..1f7a0a4a08 --- /dev/null +++ b/app/src/main/java/eu/faircode/email/RingBuffer.java @@ -0,0 +1,56 @@ +package eu.faircode.email; + +/* + This file is part of FairEmail. + + FairEmail 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 3 of the License, or + (at your option) any later version. + + FairEmail 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 FairEmail. If not, see . + + Copyright 2018-2021 by Marcel Bokhorst (M66B) +*/ + +public class RingBuffer { + private T[] buffer; + private int count = 0; + private int out = 0; + private int in = 0; + + public RingBuffer(int capacity) { + buffer = (T[]) new Object[capacity]; + } + + public synchronized void push(T item) { + if (count == buffer.length) + pop(); + + buffer[in] = item; + in = (in + 1) % buffer.length; + count++; + } + + public synchronized T pop() { + T item = buffer[out]; + buffer[out] = null; + count--; + out = (out + 1) % buffer.length; + return item; + } + + public synchronized boolean isEmpty() { + return (count == 0); + } + + public synchronized int size() { + return count; + } +} diff --git a/app/src/main/java/eu/faircode/email/ServiceSynchronize.java b/app/src/main/java/eu/faircode/email/ServiceSynchronize.java index 2fb91e0938..61b15c1e52 100644 --- a/app/src/main/java/eu/faircode/email/ServiceSynchronize.java +++ b/app/src/main/java/eu/faircode/email/ServiceSynchronize.java @@ -2130,6 +2130,7 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences Log.i(account.name + " done state=" + state); } catch (Throwable ex) { last_fail = ex; + iservice.dump(); Log.e(account.name, ex); EntityLog.log(this, EntityLog.Type.Account, account.name + " connect " + Log.formatThrowable(ex, false));