1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
\r
3 * in Industry THTH ry.
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.message.ui;
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;
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;
25 import com.ibm.icu.text.SimpleDateFormat;
27 public class LogEntry extends AbstractEntry {
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);
32 private String pluginId;
35 private String fDateString;
37 private String message;
38 private String detailedDescription;
40 private LogSession session;
42 public LogEntry() { // do nothing
45 public LogSession getSession() {
46 if ((session == null) && (parent != null) && (parent instanceof LogEntry))
47 return ((LogEntry) parent).getSession();
52 void setSession(LogSession session) {
53 this.session = session;
56 public LogEntry(IStatus status) {
57 processStatus(status);
60 public int getSeverity() {
64 public boolean isOK() {
65 return severity == IStatus.OK;
68 public int getCode() {
72 public String getPluginId() {
76 public String getMessage() {
80 public String getDetailedDescription() {
81 return detailedDescription;
84 public String getStack() {
88 public String getFormattedDate() {
89 if (fDateString == null)
90 fDateString = F_SDF.format(getDate());
94 public Date getDate() {
96 fDate = new Date(0); // unknown date - return epoch
100 public String getSeverityText() {
101 return getSeverityText(severity);
104 public String toString() {
105 return getSeverityText();
109 * @see IWorkbenchAdapter#getImageDescriptor(Object)
111 public ImageDescriptor getImageDescriptor(Object arg0) {
116 * @see IWorkbenchAdapter#getLabel(Object)
118 public String getLabel(Object obj) {
119 return getSeverityText();
122 private String getSeverityText(int severity) {
124 case IDetailStatus.DEBUG :
125 return Messages.LogView_severity_debug;
127 return Messages.LogView_severity_error;
128 case IStatus.WARNING :
129 return Messages.LogView_severity_warning;
131 return Messages.LogView_severity_info;
133 return Messages.LogView_severity_ok;
135 return "?"; //$NON-NLS-1$
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;
145 // no severity means it should be represented as OK
150 StringBuffer dateBuffer = new StringBuffer();
151 for (int i = 0; i < tokenCount; i++) {
152 String token = stok.nextToken();
161 if (dateBuffer.length() > 0)
162 dateBuffer.append(" "); //$NON-NLS-1$
163 dateBuffer.append(token);
165 severity = parseInteger(token);
170 if (dateBuffer.length() > 0)
171 dateBuffer.append(" "); //$NON-NLS-1$
172 dateBuffer.append(token);
174 code = parseInteger(token);
177 if (dateBuffer.length() > 0)
178 dateBuffer.append(" "); //$NON-NLS-1$
179 dateBuffer.append(token);
183 Date date = F_SDF.parse(dateBuffer.toString());
186 fDateString = F_SDF.format(fDate);
188 } catch (ParseException e) { // do nothing
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;
199 StringBuffer dateBuffer = new StringBuffer();
201 for (int i = 0; i < tokenCount; i++) {
202 String token = stok.nextToken();
207 depth = parseInteger(token);
214 if (dateBuffer.length() > 0)
215 dateBuffer.append(" "); //$NON-NLS-1$
216 dateBuffer.append(token);
218 severity = parseInteger(token);
223 if (dateBuffer.length() > 0)
224 dateBuffer.append(" "); //$NON-NLS-1$
225 dateBuffer.append(token);
227 code = parseInteger(token);
230 if (dateBuffer.length() > 0)
231 dateBuffer.append(" "); //$NON-NLS-1$
232 dateBuffer.append(token);
236 Date date = F_SDF.parse(dateBuffer.toString());
239 fDateString = F_SDF.format(fDate);
241 } catch (ParseException e) { // do nothing
246 private int parseInteger(String token) {
248 return Integer.parseInt(token);
249 } catch (NumberFormatException e) {
254 void setStack(String stack) {
258 void setMessage(String message) {
259 this.message = message;
262 void setDetailedDescription(String detailedDescription) {
263 this.detailedDescription = detailedDescription;
266 private void processStatus(IStatus status) {
267 pluginId = status.getPlugin();
268 severity = status.getSeverity();
269 code = status.getCode();
271 fDateString = F_SDF.format(fDate);
272 message = status.getMessage();
273 if (status instanceof IDetailStatus) {
274 IDetailStatus ds = (IDetailStatus) status;
275 detailedDescription = ds.getDetailedDescription();
277 Throwable throwable = status.getException();
278 if (throwable != null) {
279 StringWriter swriter = new StringWriter();
280 PrintWriter pwriter = new PrintWriter(swriter);
281 throwable.printStackTrace(pwriter);
284 stack = swriter.toString();
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]);
295 public void write(PrintWriter writer) {
297 writer.println(session.getSessionData());
298 writer.println(getSeverityText());
300 writer.println(getDate());
303 writer.println(getMessage());
307 writer.println(stack);