diff options
author | 2013-01-31 09:59:16 -0800 | |
---|---|---|
committer | 2013-02-05 15:18:20 -0800 | |
commit | b02a19c49d29cd7758d01349ebbfeb479a56bb37 (patch) | |
tree | f4acfd7b33d048a883b949f9f06ea3dc7b1d968f /java/src/com/android/inputmethod/research/LogStatement.java | |
parent | df1b8d5eec1909a7ff000825e20e1e45d6f39297 (diff) | |
download | latinime-b02a19c49d29cd7758d01349ebbfeb479a56bb37.tar.gz latinime-b02a19c49d29cd7758d01349ebbfeb479a56bb37.tar.xz latinime-b02a19c49d29cd7758d01349ebbfeb479a56bb37.zip |
Refactor LogStatement publishing method
The method in LogUnit for publishing a LogStatement to a JsonWriter doesn't
depend on anything in the LogUnit.
multi-project commit with Id1d6ff4851148bba0e6b5a1ec6eec2b842d9c707
Change-Id: I323cec239d6ea1cee602c2ecf9b13713791e9283
Diffstat (limited to 'java/src/com/android/inputmethod/research/LogStatement.java')
-rw-r--r-- | java/src/com/android/inputmethod/research/LogStatement.java | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/java/src/com/android/inputmethod/research/LogStatement.java b/java/src/com/android/inputmethod/research/LogStatement.java index 1d83e1a86..059146ae6 100644 --- a/java/src/com/android/inputmethod/research/LogStatement.java +++ b/java/src/com/android/inputmethod/research/LogStatement.java @@ -16,6 +16,18 @@ package com.android.inputmethod.research; +import android.content.SharedPreferences; +import android.util.JsonWriter; +import android.util.Log; +import android.view.MotionEvent; +import android.view.inputmethod.CompletionInfo; + +import com.android.inputmethod.keyboard.Key; +import com.android.inputmethod.latin.SuggestedWords; +import com.android.inputmethod.latin.define.ProductionFlag; + +import java.io.IOException; + /** * A template for typed information stored in the logs. * @@ -24,6 +36,9 @@ package com.android.inputmethod.research; * actual values are stored separately. */ class LogStatement { + private static final String TAG = LogStatement.class.getSimpleName(); + private static final boolean DEBUG = false && ProductionFlag.IS_EXPERIMENTAL_DEBUG; + // Constants for particular statements public static final String TYPE_POINTER_TRACKER_CALL_LISTENER_ON_CODE_INPUT = "PointerTrackerCallListenerOnCodeInput"; @@ -36,6 +51,11 @@ class LogStatement { public static final String TYPE_MOTION_EVENT = "MotionEvent"; public static final String KEY_IS_LOGGING_RELATED = "isLoggingRelated"; + // Keys for internal key/value pairs + private static final String CURRENT_TIME_KEY = "_ct"; + private static final String UPTIME_KEY = "_ut"; + private static final String EVENT_TYPE_KEY = "_ty"; + // Name specifying the LogStatement type. private final String mType; @@ -142,4 +162,61 @@ class LogStatement { } return false; } + + /** + * Write the contents out through jsonWriter. + * + * Note that this method is not thread safe for the same jsonWriter. Callers must ensure + * thread safety. + */ + public boolean outputToLocked(final JsonWriter jsonWriter, final Long time, + final Object... values) { + if (DEBUG) { + if (mKeys.length != values.length) { + Log.d(TAG, "Key and Value list sizes do not match. " + mType); + } + } + try { + jsonWriter.beginObject(); + jsonWriter.name(CURRENT_TIME_KEY).value(System.currentTimeMillis()); + jsonWriter.name(UPTIME_KEY).value(time); + jsonWriter.name(EVENT_TYPE_KEY).value(mType); + final int length = values.length; + for (int i = 0; i < length; i++) { + jsonWriter.name(mKeys[i]); + final Object value = values[i]; + if (value instanceof CharSequence) { + jsonWriter.value(value.toString()); + } else if (value instanceof Number) { + jsonWriter.value((Number) value); + } else if (value instanceof Boolean) { + jsonWriter.value((Boolean) value); + } else if (value instanceof CompletionInfo[]) { + JsonUtils.writeJson((CompletionInfo[]) value, jsonWriter); + } else if (value instanceof SharedPreferences) { + JsonUtils.writeJson((SharedPreferences) value, jsonWriter); + } else if (value instanceof Key[]) { + JsonUtils.writeJson((Key[]) value, jsonWriter); + } else if (value instanceof SuggestedWords) { + JsonUtils.writeJson((SuggestedWords) value, jsonWriter); + } else if (value instanceof MotionEvent) { + JsonUtils.writeJson((MotionEvent) value, jsonWriter); + } else if (value == null) { + jsonWriter.nullValue(); + } else { + if (DEBUG) { + Log.w(TAG, "Unrecognized type to be logged: " + + (value == null ? "<null>" : value.getClass().getName())); + } + jsonWriter.nullValue(); + } + } + jsonWriter.endObject(); + } catch (IOException e) { + e.printStackTrace(); + Log.w(TAG, "Error in JsonWriter; skipping LogStatement"); + return false; + } + return true; + } } |