1 11 package org.eclipse.pde.internal.runtime.logview; 12 13 import java.io.PrintWriter ; 14 import java.io.StringWriter ; 15 import com.ibm.icu.text.DateFormat; 16 import com.ibm.icu.text.SimpleDateFormat; 17 18 import java.text.ParseException ; 19 import java.util.ArrayList ; 20 import java.util.Date ; 21 import java.util.StringTokenizer ; 22 23 import org.eclipse.core.runtime.IStatus; 24 import org.eclipse.core.runtime.PlatformObject; 25 import org.eclipse.jface.resource.ImageDescriptor; 26 import org.eclipse.pde.internal.runtime.PDERuntimeMessages; 27 import org.eclipse.ui.model.IWorkbenchAdapter; 28 29 public class LogEntry extends PlatformObject implements IWorkbenchAdapter { 30 31 public static final String F_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss.SSS"; 33 private ArrayList children; 34 private LogEntry parent; 35 private String pluginId; 36 private int severity; 37 private int code; 38 private Date fDate; 39 private String message; 40 private String stack; 41 private LogSession session; 42 43 public LogEntry() { 44 } 45 46 public LogSession getSession() { 47 return session; 48 } 49 50 void setSession(LogSession session) { 51 this.session = session; 52 } 53 54 public LogEntry(IStatus status) { 55 processStatus(status); 56 } 57 public int getSeverity() { 58 return severity; 59 } 60 61 public boolean isOK() { 62 return severity == IStatus.OK; 63 } 64 public int getCode() { 65 return code; 66 } 67 public String getPluginId() { 68 return pluginId; 69 } 70 public String getMessage() { 71 return message; 72 } 73 public String getStack() { 74 return stack; 75 } 76 public Date getDate() { 77 if (fDate == null) 78 fDate = new Date (0); return fDate; 80 } 81 public String getSeverityText() { 82 return getSeverityText(severity); 83 } 84 public boolean hasChildren() { 85 return children != null && children.size() > 0; 86 } 87 public String toString() { 88 return getSeverityText(); 89 } 90 93 public Object [] getChildren(Object parent) { 94 if (children == null) 95 return new Object [0]; 96 return children.toArray(); 97 } 98 99 102 public ImageDescriptor getImageDescriptor(Object arg0) { 103 return null; 104 } 105 106 109 public String getLabel(Object obj) { 110 return getSeverityText(); 111 } 112 113 116 public Object getParent(Object obj) { 117 return parent; 118 } 119 120 void setParent(LogEntry parent) { 121 this.parent = parent; 122 } 123 124 private String getSeverityText(int severity) { 125 switch (severity) { 126 case IStatus.ERROR : 127 return PDERuntimeMessages.LogView_severity_error; 128 case IStatus.WARNING : 129 return PDERuntimeMessages.LogView_severity_warning; 130 case IStatus.INFO : 131 return PDERuntimeMessages.LogView_severity_info; 132 case IStatus.OK : 133 return PDERuntimeMessages.LogView_severity_ok; 134 } 135 return "?"; } 137 138 139 void processEntry(String line) { 140 StringTokenizer stok = new StringTokenizer (line, " "); int tokenCount = stok.countTokens(); 144 boolean noSeverity = stok.countTokens() < 5; 145 146 if (noSeverity) { 148 severity = 0; 149 code = 0; 150 } 151 StringBuffer dateBuffer = new StringBuffer (); 152 for (int i = 0; i < tokenCount; i++) { 153 String token = stok.nextToken(); 154 switch (i) { 155 case 0: 156 break; 157 case 1: 158 pluginId = token; 159 break; 160 case 2: 161 if (noSeverity) { 162 if (dateBuffer.length() > 0) 163 dateBuffer.append(" "); dateBuffer.append(token); 165 } else { 166 severity = parseInteger(token); 167 } 168 break; 169 case 3: 170 if (noSeverity) { 171 if (dateBuffer.length() > 0) 172 dateBuffer.append(" "); dateBuffer.append(token); 174 } else 175 code = parseInteger(token); 176 break; 177 default: 178 if (dateBuffer.length() > 0) 179 dateBuffer.append(" "); dateBuffer.append(token); 181 } 182 } 183 DateFormat formatter = new SimpleDateFormat(F_DATE_FORMAT); 184 try { 185 Date date = formatter.parse(dateBuffer.toString()); 186 if (date != null) 187 fDate = date; 188 } catch (ParseException e) { 189 } 190 } 191 192 int processSubEntry(String line) { 193 StringTokenizer stok = new StringTokenizer (line, " "); int tokenCount = stok.countTokens(); 197 boolean byFrameWork = stok.countTokens() < 5; 198 199 StringBuffer dateBuffer = new StringBuffer (); 200 int depth = 0; 201 for (int i = 0; i < tokenCount; i++) { 202 String token = stok.nextToken(); 203 switch (i) { 204 case 0: 205 break; 206 case 1: 207 depth = parseInteger(token); 208 break; 209 case 2: 210 pluginId = token; 211 break; 212 case 3: 213 if (byFrameWork) { 214 if (dateBuffer.length() > 0) 215 dateBuffer.append(" "); dateBuffer.append(token); 217 } else { 218 severity = parseInteger(token); 219 } 220 break; 221 case 4: 222 if (byFrameWork) { 223 if (dateBuffer.length() > 0) 224 dateBuffer.append(" "); dateBuffer.append(token); 226 } else 227 code = parseInteger(token); 228 break; 229 default: 230 if (dateBuffer.length() > 0) 231 dateBuffer.append(" "); dateBuffer.append(token); 233 } 234 } 235 DateFormat formatter = new SimpleDateFormat(F_DATE_FORMAT); 236 try { 237 Date date = formatter.parse(dateBuffer.toString()); 238 if (date != null) 239 fDate = date; 240 } catch (ParseException e) { 241 } 242 return depth; 243 } 244 245 private int parseInteger(String token) { 246 try { 247 return Integer.parseInt(token); 248 } catch (NumberFormatException e) { 249 return 0; 250 } 251 } 252 253 void setStack(String stack) { 254 this.stack = stack; 255 } 256 void setMessage(String message) { 257 this.message = message; 258 } 259 260 private void processStatus(IStatus status) { 261 pluginId = status.getPlugin(); 262 severity = status.getSeverity(); 263 code = status.getCode(); 264 fDate = new Date (); 265 message = status.getMessage(); 266 Throwable throwable = status.getException(); 267 if (throwable != null) { 268 StringWriter swriter = new StringWriter (); 269 PrintWriter pwriter = new PrintWriter (swriter); 270 throwable.printStackTrace(pwriter); 271 pwriter.flush(); 272 pwriter.close(); 273 stack = swriter.toString(); 274 } 275 IStatus[] schildren = status.getChildren(); 276 if (schildren.length > 0) { 277 children = new ArrayList (); 278 for (int i = 0; i < schildren.length; i++) { 279 LogEntry child = new LogEntry(schildren[i]); 280 addChild(child); 281 } 282 } 283 } 284 void addChild(LogEntry child) { 285 if (children == null) 286 children = new ArrayList (); 287 children.add(child); 288 child.setParent(this); 289 } 290 public void write(PrintWriter writer) { 291 if (session != null) 292 writer.println(session.getSessionData()); 293 writer.println(getSeverityText()); 294 if (fDate != null) 295 writer.println(getDate()); 296 297 if (message != null) 298 writer.println(getMessage()); 299 300 if (stack != null) { 301 writer.println(); 302 writer.println(stack); 303 } 304 } 305 } 306 | Popular Tags |