001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.internal.runtime.logview;
011:
012: import java.io.PrintWriter;
013: import java.io.StringWriter;
014: import com.ibm.icu.text.DateFormat;
015: import com.ibm.icu.text.SimpleDateFormat;
016:
017: import java.text.ParseException;
018: import java.util.ArrayList;
019: import java.util.Date;
020: import java.util.StringTokenizer;
021:
022: import org.eclipse.core.runtime.IStatus;
023: import org.eclipse.core.runtime.PlatformObject;
024: import org.eclipse.jface.resource.ImageDescriptor;
025: import org.eclipse.pde.internal.runtime.PDERuntimeMessages;
026: import org.eclipse.ui.model.IWorkbenchAdapter;
027:
028: public class LogEntry extends PlatformObject implements
029: IWorkbenchAdapter {
030:
031: public static final String F_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss.SSS"; //$NON-NLS-1$
032:
033: private ArrayList children;
034: private LogEntry parent;
035: private String pluginId;
036: private int severity;
037: private int code;
038: private Date fDate;
039: private String message;
040: private String stack;
041: private LogSession session;
042:
043: public LogEntry() {
044: }
045:
046: public LogSession getSession() {
047: return session;
048: }
049:
050: void setSession(LogSession session) {
051: this .session = session;
052: }
053:
054: public LogEntry(IStatus status) {
055: processStatus(status);
056: }
057:
058: public int getSeverity() {
059: return severity;
060: }
061:
062: public boolean isOK() {
063: return severity == IStatus.OK;
064: }
065:
066: public int getCode() {
067: return code;
068: }
069:
070: public String getPluginId() {
071: return pluginId;
072: }
073:
074: public String getMessage() {
075: return message;
076: }
077:
078: public String getStack() {
079: return stack;
080: }
081:
082: public Date getDate() {
083: if (fDate == null)
084: fDate = new Date(0); // unknown date - return epoch
085: return fDate;
086: }
087:
088: public String getSeverityText() {
089: return getSeverityText(severity);
090: }
091:
092: public boolean hasChildren() {
093: return children != null && children.size() > 0;
094: }
095:
096: public String toString() {
097: return getSeverityText();
098: }
099:
100: /**
101: * @see IWorkbenchAdapter#getChildren(Object)
102: */
103: public Object[] getChildren(Object parent) {
104: if (children == null)
105: return new Object[0];
106: return children.toArray();
107: }
108:
109: /**
110: * @see IWorkbenchAdapter#getImageDescriptor(Object)
111: */
112: public ImageDescriptor getImageDescriptor(Object arg0) {
113: return null;
114: }
115:
116: /**
117: * @see IWorkbenchAdapter#getLabel(Object)
118: */
119: public String getLabel(Object obj) {
120: return getSeverityText();
121: }
122:
123: /**
124: * @see IWorkbenchAdapter#getParent(Object)
125: */
126: public Object getParent(Object obj) {
127: return parent;
128: }
129:
130: void setParent(LogEntry parent) {
131: this .parent = parent;
132: }
133:
134: private String getSeverityText(int severity) {
135: switch (severity) {
136: case IStatus.ERROR:
137: return PDERuntimeMessages.LogView_severity_error;
138: case IStatus.WARNING:
139: return PDERuntimeMessages.LogView_severity_warning;
140: case IStatus.INFO:
141: return PDERuntimeMessages.LogView_severity_info;
142: case IStatus.OK:
143: return PDERuntimeMessages.LogView_severity_ok;
144: }
145: return "?"; //$NON-NLS-1$
146: }
147:
148: void processEntry(String line) {
149: //!ENTRY <pluginID> <severity> <code> <date>
150: //!ENTRY <pluginID> <date> if logged by the framework!!!
151: StringTokenizer stok = new StringTokenizer(line, " "); //$NON-NLS-1$
152: int tokenCount = stok.countTokens();
153: boolean noSeverity = stok.countTokens() < 5;
154:
155: // no severity means it should be represented as OK
156: if (noSeverity) {
157: severity = 0;
158: code = 0;
159: }
160: StringBuffer dateBuffer = new StringBuffer();
161: for (int i = 0; i < tokenCount; i++) {
162: String token = stok.nextToken();
163: switch (i) {
164: case 0:
165: break;
166: case 1:
167: pluginId = token;
168: break;
169: case 2:
170: if (noSeverity) {
171: if (dateBuffer.length() > 0)
172: dateBuffer.append(" "); //$NON-NLS-1$
173: dateBuffer.append(token);
174: } else {
175: severity = parseInteger(token);
176: }
177: break;
178: case 3:
179: if (noSeverity) {
180: if (dateBuffer.length() > 0)
181: dateBuffer.append(" "); //$NON-NLS-1$
182: dateBuffer.append(token);
183: } else
184: code = parseInteger(token);
185: break;
186: default:
187: if (dateBuffer.length() > 0)
188: dateBuffer.append(" "); //$NON-NLS-1$
189: dateBuffer.append(token);
190: }
191: }
192: DateFormat formatter = new SimpleDateFormat(F_DATE_FORMAT);
193: try {
194: Date date = formatter.parse(dateBuffer.toString());
195: if (date != null)
196: fDate = date;
197: } catch (ParseException e) {
198: }
199: }
200:
201: int processSubEntry(String line) {
202: //!SUBENTRY <depth> <pluginID> <severity> <code> <date>
203: //!SUBENTRY <depth> <pluginID> <date>if logged by the framework!!!
204: StringTokenizer stok = new StringTokenizer(line, " "); //$NON-NLS-1$
205: int tokenCount = stok.countTokens();
206: boolean byFrameWork = stok.countTokens() < 5;
207:
208: StringBuffer dateBuffer = new StringBuffer();
209: int depth = 0;
210: for (int i = 0; i < tokenCount; i++) {
211: String token = stok.nextToken();
212: switch (i) {
213: case 0:
214: break;
215: case 1:
216: depth = parseInteger(token);
217: break;
218: case 2:
219: pluginId = token;
220: break;
221: case 3:
222: if (byFrameWork) {
223: if (dateBuffer.length() > 0)
224: dateBuffer.append(" "); //$NON-NLS-1$
225: dateBuffer.append(token);
226: } else {
227: severity = parseInteger(token);
228: }
229: break;
230: case 4:
231: if (byFrameWork) {
232: if (dateBuffer.length() > 0)
233: dateBuffer.append(" "); //$NON-NLS-1$
234: dateBuffer.append(token);
235: } else
236: code = parseInteger(token);
237: break;
238: default:
239: if (dateBuffer.length() > 0)
240: dateBuffer.append(" "); //$NON-NLS-1$
241: dateBuffer.append(token);
242: }
243: }
244: DateFormat formatter = new SimpleDateFormat(F_DATE_FORMAT);
245: try {
246: Date date = formatter.parse(dateBuffer.toString());
247: if (date != null)
248: fDate = date;
249: } catch (ParseException e) {
250: }
251: return depth;
252: }
253:
254: private int parseInteger(String token) {
255: try {
256: return Integer.parseInt(token);
257: } catch (NumberFormatException e) {
258: return 0;
259: }
260: }
261:
262: void setStack(String stack) {
263: this .stack = stack;
264: }
265:
266: void setMessage(String message) {
267: this .message = message;
268: }
269:
270: private void processStatus(IStatus status) {
271: pluginId = status.getPlugin();
272: severity = status.getSeverity();
273: code = status.getCode();
274: fDate = new Date();
275: message = status.getMessage();
276: Throwable throwable = status.getException();
277: if (throwable != null) {
278: StringWriter swriter = new StringWriter();
279: PrintWriter pwriter = new PrintWriter(swriter);
280: throwable.printStackTrace(pwriter);
281: pwriter.flush();
282: pwriter.close();
283: stack = swriter.toString();
284: }
285: IStatus[] schildren = status.getChildren();
286: if (schildren.length > 0) {
287: children = new ArrayList();
288: for (int i = 0; i < schildren.length; i++) {
289: LogEntry child = new LogEntry(schildren[i]);
290: addChild(child);
291: }
292: }
293: }
294:
295: void addChild(LogEntry child) {
296: if (children == null)
297: children = new ArrayList();
298: children.add(child);
299: child.setParent(this );
300: }
301:
302: public void write(PrintWriter writer) {
303: if (session != null)
304: writer.println(session.getSessionData());
305: writer.println(getSeverityText());
306: if (fDate != null)
307: writer.println(getDate());
308:
309: if (message != null)
310: writer.println(getMessage());
311:
312: if (stack != null) {
313: writer.println();
314: writer.println(stack);
315: }
316: }
317: }
|