1 /*******************************************************************************
2 * Copyright (c) 2000, 2004 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Common Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/cpl-v10.html
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpeclipse.xdebug.ui.views.logview;
13 import java.io.PrintWriter;
14 import java.io.StringWriter;
15 import java.text.DateFormat;
16 import java.text.SimpleDateFormat;
17 import java.util.ArrayList;
18 import java.util.Date;
19 import java.util.StringTokenizer;
21 import net.sourceforge.phpeclipse.xdebug.ui.XDebugUIPlugin;
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.ui.model.IWorkbenchAdapter;
28 public class LogEntry extends PlatformObject implements IWorkbenchAdapter {
29 private static final String KEY_ERROR = "LogView.severity.error"; //$NON-NLS-1$
30 private static final String KEY_WARNING = "LogView.severity.warning"; //$NON-NLS-1$
31 private static final String KEY_INFO = "LogView.severity.info"; //$NON-NLS-1$
32 private ArrayList children;
33 private LogEntry parent;
34 private String pluginId;
38 private String message;
40 private LogSession session;
45 public LogSession getSession() {
49 void setSession(LogSession session) {
50 this.session = session;
53 public LogEntry(IStatus status) {
54 processStatus(status);
56 public int getSeverity() {
60 public boolean isOK() {
61 return severity == IStatus.OK;
63 public int getCode() {
66 public String getPluginId() {
69 public String getMessage() {
72 public String getStack() {
75 public String getDate() {
78 public String getSeverityText() {
79 return getSeverityText(severity);
81 public boolean hasChildren() {
82 return children != null && children.size() > 0;
84 public String toString() {
85 return getSeverityText();
88 * @see IWorkbenchAdapter#getChildren(Object)
90 public Object[] getChildren(Object parent) {
93 return children.toArray();
97 * @see IWorkbenchAdapter#getImageDescriptor(Object)
99 public ImageDescriptor getImageDescriptor(Object arg0) {
104 * @see IWorkbenchAdapter#getLabel(Object)
106 public String getLabel(Object obj) {
107 return getSeverityText();
111 * @see IWorkbenchAdapter#getParent(Object)
113 public Object getParent(Object obj) {
117 void setParent(LogEntry parent) {
118 this.parent = parent;
121 private String getSeverityText(int severity) {
124 return XDebugUIPlugin.getString(KEY_ERROR);
125 case IStatus.WARNING :
126 return XDebugUIPlugin.getString(KEY_WARNING);
128 return XDebugUIPlugin.getString(KEY_INFO);
130 return "?"; //$NON-NLS-1$
133 int processLogLine(String line, boolean root) {
134 //!ENTRY <pluginID> <severity> <code> <date>
135 //!SUBENTRY <depth> <pluginID> <severity> <code> <date>
136 StringTokenizer stok = new StringTokenizer(line, " ", true); //$NON-NLS-1$
137 StringBuffer dateBuffer = new StringBuffer();
141 for (int i = 0; stok.hasMoreTokens();) {
142 String token = stok.nextToken();
143 if (i >= dateCount) {
144 dateBuffer.append(token);
146 } else if (token.equals(" ")) //$NON-NLS-1$
149 case 0 : // entry or subentry
156 depth = parseInteger(token);
164 severity = parseInteger(token);
168 code = parseInteger(token);
173 date = dateBuffer.toString().trim();
177 private int parseInteger(String token) {
179 return Integer.parseInt(token);
180 } catch (NumberFormatException e) {
185 void setStack(String stack) {
188 void setMessage(String message) {
189 this.message = message;
192 private void processStatus(IStatus status) {
193 pluginId = status.getPlugin();
194 severity = status.getSeverity();
195 code = status.getCode();
196 DateFormat formatter = new SimpleDateFormat("MMM dd, yyyy HH:mm:ss.SS"); //$NON-NLS-1$
197 date = formatter.format(new Date());
198 message = status.getMessage();
199 Throwable throwable = status.getException();
200 if (throwable != null) {
201 StringWriter swriter = new StringWriter();
202 PrintWriter pwriter = new PrintWriter(swriter);
203 throwable.printStackTrace(pwriter);
206 stack = swriter.toString();
208 IStatus[] schildren = status.getChildren();
209 if (schildren.length > 0) {
210 children = new ArrayList();
211 for (int i = 0; i < schildren.length; i++) {
212 LogEntry child = new LogEntry(schildren[i]);
217 void addChild(LogEntry child) {
218 if (children == null)
219 children = new ArrayList();
221 child.setParent(this);
223 public void write(PrintWriter writer) {
224 writer.print(getSeverityText());
226 writer.print(" "); //$NON-NLS-1$
227 writer.print(getDate());
229 if (message != null) {
230 writer.print(" "); //$NON-NLS-1$
231 writer.print(getMessage());
235 writer.println(stack);