Merge "[Rlog68] Logger; make filenames more unique to prevent filesys conflicts"

main
Kurt Partridge 2013-01-14 10:02:16 -08:00 committed by Android (Google) Code Review
commit 019bcc62b9
2 changed files with 24 additions and 7 deletions

View File

@ -16,6 +16,7 @@
package com.android.inputmethod.research; package com.android.inputmethod.research;
import android.content.Context;
import android.util.JsonWriter; import android.util.JsonWriter;
import android.util.Log; import android.util.Log;
@ -23,7 +24,7 @@ import com.android.inputmethod.latin.define.ProductionFlag;
import java.io.BufferedWriter; import java.io.BufferedWriter;
import java.io.File; import java.io.File;
import java.io.FileWriter; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
import java.io.OutputStreamWriter; import java.io.OutputStreamWriter;
@ -50,6 +51,8 @@ public class ResearchLog {
/* package */ final ScheduledExecutorService mExecutor; /* package */ final ScheduledExecutorService mExecutor;
/* package */ final File mFile; /* package */ final File mFile;
private final Context mContext;
private JsonWriter mJsonWriter = NULL_JSON_WRITER; 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 // 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 // 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) { if (outputFile == null) {
throw new IllegalArgumentException(); throw new IllegalArgumentException();
} }
mExecutor = Executors.newSingleThreadScheduledExecutor(); mExecutor = Executors.newSingleThreadScheduledExecutor();
mFile = outputFile; mFile = outputFile;
mContext = context;
} }
public synchronized void close(final Runnable onClosed) { public synchronized void close(final Runnable onClosed) {
@ -206,7 +210,9 @@ public class ResearchLog {
public JsonWriter getValidJsonWriterLocked() { public JsonWriter getValidJsonWriterLocked() {
try { try {
if (mJsonWriter == NULL_JSON_WRITER) { 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(); mJsonWriter.beginArray();
mHasWrittenData = true; mHasWrittenData = true;
} }

View File

@ -324,11 +324,22 @@ public class ResearchLogger implements SharedPreferences.OnSharedPreferenceChang
sIsLogging = enableLogging; sIsLogging = enableLogging;
} }
private static int sLogFileCounter = 0;
private File createLogFile(File filesDir) { private File createLogFile(File filesDir) {
final StringBuilder sb = new StringBuilder(); final StringBuilder sb = new StringBuilder();
sb.append(FILENAME_PREFIX).append('-'); sb.append(FILENAME_PREFIX).append('-');
sb.append(mUUIDString).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); sb.append(FILENAME_SUFFIX);
return new File(filesDir, sb.toString()); return new File(filesDir, sb.toString());
} }
@ -374,12 +385,12 @@ public class ResearchLogger implements SharedPreferences.OnSharedPreferenceChang
return; return;
} }
if (mMainLogBuffer == null) { if (mMainLogBuffer == null) {
mMainResearchLog = new ResearchLog(createLogFile(mFilesDir)); mMainResearchLog = new ResearchLog(createLogFile(mFilesDir), mLatinIME);
mMainLogBuffer = new MainLogBuffer(mMainResearchLog); mMainLogBuffer = new MainLogBuffer(mMainResearchLog);
mMainLogBuffer.setSuggest(mSuggest); mMainLogBuffer.setSuggest(mSuggest);
} }
if (mFeedbackLogBuffer == null) { 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 // LogBuffer is one more than FEEDBACK_WORD_BUFFER_SIZE, because it must also hold
// the feedback LogUnit itself. // the feedback LogUnit itself.
mFeedbackLogBuffer = new FixedLogBuffer(FEEDBACK_WORD_BUFFER_SIZE + 1); mFeedbackLogBuffer = new FixedLogBuffer(FEEDBACK_WORD_BUFFER_SIZE + 1);
@ -599,7 +610,7 @@ public class ResearchLogger implements SharedPreferences.OnSharedPreferenceChang
uploadNow(); uploadNow();
} }
}); });
mFeedbackLog = new ResearchLog(createLogFile(mFilesDir)); mFeedbackLog = new ResearchLog(createLogFile(mFilesDir), mLatinIME);
} }
public void uploadNow() { public void uploadNow() {