2 * Copyright (C) 2007 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package com.android.ide.eclipse.ddms.views;
19 import com.android.ide.eclipse.ddms.CommonAction;
20 import com.android.ide.eclipse.ddms.DdmsPlugin;
21 import com.android.ide.eclipse.ddms.ImageLoader;
22 import com.android.ide.eclipse.ddms.preferences.PreferenceInitializer;
23 import com.android.ddmlib.Log.LogLevel;
24 import com.android.ddmuilib.logcat.LogColors;
25 import com.android.ddmuilib.logcat.LogFilter;
26 import com.android.ddmuilib.logcat.LogPanel;
27 import com.android.ddmuilib.logcat.LogPanel.ILogFilterStorageManager;
29 import org.eclipse.jface.action.Action;
30 import org.eclipse.jface.action.IAction;
31 import org.eclipse.jface.action.IMenuManager;
32 import org.eclipse.jface.action.IToolBarManager;
33 import org.eclipse.jface.action.Separator;
34 import org.eclipse.swt.dnd.Clipboard;
35 import org.eclipse.swt.graphics.Color;
36 import org.eclipse.swt.graphics.Font;
37 import org.eclipse.swt.graphics.FontData;
38 import org.eclipse.swt.widgets.Composite;
39 import org.eclipse.swt.widgets.Display;
40 import org.eclipse.ui.IActionBars;
41 import org.eclipse.ui.actions.ActionFactory;
43 import java.util.ArrayList;
46 * The log cat view displays log output from the current device selection.
49 public final class LogCatView extends SelectionDependentViewPart {
51 public static final String ID =
52 "com.android.ide.eclipse.ddms.views.LogCatView"; // $NON-NLS-1$
54 private static final String PREFS_COL_TIME =
55 DdmsPlugin.PLUGIN_ID + ".logcat.time"; // $NON-NLS-1$
56 private static final String PREFS_COL_LEVEL =
57 DdmsPlugin.PLUGIN_ID + ".logcat.level"; // $NON-NLS-1$
58 private static final String PREFS_COL_PID =
59 DdmsPlugin.PLUGIN_ID + ".logcat.pid"; // $NON-NLS-1$
60 private static final String PREFS_COL_TAG =
61 DdmsPlugin.PLUGIN_ID + ".logcat.tag"; // $NON-NLS-1$
62 private static final String PREFS_COL_MESSAGE =
63 DdmsPlugin.PLUGIN_ID + ".logcat.message"; // $NON-NLS-1$
65 private static final String PREFS_FILTERS =
66 DdmsPlugin.PLUGIN_ID + ".logcat.filters"; // $NON-NLS-1$
68 private static LogCatView sThis;
69 private LogPanel mLogPanel;
71 private CommonAction mCreateFilterAction;
72 private CommonAction mDeleteFilterAction;
73 private CommonAction mEditFilterAction;
74 private CommonAction mExportAction;
76 private CommonAction[] mLogLevelActions;
77 private String[] mLogLevelIcons = {
78 "v.png", //$NON-NLS-1S
79 "d.png", //$NON-NLS-1S
80 "i.png", //$NON-NLS-1S
81 "w.png", //$NON-NLS-1S
82 "e.png", //$NON-NLS-1S
85 private Action mClearAction;
87 private Clipboard mClipboard;
90 * An implementation of {@link ILogFilterStorageManager} to bridge to the eclipse preference
91 * store, and saves the log filters.
93 private final class FilterStorage implements ILogFilterStorageManager {
95 public LogFilter[] getFilterFromStore() {
96 String filterPrefs = DdmsPlugin.getDefault().getPreferenceStore().getString(
99 // split in a string per filter
100 String[] filters = filterPrefs.split("\\|"); // $NON-NLS-1$
102 ArrayList<LogFilter> list =
103 new ArrayList<LogFilter>(filters.length);
105 for (String f : filters) {
106 if (f.length() > 0) {
107 LogFilter logFilter = new LogFilter();
108 if (logFilter.loadFromString(f)) {
114 return list.toArray(new LogFilter[list.size()]);
117 public void saveFilters(LogFilter[] filters) {
118 StringBuilder sb = new StringBuilder();
119 for (LogFilter f : filters) {
120 String filterString = f.toString();
121 sb.append(filterString);
125 DdmsPlugin.getDefault().getPreferenceStore().setValue(PREFS_FILTERS, sb.toString());
128 public boolean requiresDefaultFilter() {
133 public LogCatView() {
135 LogPanel.PREFS_TIME = PREFS_COL_TIME;
136 LogPanel.PREFS_LEVEL = PREFS_COL_LEVEL;
137 LogPanel.PREFS_PID = PREFS_COL_PID;
138 LogPanel.PREFS_TAG = PREFS_COL_TAG;
139 LogPanel.PREFS_MESSAGE = PREFS_COL_MESSAGE;
143 * Returns the singleton instance.
145 public static LogCatView getInstance() {
150 * Sets the display font.
151 * @param font The font.
153 public static void setFont(Font font) {
154 if (sThis != null && sThis.mLogPanel != null) {
155 sThis.mLogPanel.setFont(font);
160 public void createPartControl(Composite parent) {
161 Display d = parent.getDisplay();
162 LogColors colors = new LogColors();
164 ImageLoader loader = DdmsPlugin.getImageLoader();
166 colors.infoColor = new Color(d, 0, 127, 0);
167 colors.debugColor = new Color(d, 0, 0, 127);
168 colors.errorColor = new Color(d, 255, 0, 0);
169 colors.warningColor = new Color(d, 255, 127, 0);
170 colors.verboseColor = new Color(d, 0, 0, 0);
172 mCreateFilterAction = new CommonAction("Create Filter") {
175 mLogPanel.addFilter();
178 mCreateFilterAction.setToolTipText("Create Filter");
179 mCreateFilterAction.setImageDescriptor(loader
180 .loadDescriptor("add.png")); // $NON-NLS-1$
182 mEditFilterAction = new CommonAction("Edit Filter") {
185 mLogPanel.editFilter();
188 mEditFilterAction.setToolTipText("Edit Filter");
189 mEditFilterAction.setImageDescriptor(loader
190 .loadDescriptor("edit.png")); // $NON-NLS-1$
192 mDeleteFilterAction = new CommonAction("Delete Filter") {
195 mLogPanel.deleteFilter();
198 mDeleteFilterAction.setToolTipText("Delete Filter");
199 mDeleteFilterAction.setImageDescriptor(loader
200 .loadDescriptor("delete.png")); // $NON-NLS-1$
202 mExportAction = new CommonAction("Export Selection As Text...") {
208 mExportAction.setToolTipText("Export Selection As Text...");
209 mExportAction.setImageDescriptor(loader.loadDescriptor("save.png")); // $NON-NLS-1$
211 LogLevel[] levels = LogLevel.values();
212 mLogLevelActions = new CommonAction[mLogLevelIcons.length];
213 for (int i = 0 ; i < mLogLevelActions.length; i++) {
214 String name = levels[i].getStringValue();
215 mLogLevelActions[i] = new CommonAction(name, IAction.AS_CHECK_BOX) {
218 // disable the other actions and record current index
219 for (int i = 0 ; i < mLogLevelActions.length; i++) {
220 Action a = mLogLevelActions[i];
225 mLogPanel.setCurrentFilterLogLevel(i+2);
233 mLogLevelActions[i].setToolTipText(name);
234 mLogLevelActions[i].setImageDescriptor(loader.loadDescriptor(mLogLevelIcons[i]));
237 mClearAction = new Action("Clear Log") {
243 mClearAction.setImageDescriptor(loader
244 .loadDescriptor("clear.png")); // $NON-NLS-1$
247 // now create the log view
248 mLogPanel = new LogPanel(loader, colors, new FilterStorage(), LogPanel.FILTER_MANUAL);
249 mLogPanel.setActions(mDeleteFilterAction, mEditFilterAction, mLogLevelActions);
252 String fontStr = DdmsPlugin.getDefault().getPreferenceStore().getString(
253 PreferenceInitializer.ATTR_LOGCAT_FONT);
254 if (fontStr != null) {
255 FontData data = new FontData(fontStr);
257 if (fontStr != null) {
258 mLogPanel.setFont(new Font(parent.getDisplay(), data));
262 mLogPanel.createPanel(parent);
263 setSelectionDependentPanel(mLogPanel);
265 // place the actions.
268 // setup the copy action
269 mClipboard = new Clipboard(d);
270 IActionBars actionBars = getViewSite().getActionBars();
271 actionBars.setGlobalActionHandler(ActionFactory.COPY.getId(), new Action("Copy") {
274 mLogPanel.copy(mClipboard);
278 // setup the select all action
279 actionBars.setGlobalActionHandler(ActionFactory.SELECT_ALL.getId(),
280 new Action("Select All") {
283 mLogPanel.selectAll();
289 public void dispose() {
290 mLogPanel.stopLogCat(true);
291 mClipboard.dispose();
295 public void setFocus() {
296 mLogPanel.setFocus();
300 * Place the actions in the ui.
302 private void placeActions() {
303 IActionBars actionBars = getViewSite().getActionBars();
306 IMenuManager menuManager = actionBars.getMenuManager();
307 menuManager.add(mCreateFilterAction);
308 menuManager.add(mEditFilterAction);
309 menuManager.add(mDeleteFilterAction);
310 menuManager.add(new Separator());
311 menuManager.add(mClearAction);
312 menuManager.add(new Separator());
313 menuManager.add(mExportAction);
315 // and then in the toolbar
316 IToolBarManager toolBarManager = actionBars.getToolBarManager();
317 for (CommonAction a : mLogLevelActions) {
318 toolBarManager.add(a);
320 toolBarManager.add(new Separator());
321 toolBarManager.add(mCreateFilterAction);
322 toolBarManager.add(mEditFilterAction);
323 toolBarManager.add(mDeleteFilterAction);
324 toolBarManager.add(new Separator());
325 toolBarManager.add(mClearAction);