From 67a61fc6bea2c4bf4e3d5096f9664547ecb47a9a Mon Sep 17 00:00:00 2001 From: Kurt Partridge Date: Fri, 11 Jan 2013 10:35:51 -0800 Subject: [PATCH] [Rlog68] Logger; make filenames more unique to prevent filesys conflicts multi-project commit with If6b9f8990d7cdbf570740a81096513698d0e0f4c Change-Id: I124aed24be0beb4c472869594184ecf003c2fed4 --- .../inputmethod/research/ResearchLog.java | 12 +++++++++--- .../inputmethod/research/ResearchLogger.java | 19 +++++++++++++++---- 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/java/src/com/android/inputmethod/research/ResearchLog.java b/java/src/com/android/inputmethod/research/ResearchLog.java index a2356e6a3..5edb46e27 100644 --- a/java/src/com/android/inputmethod/research/ResearchLog.java +++ b/java/src/com/android/inputmethod/research/ResearchLog.java @@ -16,6 +16,7 @@ package com.android.inputmethod.research; +import android.content.Context; import android.util.JsonWriter; import android.util.Log; @@ -23,7 +24,7 @@ import com.android.inputmethod.latin.define.ProductionFlag; import java.io.BufferedWriter; import java.io.File; -import java.io.FileWriter; +import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.io.OutputStreamWriter; @@ -50,6 +51,8 @@ public class ResearchLog { /* package */ final ScheduledExecutorService mExecutor; /* package */ final File mFile; + private final Context mContext; + private JsonWriter mJsonWriter = NULL_JSON_WRITER; // true if at least one byte of data has been written out to the log file. This must be // remembered because JsonWriter requires that calls matching calls to beginObject and @@ -78,12 +81,13 @@ public class ResearchLog { } } - public ResearchLog(final File outputFile) { + public ResearchLog(final File outputFile, Context context) { if (outputFile == null) { throw new IllegalArgumentException(); } mExecutor = Executors.newSingleThreadScheduledExecutor(); mFile = outputFile; + mContext = context; } public synchronized void close(final Runnable onClosed) { @@ -206,7 +210,9 @@ public class ResearchLog { public JsonWriter getValidJsonWriterLocked() { try { if (mJsonWriter == NULL_JSON_WRITER) { - mJsonWriter = new JsonWriter(new BufferedWriter(new FileWriter(mFile))); + final FileOutputStream fos = + mContext.openFileOutput(mFile.getName(), Context.MODE_PRIVATE); + mJsonWriter = new JsonWriter(new BufferedWriter(new OutputStreamWriter(fos))); mJsonWriter.beginArray(); mHasWrittenData = true; } diff --git a/java/src/com/android/inputmethod/research/ResearchLogger.java b/java/src/com/android/inputmethod/research/ResearchLogger.java index f464facf4..f609a2710 100644 --- a/java/src/com/android/inputmethod/research/ResearchLogger.java +++ b/java/src/com/android/inputmethod/research/ResearchLogger.java @@ -324,11 +324,22 @@ public class ResearchLogger implements SharedPreferences.OnSharedPreferenceChang sIsLogging = enableLogging; } + private static int sLogFileCounter = 0; + private File createLogFile(File filesDir) { final StringBuilder sb = new StringBuilder(); sb.append(FILENAME_PREFIX).append('-'); sb.append(mUUIDString).append('-'); - sb.append(TIMESTAMP_DATEFORMAT.format(new Date())); + sb.append(TIMESTAMP_DATEFORMAT.format(new Date())).append('-'); + // Sometimes logFiles are created within milliseconds of each other. Append a counter to + // separate these. + if (sLogFileCounter < Integer.MAX_VALUE) { + sLogFileCounter++; + } else { + // Wrap the counter, in the unlikely event of overflow. + sLogFileCounter = 0; + } + sb.append(sLogFileCounter); sb.append(FILENAME_SUFFIX); return new File(filesDir, sb.toString()); } @@ -374,12 +385,12 @@ public class ResearchLogger implements SharedPreferences.OnSharedPreferenceChang return; } if (mMainLogBuffer == null) { - mMainResearchLog = new ResearchLog(createLogFile(mFilesDir)); + mMainResearchLog = new ResearchLog(createLogFile(mFilesDir), mLatinIME); mMainLogBuffer = new MainLogBuffer(mMainResearchLog); mMainLogBuffer.setSuggest(mSuggest); } if (mFeedbackLogBuffer == null) { - mFeedbackLog = new ResearchLog(createLogFile(mFilesDir)); + mFeedbackLog = new ResearchLog(createLogFile(mFilesDir), mLatinIME); // LogBuffer is one more than FEEDBACK_WORD_BUFFER_SIZE, because it must also hold // the feedback LogUnit itself. mFeedbackLogBuffer = new FixedLogBuffer(FEEDBACK_WORD_BUFFER_SIZE + 1); @@ -599,7 +610,7 @@ public class ResearchLogger implements SharedPreferences.OnSharedPreferenceChang uploadNow(); } }); - mFeedbackLog = new ResearchLog(createLogFile(mFilesDir)); + mFeedbackLog = new ResearchLog(createLogFile(mFilesDir), mLatinIME); } public void uploadNow() {