b4922404bb37ff2a5ba62cff331d96d21ea87ea5
[phpeclipse.git] /
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
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpeclipse.xdebug.ui.views.logview;
12
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;
20
21 import net.sourceforge.phpeclipse.xdebug.ui.XDebugUIPlugin;
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.ui.model.IWorkbenchAdapter;
27
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;
35         private int severity;
36         private int code;
37         private String date;
38         private String message;
39         private String stack;
40         private LogSession session;
41
42         public LogEntry() {
43         }
44
45         public LogSession getSession() {
46                 return session;
47         }
48
49         void setSession(LogSession session) {
50                 this.session = session;
51         }
52
53         public LogEntry(IStatus status) {
54                 processStatus(status);
55         }
56         public int getSeverity() {
57                 return severity;
58         }
59
60         public boolean isOK() {
61                 return severity == IStatus.OK;
62         }
63         public int getCode() {
64                 return code;
65         }
66         public String getPluginId() {
67                 return pluginId;
68         }
69         public String getMessage() {
70                 return message;
71         }
72         public String getStack() {
73                 return stack;
74         }
75         public String getDate() {
76                 return date;
77         }
78         public String getSeverityText() {
79                 return getSeverityText(severity);
80         }
81         public boolean hasChildren() {
82                 return children != null && children.size() > 0;
83         }
84         public String toString() {
85                 return getSeverityText();
86         }
87         /**
88          * @see IWorkbenchAdapter#getChildren(Object)
89          */
90         public Object[] getChildren(Object parent) {
91                 if (children == null)
92                         return new Object[0];
93                 return children.toArray();
94         }
95
96         /**
97          * @see IWorkbenchAdapter#getImageDescriptor(Object)
98          */
99         public ImageDescriptor getImageDescriptor(Object arg0) {
100                 return null;
101         }
102
103         /**
104          * @see IWorkbenchAdapter#getLabel(Object)
105          */
106         public String getLabel(Object obj) {
107                 return getSeverityText();
108         }
109
110         /**
111          * @see IWorkbenchAdapter#getParent(Object)
112          */
113         public Object getParent(Object obj) {
114                 return parent;
115         }
116
117         void setParent(LogEntry parent) {
118                 this.parent = parent;
119         }
120
121         private String getSeverityText(int severity) {
122                 switch (severity) {
123                         case IStatus.ERROR :
124                                 return XDebugUIPlugin.getString(KEY_ERROR);
125                         case IStatus.WARNING :
126                                 return XDebugUIPlugin.getString(KEY_WARNING);
127                         case IStatus.INFO :
128                                 return XDebugUIPlugin.getString(KEY_INFO);
129                 }
130                 return "?"; //$NON-NLS-1$
131         }
132
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();
138
139                 int dateCount = 5;
140                 int depth = 0;
141                 for (int i = 0; stok.hasMoreTokens();) {
142                         String token = stok.nextToken();
143                         if (i >= dateCount) {
144                                 dateBuffer.append(token);
145                                 continue;
146                         } else if (token.equals(" ")) //$NON-NLS-1$
147                                 continue;
148                         switch (i) {
149                                 case 0 : // entry or subentry
150                                         if (root)
151                                                 i += 2;
152                                         else
153                                                 i++;
154                                         break;
155                                 case 1 : // depth
156                                         depth = parseInteger(token);
157                                         i++;
158                                         break;
159                                 case 2 :
160                                         pluginId = token;
161                                         i++;
162                                         break;
163                                 case 3 : // severity
164                                         severity = parseInteger(token);
165                                         i++;
166                                         break;
167                                 case 4 : // code
168                                         code = parseInteger(token);
169                                         i++;
170                                         break;
171                         }
172                 }
173                 date = dateBuffer.toString().trim();
174                 return depth;
175         }
176
177         private int parseInteger(String token) {
178                 try {
179                         return Integer.parseInt(token);
180                 } catch (NumberFormatException e) {
181                         return 0;
182                 }
183         }
184
185         void setStack(String stack) {
186                 this.stack = stack;
187         }
188         void setMessage(String message) {
189                 this.message = message;
190         }
191
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);
204                         pwriter.flush();
205                         pwriter.close();
206                         stack = swriter.toString();
207                 }
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]);
213                                 addChild(child);
214                         }
215                 }
216         }
217         void addChild(LogEntry child) {
218                 if (children == null)
219                         children = new ArrayList();
220                 children.add(child);
221                 child.setParent(this);
222         }
223         public void write(PrintWriter writer) {
224                 writer.print(getSeverityText());
225                 if (date != null) {
226                         writer.print(" "); //$NON-NLS-1$
227                         writer.print(getDate());
228                 }
229                 if (message != null) {
230                         writer.print(" "); //$NON-NLS-1$
231                         writer.print(getMessage());
232                 }
233                 writer.println();
234                 if (stack != null)
235                         writer.println(stack);
236         }
237 }