]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.message.ui/src/org/simantics/message/ui/LogEntry.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.message.ui / src / org / simantics / message / ui / LogEntry.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.message.ui;
13
14 import java.io.PrintWriter;
15 import java.io.StringWriter;
16 import java.text.ParseException;
17 import java.util.Date;
18 import java.util.StringTokenizer;
19
20 import org.eclipse.core.runtime.IStatus;
21 import org.eclipse.jface.resource.ImageDescriptor;
22 import org.eclipse.ui.model.IWorkbenchAdapter;
23 import org.simantics.message.IDetailStatus;
24
25 import com.ibm.icu.text.SimpleDateFormat;
26
27 public class LogEntry extends AbstractEntry {
28
29         public static final String F_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss.SSS"; //$NON-NLS-1$
30         private static final SimpleDateFormat F_SDF = new SimpleDateFormat(F_DATE_FORMAT);
31
32         private String pluginId;
33         private int severity;
34         private int code;
35         private String fDateString;
36         private Date fDate;
37         private String message;
38         private String detailedDescription;
39         private String stack;
40         private LogSession session;
41
42         public LogEntry() { // do nothing
43         }
44
45         public LogSession getSession() {
46                 if ((session == null) && (parent != null) && (parent instanceof LogEntry))
47                         return ((LogEntry) parent).getSession();
48
49                 return session;
50         }
51
52         void setSession(LogSession session) {
53                 this.session = session;
54         }
55
56         public LogEntry(IStatus status) {
57                 processStatus(status);
58         }
59
60         public int getSeverity() {
61                 return severity;
62         }
63
64         public boolean isOK() {
65                 return severity == IStatus.OK;
66         }
67
68         public int getCode() {
69                 return code;
70         }
71
72         public String getPluginId() {
73                 return pluginId;
74         }
75
76         public String getMessage() {
77                 return message;
78         }
79
80         public String getDetailedDescription() {
81             return detailedDescription;
82         }
83
84         public String getStack() {
85                 return stack;
86         }
87
88         public String getFormattedDate() {
89                 if (fDateString == null)
90                         fDateString = F_SDF.format(getDate());
91                 return fDateString;
92         }
93
94         public Date getDate() {
95                 if (fDate == null)
96                         fDate = new Date(0); // unknown date - return epoch
97                 return fDate;
98         }
99
100         public String getSeverityText() {
101                 return getSeverityText(severity);
102         }
103
104         public String toString() {
105                 return getSeverityText();
106         }
107
108         /**
109          * @see IWorkbenchAdapter#getImageDescriptor(Object)
110          */
111         public ImageDescriptor getImageDescriptor(Object arg0) {
112                 return null;
113         }
114
115         /**
116          * @see IWorkbenchAdapter#getLabel(Object)
117          */
118         public String getLabel(Object obj) {
119                 return getSeverityText();
120         }
121
122         private String getSeverityText(int severity) {
123                 switch (severity) {
124             case IDetailStatus.DEBUG :
125                 return Messages.LogView_severity_debug;
126             case IStatus.ERROR :
127                 return Messages.LogView_severity_error;
128                         case IStatus.WARNING :
129                                 return Messages.LogView_severity_warning;
130                         case IStatus.INFO :
131                                 return Messages.LogView_severity_info;
132                         case IStatus.OK :
133                                 return Messages.LogView_severity_ok;
134                 }
135                 return "?"; //$NON-NLS-1$
136         }
137
138         void processEntry(String line) {
139                 //!ENTRY <pluginID> <severity> <code> <date>
140                 //!ENTRY <pluginID> <date> if logged by the framework!!!
141                 StringTokenizer stok = new StringTokenizer(line, " "); //$NON-NLS-1$
142                 int tokenCount = stok.countTokens();
143                 boolean noSeverity = stok.countTokens() < 5;
144
145                 // no severity means it should be represented as OK
146                 if (noSeverity) {
147                         severity = 0;
148                         code = 0;
149                 }
150                 StringBuffer dateBuffer = new StringBuffer();
151                 for (int i = 0; i < tokenCount; i++) {
152                         String token = stok.nextToken();
153                         switch (i) {
154                                 case 0 :
155                                         break;
156                                 case 1 :
157                                         pluginId = token;
158                                         break;
159                                 case 2 :
160                                         if (noSeverity) {
161                                                 if (dateBuffer.length() > 0)
162                                                         dateBuffer.append(" "); //$NON-NLS-1$
163                                                 dateBuffer.append(token);
164                                         } else {
165                                                 severity = parseInteger(token);
166                                         }
167                                         break;
168                                 case 3 :
169                                         if (noSeverity) {
170                                                 if (dateBuffer.length() > 0)
171                                                         dateBuffer.append(" "); //$NON-NLS-1$
172                                                 dateBuffer.append(token);
173                                         } else
174                                                 code = parseInteger(token);
175                                         break;
176                                 default :
177                                         if (dateBuffer.length() > 0)
178                                                 dateBuffer.append(" "); //$NON-NLS-1$
179                                         dateBuffer.append(token);
180                         }
181                 }
182                 try {
183                         Date date = F_SDF.parse(dateBuffer.toString());
184                         if (date != null) {
185                                 fDate = date;
186                                 fDateString = F_SDF.format(fDate);
187                         }
188                 } catch (ParseException e) { // do nothing
189                 }
190         }
191
192         int processSubEntry(String line) {
193                 //!SUBENTRY <depth> <pluginID> <severity> <code> <date>
194                 //!SUBENTRY  <depth> <pluginID> <date>if logged by the framework!!!
195                 StringTokenizer stok = new StringTokenizer(line, " "); //$NON-NLS-1$
196                 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(" "); //$NON-NLS-1$
216                                                 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(" "); //$NON-NLS-1$
225                                                 dateBuffer.append(token);
226                                         } else
227                                                 code = parseInteger(token);
228                                         break;
229                                 default :
230                                         if (dateBuffer.length() > 0)
231                                                 dateBuffer.append(" "); //$NON-NLS-1$
232                                         dateBuffer.append(token);
233                         }
234                 }
235                 try {
236                         Date date = F_SDF.parse(dateBuffer.toString());
237                         if (date != null) {
238                                 fDate = date;
239                                 fDateString = F_SDF.format(fDate);
240                         }
241                 } catch (ParseException e) { // do nothing
242                 }
243                 return depth;
244         }
245
246         private int parseInteger(String token) {
247                 try {
248                         return Integer.parseInt(token);
249                 } catch (NumberFormatException e) {
250                         return 0;
251                 }
252         }
253
254         void setStack(String stack) {
255                 this.stack = stack;
256         }
257
258     void setMessage(String message) {
259         this.message = message;
260     }
261
262     void setDetailedDescription(String detailedDescription) {
263         this.detailedDescription = detailedDescription;
264     }
265
266         private void processStatus(IStatus status) {
267                 pluginId = status.getPlugin();
268                 severity = status.getSeverity();
269                 code = status.getCode();
270                 fDate = new Date();
271                 fDateString = F_SDF.format(fDate);
272                 message = status.getMessage();
273                 if (status instanceof IDetailStatus) {
274                     IDetailStatus ds = (IDetailStatus) status;
275                     detailedDescription = ds.getDetailedDescription();
276                 }
277                 Throwable throwable = status.getException();
278                 if (throwable != null) {
279                         StringWriter swriter = new StringWriter();
280                         PrintWriter pwriter = new PrintWriter(swriter);
281                         throwable.printStackTrace(pwriter);
282                         pwriter.flush();
283                         pwriter.close();
284                         stack = swriter.toString();
285                 }
286                 IStatus[] schildren = status.getChildren();
287                 if (schildren.length > 0) {
288                         for (int i = 0; i < schildren.length; i++) {
289                                 LogEntry child = new LogEntry(schildren[i]);
290                                 addChild(child);
291                         }
292                 }
293         }
294
295         public void write(PrintWriter writer) {
296                 if (session != null)
297                         writer.println(session.getSessionData());
298                 writer.println(getSeverityText());
299                 if (fDate != null)
300                         writer.println(getDate());
301
302                 if (message != null)
303                         writer.println(getMessage());
304
305                 if (stack != null) {
306                         writer.println();
307                         writer.println(stack);
308                 }
309         }
310 }