diff options
author | Siva Velusamy <vsiva@google.com> | 2012-05-08 17:17:51 -0700 |
---|---|---|
committer | Siva Velusamy <vsiva@google.com> | 2012-05-09 14:00:26 -0700 |
commit | eef4ed8effbf4b698f0ae683331d1c2fe2515d42 (patch) | |
tree | 2a1932c06058a8233379aeb008bf1e101e19b8a7 | |
parent | 21a74431ed9e69c1f6cfe6b5c88cbecaaff76b7c (diff) | |
download | sdk-eef4ed8effbf4b698f0ae683331d1c2fe2515d42.zip sdk-eef4ed8effbf4b698f0ae683331d1c2fe2515d42.tar.gz sdk-eef4ed8effbf4b698f0ae683331d1c2fe2515d42.tar.bz2 |
Allow users more control over auto monitoring logcat.
To help users who don't know about logcat, we automatically monitor
logcat output to see if there are any errors from a user application,
and if so, display the logcat view.
This patch makes this feature a bit easier to use:
- The first time the logcat view is about to be displayed, a dialog
is shown that allows the users to enable/disable auto monitoring.
Note that this can also be done via logcat preferences.
- Users can now control the message priority that will trigger
auto monitoring.
- Once the logcat view has been displayed, we turn off auto monitoring
until the next launch. This reduces overhead and avoids unnecessary
change of focus in cases where the user is already interacting with
the logcat view.
Change-Id: I44a9ccea9148dea7bf6dc7d7864a1bb56a7376fe
7 files changed, 215 insertions, 7 deletions
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 d4da8e4..c2942d0 100644 --- a/ddms/libs/ddmuilib/src/com/android/ddmuilib/logcat/LogCatPanel.java +++ b/ddms/libs/ddmuilib/src/com/android/ddmuilib/logcat/LogCatPanel.java @@ -499,8 +499,11 @@ public final class LogCatPanel extends SelectionDependentPanel private void selectFilterAt(final int index) { mFiltersTableViewer.refresh(); - mFiltersTableViewer.getTable().setSelection(index); - filterSelectionChanged(); + + if (index != mFiltersTableViewer.getTable().getSelectionIndex()) { + mFiltersTableViewer.getTable().setSelection(index); + filterSelectionChanged(); + } } private void createFiltersTable(Composite parent) { diff --git a/eclipse/plugins/com.android.ide.eclipse.ddms/src/com/android/ide/eclipse/ddms/LogCatMonitor.java b/eclipse/plugins/com.android.ide.eclipse.ddms/src/com/android/ide/eclipse/ddms/LogCatMonitor.java index 1e50822..65ddbd4 100644 --- a/eclipse/plugins/com.android.ide.eclipse.ddms/src/com/android/ide/eclipse/ddms/LogCatMonitor.java +++ b/eclipse/plugins/com.android.ide.eclipse.ddms/src/com/android/ide/eclipse/ddms/LogCatMonitor.java @@ -28,7 +28,9 @@ import com.android.ide.eclipse.ddms.views.LogCatView; import org.eclipse.jface.preference.IPreferenceStore; import org.eclipse.jface.util.IPropertyChangeListener; import org.eclipse.jface.util.PropertyChangeEvent; +import org.eclipse.jface.window.Window; import org.eclipse.swt.widgets.Display; +import org.eclipse.swt.widgets.Shell; import org.eclipse.ui.IViewPart; import org.eclipse.ui.IWorkbenchPage; import org.eclipse.ui.IWorkbenchWindow; @@ -38,6 +40,7 @@ import org.eclipse.ui.PlatformUI; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.concurrent.atomic.AtomicBoolean; /** * LogCatMonitor helps in monitoring the logcat output from a set of devices. @@ -45,15 +48,27 @@ import java.util.Map; * if any message is deemed important. */ public class LogCatMonitor { - public static final String AUTO_MONITOR_PREFKEY = "ddms.logcat.automonitor"; + public static final String AUTO_MONITOR_PREFKEY = "ddms.logcat.automonitor"; //$NON-NLS-1$ + public static final String AUTO_MONITOR_LOGLEVEL = "ddms.logcat.auotmonitor.level"; //$NON-NLS-1$ + private static final String AUTO_MONITOR_PROMPT_SHOWN = "ddms.logcat.automonitor.userprompt"; //$NON-NLS-1$ private IPreferenceStore mPrefStore; private Map<String, DeviceData> mMonitoredDevices; private IDebuggerConnector[] mConnectors; + private int mMinMessagePriority; + + /** + * Flag that controls when the logcat stream is checked. This flag is set when the user + * performs a launch, and is reset as soon as the logcat view is displayed. + */ + final AtomicBoolean mMonitorEnabled = new AtomicBoolean(false); + public LogCatMonitor(IDebuggerConnector[] debuggerConnectors, IPreferenceStore prefStore) { mConnectors = debuggerConnectors; mPrefStore = prefStore; + mMinMessagePriority = + LogLevel.getByString(mPrefStore.getString(AUTO_MONITOR_LOGLEVEL)).getPriority(); mMonitoredDevices = new HashMap<String, DeviceData>(); @@ -79,6 +94,9 @@ public class LogCatMonitor { if (AUTO_MONITOR_PREFKEY.equals(event.getProperty()) && event.getNewValue().equals(false)) { unmonitorAllDevices(); + } else if (AUTO_MONITOR_LOGLEVEL.equals(event.getProperty())) { + mMinMessagePriority = + LogLevel.getByString((String) event.getNewValue()).getPriority(); } } }); @@ -107,6 +125,8 @@ public class LogCatMonitor { return; } + mMonitorEnabled.set(true); + if (mMonitoredDevices.keySet().contains(device.getSerialNumber())) { // the device is already monitored return; @@ -126,12 +146,20 @@ public class LogCatMonitor { } private void checkMessages(List<LogCatMessage> receivedMessages, IDevice device) { + if (!mMonitorEnabled.get()) { + return; + } + // check the received list of messages to see if any of them are // significant enough to be seen by the user. If so, activate the logcat view // to display those messages for (LogCatMessage m : receivedMessages) { if (isImportantMessage(m)) { focusLogCatView(device, m.getAppName()); + + // now that logcat view is active, no need to check messages until the next + // time user launches an application. + mMonitorEnabled.set(false); break; } } @@ -142,7 +170,7 @@ public class LogCatMonitor { * it is of severity level error or higher, and it belongs to an app currently in the workspace. */ private boolean isImportantMessage(LogCatMessage m) { - if (m.getLogLevel().getPriority() < LogLevel.ERROR.getPriority()) { + if (m.getLogLevel().getPriority() < mMinMessagePriority) { return false; } @@ -170,6 +198,15 @@ public class LogCatMonitor { return; } + // if the logcat view is not visible, then prompt the user once to set + // logcat monitoring preferences + if (!isLogCatViewVisible(page)) { + boolean showLogCatView = promptUserOnce(page.getWorkbenchWindow().getShell()); + if (!showLogCatView) { + return; + } + } + // display view final LogCatView v = displayLogCatView(page); if (v == null) { @@ -183,6 +220,11 @@ public class LogCatMonitor { v.selectTransientAppFilter(appName); } + private boolean isLogCatViewVisible(IWorkbenchPage page) { + IViewPart view = page.findView(LogCatView.ID); + return view != null && page.isPartVisible(view); + } + private LogCatView displayLogCatView(IWorkbenchPage page) { // if the view is already in the page, just bring it to the front // without giving it focus. @@ -201,6 +243,25 @@ public class LogCatMonitor { return null; } } + + private boolean promptUserOnce(Shell shell) { + // see if this prompt was already displayed + boolean promptShown = mPrefStore.getBoolean(AUTO_MONITOR_PROMPT_SHOWN); + if (promptShown) { + return mPrefStore.getBoolean(AUTO_MONITOR_PREFKEY); + } + + LogCatMonitorDialog dlg = new LogCatMonitorDialog(shell); + int r = dlg.open(); + + // save preference indicating that this dialog has been displayed once + mPrefStore.setValue(AUTO_MONITOR_PROMPT_SHOWN, true); + mPrefStore.setValue(AUTO_MONITOR_PREFKEY, dlg.shouldMonitor()); + mPrefStore.setValue(AUTO_MONITOR_LOGLEVEL, dlg.getMinimumPriority()); + + return r == Window.OK && dlg.shouldMonitor(); + } + }); } diff --git a/eclipse/plugins/com.android.ide.eclipse.ddms/src/com/android/ide/eclipse/ddms/LogCatMonitorDialog.java b/eclipse/plugins/com.android.ide.eclipse.ddms/src/com/android/ide/eclipse/ddms/LogCatMonitorDialog.java new file mode 100644 index 0000000..44959e9 --- /dev/null +++ b/eclipse/plugins/com.android.ide.eclipse.ddms/src/com/android/ide/eclipse/ddms/LogCatMonitorDialog.java @@ -0,0 +1,116 @@ +/* + * Copyright (C) 2012 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.ide.eclipse.ddms; + +import com.android.ddmlib.Log.LogLevel; + +import org.eclipse.jface.dialogs.IDialogConstants; +import org.eclipse.jface.dialogs.TitleAreaDialog; +import org.eclipse.jface.layout.GridDataFactory; +import org.eclipse.swt.SWT; +import org.eclipse.swt.events.SelectionAdapter; +import org.eclipse.swt.events.SelectionEvent; +import org.eclipse.swt.events.SelectionListener; +import org.eclipse.swt.layout.GridData; +import org.eclipse.swt.layout.GridLayout; +import org.eclipse.swt.widgets.Button; +import org.eclipse.swt.widgets.Combo; +import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Control; +import org.eclipse.swt.widgets.Shell; + +public class LogCatMonitorDialog extends TitleAreaDialog { + private static final String TITLE = "Auto Monitor Logcat"; + private static final String DEFAULT_MESSAGE = + "Would you like ADT to automatically monitor logcat output for messages " + + "from applications in the workspace?"; + + private boolean mShouldMonitor = true; + + private static final String[] LOG_PRIORITIES = new String[] { + LogLevel.VERBOSE.getStringValue(), + LogLevel.DEBUG.getStringValue(), + LogLevel.INFO.getStringValue(), + LogLevel.WARN.getStringValue(), + LogLevel.ERROR.getStringValue(), + LogLevel.ASSERT.getStringValue(), + }; + private static final int ERROR_PRIORITY_INDEX = 4; + + private String mMinimumLogPriority = LOG_PRIORITIES[ERROR_PRIORITY_INDEX]; + + public LogCatMonitorDialog(Shell parentShell) { + super(parentShell); + setHelpAvailable(false); + } + + @Override + protected Control createDialogArea(Composite shell) { + setTitle(TITLE); + setMessage(DEFAULT_MESSAGE); + + Composite parent = (Composite) super.createDialogArea(shell); + Composite c = new Composite(parent, SWT.BORDER); + c.setLayout(new GridLayout(2, false)); + c.setLayoutData(new GridData(GridData.FILL_BOTH)); + + final Button disableButton = new Button(c, SWT.RADIO); + disableButton.setText("No, do not monitor logcat output."); + GridDataFactory.defaultsFor(disableButton).span(2, 1).applyTo(disableButton); + + final Button enableButton = new Button(c, SWT.RADIO); + enableButton.setText("Yes, monitor logcat and display logcat view if there are " + + "messages with priority higher than:"); + enableButton.setSelection(true); + + final Combo levelCombo = new Combo(c, SWT.READ_ONLY | SWT.DROP_DOWN); + levelCombo.setItems(LOG_PRIORITIES); + levelCombo.select(ERROR_PRIORITY_INDEX); + + SelectionListener s = new SelectionAdapter() { + @Override + public void widgetSelected(SelectionEvent e) { + if (e.getSource() == enableButton) { + mShouldMonitor = enableButton.getSelection(); + levelCombo.setEnabled(mShouldMonitor); + } else if (e.getSource() == levelCombo) { + mMinimumLogPriority = LOG_PRIORITIES[levelCombo.getSelectionIndex()]; + } + } + }; + + levelCombo.addSelectionListener(s); + enableButton.addSelectionListener(s); + + return parent; + } + + @Override + protected void createButtonsForButtonBar(Composite parent) { + // Only need OK button + createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL, + true); + } + + public boolean shouldMonitor() { + return mShouldMonitor; + } + + public String getMinimumPriority() { + return mMinimumLogPriority; + } +} diff --git a/eclipse/plugins/com.android.ide.eclipse.ddms/src/com/android/ide/eclipse/ddms/i18n/Messages.java b/eclipse/plugins/com.android.ide.eclipse.ddms/src/com/android/ide/eclipse/ddms/i18n/Messages.java index 45602e6..576b598 100644 --- a/eclipse/plugins/com.android.ide.eclipse.ddms/src/com/android/ide/eclipse/ddms/i18n/Messages.java +++ b/eclipse/plugins/com.android.ide.eclipse.ddms/src/com/android/ide/eclipse/ddms/i18n/Messages.java @@ -80,6 +80,7 @@ public class Messages extends NLS { public static String LogCatPreferencePage_Switch_Perspective; public static String LogCatPreferencePage_Switch_To; public static String LogCatPreferencePage_AutoMonitorLogcat; + public static String LogCatPreferencePage_SessionFilterLogLevel; public static String LogCatView_Clear_Log; public static String LogCatView_Copy; public static String LogCatView_Create_Filter; diff --git a/eclipse/plugins/com.android.ide.eclipse.ddms/src/com/android/ide/eclipse/ddms/i18n/messages.properties b/eclipse/plugins/com.android.ide.eclipse.ddms/src/com/android/ide/eclipse/ddms/i18n/messages.properties index 07c4f16..3698ebc 100644 --- a/eclipse/plugins/com.android.ide.eclipse.ddms/src/com/android/ide/eclipse/ddms/i18n/messages.properties +++ b/eclipse/plugins/com.android.ide.eclipse.ddms/src/com/android/ide/eclipse/ddms/i18n/messages.properties @@ -73,7 +73,8 @@ LogCatPreferencePage_Display_Font=Display Font: LogCatPreferencePage_MaxMessages=Maximum number of logcat messages to buffer: LogCatPreferencePage_Switch_Perspective=Switch Perspective LogCatPreferencePage_Switch_To=Switch to: -LogCatPreferencePage_AutoMonitorLogcat=Display logcat view when there are messages from an application in the workspace +LogCatPreferencePage_AutoMonitorLogcat=Monitor logcat for messages from applications in workspace +LogCatPreferencePage_SessionFilterLogLevel=Show logcat view if message priority is atleast: LogCatView_Clear_Log=Clear Log LogCatView_Copy=Copy LogCatView_Create_Filter=Create Filter diff --git a/eclipse/plugins/com.android.ide.eclipse.ddms/src/com/android/ide/eclipse/ddms/preferences/LogCatPreferencePage.java b/eclipse/plugins/com.android.ide.eclipse.ddms/src/com/android/ide/eclipse/ddms/preferences/LogCatPreferencePage.java index b315ee9..9971e18 100644 --- a/eclipse/plugins/com.android.ide.eclipse.ddms/src/com/android/ide/eclipse/ddms/preferences/LogCatPreferencePage.java +++ b/eclipse/plugins/com.android.ide.eclipse.ddms/src/com/android/ide/eclipse/ddms/preferences/LogCatPreferencePage.java @@ -16,6 +16,7 @@ package com.android.ide.eclipse.ddms.preferences; +import com.android.ddmlib.Log.LogLevel; import com.android.ddmuilib.logcat.LogCatMessageList; import com.android.ddmuilib.logcat.LogCatPanel; import com.android.ide.eclipse.base.InstallDetails; @@ -43,6 +44,7 @@ public class LogCatPreferencePage extends FieldEditorPreferencePage implements private ComboFieldEditor mWhichPerspective; private IntegerFieldEditor mMaxMessages; private BooleanFieldEditor mAutoMonitorLogcat; + private ComboFieldEditor mAutoMonitorLogcatLevel; public LogCatPreferencePage() { super(GRID); @@ -92,6 +94,22 @@ public class LogCatPreferencePage extends FieldEditorPreferencePage implements Messages.LogCatPreferencePage_AutoMonitorLogcat, getFieldEditorParent()); addField(mAutoMonitorLogcat); + + mAutoMonitorLogcatLevel = new ComboFieldEditor(LogCatMonitor.AUTO_MONITOR_LOGLEVEL, + Messages.LogCatPreferencePage_SessionFilterLogLevel, + new String[][] { + { LogLevel.VERBOSE.toString(), LogLevel.VERBOSE.getStringValue() }, + { LogLevel.DEBUG.toString(), LogLevel.DEBUG.getStringValue() }, + { LogLevel.INFO.toString(), LogLevel.INFO.getStringValue() }, + { LogLevel.WARN.toString(), LogLevel.WARN.getStringValue() }, + { LogLevel.ERROR.toString(), LogLevel.ERROR.getStringValue() }, + { LogLevel.ASSERT.toString(), LogLevel.ASSERT.getStringValue() }, + }, + getFieldEditorParent()); + mAutoMonitorLogcatLevel.setEnabled( + getPreferenceStore().getBoolean(LogCatMonitor.AUTO_MONITOR_PREFKEY), + getFieldEditorParent()); + addField(mAutoMonitorLogcatLevel); } @Override @@ -101,8 +119,11 @@ public class LogCatPreferencePage extends FieldEditorPreferencePage implements @Override public void propertyChange(PropertyChangeEvent event) { if (event.getSource().equals(mSwitchPerspective)) { - mWhichPerspective.setEnabled(mSwitchPerspective.getBooleanValue() - , getFieldEditorParent()); + mWhichPerspective.setEnabled(mSwitchPerspective.getBooleanValue(), + getFieldEditorParent()); + } else if (event.getSource().equals(mAutoMonitorLogcat)) { + mAutoMonitorLogcatLevel.setEnabled(mAutoMonitorLogcat.getBooleanValue(), + getFieldEditorParent()); } } @@ -113,5 +134,8 @@ public class LogCatPreferencePage extends FieldEditorPreferencePage implements mMaxMessages.setStringValue( Integer.toString(LogCatMessageList.MAX_MESSAGES_DEFAULT)); + + mAutoMonitorLogcatLevel.setEnabled(mAutoMonitorLogcat.getBooleanValue(), + getFieldEditorParent()); } } diff --git a/eclipse/plugins/com.android.ide.eclipse.ddms/src/com/android/ide/eclipse/ddms/preferences/PreferenceInitializer.java b/eclipse/plugins/com.android.ide.eclipse.ddms/src/com/android/ide/eclipse/ddms/preferences/PreferenceInitializer.java index cce470f..2415e20 100644 --- a/eclipse/plugins/com.android.ide.eclipse.ddms/src/com/android/ide/eclipse/ddms/preferences/PreferenceInitializer.java +++ b/eclipse/plugins/com.android.ide.eclipse.ddms/src/com/android/ide/eclipse/ddms/preferences/PreferenceInitializer.java @@ -21,6 +21,7 @@ import com.android.ide.eclipse.ddms.LogCatMonitor; import com.android.ide.eclipse.ddms.views.DeviceView.HProfHandler; import com.android.ide.eclipse.ddms.views.LogCatView; import com.android.ddmlib.DdmPreferences; +import com.android.ddmlib.Log.LogLevel; import com.android.ddmuilib.DdmUiPreferences; import org.eclipse.core.runtime.Platform; @@ -126,6 +127,7 @@ public class PreferenceInitializer extends AbstractPreferenceInitializer { store.setDefault(ATTR_PERSPECTIVE_ID, LogCatView.DEFAULT_PERSPECTIVE_ID); store.setDefault(LogCatMonitor.AUTO_MONITOR_PREFKEY, true); + store.setDefault(LogCatMonitor.AUTO_MONITOR_LOGLEVEL, LogLevel.VERBOSE.getStringValue()); } /** |