Added log types

pull/204/head
M66B 3 years ago
parent f6ab6ced81
commit b8bddfedbf

File diff suppressed because it is too large Load Diff

@ -24,6 +24,8 @@ import static androidx.core.app.NotificationCompat.DEFAULT_LIGHTS;
import static androidx.core.app.NotificationCompat.DEFAULT_SOUND; import static androidx.core.app.NotificationCompat.DEFAULT_SOUND;
import static javax.mail.Folder.READ_WRITE; import static javax.mail.Folder.READ_WRITE;
import static eu.faircode.email.EntityLog.LOG_STATS;
import android.app.AlarmManager; import android.app.AlarmManager;
import android.app.Notification; import android.app.Notification;
import android.app.NotificationChannel; import android.app.NotificationChannel;
@ -1480,7 +1482,8 @@ class Core {
} }
if (!stats.isEmpty()) if (!stats.isEmpty())
EntityLog.log(context, account.name + "/" + folder.name + " fetch stats " + stats); EntityLog.log(context, LOG_STATS,
account.name + "/" + folder.name + " fetch stats " + stats);
} catch (MessageRemovedException | MessageRemovedIOException ex) { } catch (MessageRemovedException | MessageRemovedIOException ex) {
Log.i(ex); Log.i(ex);
@ -3206,7 +3209,8 @@ class Core {
stats.total = (SystemClock.elapsedRealtime() - search); stats.total = (SystemClock.elapsedRealtime() - search);
EntityLog.log(context, account.name + "/" + folder.name + " sync stats " + stats); EntityLog.log(context, LOG_STATS,
account.name + "/" + folder.name + " sync stats " + stats);
} finally { } finally {
Log.i(folder.name + " end sync state=" + state); Log.i(folder.name + " end sync state=" + state);
db.folder().setFolderSyncState(folder.id, null); db.folder().setFolderSyncState(folder.id, null);

@ -67,7 +67,7 @@ import io.requery.android.database.sqlite.SQLiteDatabase;
// https://developer.android.com/topic/libraries/architecture/room.html // https://developer.android.com/topic/libraries/architecture/room.html
@Database( @Database(
version = 207, version = 208,
entities = { entities = {
EntityIdentity.class, EntityIdentity.class,
EntityAccount.class, EntityAccount.class,
@ -2100,6 +2100,12 @@ public abstract class DB extends RoomDatabase {
db.execSQL("DROP VIEW `account_view`"); db.execSQL("DROP VIEW `account_view`");
db.execSQL("CREATE VIEW IF NOT EXISTS `account_view` AS " + TupleAccountView.query); db.execSQL("CREATE VIEW IF NOT EXISTS `account_view` AS " + TupleAccountView.query);
} }
}).addMigrations(new Migration(207, 208) {
@Override
public void migrate(@NonNull SupportSQLiteDatabase db) {
Log.i("DB migration from version " + startVersion + " to " + endVersion);
db.execSQL("ALTER TABLE `log` ADD COLUMN `type` INTEGER NOT NULL DEFAULT " + EntityLog.LOG_GENERAL);
}
}).addMigrations(new Migration(998, 999) { }).addMigrations(new Migration(998, 999) {
@Override @Override
public void migrate(@NonNull SupportSQLiteDatabase db) { public void migrate(@NonNull SupportSQLiteDatabase db) {

@ -30,14 +30,16 @@ import java.util.List;
public interface DaoLog { public interface DaoLog {
@Query("SELECT * FROM log" + @Query("SELECT * FROM log" +
" WHERE time > :from" + " WHERE time > :from" +
" AND (:type IS NULL OR type = :type)" +
" ORDER BY time DESC" + " ORDER BY time DESC" +
" LIMIT 2000") " LIMIT 2000")
LiveData<List<EntityLog>> liveLogs(long from); LiveData<List<EntityLog>> liveLogs(long from, Integer type);
@Query("SELECT * FROM log" + @Query("SELECT * FROM log" +
" WHERE time > :from" + " WHERE time > :from" +
" AND (:type IS NULL OR type = :type)" +
" ORDER BY time DESC") " ORDER BY time DESC")
List<EntityLog> getLogs(long from); List<EntityLog> getLogs(long from, Integer type);
@Insert @Insert
long insertLog(EntityLog log); long insertLog(EntityLog log);

@ -55,12 +55,24 @@ public class EntityLog {
@NonNull @NonNull
public Long time; public Long time;
@NonNull @NonNull
public Integer type = LOG_GENERAL;
@NonNull
public String data; public String data;
static final int LOG_GENERAL = 0;
static final int LOG_STATS = 1;
static final int LOG_SCHEDULE = 2;
static final int LOG_NETWORK = 3;
static final int LOG_ACCOUNT = 4;
private static final ExecutorService executor = private static final ExecutorService executor =
Helper.getBackgroundExecutor(1, "log"); Helper.getBackgroundExecutor(1, "log");
static void log(final Context context, String data) { static void log(final Context context, String data) {
log(context, LOG_GENERAL, data);
}
static void log(final Context context, int type, String data) {
Log.i(data); Log.i(data);
if (context == null) if (context == null)
@ -73,6 +85,7 @@ public class EntityLog {
final EntityLog entry = new EntityLog(); final EntityLog entry = new EntityLog();
entry.time = new Date().getTime(); entry.time = new Date().getTime();
entry.type = type;
entry.data = data; entry.data = data;
final DB db = DB.getInstance(context); final DB db = DB.getInstance(context);

@ -84,7 +84,7 @@ public class FragmentLogs extends FragmentBase {
long from = new Date().getTime() - 24 * 3600 * 1000L; long from = new Date().getTime() - 24 * 3600 * 1000L;
DB db = DB.getInstance(getContext()); DB db = DB.getInstance(getContext());
db.log().liveLogs(from).observe(getViewLifecycleOwner(), new Observer<List<EntityLog>>() { db.log().liveLogs(from, null).observe(getViewLifecycleOwner(), new Observer<List<EntityLog>>() {
@Override @Override
public void onChanged(List<EntityLog> logs) { public void onChanged(List<EntityLog> logs) {
if (logs == null) if (logs == null)

@ -2099,7 +2099,7 @@ public class Log {
long from = new Date().getTime() - 24 * 3600 * 1000L; long from = new Date().getTime() - 24 * 3600 * 1000L;
DateFormat TF = Helper.getTimeInstance(context); DateFormat TF = Helper.getTimeInstance(context);
for (EntityLog entry : db.log().getLogs(from)) for (EntityLog entry : db.log().getLogs(from, null))
size += write(os, String.format("%s %s\r\n", TF.format(entry.time), entry.data)); size += write(os, String.format("%s %s\r\n", TF.format(entry.time), entry.data));
} }

@ -21,6 +21,10 @@ package eu.faircode.email;
import static android.os.Process.THREAD_PRIORITY_BACKGROUND; import static android.os.Process.THREAD_PRIORITY_BACKGROUND;
import static eu.faircode.email.EntityLog.LOG_ACCOUNT;
import static eu.faircode.email.EntityLog.LOG_NETWORK;
import static eu.faircode.email.EntityLog.LOG_SCHEDULE;
import android.app.AlarmManager; import android.app.AlarmManager;
import android.app.NotificationManager; import android.app.NotificationManager;
import android.app.PendingIntent; import android.app.PendingIntent;
@ -273,17 +277,18 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
int index = accountStates.indexOf(current); int index = accountStates.indexOf(current);
if (index < 0) { if (index < 0) {
if (current.canRun()) { if (current.canRun()) {
EntityLog.log(ServiceSynchronize.this, "### new " + current + EntityLog.log(ServiceSynchronize.this, LOG_SCHEDULE,
" force=" + force + "### new " + current +
" start=" + current.canRun() + " force=" + force +
" sync=" + current.accountState.isEnabled(current.enabled) + " start=" + current.canRun() +
" enabled=" + current.accountState.synchronize + " sync=" + current.accountState.isEnabled(current.enabled) +
" ondemand=" + current.accountState.ondemand + " enabled=" + current.accountState.synchronize +
" folders=" + current.accountState.folders + " ondemand=" + current.accountState.ondemand +
" ops=" + current.accountState.operations + " folders=" + current.accountState.folders +
" tbd=" + current.accountState.tbd + " ops=" + current.accountState.operations +
" state=" + current.accountState.state + " tbd=" + current.accountState.tbd +
" active=" + current.networkState.getActive()); " state=" + current.accountState.state +
" active=" + current.networkState.getActive());
event = true; event = true;
start(current, current.accountState.isEnabled(current.enabled) || sync, force); start(current, current.accountState.isEnabled(current.enabled) || sync, force);
} }
@ -308,22 +313,23 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
prev.canRun() != current.canRun() || prev.canRun() != current.canRun() ||
!prev.accountState.equals(current.accountState)) { !prev.accountState.equals(current.accountState)) {
if (prev.canRun() || current.canRun()) if (prev.canRun() || current.canRun())
EntityLog.log(ServiceSynchronize.this, "### changed " + current + EntityLog.log(ServiceSynchronize.this, LOG_SCHEDULE,
" reload=" + reload + "### changed " + current +
" force=" + force + " reload=" + reload +
" stop=" + prev.canRun() + " force=" + force +
" start=" + current.canRun() + " stop=" + prev.canRun() +
" sync=" + sync + " start=" + current.canRun() +
" enabled=" + current.accountState.isEnabled(current.enabled) + " sync=" + sync +
" should=" + current.accountState.shouldRun(current.enabled) + " enabled=" + current.accountState.isEnabled(current.enabled) +
" changed=" + !prev.accountState.equals(current.accountState) + " should=" + current.accountState.shouldRun(current.enabled) +
" synchronize=" + current.accountState.synchronize + " changed=" + !prev.accountState.equals(current.accountState) +
" ondemand=" + current.accountState.ondemand + " synchronize=" + current.accountState.synchronize +
" folders=" + current.accountState.folders + " ondemand=" + current.accountState.ondemand +
" ops=" + current.accountState.operations + " folders=" + current.accountState.folders +
" tbd=" + current.accountState.tbd + " ops=" + current.accountState.operations +
" state=" + current.accountState.state + " tbd=" + current.accountState.tbd +
" active=" + prev.networkState.getActive() + "/" + current.networkState.getActive()); " state=" + current.accountState.state +
" active=" + prev.networkState.getActive() + "/" + current.networkState.getActive());
if (prev.canRun()) { if (prev.canRun()) {
event = true; event = true;
stop(prev); stop(prev);
@ -336,8 +342,9 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
if (state != null) { if (state != null) {
Network p = prev.networkState.getActive(); Network p = prev.networkState.getActive();
if (p != null && !p.equals(current.networkState.getActive())) { if (p != null && !p.equals(current.networkState.getActive())) {
EntityLog.log(ServiceSynchronize.this, "### changed " + current + EntityLog.log(ServiceSynchronize.this, LOG_SCHEDULE,
" active=" + prev.networkState.getActive() + "/" + current.networkState.getActive()); "### changed " + current +
" active=" + prev.networkState.getActive() + "/" + current.networkState.getActive());
state.error(new OperationCanceledException("Active network changed")); state.error(new OperationCanceledException("Active network changed"));
} }
} }
@ -354,7 +361,8 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
if (event) { if (event) {
lastEventId++; lastEventId++;
EntityLog.log(ServiceSynchronize.this, "### eventId=" + lastEventId); EntityLog.log(ServiceSynchronize.this, LOG_SCHEDULE,
"### eventId=" + lastEventId);
} }
if (lastAccounts != accounts || lastOperations != operations) { if (lastAccounts != accounts || lastOperations != operations) {
@ -394,9 +402,10 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
} }
if (!runService && lastQuitId != lastEventId) { if (!runService && lastQuitId != lastEventId) {
EntityLog.log(ServiceSynchronize.this, "### quitting" + EntityLog.log(ServiceSynchronize.this, LOG_SCHEDULE,
" run=" + runService + "### quitting" +
" startId=" + lastQuitId + "/" + lastEventId); " run=" + runService +
" startId=" + lastQuitId + "/" + lastEventId);
lastQuitId = lastEventId; lastQuitId = lastEventId;
quit(lastEventId); quit(lastEventId);
} }
@ -407,7 +416,8 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
queue.submit(new Runnable() { queue.submit(new Runnable() {
@Override @Override
public void run() { public void run() {
EntityLog.log(ServiceSynchronize.this, "### init " + accountNetworkState); EntityLog.log(ServiceSynchronize.this, LOG_SCHEDULE,
"### init " + accountNetworkState);
DB db = DB.getInstance(ServiceSynchronize.this); DB db = DB.getInstance(ServiceSynchronize.this);
try { try {
@ -435,7 +445,7 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
} }
private void start(final TupleAccountNetworkState accountNetworkState, boolean sync, boolean force) { private void start(final TupleAccountNetworkState accountNetworkState, boolean sync, boolean force) {
EntityLog.log(ServiceSynchronize.this, EntityLog.log(ServiceSynchronize.this, LOG_SCHEDULE,
"Service start=" + accountNetworkState + " sync=" + sync + " force=" + force); "Service start=" + accountNetworkState + " sync=" + sync + " force=" + force);
final Core.State astate = new Core.State(accountNetworkState.networkState); final Core.State astate = new Core.State(accountNetworkState.networkState);
@ -466,7 +476,8 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
Log.i("### start=" + accountNetworkState + " sync=" + sync); Log.i("### start=" + accountNetworkState + " sync=" + sync);
astate.start(); astate.start();
EntityLog.log(ServiceSynchronize.this, "### started=" + accountNetworkState); EntityLog.log(ServiceSynchronize.this, LOG_SCHEDULE,
"### started=" + accountNetworkState);
} catch (Throwable ex) { } catch (Throwable ex) {
Log.e(ex); Log.e(ex);
} }
@ -480,7 +491,8 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
return; return;
coreStates.remove(accountNetworkState.accountState.id); coreStates.remove(accountNetworkState.accountState.id);
EntityLog.log(ServiceSynchronize.this, "Service stop=" + accountNetworkState); EntityLog.log(ServiceSynchronize.this, LOG_SCHEDULE,
"Service stop=" + accountNetworkState);
queue.submit(new Runnable() { queue.submit(new Runnable() {
@Override @Override
@ -499,7 +511,8 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
db.account().setAccountThread(accountNetworkState.accountState.id, null); db.account().setAccountThread(accountNetworkState.accountState.id, null);
state.stop(); state.stop();
state.join(); state.join();
EntityLog.log(ServiceSynchronize.this, "### stopped=" + accountNetworkState); EntityLog.log(ServiceSynchronize.this, LOG_SCHEDULE,
"### stopped=" + accountNetworkState);
} catch (Throwable ex) { } catch (Throwable ex) {
Log.e(ex); Log.e(ex);
} }
@ -508,7 +521,8 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
} }
private void delete(final TupleAccountNetworkState accountNetworkState) { private void delete(final TupleAccountNetworkState accountNetworkState) {
EntityLog.log(ServiceSynchronize.this, "Service delete=" + accountNetworkState); EntityLog.log(ServiceSynchronize.this, LOG_SCHEDULE,
"Service delete=" + accountNetworkState);
queue.submit(new Runnable() { queue.submit(new Runnable() {
@Override @Override
@ -533,7 +547,8 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
@Override @Override
public void run() { public void run() {
try { try {
EntityLog.log(ServiceSynchronize.this, "### quit eventId=" + eventId); EntityLog.log(ServiceSynchronize.this, LOG_SCHEDULE,
"### quit eventId=" + eventId);
if (eventId == null) { if (eventId == null) {
// Service destroy // Service destroy
@ -554,14 +569,16 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
} }
if (!eventId.equals(lastEventId)) { if (!eventId.equals(lastEventId)) {
EntityLog.log(ServiceSynchronize.this, "### quit cancelled eventId=" + eventId + "/" + lastEventId); EntityLog.log(ServiceSynchronize.this, LOG_SCHEDULE,
"### quit cancelled eventId=" + eventId + "/" + lastEventId);
return; return;
} }
} }
// Stop service // Stop service
stopSelf(); stopSelf();
EntityLog.log(ServiceSynchronize.this, "### stop self eventId=" + eventId); EntityLog.log(ServiceSynchronize.this, LOG_SCHEDULE,
"### stop self eventId=" + eventId);
WorkerCleanup.cleanupConditionally(ServiceSynchronize.this); WorkerCleanup.cleanupConditionally(ServiceSynchronize.this);
} }
@ -799,8 +816,9 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
public int onStartCommand(Intent intent, int flags, int startId) { public int onStartCommand(Intent intent, int flags, int startId) {
String action = (intent == null ? null : intent.getAction()); String action = (intent == null ? null : intent.getAction());
String reason = (intent == null ? null : intent.getStringExtra("reason")); String reason = (intent == null ? null : intent.getStringExtra("reason"));
EntityLog.log(ServiceSynchronize.this, "### Service command " + intent + EntityLog.log(ServiceSynchronize.this, LOG_SCHEDULE,
" action=" + action + " reason=" + reason); "### Service command " + intent +
" action=" + action + " reason=" + reason);
Log.logExtras(intent); Log.logExtras(intent);
super.onStartCommand(intent, flags, startId); super.onStartCommand(intent, flags, startId);
@ -899,11 +917,14 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
Core.State state = coreStates.get(account); Core.State state = coreStates.get(account);
if (state == null) if (state == null)
EntityLog.log(this, "### wakeup missing account=" + account); EntityLog.log(this, LOG_SCHEDULE,
"### wakeup missing account=" + account);
else { else {
EntityLog.log(this, "### waking up account=" + account); EntityLog.log(this, LOG_SCHEDULE,
"### waking up account=" + account);
if (!state.release()) if (!state.release())
EntityLog.log(this, "### waking up failed account=" + account); EntityLog.log(this, LOG_SCHEDULE,
"### waking up failed account=" + account);
} }
} }
@ -940,18 +961,19 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
EntityOperation.queue(ServiceSynchronize.this, message, EntityOperation.SEND); EntityOperation.queue(ServiceSynchronize.this, message, EntityOperation.SEND);
} }
} else { } else {
EntityLog.log(ServiceSynchronize.this, folder.name + " Unsnooze" + EntityLog.log(ServiceSynchronize.this, LOG_SCHEDULE,
" id=" + message.id + folder.name + " Unsnooze" +
" ui_seen=" + message.ui_seen + "" + " id=" + message.id +
" ui_ignored=" + message.ui_ignored + " ui_seen=" + message.ui_seen + "" +
" ui_hide=" + message.ui_hide + " ui_ignored=" + message.ui_ignored +
" notifying=" + message.notifying + " ui_hide=" + message.ui_hide +
" silent=" + message.ui_silent + " notifying=" + message.notifying +
" received=" + new Date(message.received) + " silent=" + message.ui_silent +
" sent=" + (message.sent == null ? null : new Date(message.sent)) + " received=" + new Date(message.received) +
" created=" + (account.created == null ? null : new Date(account.created)) + " sent=" + (message.sent == null ? null : new Date(message.sent)) +
" notify=" + folder.notify + " created=" + (account.created == null ? null : new Date(account.created)) +
" sync=" + account.synchronize); " notify=" + folder.notify +
" sync=" + account.synchronize);
if (folder.notify) { if (folder.notify) {
List<EntityAttachment> attachments = db.attachment().getAttachments(id); List<EntityAttachment> attachments = db.attachment().getAttachments(id);
@ -1109,7 +1131,7 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
} }
private void onWatchdog(Intent intent) { private void onWatchdog(Intent intent) {
EntityLog.log(this, "Watchdog"); EntityLog.log(this, LOG_SCHEDULE, "Watchdog");
schedule(this, false); schedule(this, false);
if (lastNetworkState == null || !lastNetworkState.isSuitable()) if (lastNetworkState == null || !lastNetworkState.isSuitable())
@ -1259,7 +1281,8 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
if (TextUtils.isEmpty(message)) if (TextUtils.isEmpty(message))
message = "?"; message = "?";
if (e.getMessageType() == StoreEvent.NOTICE) { if (e.getMessageType() == StoreEvent.NOTICE) {
EntityLog.log(ServiceSynchronize.this, account.name + " notice: " + message); EntityLog.log(ServiceSynchronize.this, LOG_ACCOUNT,
account.name + " notice: " + message);
if ("Still here".equals(message) && if ("Still here".equals(message) &&
!account.isTransient(ServiceSynchronize.this)) { !account.isTransient(ServiceSynchronize.this)) {
@ -1277,7 +1300,8 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
try { try {
wlFolder.acquire(); wlFolder.acquire();
EntityLog.log(ServiceSynchronize.this, account.name + " alert: " + message); EntityLog.log(ServiceSynchronize.this, LOG_ACCOUNT,
account.name + " alert: " + message);
if (!ConnectionHelper.isMaxConnections(message)) if (!ConnectionHelper.isMaxConnections(message))
try { try {
@ -1298,7 +1322,8 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
List<Thread> idlers = new ArrayList<>(); List<Thread> idlers = new ArrayList<>();
try { try {
// Initiate connection // Initiate connection
EntityLog.log(this, account.name + " connecting"); EntityLog.log(this, LOG_ACCOUNT,
account.name + " connecting");
db.folder().setFolderStates(account.id, null); db.folder().setFolderStates(account.id, null);
db.account().setAccountState(account.id, "connecting"); db.account().setAccountState(account.id, "connecting");
@ -1353,10 +1378,12 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
Store istore = iservice.getStore(); Store istore = iservice.getStore();
if (istore instanceof IMAPStore) { if (istore instanceof IMAPStore) {
Map<String, String> caps = ((IMAPStore) istore).getCapabilities(); Map<String, String> caps = ((IMAPStore) istore).getCapabilities();
EntityLog.log(this, account.name + " connected" + EntityLog.log(this, LOG_ACCOUNT,
" caps=" + (caps == null ? null : TextUtils.join(" ", caps.keySet()))); account.name + " connected" +
" caps=" + (caps == null ? null : TextUtils.join(" ", caps.keySet())));
} else } else
EntityLog.log(this, account.name + " connected"); EntityLog.log(this, LOG_ACCOUNT,
account.name + " connected");
db.account().setAccountMaxSize(account.id, iservice.getMaxSize()); db.account().setAccountMaxSize(account.id, iservice.getMaxSize());
if (istore instanceof IMAPStore) if (istore instanceof IMAPStore)
@ -1523,8 +1550,7 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
Thread.sleep(FETCH_YIELD_DURATION); Thread.sleep(FETCH_YIELD_DURATION);
} catch (Throwable ex) { } catch (Throwable ex) {
Log.e(folder.name, ex); Log.e(folder.name, ex);
EntityLog.log( EntityLog.log(ServiceSynchronize.this,
ServiceSynchronize.this,
folder.name + " added " + Log.formatThrowable(ex, false)); folder.name + " added " + Log.formatThrowable(ex, false));
state.error(ex); state.error(ex);
} finally { } finally {
@ -1554,8 +1580,7 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
Thread.sleep(FETCH_YIELD_DURATION); Thread.sleep(FETCH_YIELD_DURATION);
} catch (Throwable ex) { } catch (Throwable ex) {
Log.e(folder.name, ex); Log.e(folder.name, ex);
EntityLog.log( EntityLog.log(ServiceSynchronize.this,
ServiceSynchronize.this,
folder.name + " removed " + Log.formatThrowable(ex, false)); folder.name + " removed " + Log.formatThrowable(ex, false));
state.error(ex); state.error(ex);
} finally { } finally {
@ -1580,8 +1605,7 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
Thread.sleep(FETCH_YIELD_DURATION); Thread.sleep(FETCH_YIELD_DURATION);
} catch (Throwable ex) { } catch (Throwable ex) {
Log.e(folder.name, ex); Log.e(folder.name, ex);
EntityLog.log( EntityLog.log(ServiceSynchronize.this,
ServiceSynchronize.this,
folder.name + " changed " + Log.formatThrowable(ex, false)); folder.name + " changed " + Log.formatThrowable(ex, false));
state.error(ex); state.error(ex);
} finally { } finally {
@ -1603,8 +1627,7 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
} }
} catch (Throwable ex) { } catch (Throwable ex) {
Log.e(folder.name, ex); Log.e(folder.name, ex);
EntityLog.log( EntityLog.log(ServiceSynchronize.this, LOG_ACCOUNT,
ServiceSynchronize.this,
folder.name + " idle " + Log.formatThrowable(ex, false)); folder.name + " idle " + Log.formatThrowable(ex, false));
state.error(new FolderClosedException(ifolder, "IDLE", new Exception(ex))); state.error(new FolderClosedException(ifolder, "IDLE", new Exception(ex)));
} finally { } finally {
@ -1847,8 +1870,7 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
} catch (Throwable ex) { } catch (Throwable ex) {
Log.e(folder.name, ex); Log.e(folder.name, ex);
EntityLog.log( EntityLog.log(ServiceSynchronize.this, LOG_ACCOUNT,
ServiceSynchronize.this,
folder.name + " process " + Log.formatThrowable(ex, false)); folder.name + " process " + Log.formatThrowable(ex, false));
db.folder().setFolderError(folder.id, Log.formatThrowable(ex)); db.folder().setFolderError(folder.id, Log.formatThrowable(ex));
if (!(ex instanceof FolderNotFoundException)) if (!(ex instanceof FolderNotFoundException))
@ -1887,9 +1909,10 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
!account.keep_alive_ok && account.poll_interval > 9 && !account.keep_alive_ok && account.poll_interval > 9 &&
Math.abs(idleTime - account.poll_interval * 60 * 1000L) < 60 * 1000L); Math.abs(idleTime - account.poll_interval * 60 * 1000L) < 60 * 1000L);
if (tune_keep_alive && !first && !account.keep_alive_ok) if (tune_keep_alive && !first && !account.keep_alive_ok)
EntityLog.log(this, account.name + EntityLog.log(this, LOG_ACCOUNT,
" Tune interval=" + account.poll_interval + account.name +
" idle=" + idleTime + "/" + tune); " Tune interval=" + account.poll_interval +
" idle=" + idleTime + "/" + tune);
try { try {
if (!state.isRecoverable()) { if (!state.isRecoverable()) {
Throwable unrecoverable = state.getUnrecoverable(); Throwable unrecoverable = state.getUnrecoverable();
@ -1902,9 +1925,10 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
// Sends store NOOP // Sends store NOOP
if (EmailService.SEPARATE_STORE_CONNECTION) { if (EmailService.SEPARATE_STORE_CONNECTION) {
EntityLog.log(this, account.name + " checking store" + EntityLog.log(this, LOG_ACCOUNT,
" memory=" + Log.getFreeMemMb() + account.name + " checking store" +
" battery=" + Helper.getBatteryLevel(this)); " memory=" + Log.getFreeMemMb() +
" battery=" + Helper.getBatteryLevel(this));
if (!iservice.getStore().isConnected()) if (!iservice.getStore().isConnected())
throw new StoreClosedException(iservice.getStore(), "NOOP"); throw new StoreClosedException(iservice.getStore(), "NOOP");
} }
@ -1917,7 +1941,8 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
} }
if (sync) { if (sync) {
EntityLog.log(this, account.name + " checking folders"); EntityLog.log(this, LOG_ACCOUNT,
account.name + " checking folders");
for (EntityFolder folder : mapFolders.keySet()) for (EntityFolder folder : mapFolders.keySet())
if (folder.selectable && folder.synchronize) if (folder.selectable && folder.synchronize)
if (!folder.poll && capIdle) { if (!folder.poll && capIdle) {
@ -1981,7 +2006,8 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
// Record successful connection // Record successful connection
account.last_connected = new Date().getTime(); account.last_connected = new Date().getTime();
EntityLog.log(this, account.name + " set last_connected=" + new Date(account.last_connected)); EntityLog.log(this, LOG_ACCOUNT,
account.name + " set last_connected=" + new Date(account.last_connected));
db.account().setAccountConnected(account.id, account.last_connected); db.account().setAccountConnected(account.id, account.last_connected);
db.account().setAccountWarning(account.id, capIdle ? null : getString(R.string.title_no_idle)); db.account().setAccountWarning(account.id, capIdle ? null : getString(R.string.title_no_idle));
@ -2000,8 +2026,9 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
try { try {
long duration = account.poll_interval * 60 * 1000L; long duration = account.poll_interval * 60 * 1000L;
long trigger = System.currentTimeMillis() + duration; long trigger = System.currentTimeMillis() + duration;
EntityLog.log(this, "### " + account.name + " keep alive" + EntityLog.log(this, LOG_ACCOUNT,
" wait=" + account.poll_interval + " until=" + new Date(trigger)); "### " + account.name + " keep alive" +
" wait=" + account.poll_interval + " until=" + new Date(trigger));
AlarmManagerCompatEx.setAndAllowWhileIdle(ServiceSynchronize.this, am, AlarmManager.RTC_WAKEUP, trigger, pi); AlarmManagerCompatEx.setAndAllowWhileIdle(ServiceSynchronize.this, am, AlarmManager.RTC_WAKEUP, trigger, pi);
try { try {
@ -2024,13 +2051,14 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
} catch (Throwable ex) { } catch (Throwable ex) {
last_fail = ex; last_fail = ex;
Log.e(account.name, ex); Log.e(account.name, ex);
EntityLog.log(this, EntityLog.log(this, LOG_ACCOUNT,
account.name + " connect " + Log.formatThrowable(ex, false)); account.name + " connect " + Log.formatThrowable(ex, false));
db.account().setAccountError(account.id, Log.formatThrowable(ex)); db.account().setAccountError(account.id, Log.formatThrowable(ex));
// Report account connection error // Report account connection error
if (account.last_connected != null && !ConnectionHelper.airplaneMode(this)) { if (account.last_connected != null && !ConnectionHelper.airplaneMode(this)) {
EntityLog.log(this, account.name + " last connected: " + new Date(account.last_connected)); EntityLog.log(this, LOG_ACCOUNT,
account.name + " last connected: " + new Date(account.last_connected));
int pollInterval = getPollInterval(this); int pollInterval = getPollInterval(this);
long now = new Date().getTime(); long now = new Date().getTime();
@ -2057,7 +2085,8 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
} }
} finally { } finally {
// Update state // Update state
EntityLog.log(this, account.name + " closing"); EntityLog.log(this, LOG_ACCOUNT,
account.name + " closing");
// Stop watching operations // Stop watching operations
Log.i(account.name + " stop watching operations"); Log.i(account.name + " stop watching operations");
@ -2084,13 +2113,16 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
for (EntityFolder folder : mapFolders.keySet()) for (EntityFolder folder : mapFolders.keySet())
if (folder.selectable && folder.synchronize && !folder.poll && mapFolders.get(folder) != null) if (folder.selectable && folder.synchronize && !folder.poll && mapFolders.get(folder) != null)
db.folder().setFolderState(folder.id, "closing"); db.folder().setFolderState(folder.id, "closing");
EntityLog.log(this, account.name + " store closing"); EntityLog.log(this, LOG_ACCOUNT,
account.name + " store closing");
iservice.close(); iservice.close();
EntityLog.log(this, account.name + " store closed"); EntityLog.log(this, LOG_ACCOUNT,
account.name + " store closed");
} catch (Throwable ex) { } catch (Throwable ex) {
Log.w(account.name, ex); Log.w(account.name, ex);
} finally { } finally {
EntityLog.log(this, account.name + " closed"); EntityLog.log(this, LOG_ACCOUNT,
account.name + " closed");
db.account().setAccountState(account.id, null); db.account().setAccountState(account.id, null);
for (EntityFolder folder : mapFolders.keySet()) for (EntityFolder folder : mapFolders.keySet())
db.folder().setFolderState(folder.id, null); db.folder().setFolderState(folder.id, null);
@ -2140,7 +2172,7 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
" ex=" + Log.formatThrowable(last_fail, false); " ex=" + Log.formatThrowable(last_fail, false);
if (compensate > 2) if (compensate > 2)
Log.e(msg); Log.e(msg);
EntityLog.log(this, msg); EntityLog.log(this, LOG_ACCOUNT, msg);
state.setBackoff(backoff * 60); state.setBackoff(backoff * 60);
} }
@ -2154,7 +2186,8 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
int backoff = state.getBackoff(); int backoff = state.getBackoff();
int recently = (lastLost + LOST_RECENTLY < now ? 1 : 2); int recently = (lastLost + LOST_RECENTLY < now ? 1 : 2);
EntityLog.log(this, account.name + " backoff=" + backoff + " recently=" + recently + "x"); EntityLog.log(this, LOG_ACCOUNT,
account.name + " backoff=" + backoff + " recently=" + recently + "x");
if (backoff < CONNECT_BACKOFF_MAX) if (backoff < CONNECT_BACKOFF_MAX)
state.setBackoff(backoff * 2); state.setBackoff(backoff * 2);
@ -2205,7 +2238,8 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE); AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
try { try {
long trigger = System.currentTimeMillis() + backoff * 1000L; long trigger = System.currentTimeMillis() + backoff * 1000L;
EntityLog.log(this, "### " + account.name + " backoff until=" + new Date(trigger)); EntityLog.log(this, LOG_ACCOUNT,
"### " + account.name + " backoff until=" + new Date(trigger));
AlarmManagerCompatEx.setAndAllowWhileIdle(ServiceSynchronize.this, am, AlarmManager.RTC_WAKEUP, trigger, pi); AlarmManagerCompatEx.setAndAllowWhileIdle(ServiceSynchronize.this, am, AlarmManager.RTC_WAKEUP, trigger, pi);
try { try {
@ -2231,7 +2265,8 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
if (!currentThread.equals(accountThread) && accountThread != null) if (!currentThread.equals(accountThread) && accountThread != null)
Log.w(account.name + " orphan thread id=" + currentThread + "/" + accountThread); Log.w(account.name + " orphan thread id=" + currentThread + "/" + accountThread);
} finally { } finally {
EntityLog.log(this, account.name + " stopped"); EntityLog.log(this, LOG_ACCOUNT,
account.name + " stopped");
wlAccount.release(); wlAccount.release();
} }
} }
@ -2276,9 +2311,10 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
DB db = DB.getInstance(this); DB db = DB.getInstance(this);
int pollInterval = getPollInterval(this); int pollInterval = getPollInterval(this);
EntityLog.log(this, account.name + " auto optimize" + EntityLog.log(this, LOG_ACCOUNT,
" reason=" + reason + account.name + " auto optimize" +
" poll interval=" + pollInterval); " reason=" + reason +
" poll interval=" + pollInterval);
if (pollInterval == 0) { if (pollInterval == 0) {
try { try {
db.beginTransaction(); db.beginTransaction();
@ -2315,7 +2351,8 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
@Override @Override
public void onBlockedStatusChanged(@NonNull Network network, boolean blocked) { public void onBlockedStatusChanged(@NonNull Network network, boolean blocked) {
EntityLog.log(ServiceSynchronize.this, "Network " + network + " blocked=" + blocked); EntityLog.log(ServiceSynchronize.this, LOG_NETWORK,
"Network " + network + " blocked=" + blocked);
} }
@Override @Override
@ -2332,7 +2369,8 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
if (Intent.ACTION_AIRPLANE_MODE_CHANGED.equals(intent.getAction())) { if (Intent.ACTION_AIRPLANE_MODE_CHANGED.equals(intent.getAction())) {
boolean on = intent.getBooleanExtra("state", false); boolean on = intent.getBooleanExtra("state", false);
EntityLog.log(context, "Airplane mode on=" + on); EntityLog.log(context, LOG_NETWORK,
"Airplane mode on=" + on);
if (!on) if (!on)
lastLost = 0; lastLost = 0;
} }
@ -2375,13 +2413,13 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
if (active != null && !active.equals(lastActive)) { if (active != null && !active.equals(lastActive)) {
if (ConnectionHelper.isConnected(ServiceSynchronize.this, active)) { if (ConnectionHelper.isConnected(ServiceSynchronize.this, active)) {
EntityLog.log(ServiceSynchronize.this, EntityLog.log(ServiceSynchronize.this, LOG_NETWORK,
reason + ": new active network=" + active + "/" + lastActive); reason + ": new active network=" + active + "/" + lastActive);
lastActive = active; lastActive = active;
} }
} else if (lastActive != null) { } else if (lastActive != null) {
if (!ConnectionHelper.isConnected(ServiceSynchronize.this, lastActive)) { if (!ConnectionHelper.isConnected(ServiceSynchronize.this, lastActive)) {
EntityLog.log(ServiceSynchronize.this, EntityLog.log(ServiceSynchronize.this, LOG_NETWORK,
reason + ": lost active network=" + lastActive); reason + ": lost active network=" + lastActive);
lastActive = null; lastActive = null;
lastLost = new Date().getTime(); lastLost = new Date().getTime();
@ -2391,7 +2429,7 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
if (network == null || Objects.equals(network, active)) { if (network == null || Objects.equals(network, active)) {
ConnectionHelper.NetworkState ns = ConnectionHelper.getNetworkState(ServiceSynchronize.this); ConnectionHelper.NetworkState ns = ConnectionHelper.getNetworkState(ServiceSynchronize.this);
if (!Objects.equals(lastNetworkState, ns)) { if (!Objects.equals(lastNetworkState, ns)) {
EntityLog.log(ServiceSynchronize.this, EntityLog.log(ServiceSynchronize.this, LOG_NETWORK,
reason + ": updating state network=" + active + reason + ": updating state network=" + active +
" info=" + ConnectionHelper.getNetworkInfo(ServiceSynchronize.this, active) + " " + ns); " info=" + ConnectionHelper.getNetworkInfo(ServiceSynchronize.this, active) + " " + ns);
lastNetworkState = ns; lastNetworkState = ns;
@ -2402,7 +2440,8 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
boolean isSuitable = (lastNetworkState != null && lastNetworkState.isSuitable()); boolean isSuitable = (lastNetworkState != null && lastNetworkState.isSuitable());
if (lastSuitable == null || lastSuitable != isSuitable) { if (lastSuitable == null || lastSuitable != isSuitable) {
lastSuitable = isSuitable; lastSuitable = isSuitable;
EntityLog.log(ServiceSynchronize.this, reason + ": updated suitable=" + lastSuitable); EntityLog.log(ServiceSynchronize.this, LOG_NETWORK,
reason + ": updated suitable=" + lastSuitable);
if (!isBackgroundService(ServiceSynchronize.this)) if (!isBackgroundService(ServiceSynchronize.this))
try { try {
@ -2428,8 +2467,8 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
private List<TupleAccountState> lastAccountStates = null; private List<TupleAccountState> lastAccountStates = null;
private void post(Bundle command) { private void post(Bundle command) {
EntityLog.log(ServiceSynchronize.this, "### command " + EntityLog.log(ServiceSynchronize.this, LOG_SCHEDULE,
TextUtils.join(" ", Log.getExtras(command))); "### command " + TextUtils.join(" ", Log.getExtras(command)));
if (command.getBoolean("sync") || command.getBoolean("force")) if (command.getBoolean("sync") || command.getBoolean("force"))
lastNetworkState = ConnectionHelper.getNetworkState(ServiceSynchronize.this); lastNetworkState = ConnectionHelper.getNetworkState(ServiceSynchronize.this);
@ -2464,7 +2503,7 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
networkState = ConnectionHelper.getNetworkState(ServiceSynchronize.this); networkState = ConnectionHelper.getNetworkState(ServiceSynchronize.this);
if (accountStates == null) { if (accountStates == null) {
EntityLog.log(ServiceSynchronize.this, "### no accounts"); EntityLog.log(ServiceSynchronize.this, LOG_SCHEDULE, "### no accounts");
lastCommand = command; lastCommand = command;
return; return;
} }
@ -2589,7 +2628,8 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
if (polled && next < now + interval / 5) if (polled && next < now + interval / 5)
next += interval; next += interval;
EntityLog.log(context, "Poll next=" + new Date(next) + " polled=" + polled); EntityLog.log(context, LOG_SCHEDULE,
"Poll next=" + new Date(next) + " polled=" + polled);
AlarmManagerCompatEx.setAndAllowWhileIdle(context, am, AlarmManager.RTC_WAKEUP, next, piSync); AlarmManagerCompatEx.setAndAllowWhileIdle(context, am, AlarmManager.RTC_WAKEUP, next, piSync);
} }

Loading…
Cancel
Save