From f2113c1dcfa42b8ab8271ac0ea69c62698acdb61 Mon Sep 17 00:00:00 2001 From: Marc Blank Date: Tue, 18 Aug 2009 11:43:42 -0700 Subject: [PATCH] Make sure we log exceptions in FileLogger --- .../android/exchange/AbstractSyncService.java | 6 ++++ src/com/android/exchange/EasSyncService.java | 3 +- .../exchange/adapter/AbstractSyncParser.java | 2 +- .../android/exchange/utility/FileLogger.java | 35 +++++++++++-------- 4 files changed, 29 insertions(+), 17 deletions(-) diff --git a/src/com/android/exchange/AbstractSyncService.java b/src/com/android/exchange/AbstractSyncService.java index d06793842..59dc58b16 100644 --- a/src/com/android/exchange/AbstractSyncService.java +++ b/src/com/android/exchange/AbstractSyncService.java @@ -203,6 +203,12 @@ public abstract class AbstractSyncService implements Runnable { } } + public void userLog(Exception e) { + if (Eas.FILE_LOG) { + FileLogger.log(e); + } + } + /** * Standard logging for EAS. * If user logging is active, we concatenate any arguments and log them using Log.d diff --git a/src/com/android/exchange/EasSyncService.java b/src/com/android/exchange/EasSyncService.java index 2afc036c1..b22d2c81c 100644 --- a/src/com/android/exchange/EasSyncService.java +++ b/src/com/android/exchange/EasSyncService.java @@ -1025,7 +1025,8 @@ public class EasSyncService extends AbstractSyncService { userLog("Caught IOException: ", ((message == null) ? "" : message)); mExitStatus = EXIT_IO_ERROR; } catch (Exception e) { - Log.e(TAG, "Uncaught exception in EasSyncService", e); + Log.e(TAG, "Uncaught exception in EasSyncService" + e); + userLog(e); } finally { if (!mStop) { userLog(mMailbox.mDisplayName, ": sync finished"); diff --git a/src/com/android/exchange/adapter/AbstractSyncParser.java b/src/com/android/exchange/adapter/AbstractSyncParser.java index 45c23e429..2a323cf8d 100644 --- a/src/com/android/exchange/adapter/AbstractSyncParser.java +++ b/src/com/android/exchange/adapter/AbstractSyncParser.java @@ -105,7 +105,7 @@ public abstract class AbstractSyncParser extends Parser { // Make this a push box through the first sync // TODO Make frequency conditional on user settings! mMailbox.mSyncInterval = Mailbox.CHECK_INTERVAL_PUSH; - mService.errorLog("Bad sync key; RESET and delete contacts"); + mService.errorLog("Bad sync key; RESET and delete data"); wipe(); // Indicate there's more so that we'll start syncing again moreAvailable = true; diff --git a/src/com/android/exchange/utility/FileLogger.java b/src/com/android/exchange/utility/FileLogger.java index 9c305d2d5..c5f463546 100644 --- a/src/com/android/exchange/utility/FileLogger.java +++ b/src/com/android/exchange/utility/FileLogger.java @@ -20,13 +20,13 @@ import android.content.Context; import java.io.FileWriter; import java.io.IOException; +import java.io.PrintWriter; import java.util.Date; public class FileLogger { private static FileLogger LOGGER = null; private static FileWriter mLogWriter = null; public static String LOG_FILE_NAME = "/sdcard/emaillog.txt"; - private static Object mLock = new Object(); public synchronized static FileLogger getLogger (Context c) { LOGGER = new FileLogger(); @@ -34,12 +34,10 @@ public class FileLogger { } private FileLogger() { - synchronized (mLock) { - try { - mLogWriter = new FileWriter(LOG_FILE_NAME, true); - } catch (IOException e) { - // Doesn't matter - } + try { + mLogWriter = new FileWriter(LOG_FILE_NAME, true); + } catch (IOException e) { + // Doesn't matter } } @@ -54,6 +52,15 @@ public class FileLogger { } } + static public synchronized void log(Exception e) { + if (mLogWriter != null) { + log("Exception", "Stack trace follows..."); + PrintWriter pw = new PrintWriter(mLogWriter); + e.printStackTrace(pw); + pw.flush(); + } + } + @SuppressWarnings("deprecation") static public synchronized void log(String prefix, String str) { if (LOGGER == null) { @@ -87,14 +94,12 @@ public class FileLogger { sb.append("\r\n"); String s = sb.toString(); - synchronized (mLock) { - if (mLogWriter != null) { - try { - mLogWriter.write(s); - mLogWriter.flush(); - } catch (IOException e) { - // Doesn't matter - } + if (mLogWriter != null) { + try { + mLogWriter.write(s); + mLogWriter.flush(); + } catch (IOException e) { + // Doesn't matter } } }