aboutsummaryrefslogtreecommitdiffstats
path: root/ddms
diff options
context:
space:
mode:
authorSiva Velusamy <vsiva@google.com>2011-08-15 17:31:35 -0700
committerSiva Velusamy <vsiva@google.com>2011-08-15 17:40:24 -0700
commit4d932c0e3fe3f0e3716ae9a32174fa6e4453aab8 (patch)
treeab16942db4657df4067abae08449bd571986b625 /ddms
parent3915987c9d9959e18fdcd3fa83d2db31cd14ccc5 (diff)
downloadsdk-4d932c0e3fe3f0e3716ae9a32174fa6e4453aab8.zip
sdk-4d932c0e3fe3f0e3716ae9a32174fa6e4453aab8.tar.gz
sdk-4d932c0e3fe3f0e3716ae9a32174fa6e4453aab8.tar.bz2
Refactor code for filtering into LogCatFilter class.
- Move the code out of LogCatViewerFilter into LogCatFilterSettings - Rename LogCatFilterSettings to LogCatFilter as it now does the filtering as well. - Update tests appropriately. Change-Id: I68840d8292cc7ad2b3ef5415bd7a0bdd56aa4da2
Diffstat (limited to 'ddms')
-rw-r--r--ddms/libs/ddmuilib/src/com/android/ddmuilib/logcat/LogCatFilter.java184
-rw-r--r--ddms/libs/ddmuilib/src/com/android/ddmuilib/logcat/LogCatFilterContentProvider.java4
-rw-r--r--ddms/libs/ddmuilib/src/com/android/ddmuilib/logcat/LogCatFilterLabelProvider.java8
-rw-r--r--ddms/libs/ddmuilib/src/com/android/ddmuilib/logcat/LogCatFilterSettings.java101
-rw-r--r--ddms/libs/ddmuilib/src/com/android/ddmuilib/logcat/LogCatFilterSettingsSerializer.java26
-rw-r--r--ddms/libs/ddmuilib/src/com/android/ddmuilib/logcat/LogCatPanel.java31
-rw-r--r--ddms/libs/ddmuilib/src/com/android/ddmuilib/logcat/LogCatViewerFilter.java78
-rw-r--r--ddms/libs/ddmuilib/tests/src/com/android/ddmuilib/logcat/LogCatFilterSettingsSerializerTest.java6
-rw-r--r--ddms/libs/ddmuilib/tests/src/com/android/ddmuilib/logcat/LogCatFilterSettingsTest.java74
-rw-r--r--ddms/libs/ddmuilib/tests/src/com/android/ddmuilib/logcat/LogCatFilterTest.java126
-rw-r--r--ddms/libs/ddmuilib/tests/src/com/android/ddmuilib/logcat/LogCatViewerFilterTest.java84
11 files changed, 357 insertions, 365 deletions
diff --git a/ddms/libs/ddmuilib/src/com/android/ddmuilib/logcat/LogCatFilter.java b/ddms/libs/ddmuilib/src/com/android/ddmuilib/logcat/LogCatFilter.java
new file mode 100644
index 0000000..4de950a
--- /dev/null
+++ b/ddms/libs/ddmuilib/src/com/android/ddmuilib/logcat/LogCatFilter.java
@@ -0,0 +1,184 @@
+/*
+ * 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;
+import com.android.ddmlib.Log.LogLevel;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+import java.util.regex.PatternSyntaxException;
+
+/**
+ * A Filter for logcat messages. A filter can be constructed to match
+ * different fields of a logcat message. It can then be queried to see if
+ * a message matches the filter's settings.
+ */
+public final class LogCatFilter {
+ private static final String PID_KEYWORD = "pid:"; //$NON-NLS-1$
+ private static final String TAG_KEYWORD = "tag:"; //$NON-NLS-1$
+ private static final String TEXT_KEYWORD = "text:"; //$NON-NLS-1$
+
+ private final String mName;
+ private final String mTag;
+ private final String mText;
+ private final String mPid;
+ private final LogLevel mLogLevel;
+
+ private boolean mCheckPID;
+ private boolean mCheckTag;
+ private boolean mCheckText;
+
+ private Pattern mTagPattern;
+ private Pattern mTextPattern;
+
+ /**
+ * Construct a filter with the provided restrictions for the logcat message.
+ * @param name name for the filter
+ * @param tag value for the logcat message's tag field. Can be a regex.
+ * Invalid regexes are ignored.
+ * @param text value for the logcat message's text field. Can be a regex.
+ * Invalid regexes are ignored.
+ * @param pid value for the logcat message's pid field.
+ * @param logLevel value for the logcat message's log level. Only messages of
+ * higher priority will be accepted by the filter.
+ */
+ public LogCatFilter(String name, String tag, String text, String pid,
+ LogLevel logLevel) {
+ mName = name.trim();
+ mTag = tag.trim();
+ mText = text.trim();
+ mPid = pid.trim();
+ mLogLevel = logLevel;
+
+ mCheckPID = mPid.length() != 0;
+
+ if (mTag.length() != 0) {
+ try {
+ mTagPattern = Pattern.compile(mTag);
+ mCheckTag = true;
+ } catch (PatternSyntaxException e) {
+ Log.e("LogCatFilter", "Ignoring invalid tag regex.");
+ Log.e("LogCatFilter", e.getMessage());
+ mCheckTag = false;
+ }
+ }
+
+ if (mText.length() != 0) {
+ try {
+ mTextPattern = Pattern.compile(mText);
+ mCheckText = true;
+ } catch (PatternSyntaxException e) {
+ Log.e("LogCatFilter", "Ignoring invalid text regex.");
+ Log.e("LogCatFilter", e.getMessage());
+ mCheckText = false;
+ }
+ }
+ }
+
+ /**
+ * Construct a list of {@link LogCatFilter} objects by decoding the query.
+ * @param query encoded search string. The query is simply a list of words (can be regexes)
+ * a user would type in a search bar. These words are searched for in the text field of
+ * each collected logcat message. To search in a different field, the word could be prefixed
+ * with a keyword corresponding to the field name. Currently, the following keywords are
+ * supported: "pid:", "tag:" and "text:". Invalid regexes are ignored.
+ * @param minLevel minimum log level to match
+ * @return list of filter settings that fully match the given query
+ */
+ public static List<LogCatFilter> fromString(String query, LogLevel minLevel) {
+ List<LogCatFilter> filterSettings = new ArrayList<LogCatFilter>();
+
+ for (String s : query.trim().split(" ")) {
+ String tag = "";
+ String text = "";
+ String pid = "";
+
+ if (s.startsWith(PID_KEYWORD)) {
+ pid = s.substring(PID_KEYWORD.length());
+ } else if (s.startsWith(TAG_KEYWORD)) {
+ tag = s.substring(TAG_KEYWORD.length());
+ } else {
+ if (s.startsWith(TEXT_KEYWORD)) {
+ text = s.substring(TEXT_KEYWORD.length());
+ } else {
+ text = s;
+ }
+ }
+ filterSettings.add(new LogCatFilter("livefilter-" + s,
+ tag, text, pid, minLevel));
+ }
+
+ return filterSettings;
+ }
+
+ public String getName() {
+ return mName;
+ }
+
+ public String getTag() {
+ return mTag;
+ }
+
+ public String getText() {
+ return mText;
+ }
+
+ public String getPidString() {
+ return mPid;
+ }
+
+ public LogLevel getLogLevel() {
+ return mLogLevel;
+ }
+
+ /**
+ * Check whether a given message will make it through this filter.
+ * @param m message to check
+ * @return true if the message matches the filter's conditions.
+ */
+ public boolean matches(LogCatMessage m) {
+ /* filter out messages of a lower priority */
+ if (m.getLogLevel().getPriority() < mLogLevel.getPriority()) {
+ return false;
+ }
+
+ /* if pid filter is enabled, filter out messages whose pid does not match
+ * the filter's pid */
+ if (mCheckPID && !m.getPidString().equals(mPid)) {
+ return false;
+ }
+
+ /* if tag filter is enabled, filter out messages not matching the tag */
+ if (mCheckTag) {
+ Matcher matcher = mTagPattern.matcher(m.getTag());
+ if (!matcher.find()) {
+ return false;
+ }
+ }
+
+ if (mCheckText) {
+ Matcher matcher = mTextPattern.matcher(m.getMessage());
+ if (!matcher.find()) {
+ return false;
+ }
+ }
+
+ return true;
+ }
+}
diff --git a/ddms/libs/ddmuilib/src/com/android/ddmuilib/logcat/LogCatFilterContentProvider.java b/ddms/libs/ddmuilib/src/com/android/ddmuilib/logcat/LogCatFilterContentProvider.java
index c55a308..2adbc4c 100644
--- a/ddms/libs/ddmuilib/src/com/android/ddmuilib/logcat/LogCatFilterContentProvider.java
+++ b/ddms/libs/ddmuilib/src/com/android/ddmuilib/logcat/LogCatFilterContentProvider.java
@@ -32,8 +32,8 @@ public final class LogCatFilterContentProvider implements IStructuredContentProv
/**
* Obtain the list of filters currently in use.
- * @param model list of {@link LogCatFilterSettings}'s
- * @return array of {@link LogCatFilterSettings} objects, or null.
+ * @param model list of {@link LogCatFilter}'s
+ * @return array of {@link LogCatFilter} objects, or null.
*/
public Object[] getElements(Object model) {
if (model instanceof List<?>) {
diff --git a/ddms/libs/ddmuilib/src/com/android/ddmuilib/logcat/LogCatFilterLabelProvider.java b/ddms/libs/ddmuilib/src/com/android/ddmuilib/logcat/LogCatFilterLabelProvider.java
index c5a1e68..3dbc050 100644
--- a/ddms/libs/ddmuilib/src/com/android/ddmuilib/logcat/LogCatFilterLabelProvider.java
+++ b/ddms/libs/ddmuilib/src/com/android/ddmuilib/logcat/LogCatFilterLabelProvider.java
@@ -21,7 +21,7 @@ import org.eclipse.swt.graphics.Image;
/**
* A JFace label provider for the LogCat filters. It expects elements of type
- * {@link LogCatFilterSettings}.
+ * {@link LogCatFilter}.
*/
public final class LogCatFilterLabelProvider extends LabelProvider implements ITableLabelProvider {
public Image getColumnImage(Object arg0, int arg1) {
@@ -30,16 +30,16 @@ public final class LogCatFilterLabelProvider extends LabelProvider implements IT
/**
* Implements {@link ITableLabelProvider#getColumnText(Object, int)}.
- * @param element an instance of {@link LogCatFilterSettings}
+ * @param element an instance of {@link LogCatFilter}
* @param index index of the column
* @return text to use in the column
*/
public String getColumnText(Object element, int index) {
- if (!(element instanceof LogCatFilterSettings)) {
+ if (!(element instanceof LogCatFilter)) {
return null;
}
- LogCatFilterSettings f = (LogCatFilterSettings) element;
+ LogCatFilter f = (LogCatFilter) element;
/* FIXME: Currently, only the name of the filter is displayed.
* A future fix will also display the "unread count" associated
diff --git a/ddms/libs/ddmuilib/src/com/android/ddmuilib/logcat/LogCatFilterSettings.java b/ddms/libs/ddmuilib/src/com/android/ddmuilib/logcat/LogCatFilterSettings.java
deleted file mode 100644
index 2b16897..0000000
--- a/ddms/libs/ddmuilib/src/com/android/ddmuilib/logcat/LogCatFilterSettings.java
+++ /dev/null
@@ -1,101 +0,0 @@
-/*
- * 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;
-
-/**
- * Settings for a filter for logcat messages.
- */
-public final class LogCatFilterSettings {
- private static final String PID_KEYWORD = "pid:"; //$NON-NLS-1$
- private static final String TAG_KEYWORD = "tag:"; //$NON-NLS-1$
- private static final String TEXT_KEYWORD = "text:"; //$NON-NLS-1$
-
- private final String mName;
- private final String mTag;
- private String mText;
- private final String mPid;
- private final LogLevel mLogLevel;
-
- public LogCatFilterSettings(String name, String tag, String text, String pid,
- LogLevel logLevel) {
- mName = name;
- mTag = tag;
- mText = text;
- mPid = pid;
- mLogLevel = logLevel;
- }
-
- /**
- * Construct a list of {@link LogCatFilterSettings} objects by decoding the query.
- * @param query encoded search string. The query is simply a list of words (can be regexes)
- * a user would type in a search bar. These words are searched for in the text field of
- * each collected logcat message. To search in a different field, the word could be prefixed
- * with a keyword corresponding to the field name. Currently, the following keywords are
- * supported: "pid:", "tag:" and "text:". Invalid regexes are ignored.
- * @param minLevel minimum log level to match
- * @return list of filter settings that fully match the given query
- */
- public static List<LogCatFilterSettings> fromString(String query, LogLevel minLevel) {
- List<LogCatFilterSettings> filterSettings = new ArrayList<LogCatFilterSettings>();
-
- for (String s : query.trim().split(" ")) {
- String tag = "";
- String text = "";
- String pid = "";
-
- if (s.startsWith(PID_KEYWORD)) {
- pid = s.substring(PID_KEYWORD.length());
- } else if (s.startsWith(TAG_KEYWORD)) {
- tag = s.substring(TAG_KEYWORD.length());
- } else {
- if (s.startsWith(TEXT_KEYWORD)) {
- text = s.substring(TEXT_KEYWORD.length());
- } else {
- text = s;
- }
- }
- filterSettings.add(new LogCatFilterSettings("livefilter-" + s,
- tag, text, pid, minLevel));
- }
-
- return filterSettings;
- }
-
- public String getName() {
- return mName;
- }
-
- public String getTag() {
- return mTag;
- }
-
- public String getText() {
- return mText;
- }
-
- public String getPidString() {
- return mPid;
- }
-
- public LogLevel getLogLevel() {
- return mLogLevel;
- }
-}
diff --git a/ddms/libs/ddmuilib/src/com/android/ddmuilib/logcat/LogCatFilterSettingsSerializer.java b/ddms/libs/ddmuilib/src/com/android/ddmuilib/logcat/LogCatFilterSettingsSerializer.java
index d1061e5..9d66f8e 100644
--- a/ddms/libs/ddmuilib/src/com/android/ddmuilib/logcat/LogCatFilterSettingsSerializer.java
+++ b/ddms/libs/ddmuilib/src/com/android/ddmuilib/logcat/LogCatFilterSettingsSerializer.java
@@ -26,8 +26,8 @@ import java.util.List;
* 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.
+ * {@link LogCatFilter}'s, and also does the reverse of creating the list of filters
+ * given the encoded string.
*/
public final class LogCatFilterSettingsSerializer {
private static final char SINGLE_QUOTE = '\'';
@@ -43,17 +43,18 @@ public final class LogCatFilterSettingsSerializer {
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
+ * Encode the settings from a list of {@link LogCatFilter}'s into a string for saving to
+ * the preference store. See
+ * {@link LogCatFilterSettingsSerializer#decodeFromPreferenceString(String)} for the
* reverse operation.
- * @param filterSettings list of settings to save.
+ * @param filters list of filters 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) {
+ public String encodeToPreferenceString(List<LogCatFilter> filters) {
StringBuffer sb = new StringBuffer();
- for (LogCatFilterSettings f : filterSettings) {
+ for (LogCatFilter f : filters) {
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()));
@@ -70,12 +71,13 @@ public final class LogCatFilterSettingsSerializer {
}
/**
- * Decode an encoded string into a list of {@link LogCatFilterSettings}.
+ * Decode an encoded string representing the settings of a list of logcat
+ * filters into a list of {@link LogCatFilter}'s.
* @param pref encoded preference string
- * @return a list of {@link LogCatFilterSettings}
+ * @return a list of {@link LogCatFilter}
*/
- public List<LogCatFilterSettings> decodeFromPreferenceString(String pref) {
- List<LogCatFilterSettings> fs = new ArrayList<LogCatFilterSettings>();
+ public List<LogCatFilter> decodeFromPreferenceString(String pref) {
+ List<LogCatFilter> fs = new ArrayList<LogCatFilter>();
/* first split the string into a list of key, value pairs */
List<String> kv = getKeyValues(pref);
@@ -112,7 +114,7 @@ public final class LogCatFilterSettingsSerializer {
}
}
- fs.add(new LogCatFilterSettings(name, tag, text, pid, level));
+ fs.add(new LogCatFilter(name, tag, text, pid, level));
}
return fs;
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 fa752b6..469cf57 100644
--- a/ddms/libs/ddmuilib/src/com/android/ddmuilib/logcat/LogCatPanel.java
+++ b/ddms/libs/ddmuilib/src/com/android/ddmuilib/logcat/LogCatPanel.java
@@ -61,8 +61,12 @@ import java.util.List;
*/
public final class LogCatPanel extends SelectionDependentPanel
implements ILogCatMessageEventListener {
+ /** Preference key to use for storing list of logcat filters. */
+ public static final String LOGCAT_FILTERS_LIST = "logcat.view.filters.list";
+
/** Preference key to use for storing font settings. */
public static final String LOGCAT_VIEW_FONT_PREFKEY = "logcat.view.font";
+
private static final String DEFAULT_LOGCAT_FONT = new FontData(
"Courier New", 10, SWT.NORMAL).toString();
@@ -87,7 +91,7 @@ public final class LogCatPanel extends SelectionDependentPanel
private LogCatReceiver mReceiver;
private IPreferenceStore mPrefStore;
- private List<LogCatFilterSettings> mLogCatFilters;
+ private List<LogCatFilter> mLogCatFilters;
private ToolItem mNewFilterToolItem;
private ToolItem mDeleteFilterToolItem;
@@ -118,17 +122,17 @@ public final class LogCatPanel extends SelectionDependentPanel
}
private void initializeFilters(IPreferenceStore prefStore) {
- mLogCatFilters = new ArrayList<LogCatFilterSettings>();
+ mLogCatFilters = new ArrayList<LogCatFilter>();
- /* add a couple of filters by default */
+ /* add default filter matching all messages */
String tag = "";
String text = "";
String pid = "";
- mLogCatFilters.add(new LogCatFilterSettings("All messages (no filters)",
+ mLogCatFilters.add(new LogCatFilter("All messages (no filters)",
tag, text, pid, LogLevel.VERBOSE));
/* restore saved filters from prefStore */
- List<LogCatFilterSettings> savedFilters = getSavedFilterSettings();
+ List<LogCatFilter> savedFilters = getSavedFilters();
mLogCatFilters.addAll(savedFilters);
}
@@ -151,12 +155,12 @@ public final class LogCatPanel extends SelectionDependentPanel
/* 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);
+ mPrefStore.setValue(LOGCAT_FILTERS_LIST, e);
}
- private List<LogCatFilterSettings> getSavedFilterSettings() {
+ private List<LogCatFilter> getSavedFilters() {
LogCatFilterSettingsSerializer serializer = new LogCatFilterSettingsSerializer();
- String e = mPrefStore.getString("logcat.filters.list");
+ String e = mPrefStore.getString(LOGCAT_FILTERS_LIST);
return serializer.decodeFromPreferenceString(e);
}
@@ -269,7 +273,7 @@ public final class LogCatPanel extends SelectionDependentPanel
return;
}
- LogCatFilterSettings f = new LogCatFilterSettings(d.getFilterName().trim(),
+ LogCatFilter f = new LogCatFilter(d.getFilterName().trim(),
d.getTag().trim(),
d.getText().trim(),
d.getPID().trim(),
@@ -307,7 +311,7 @@ public final class LogCatPanel extends SelectionDependentPanel
return;
}
- LogCatFilterSettings curFilter = mLogCatFilters.get(selectedIndex);
+ LogCatFilter curFilter = mLogCatFilters.get(selectedIndex);
LogCatFilterSettingsDialog dialog = new LogCatFilterSettingsDialog(
Display.getCurrent().getActiveShell());
@@ -317,7 +321,7 @@ public final class LogCatPanel extends SelectionDependentPanel
return;
}
- LogCatFilterSettings f = new LogCatFilterSettings(dialog.getFilterName(),
+ LogCatFilter f = new LogCatFilter(dialog.getFilterName(),
dialog.getTag(),
dialog.getText(),
dialog.getPID(),
@@ -595,10 +599,10 @@ public final class LogCatPanel extends SelectionDependentPanel
private List<LogCatViewerFilter> getCurrentLiveFilters() {
List<LogCatViewerFilter> liveFilters = new ArrayList<LogCatViewerFilter>();
- List<LogCatFilterSettings> liveFilterSettings = LogCatFilterSettings.fromString(
+ List<LogCatFilter> liveFilterSettings = LogCatFilter.fromString(
mLiveFilterText.getText(), /* current query */
LogLevel.getByString(mLiveFilterLevelCombo.getText())); /* current log level */
- for (LogCatFilterSettings s : liveFilterSettings) {
+ for (LogCatFilter s : liveFilterSettings) {
liveFilters.add(new LogCatViewerFilter(s));
}
@@ -617,6 +621,7 @@ public final class LogCatPanel extends SelectionDependentPanel
/**
* Update view whenever a message is received.
+ * @param receivedMessages list of messages from logcat
* Implements {@link ILogCatMessageEventListener#messageReceived()}.
*/
public void messageReceived(List<LogCatMessage> receivedMessages) {
diff --git a/ddms/libs/ddmuilib/src/com/android/ddmuilib/logcat/LogCatViewerFilter.java b/ddms/libs/ddmuilib/src/com/android/ddmuilib/logcat/LogCatViewerFilter.java
index 1b04615..f7b8dce 100644
--- a/ddms/libs/ddmuilib/src/com/android/ddmuilib/logcat/LogCatViewerFilter.java
+++ b/ddms/libs/ddmuilib/src/com/android/ddmuilib/logcat/LogCatViewerFilter.java
@@ -15,62 +15,23 @@
*/
package com.android.ddmuilib.logcat;
-import com.android.ddmlib.Log;
-
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerFilter;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
-import java.util.regex.PatternSyntaxException;
-
-
/**
* A JFace {@link ViewerFilter} for the {@link LogCatPanel} displaying logcat messages.
- * This can filter logcat messages based on various properties of the message.
+ * This is a simple wrapper around {@link LogCatFilter}.
*/
public final class LogCatViewerFilter extends ViewerFilter {
- private LogCatFilterSettings mFilterSettings;
-
- private boolean mCheckPID;
- private boolean mCheckTag;
- private boolean mCheckText;
-
- private Pattern mTagPattern;
- private Pattern mTextPattern;
+ private LogCatFilter mFilter;
/**
* Construct a {@link ViewerFilter} filtering logcat messages based on
* user provided filter settings for PID, Tag and log level.
- * @param filterSettings settings to use for the filter. Invalid regexes will
- * be ignored, so they should be checked for validity before constructing this object.
+ * @param filter filter to use
*/
- public LogCatViewerFilter(LogCatFilterSettings filterSettings) {
- mFilterSettings = filterSettings;
-
- mCheckPID = mFilterSettings.getPidString().trim().length() != 0;
- mCheckTag = mFilterSettings.getTag().trim().length() != 0;
- mCheckText = mFilterSettings.getText().trim().length() != 0;
-
- if (mCheckTag) {
- try {
- mTagPattern = Pattern.compile(mFilterSettings.getTag());
- } catch (PatternSyntaxException e) {
- Log.e("LogCatFilter", "Ignoring invalid tag regex.");
- Log.e("LogCatFilter", e.getMessage());
- mCheckTag = false;
- }
- }
-
- if (mCheckText) {
- try {
- mTextPattern = Pattern.compile(mFilterSettings.getText());
- } catch (PatternSyntaxException e) {
- Log.e("LogCatFilter", "Ignoring invalid text regex.");
- Log.e("LogCatFilter", e.getMessage());
- mCheckText = false;
- }
- }
+ public LogCatViewerFilter(LogCatFilter filter) {
+ mFilter = filter;
}
@Override
@@ -80,33 +41,6 @@ public final class LogCatViewerFilter extends ViewerFilter {
}
LogCatMessage m = (LogCatMessage) element;
-
- /* filter out messages of a lower priority */
- if (m.getLogLevel().getPriority() < mFilterSettings.getLogLevel().getPriority()) {
- return false;
- }
-
- /* if pid filter is enabled, filter out messages whose pid does not match
- * the filter's pid */
- if (mCheckPID && !m.getPidString().equals(mFilterSettings.getPidString())) {
- return false;
- }
-
- /* if tag filter is enabled, filter out messages not matching the tag */
- if (mCheckTag) {
- Matcher matcher = mTagPattern.matcher(m.getTag());
- if (!matcher.find()) {
- return false;
- }
- }
-
- if (mCheckText) {
- Matcher matcher = mTextPattern.matcher(m.getMessage());
- if (!matcher.find()) {
- return false;
- }
- }
-
- return true;
+ return mFilter.matches(m);
}
}
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
index 067df4d..96a2f54 100644
--- a/ddms/libs/ddmuilib/tests/src/com/android/ddmuilib/logcat/LogCatFilterSettingsSerializerTest.java
+++ b/ddms/libs/ddmuilib/tests/src/com/android/ddmuilib/logcat/LogCatFilterSettingsSerializerTest.java
@@ -25,7 +25,7 @@ import junit.framework.TestCase;
public class LogCatFilterSettingsSerializerTest extends TestCase {
/* test that decode(encode(f)) = f */
public void testSerializer() {
- LogCatFilterSettings fs = new LogCatFilterSettings(
+ LogCatFilter fs = new LogCatFilter(
"TestFilter", //$NON-NLS-1$
"Tag'.*Regex", //$NON-NLS-1$
"regexForTextField..''", //$NON-NLS-1$
@@ -34,11 +34,11 @@ public class LogCatFilterSettingsSerializerTest extends TestCase {
LogCatFilterSettingsSerializer serializer = new LogCatFilterSettingsSerializer();
String s = serializer.encodeToPreferenceString(Arrays.asList(fs));
- List<LogCatFilterSettings> decodedFiltersList = serializer.decodeFromPreferenceString(s);
+ List<LogCatFilter> decodedFiltersList = serializer.decodeFromPreferenceString(s);
assertEquals(1, decodedFiltersList.size());
- LogCatFilterSettings dfs = decodedFiltersList.get(0);
+ LogCatFilter dfs = decodedFiltersList.get(0);
assertEquals(fs.getName(), dfs.getName());
assertEquals(fs.getTag(), dfs.getTag());
assertEquals(fs.getText(), dfs.getText());
diff --git a/ddms/libs/ddmuilib/tests/src/com/android/ddmuilib/logcat/LogCatFilterSettingsTest.java b/ddms/libs/ddmuilib/tests/src/com/android/ddmuilib/logcat/LogCatFilterSettingsTest.java
deleted file mode 100644
index 8bed741..0000000
--- a/ddms/libs/ddmuilib/tests/src/com/android/ddmuilib/logcat/LogCatFilterSettingsTest.java
+++ /dev/null
@@ -1,74 +0,0 @@
-/*
- * 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;
-
-import junit.framework.TestCase;
-
-public class LogCatFilterSettingsTest extends TestCase {
- public void testMatchingText() {
- LogCatMessage msg = new LogCatMessage(LogLevel.VERBOSE,
- "", "", "", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
- "message with word1 and word2"); //$NON-NLS-1$
- assertEquals(true, search("word1 with", msg)); //$NON-NLS-1$
- assertEquals(true, search("text:w.* ", msg)); //$NON-NLS-1$
- assertEquals(false, search("absent", msg)); //$NON-NLS-1$
- }
-
- public void testTagKeyword() {
- LogCatMessage msg = new LogCatMessage(LogLevel.VERBOSE,
- "", "tag", "", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
- "sample message"); //$NON-NLS-1$
- assertEquals(false, search("t.*", msg)); //$NON-NLS-1$
- assertEquals(true, search("tag:t.*", msg)); //$NON-NLS-1$
- }
-
- public void testPidKeyword() {
- LogCatMessage msg = new LogCatMessage(LogLevel.VERBOSE,
- "123", "", "", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
- "sample message"); //$NON-NLS-1$
- assertEquals(false, search("123", msg)); //$NON-NLS-1$
- assertEquals(true, search("pid:123", msg)); //$NON-NLS-1$
- }
-
- /**
- * Helper method: search if the query string matches the message.
- * @param query words to search for
- * @param message text to search in
- * @return true if the encoded query is present in message
- */
- private boolean search(String query, LogCatMessage message) {
- List<LogCatFilterSettings> settings = LogCatFilterSettings.fromString(query,
- LogLevel.VERBOSE);
-
- List<LogCatViewerFilter> filters = new ArrayList<LogCatViewerFilter>();
- for (LogCatFilterSettings s : settings) {
- filters.add(new LogCatViewerFilter(s));
- }
-
- /* all filters have to match for the query to match */
- for (LogCatViewerFilter f : filters) {
- if (!f.select(null, null, message)) {
- return false;
- }
- }
- return true;
- }
-}
diff --git a/ddms/libs/ddmuilib/tests/src/com/android/ddmuilib/logcat/LogCatFilterTest.java b/ddms/libs/ddmuilib/tests/src/com/android/ddmuilib/logcat/LogCatFilterTest.java
new file mode 100644
index 0000000..a8186e3
--- /dev/null
+++ b/ddms/libs/ddmuilib/tests/src/com/android/ddmuilib/logcat/LogCatFilterTest.java
@@ -0,0 +1,126 @@
+/*
+ * 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.List;
+
+import junit.framework.TestCase;
+
+public class LogCatFilterTest extends TestCase {
+ public void testFilterByLogLevel() {
+ LogCatFilter filter = new LogCatFilter("", //$NON-NLS-1$
+ "", "", "", LogLevel.DEBUG); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
+
+ /* filter message below filter's log level */
+ LogCatMessage msg = new LogCatMessage(LogLevel.VERBOSE,
+ "", "", "", ""); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
+ assertEquals(false, filter.matches(msg));
+
+ /* do not filter message above filter's log level */
+ msg = new LogCatMessage(LogLevel.ERROR,
+ "", "", "", ""); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
+ assertEquals(true, filter.matches(msg));
+ }
+
+ public void testFilterByPid() {
+ LogCatFilter filter = new LogCatFilter("", //$NON-NLS-1$
+ "", "", "123", LogLevel.VERBOSE); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
+
+ /* show message with pid matching filter */
+ LogCatMessage msg = new LogCatMessage(LogLevel.VERBOSE,
+ "123", "", "", ""); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
+ assertEquals(true, filter.matches(msg));
+
+ /* don't show message with pid not matching filter */
+ msg = new LogCatMessage(LogLevel.VERBOSE,
+ "12", "", "", ""); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
+ assertEquals(false, filter.matches(msg));
+ }
+
+ public void testFilterByTagRegex() {
+ LogCatFilter filter = new LogCatFilter("", //$NON-NLS-1$
+ "tag.*", "", "", LogLevel.VERBOSE); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
+
+ /* show message with tag matching filter */
+ LogCatMessage msg = new LogCatMessage(LogLevel.VERBOSE,
+ "", "tag123", "", ""); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
+ assertEquals(true, filter.matches(msg));
+
+ msg = new LogCatMessage(LogLevel.VERBOSE,
+ "", "ta123", "", ""); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
+ assertEquals(false, filter.matches(msg));
+ }
+
+ public void testFilterByTextRegex() {
+ LogCatFilter filter = new LogCatFilter("", //$NON-NLS-1$
+ "", "text.*", "", LogLevel.VERBOSE); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
+
+ /* show message with text matching filter */
+ LogCatMessage msg = new LogCatMessage(LogLevel.VERBOSE,
+ "", "", "", "text123"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
+ assertEquals(true, filter.matches(msg));
+
+ msg = new LogCatMessage(LogLevel.VERBOSE,
+ "", "", "", "te123"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
+ assertEquals(false, filter.matches(msg));
+ }
+
+ public void testMatchingText() {
+ LogCatMessage msg = new LogCatMessage(LogLevel.VERBOSE,
+ "", "", "", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
+ "message with word1 and word2"); //$NON-NLS-1$
+ assertEquals(true, search("word1 with", msg)); //$NON-NLS-1$
+ assertEquals(true, search("text:w.* ", msg)); //$NON-NLS-1$
+ assertEquals(false, search("absent", msg)); //$NON-NLS-1$
+ }
+
+ public void testTagKeyword() {
+ LogCatMessage msg = new LogCatMessage(LogLevel.VERBOSE,
+ "", "tag", "", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
+ "sample message"); //$NON-NLS-1$
+ assertEquals(false, search("t.*", msg)); //$NON-NLS-1$
+ assertEquals(true, search("tag:t.*", msg)); //$NON-NLS-1$
+ }
+
+ public void testPidKeyword() {
+ LogCatMessage msg = new LogCatMessage(LogLevel.VERBOSE,
+ "123", "", "", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
+ "sample message"); //$NON-NLS-1$
+ assertEquals(false, search("123", msg)); //$NON-NLS-1$
+ assertEquals(true, search("pid:123", msg)); //$NON-NLS-1$
+ }
+
+ /**
+ * Helper method: search if the query string matches the message.
+ * @param query words to search for
+ * @param message text to search in
+ * @return true if the encoded query is present in message
+ */
+ private boolean search(String query, LogCatMessage message) {
+ List<LogCatFilter> filters = LogCatFilter.fromString(query,
+ LogLevel.VERBOSE);
+
+ /* all filters have to match for the query to match */
+ for (LogCatFilter f : filters) {
+ if (!f.matches(message)) {
+ return false;
+ }
+ }
+ return true;
+ }
+}
diff --git a/ddms/libs/ddmuilib/tests/src/com/android/ddmuilib/logcat/LogCatViewerFilterTest.java b/ddms/libs/ddmuilib/tests/src/com/android/ddmuilib/logcat/LogCatViewerFilterTest.java
deleted file mode 100644
index 7bd94c4..0000000
--- a/ddms/libs/ddmuilib/tests/src/com/android/ddmuilib/logcat/LogCatViewerFilterTest.java
+++ /dev/null
@@ -1,84 +0,0 @@
-/*
- * 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 junit.framework.TestCase;
-
-public class LogCatViewerFilterTest extends TestCase {
- public void testFilterByLogLevel() {
- LogCatFilterSettings settings = new LogCatFilterSettings("", //$NON-NLS-1$
- "", "", "", LogLevel.DEBUG); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
- LogCatViewerFilter filter = new LogCatViewerFilter(settings);
-
- /* filter message below filter's log level */
- LogCatMessage msg = new LogCatMessage(LogLevel.VERBOSE,
- "", "", "", ""); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
- assertEquals(false, filter.select(null, null, msg));
-
- /* do not filter message above filter's log level */
- msg = new LogCatMessage(LogLevel.ERROR,
- "", "", "", ""); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
- assertEquals(true, filter.select(null, null, msg));
- }
-
- public void testFilterByPid() {
- LogCatFilterSettings settings = new LogCatFilterSettings("", //$NON-NLS-1$
- "", "", "123", LogLevel.VERBOSE); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
- LogCatViewerFilter filter = new LogCatViewerFilter(settings);
-
- /* show message with pid matching filter */
- LogCatMessage msg = new LogCatMessage(LogLevel.VERBOSE,
- "123", "", "", ""); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
- assertEquals(true, filter.select(null, null, msg));
-
- /* don't show message with pid not matching filter */
- msg = new LogCatMessage(LogLevel.VERBOSE,
- "12", "", "", ""); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
- assertEquals(false, filter.select(null, null, msg));
- }
-
- public void testFilterByTagRegex() {
- LogCatFilterSettings settings = new LogCatFilterSettings("", //$NON-NLS-1$
- "tag.*", "", "", LogLevel.VERBOSE); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
- LogCatViewerFilter filter = new LogCatViewerFilter(settings);
-
- /* show message with tag matching filter */
- LogCatMessage msg = new LogCatMessage(LogLevel.VERBOSE,
- "", "tag123", "", ""); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
- assertEquals(true, filter.select(null, null, msg));
-
- msg = new LogCatMessage(LogLevel.VERBOSE,
- "", "ta123", "", ""); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
- assertEquals(false, filter.select(null, null, msg));
- }
-
- public void testFilterByTextRegex() {
- LogCatFilterSettings settings = new LogCatFilterSettings("", //$NON-NLS-1$
- "", "text.*", "", LogLevel.VERBOSE); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
- LogCatViewerFilter filter = new LogCatViewerFilter(settings);
-
- /* show message with text matching filter */
- LogCatMessage msg = new LogCatMessage(LogLevel.VERBOSE,
- "", "", "", "text123"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
- assertEquals(true, filter.select(null, null, msg));
-
- msg = new LogCatMessage(LogLevel.VERBOSE,
- "", "", "", "te123"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
- assertEquals(false, filter.select(null, null, msg));
- }
-}