aboutsummaryrefslogtreecommitdiffstats
path: root/ddms
diff options
context:
space:
mode:
authorSiva Velusamy <vsiva@google.com>2011-08-15 15:09:16 -0700
committerSiva Velusamy <vsiva@google.com>2011-08-15 17:40:24 -0700
commitc377e904d50b9ebd91ad2729888bd451062f1b22 (patch)
tree4246400bccda08be0942129ce9832fb811c66275 /ddms
parent12690b66d734385854e0c9dab384c8e509489b2f (diff)
downloadsdk-c377e904d50b9ebd91ad2729888bd451062f1b22.zip
sdk-c377e904d50b9ebd91ad2729888bd451062f1b22.tar.gz
sdk-c377e904d50b9ebd91ad2729888bd451062f1b22.tar.bz2
Save/Restore user created filters.
Change-Id: I4e5ba3d6c16889b0a1ac3195e616f0e53026f947
Diffstat (limited to 'ddms')
-rw-r--r--ddms/libs/ddmuilib/src/com/android/ddmuilib/logcat/LogCatFilterSettingsSerializer.java193
-rw-r--r--ddms/libs/ddmuilib/src/com/android/ddmuilib/logcat/LogCatPanel.java24
-rw-r--r--ddms/libs/ddmuilib/tests/src/com/android/ddmuilib/logcat/LogCatFilterSettingsSerializerTest.java48
3 files changed, 262 insertions, 3 deletions
diff --git a/ddms/libs/ddmuilib/src/com/android/ddmuilib/logcat/LogCatFilterSettingsSerializer.java b/ddms/libs/ddmuilib/src/com/android/ddmuilib/logcat/LogCatFilterSettingsSerializer.java
new file mode 100644
index 0000000..d1061e5
--- /dev/null
+++ b/ddms/libs/ddmuilib/src/com/android/ddmuilib/logcat/LogCatFilterSettingsSerializer.java
@@ -0,0 +1,193 @@
+/*
+ * Copyright (C) 2011 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.android.ddmuilib.logcat;
+
+import com.android.ddmlib.Log.LogLevel;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * Class to help save/restore user created filters.
+ *
+ * Users can create multiple filters in the logcat view. These filters could have regexes
+ * in their settings. All of the user created filters are saved into a single Eclipse
+ * preference. This class helps in generating the string to be saved given a list of
+ * {@link LogCatFilterSettings}, and also does the reverse of creating the list of filter
+ * settings given the encoded string.
+ */
+public final class LogCatFilterSettingsSerializer {
+ private static final char SINGLE_QUOTE = '\'';
+ private static final char ESCAPE_CHAR = '\\';
+
+ private static final String ATTR_DELIM = ", ";
+ private static final String KW_DELIM = ": ";
+
+ private static final String KW_NAME = "name";
+ private static final String KW_TAG = "tag";
+ private static final String KW_TEXT = "text";
+ private static final String KW_PID = "pid";
+ private static final String KW_LOGLEVEL = "level";
+
+ /**
+ * Encode a list of {@link LogCatFilterSettings} into a string for saving to preference store.
+ * See {@link LogCatFilterSettingsSerializer#decodeFromPreferenceString(String)} for the
+ * reverse operation.
+ * @param filterSettings list of settings to save.
+ * @return an encoded string that can be saved in Eclipse preference store. The encoded string
+ * is of a list of key:'value' pairs.
+ */
+ public String encodeToPreferenceString(List<LogCatFilterSettings> filterSettings) {
+ StringBuffer sb = new StringBuffer();
+
+ for (LogCatFilterSettings f : filterSettings) {
+ sb.append(KW_NAME); sb.append(KW_DELIM); sb.append(quoteString(f.getName()));
+ sb.append(ATTR_DELIM);
+ sb.append(KW_TAG); sb.append(KW_DELIM); sb.append(quoteString(f.getTag()));
+ sb.append(ATTR_DELIM);
+ sb.append(KW_TEXT); sb.append(KW_DELIM); sb.append(quoteString(f.getText()));
+ sb.append(ATTR_DELIM);
+ sb.append(KW_PID); sb.append(KW_DELIM); sb.append(quoteString(f.getPidString()));
+ sb.append(ATTR_DELIM);
+ sb.append(KW_LOGLEVEL); sb.append(KW_DELIM);
+ sb.append(quoteString(f.getLogLevel().getStringValue()));
+ sb.append(ATTR_DELIM);
+ }
+ return sb.toString();
+ }
+
+ /**
+ * Decode an encoded string into a list of {@link LogCatFilterSettings}.
+ * @param pref encoded preference string
+ * @return a list of {@link LogCatFilterSettings}
+ */
+ public List<LogCatFilterSettings> decodeFromPreferenceString(String pref) {
+ List<LogCatFilterSettings> fs = new ArrayList<LogCatFilterSettings>();
+
+ /* first split the string into a list of key, value pairs */
+ List<String> kv = getKeyValues(pref);
+ if (kv.size() == 0) {
+ return fs;
+ }
+
+ /* construct filter settings from the key value pairs */
+ int index = 0;
+ while (index < kv.size()) {
+ String name = "";
+ String tag = "";
+ String pid = "";
+ String text = "";
+ LogLevel level = LogLevel.VERBOSE;
+
+ assert kv.get(index) == KW_NAME;
+ name = kv.get(index + 1);
+
+ index += 2;
+ while (index < kv.size() && !kv.get(index).equals(KW_NAME)) {
+ String key = kv.get(index);
+ String value = kv.get(index + 1);
+ index += 2;
+
+ if (key.equals(KW_TAG)) {
+ tag = value;
+ } else if (key.equals(KW_TEXT)) {
+ text = value;
+ } else if (key.equals(KW_PID)) {
+ pid = value;
+ } else if (key.equals(KW_LOGLEVEL)) {
+ level = LogLevel.getByString(value);
+ }
+ }
+
+ fs.add(new LogCatFilterSettings(name, tag, text, pid, level));
+ }
+
+ return fs;
+ }
+
+ private List<String> getKeyValues(String pref) {
+ List<String> kv = new ArrayList<String>();
+ int index = 0;
+ while (index < pref.length()) {
+ String kw = getKeyword(pref.substring(index));
+ if (kw == null) {
+ break;
+ }
+ index += kw.length() + KW_DELIM.length();
+
+ String value = getNextString(pref.substring(index));
+ index += value.length() + ATTR_DELIM.length();
+
+ value = unquoteString(value);
+
+ kv.add(kw);
+ kv.add(value);
+ }
+
+ return kv;
+ }
+
+ /**
+ * Enclose a string in quotes, escaping all the quotes within the string.
+ */
+ private String quoteString(String s) {
+ return SINGLE_QUOTE + s.replace(new Character(SINGLE_QUOTE).toString(), "\\'")
+ + SINGLE_QUOTE;
+ }
+
+ /**
+ * Recover original string from its escaped version created using
+ * {@link LogCatFilterSettingsSerializer#quoteString(String)}.
+ */
+ private String unquoteString(String s) {
+ s = s.substring(1, s.length() - 1); /* remove start and end QUOTES */
+ return s.replace("\\'", new Character(SINGLE_QUOTE).toString());
+ }
+
+ private String getKeyword(String pref) {
+ int kwlen = pref.indexOf(KW_DELIM);
+ if (kwlen == -1) {
+ return null;
+ }
+
+ return pref.substring(0, kwlen);
+ }
+
+ /**
+ * Get the next quoted string from the input stream of characters.
+ */
+ private String getNextString(String s) {
+ assert s.charAt(0) == SINGLE_QUOTE;
+
+ StringBuffer sb = new StringBuffer();
+
+ int index = 0;
+ while (index < s.length()) {
+ sb.append(s.charAt(index));
+
+ if (index > 0
+ && s.charAt(index) == SINGLE_QUOTE // current char is a single quote
+ && s.charAt(index - 1) != ESCAPE_CHAR) { // prev char wasn't a backslash
+ /* break if an unescaped SINGLE QUOTE (end of string) is seen */
+ break;
+ }
+
+ index++;
+ }
+
+ return sb.toString();
+ }
+}
diff --git a/ddms/libs/ddmuilib/src/com/android/ddmuilib/logcat/LogCatPanel.java b/ddms/libs/ddmuilib/src/com/android/ddmuilib/logcat/LogCatPanel.java
index 9d417df..be2955a 100644
--- a/ddms/libs/ddmuilib/src/com/android/ddmuilib/logcat/LogCatPanel.java
+++ b/ddms/libs/ddmuilib/src/com/android/ddmuilib/logcat/LogCatPanel.java
@@ -126,10 +126,10 @@ public final class LogCatPanel extends SelectionDependentPanel
String pid = "";
mLogCatFilters.add(new LogCatFilterSettings("All messages (no filters)",
tag, text, pid, LogLevel.VERBOSE));
- mLogCatFilters.add(new LogCatFilterSettings("Errors only",
- tag, text, pid, LogLevel.ERROR));
- /* FIXME restore saved filters from prefStore */
+ /* restore saved filters from prefStore */
+ List<LogCatFilterSettings> savedFilters = getSavedFilterSettings();
+ mLogCatFilters.addAll(savedFilters);
}
private void initializeFonts(IPreferenceStore prefStore) {
@@ -145,6 +145,21 @@ public final class LogCatPanel extends SelectionDependentPanel
});
}
+ private void saveFilterPreferences() {
+ LogCatFilterSettingsSerializer serializer = new LogCatFilterSettingsSerializer();
+
+ /* save all filter settings except the first one which is the default */
+ String e = serializer.encodeToPreferenceString(
+ mLogCatFilters.subList(1, mLogCatFilters.size()));
+ mPrefStore.setValue("logcat.filters.list", e);
+ }
+
+ private List<LogCatFilterSettings> getSavedFilterSettings() {
+ LogCatFilterSettingsSerializer serializer = new LogCatFilterSettingsSerializer();
+ String e = mPrefStore.getString("logcat.filters.list");
+ return serializer.decodeFromPreferenceString(e);
+ }
+
@Override
public void deviceSelected() {
mReceiver.stop();
@@ -268,6 +283,7 @@ public final class LogCatPanel extends SelectionDependentPanel
mFiltersTableViewer.getTable().setSelection(idx);
filterSelectionChanged();
+ saveFilterPreferences();
}
private void deleteSelectedFilter() {
@@ -282,6 +298,7 @@ public final class LogCatPanel extends SelectionDependentPanel
mFiltersTableViewer.getTable().setSelection(selectedIndex - 1);
filterSelectionChanged();
+ saveFilterPreferences();
}
private void editSelectedFilter() {
@@ -310,6 +327,7 @@ public final class LogCatPanel extends SelectionDependentPanel
mFiltersTableViewer.getTable().setSelection(selectedIndex);
filterSelectionChanged();
+ saveFilterPreferences();
}
private void createFiltersTable(Composite parent) {
diff --git a/ddms/libs/ddmuilib/tests/src/com/android/ddmuilib/logcat/LogCatFilterSettingsSerializerTest.java b/ddms/libs/ddmuilib/tests/src/com/android/ddmuilib/logcat/LogCatFilterSettingsSerializerTest.java
new file mode 100644
index 0000000..067df4d
--- /dev/null
+++ b/ddms/libs/ddmuilib/tests/src/com/android/ddmuilib/logcat/LogCatFilterSettingsSerializerTest.java
@@ -0,0 +1,48 @@
+/*
+ * Copyright (C) 2011 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.android.ddmuilib.logcat;
+
+import com.android.ddmlib.Log.LogLevel;
+
+import java.util.Arrays;
+import java.util.List;
+
+import junit.framework.TestCase;
+
+public class LogCatFilterSettingsSerializerTest extends TestCase {
+ /* test that decode(encode(f)) = f */
+ public void testSerializer() {
+ LogCatFilterSettings fs = new LogCatFilterSettings(
+ "TestFilter", //$NON-NLS-1$
+ "Tag'.*Regex", //$NON-NLS-1$
+ "regexForTextField..''", //$NON-NLS-1$
+ "123", //$NON-NLS-1$
+ LogLevel.ERROR);
+
+ LogCatFilterSettingsSerializer serializer = new LogCatFilterSettingsSerializer();
+ String s = serializer.encodeToPreferenceString(Arrays.asList(fs));
+ List<LogCatFilterSettings> decodedFiltersList = serializer.decodeFromPreferenceString(s);
+
+ assertEquals(1, decodedFiltersList.size());
+
+ LogCatFilterSettings dfs = decodedFiltersList.get(0);
+ assertEquals(fs.getName(), dfs.getName());
+ assertEquals(fs.getTag(), dfs.getTag());
+ assertEquals(fs.getText(), dfs.getText());
+ assertEquals(fs.getPidString(), dfs.getPidString());
+ assertEquals(fs.getLogLevel(), dfs.getLogLevel());
+ }
+}