1 /* 2 * Copyright (C) 2011 The Android Open Source Project 3 * 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 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 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. 15 */ 16 17 package com.android.ddmuilib.logcat; 18 19 import com.android.ddmlib.Log.LogLevel; 20 21 import org.eclipse.jface.viewers.ColumnLabelProvider; 22 import org.eclipse.jface.viewers.ViewerCell; 23 import org.eclipse.swt.graphics.Color; 24 import org.eclipse.swt.graphics.Font; 25 26 /** 27 * A JFace Column label provider for the LogCat log messages. It expects elements of type 28 * {@link LogCatMessage}. 29 */ 30 public final class LogCatMessageLabelProvider extends ColumnLabelProvider { 31 private static final int INDEX_LOGLEVEL = 0; 32 private static final int INDEX_LOGTIME = 1; 33 private static final int INDEX_PID = 2; 34 private static final int INDEX_APPNAME = 3; 35 private static final int INDEX_TAG = 4; 36 private static final int INDEX_TEXT = 5; 37 38 /* Default Colors for different log levels. */ 39 private static final Color INFO_MSG_COLOR = new Color(null, 0, 127, 0); 40 private static final Color DEBUG_MSG_COLOR = new Color(null, 0, 0, 127); 41 private static final Color ERROR_MSG_COLOR = new Color(null, 255, 0, 0); 42 private static final Color WARN_MSG_COLOR = new Color(null, 255, 127, 0); 43 private static final Color VERBOSE_MSG_COLOR = new Color(null, 0, 0, 0); 44 45 private Font mLogFont; 46 private int mWrapWidth = 100; 47 48 /** 49 * Construct a column label provider for the logcat table. 50 * @param font default font to use 51 */ LogCatMessageLabelProvider(Font font)52 public LogCatMessageLabelProvider(Font font) { 53 mLogFont = font; 54 } 55 getCellText(LogCatMessage m, int columnIndex)56 private String getCellText(LogCatMessage m, int columnIndex) { 57 switch (columnIndex) { 58 case INDEX_LOGLEVEL: 59 return Character.toString(m.getLogLevel().getPriorityLetter()); 60 case INDEX_LOGTIME: 61 return m.getTime(); 62 case INDEX_PID: 63 return m.getPid(); 64 case INDEX_APPNAME: 65 return m.getAppName(); 66 case INDEX_TAG: 67 return m.getTag(); 68 case INDEX_TEXT: 69 return m.getMessage(); 70 default: 71 return ""; 72 } 73 } 74 75 @Override update(ViewerCell cell)76 public void update(ViewerCell cell) { 77 Object element = cell.getElement(); 78 if (!(element instanceof LogCatMessage)) { 79 return; 80 } 81 LogCatMessage m = (LogCatMessage) element; 82 83 String text = getCellText(m, cell.getColumnIndex()); 84 cell.setText(text); 85 cell.setFont(mLogFont); 86 cell.setForeground(getForegroundColor(m)); 87 } 88 getForegroundColor(LogCatMessage m)89 private Color getForegroundColor(LogCatMessage m) { 90 LogLevel l = m.getLogLevel(); 91 92 if (l.equals(LogLevel.VERBOSE)) { 93 return VERBOSE_MSG_COLOR; 94 } else if (l.equals(LogLevel.INFO)) { 95 return INFO_MSG_COLOR; 96 } else if (l.equals(LogLevel.DEBUG)) { 97 return DEBUG_MSG_COLOR; 98 } else if (l.equals(LogLevel.ERROR)) { 99 return ERROR_MSG_COLOR; 100 } else if (l.equals(LogLevel.WARN)) { 101 return WARN_MSG_COLOR; 102 } 103 104 return null; 105 } 106 setFont(Font preferredFont)107 public void setFont(Font preferredFont) { 108 if (mLogFont != null) { 109 mLogFont.dispose(); 110 } 111 112 mLogFont = preferredFont; 113 } 114 setMinimumLengthForToolTips(int widthInChars)115 public void setMinimumLengthForToolTips(int widthInChars) { 116 mWrapWidth = widthInChars; 117 } 118 119 /** 120 * Obtain the tool tip to show for a particular logcat message. 121 * We display a tool tip only for messages longer than the width set by 122 * {@link #setMinimumLengthForToolTips(int)}. 123 */ 124 @Override getToolTipText(Object element)125 public String getToolTipText(Object element) { 126 String text = element.toString(); 127 if (text.length() > mWrapWidth) { 128 return text; 129 } else { 130 return null; 131 } 132 } 133 } 134