Browse Source

Improved logging

main
M66B 6 years ago
parent
commit
4802a8b2c8
5 changed files with 50 additions and 22 deletions
  1. +1
    -1
      .idea/misc.xml
  2. +9
    -2
      app/src/main/java/eu/faircode/email/DaoLog.java
  3. +10
    -1
      app/src/main/java/eu/faircode/email/FragmentAbout.java
  4. +4
    -1
      app/src/main/java/eu/faircode/email/FragmentLogs.java
  5. +26
    -17
      app/src/main/java/eu/faircode/email/ServiceSynchronize.java

+ 1
- 1
.idea/misc.xml View File

@ -30,7 +30,7 @@
</value>
</option>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_7" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/build/classes" />
</component>
<component name="ProjectType">


+ 9
- 2
app/src/main/java/eu/faircode/email/DaoLog.java View File

@ -28,8 +28,15 @@ import androidx.room.Query;
@Dao
public interface DaoLog {
@Query("SELECT * FROM log ORDER BY time DESC LIMIT 500")
LiveData<List<EntityLog>> liveLogs();
@Query("SELECT * FROM log" +
" WHERE time > :from" +
" ORDER BY time DESC")
LiveData<List<EntityLog>> liveLogs(long from);
@Query("SELECT * FROM log" +
" WHERE time > :from" +
" ORDER BY time DESC")
List<EntityLog> getLogs(long from);
@Insert
long insertLog(EntityLog log);


+ 10
- 1
app/src/main/java/eu/faircode/email/FragmentAbout.java View File

@ -33,6 +33,8 @@ import android.widget.Toast;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.mail.Address;
@ -95,13 +97,20 @@ public class FragmentAbout extends FragmentEx {
sb.append(String.format("Id: %s\r\n", Build.ID));
sb.append("\r\n");
// Get recent log
long from = new Date().getTime() - 12 * 3600 * 1000L;
DateFormat DF = SimpleDateFormat.getTimeInstance();
DB db = DB.getInstance(context);
for (EntityLog log : db.log().getLogs(from))
sb.append(DF.format(log.time)).append(" ").append(log.data).append("\r\n");
sb.append("\r\n");
sb.append(Helper.getLogcat());
String body = "<pre>" + sb.toString().replaceAll("\\r?\\n", "<br />") + "</pre>";
EntityMessage draft;
DB db = DB.getInstance(context);
try {
db.beginTransaction();


+ 4
- 1
app/src/main/java/eu/faircode/email/FragmentLogs.java View File

@ -26,6 +26,7 @@ import android.view.ViewGroup;
import android.widget.ProgressBar;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import androidx.annotation.NonNull;
@ -74,8 +75,10 @@ public class FragmentLogs extends FragmentEx {
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
long from = new Date().getTime() - 24 * 3600 * 1000L;
DB db = DB.getInstance(getContext());
db.log().liveLogs().observe(getViewLifecycleOwner(), new Observer<List<EntityLog>>() {
db.log().liveLogs(from).observe(getViewLifecycleOwner(), new Observer<List<EntityLog>>() {
@Override
public void onChanged(List<EntityLog> logs) {
if (logs == null)


+ 26
- 17
app/src/main/java/eu/faircode/email/ServiceSynchronize.java View File

@ -358,7 +358,17 @@ public class ServiceSynchronize extends LifecycleService {
// MailConnectException
// - on connectity problems when connecting to store
EntityLog.log(this, ex.toString());
String action;
if (TextUtils.isEmpty(account))
action = folder;
else if (TextUtils.isEmpty(folder))
action = account;
else
action = account + "/" + folder;
StackTraceElement[] ste = ex.getStackTrace();
EntityLog.log(this, action + "\n" + ex.toString() +
(ste != null && ste.length > 0 ? "\n" + ste[0].toString() : ""));
if (!(ex instanceof MailConnectException) &&
!(ex instanceof FolderClosedException) &&
@ -369,27 +379,20 @@ public class ServiceSynchronize extends LifecycleService {
!(ex instanceof MessagingException && ex.getCause() instanceof SocketException) &&
!(ex instanceof MessagingException && ex.getCause() instanceof SocketTimeoutException) &&
!(ex instanceof MessagingException && ex.getCause() instanceof SSLException)) {
String action;
if (TextUtils.isEmpty(account))
action = folder;
else if (TextUtils.isEmpty(folder))
action = account;
else
action = account + "/" + folder;
NotificationManager nm = getSystemService(NotificationManager.class);
nm.notify(action, 1, getNotificationError(action, ex).build());
}
}
private void monitorAccount(final EntityAccount account, final ServiceState state) throws NoSuchProviderException {
Log.i(Helper.TAG, account.name + " start");
final DB db = DB.getInstance(this);
final ExecutorService executor = Executors.newSingleThreadExecutor();
int backoff = CONNECT_BACKOFF_START;
while (state.running) {
Log.i(Helper.TAG, account.name + " run");
EntityLog.log(this, account.name + " run");
// Debug
boolean debug = PreferenceManager.getDefaultSharedPreferences(this).getBoolean("debug", false);
System.setProperty("mail.socket.debug", Boolean.toString(debug));
@ -472,6 +475,8 @@ public class ServiceSynchronize extends LifecycleService {
db.account().setAccountState(account.id, "connected");
db.account().setAccountError(account.id, null);
EntityLog.log(this, account.name + " connected");
// Update folder list
synchronizeFolders(account, istore, state);
@ -764,6 +769,7 @@ public class ServiceSynchronize extends LifecycleService {
db.account().setAccountState(account.id, "closing");
for (EntityFolder folder : folders.keySet())
db.folder().setFolderState(folder.id, "closing");
EntityLog.log(this, account.name + " closing");
try {
// This can take some time
istore.close();
@ -771,6 +777,7 @@ public class ServiceSynchronize extends LifecycleService {
Log.w(Helper.TAG, account.name + " " + ex + "\n" + Log.getStackTraceString(ex));
} finally {
Log.i(Helper.TAG, account.name + " closed");
EntityLog.log(this, account.name + " closed");
db.account().setAccountState(account.id, null);
for (EntityFolder folder : folders.keySet())
db.folder().setFolderState(folder.id, null);
@ -791,7 +798,8 @@ public class ServiceSynchronize extends LifecycleService {
if (state.running) {
try {
Log.i(Helper.TAG, "Backoff seconds=" + backoff);
Log.i(Helper.TAG, account.name + " backoff=" + backoff);
EntityLog.log(this, account.name + " backoff=" + backoff);
Thread.sleep(backoff * 1000L);
if (backoff < CONNECT_BACKOFF_MAX)
@ -803,6 +811,7 @@ public class ServiceSynchronize extends LifecycleService {
}
Log.i(Helper.TAG, account.name + " stopped");
EntityLog.log(this, account.name + " stopped");
}
private void processOperations(EntityFolder folder, Session isession, IMAPStore istore, IMAPFolder ifolder) throws MessagingException, JSONException, IOException {
@ -1482,7 +1491,7 @@ public class ServiceSynchronize extends LifecycleService {
}
private void start() {
EntityLog.log(ServiceSynchronize.this, "Start");
EntityLog.log(ServiceSynchronize.this, "Main start");
state = new ServiceState();
main = new Thread(new Runnable() {
@ -1538,7 +1547,7 @@ public class ServiceSynchronize extends LifecycleService {
threads.add(t);
}
EntityLog.log(ServiceSynchronize.this, "Started");
EntityLog.log(ServiceSynchronize.this, "Main started");
// Stop monitoring accounts
for (Thread t : threads)
@ -1550,7 +1559,7 @@ public class ServiceSynchronize extends LifecycleService {
Log.i(Helper.TAG, outbox.name + " unlisten operations");
db.folder().setFolderState(outbox.id, null);
EntityLog.log(ServiceSynchronize.this, "Exited");
EntityLog.log(ServiceSynchronize.this, "Main exited");
} catch (Throwable ex) {
// Fail-safe
Log.e(Helper.TAG, ex + "\n" + Log.getStackTraceString(ex));
@ -1563,7 +1572,7 @@ public class ServiceSynchronize extends LifecycleService {
private void stop(boolean disconnected) {
if (main != null) {
EntityLog.log(ServiceSynchronize.this, "Stop disconnected=" + disconnected);
EntityLog.log(ServiceSynchronize.this, "Main stop disconnected=" + disconnected);
synchronized (state) {
state.running = false;
state.disconnected = disconnected;
@ -1576,7 +1585,7 @@ public class ServiceSynchronize extends LifecycleService {
main = null;
EntityLog.log(ServiceSynchronize.this, "Stopped");
EntityLog.log(ServiceSynchronize.this, "Main stopped");
}
}


Loading…
Cancel
Save