Project

General

Profile

Download (7.44 KB) Statistics
| Branch: | Tag: | Revision:
1
/*******************************************************************************
2
 * Copyright (c) 2000, 2006 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
10
 *******************************************************************************/
11
package org.eclipse.pde.internal.runtime.logview;
12

    
13
import java.io.PrintWriter;
14
import java.io.StringWriter;
15
import java.text.DateFormat;
16
import java.text.ParseException;
17
import java.text.SimpleDateFormat;
18
import java.util.ArrayList;
19
import java.util.Date;
20
import java.util.StringTokenizer;
21

    
22
import org.eclipse.core.runtime.IStatus;
23
import org.eclipse.core.runtime.PlatformObject;
24
import org.eclipse.jface.resource.ImageDescriptor;
25
import org.eclipse.pde.internal.runtime.PDERuntimeMessages;
26
import org.eclipse.ui.model.IWorkbenchAdapter;
27

    
28
public class LogEntry extends PlatformObject implements IWorkbenchAdapter {
29
	
30
	public static final String F_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss.SSS"; //$NON-NLS-1$
31
	
32
	private ArrayList children;
33
	private LogEntry parent;
34
	private String pluginId;
35
	private int severity;
36
	private int code;
37
	private Date fDate;
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 Date getDate() {
76
		if (fDate == null)
77
			fDate = new Date(0); // unknown date - return epoch
78
		return fDate;
79
	}
80
	public String getSeverityText() {
81
		return getSeverityText(severity);
82
	}
83
	public boolean hasChildren() {
84
		return children != null && children.size() > 0;
85
	}
86
	public String toString() {
87
		return getSeverityText();
88
	}
89
	/**
90
	 * @see IWorkbenchAdapter#getChildren(Object)
91
	 */
92
	public Object[] getChildren(Object parent) {
93
		if (children == null)
94
			return new Object[0];
95
		return children.toArray();
96
	}
97

    
98
	/**
99
	 * @see IWorkbenchAdapter#getImageDescriptor(Object)
100
	 */
101
	public ImageDescriptor getImageDescriptor(Object arg0) {
102
		return null;
103
	}
104

    
105
	/**
106
	 * @see IWorkbenchAdapter#getLabel(Object)
107
	 */
108
	public String getLabel(Object obj) {
109
		return getSeverityText();
110
	}
111

    
112
	/**
113
	 * @see IWorkbenchAdapter#getParent(Object)
114
	 */
115
	public Object getParent(Object obj) {
116
		return parent;
117
	}
118

    
119
	void setParent(LogEntry parent) {
120
		this.parent = parent;
121
	}
122

    
123
	private String getSeverityText(int severity) {
124
		switch (severity) {
125
			case IStatus.ERROR :
126
				return PDERuntimeMessages.get().LogView_severity_error;
127
			case IStatus.WARNING :
128
				return PDERuntimeMessages.get().LogView_severity_warning;
129
			case IStatus.INFO :
130
				return PDERuntimeMessages.get().LogView_severity_info;
131
			case IStatus.OK :
132
				return PDERuntimeMessages.get().LogView_severity_ok;
133
		}
134
		return "?"; //$NON-NLS-1$
135
	}
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
		DateFormat formatter = new SimpleDateFormat(F_DATE_FORMAT);
183
		try {
184
			Date date = formatter.parse(dateBuffer.toString());
185
			if (date != null)
186
				fDate = date; 
187
		} catch (ParseException e) {
188
		}
189
	}
190
	
191
	int processSubEntry(String line) {
192
		//!SUBENTRY <depth> <pluginID> <severity> <code> <date>
193
		//!SUBENTRY  <depth> <pluginID> <date>if logged by the framework!!!
194
		StringTokenizer stok = new StringTokenizer(line, " "); //$NON-NLS-1$
195
		int tokenCount = stok.countTokens();		
196
		boolean byFrameWork = stok.countTokens() < 5;
197
		
198
		StringBuffer dateBuffer = new StringBuffer();
199
		int depth = 0;
200
		for (int i = 0; i < tokenCount; i++) {
201
			String token = stok.nextToken();
202
			switch (i) {
203
				case 0:
204
					break;
205
				case 1:
206
					depth = parseInteger(token);
207
					break;
208
				case 2:
209
					pluginId = token;
210
					break;
211
				case 3:
212
					if (byFrameWork) {
213
						if (dateBuffer.length() > 0)
214
							dateBuffer.append(" "); //$NON-NLS-1$
215
						dateBuffer.append(token);
216
					} else {
217
						severity = parseInteger(token);
218
					}
219
					break;
220
				case 4:
221
					if (byFrameWork) {
222
						if (dateBuffer.length() > 0)
223
							dateBuffer.append(" "); //$NON-NLS-1$
224
						dateBuffer.append(token);
225
					} else
226
						code = parseInteger(token);
227
					break;
228
				default:
229
					if (dateBuffer.length() > 0)
230
						dateBuffer.append(" "); //$NON-NLS-1$
231
					dateBuffer.append(token);
232
			}
233
		}
234
		DateFormat formatter = new SimpleDateFormat(F_DATE_FORMAT);
235
		try {
236
			Date date = formatter.parse(dateBuffer.toString());
237
			if (date != null)
238
				fDate = date; 
239
		} catch (ParseException e) {
240
		}
241
		return depth;	
242
	}
243
	
244
	private int parseInteger(String token) {
245
		try {
246
			return Integer.parseInt(token);
247
		} catch (NumberFormatException e) {
248
			return 0;
249
		}
250
	}
251

    
252
	void setStack(String stack) {
253
		this.stack = stack;
254
	}
255
	void setMessage(String message) {
256
		this.message = message;
257
	}
258

    
259
	private void processStatus(IStatus status) {
260
		pluginId = status.getPlugin();
261
		severity = status.getSeverity();
262
		code = status.getCode();
263
		fDate = new Date();
264
		message = status.getMessage();
265
		Throwable throwable = status.getException();
266
		if (throwable != null) {
267
			StringWriter swriter = new StringWriter();
268
			PrintWriter pwriter = new PrintWriter(swriter);
269
			throwable.printStackTrace(pwriter);
270
			pwriter.flush();
271
			pwriter.close();
272
			stack = swriter.toString();
273
		}
274
		IStatus[] schildren = status.getChildren();
275
		if (schildren.length > 0) {
276
			children = new ArrayList();
277
			for (int i = 0; i < schildren.length; i++) {
278
				LogEntry child = new LogEntry(schildren[i]);
279
				addChild(child);
280
			}
281
		}
282
	}
283
	void addChild(LogEntry child) {
284
		if (children == null)
285
			children = new ArrayList();
286
		children.add(child);
287
		child.setParent(this);
288
	}
289
	public void write(PrintWriter writer) {
290
		if (session != null)
291
			writer.println(session.getSessionData());
292
		writer.println(getSeverityText());
293
		if (fDate != null)
294
			writer.println(getDate());
295
		
296
		if (message != null)
297
			writer.println(getMessage());
298
	
299
		if (stack != null) {
300
			writer.println();
301
			writer.println(stack);
302
		}
303
	}
304
}
(4-4/12)