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.BufferedReader;
|
14
|
import java.io.File;
|
15
|
import java.io.FileNotFoundException;
|
16
|
import java.io.IOException;
|
17
|
import java.io.InputStreamReader;
|
18
|
import java.io.PrintWriter;
|
19
|
import java.io.StringWriter;
|
20
|
import java.util.ArrayList;
|
21
|
import java.util.Date;
|
22
|
|
23
|
import org.eclipse.ui.IMemento;
|
24
|
|
25
|
class LogReader {
|
26
|
private static final int SESSION_STATE = 10;
|
27
|
public static final long MAX_FILE_LENGTH = 1024*1024;
|
28
|
private static final int ENTRY_STATE = 20;
|
29
|
private static final int SUBENTRY_STATE = 30;
|
30
|
private static final int MESSAGE_STATE = 40;
|
31
|
private static final int STACK_STATE = 50;
|
32
|
private static final int TEXT_STATE = 60;
|
33
|
private static final int UNKNOWN_STATE = 70;
|
34
|
|
35
|
private static LogSession currentSession;
|
36
|
|
37
|
public static void parseLogFile(File file, ArrayList entries, IMemento memento) {
|
38
|
ArrayList parents = new ArrayList();
|
39
|
LogEntry current = null;
|
40
|
LogSession session = null;
|
41
|
int writerState = UNKNOWN_STATE;
|
42
|
StringWriter swriter = null;
|
43
|
PrintWriter writer = null;
|
44
|
int state = UNKNOWN_STATE;
|
45
|
currentSession = null;
|
46
|
BufferedReader reader = null;
|
47
|
try {
|
48
|
|
49
|
reader = new BufferedReader(new InputStreamReader(
|
50
|
new TailInputStream(file, MAX_FILE_LENGTH), "UTF-8")); //$NON-NLS-1$
|
51
|
for (;;) {
|
52
|
String line = reader.readLine();
|
53
|
if (line == null)
|
54
|
break;
|
55
|
line = line.trim();
|
56
|
if (line.length() == 0)
|
57
|
continue;
|
58
|
|
59
|
if (line.startsWith("!SESSION")) { //$NON-NLS-1$
|
60
|
state = SESSION_STATE;
|
61
|
} else if (line.startsWith("!ENTRY")) { //$NON-NLS-1$
|
62
|
state = ENTRY_STATE;
|
63
|
} else if (line.startsWith("!SUBENTRY")) { //$NON-NLS-1$
|
64
|
state = SUBENTRY_STATE;
|
65
|
} else if (line.startsWith("!MESSAGE")) { //$NON-NLS-1$
|
66
|
state = MESSAGE_STATE;
|
67
|
} else if (line.startsWith("!STACK")) { //$NON-NLS-1$
|
68
|
state = STACK_STATE;
|
69
|
} else
|
70
|
state = TEXT_STATE;
|
71
|
|
72
|
if (state == TEXT_STATE) {
|
73
|
if (writer != null)
|
74
|
writer.println(line);
|
75
|
continue;
|
76
|
}
|
77
|
|
78
|
if (writer != null) {
|
79
|
if (writerState == STACK_STATE && current != null) {
|
80
|
current.setStack(swriter.toString());
|
81
|
} else if (writerState == SESSION_STATE && session != null) {
|
82
|
session.setSessionData(swriter.toString());
|
83
|
} else if (writerState == MESSAGE_STATE && current != null){
|
84
|
StringBuffer sb = new StringBuffer(current.getMessage());
|
85
|
sb.append(swriter.toString());
|
86
|
current.setMessage(sb.toString().trim());
|
87
|
}
|
88
|
writerState = UNKNOWN_STATE;
|
89
|
swriter = null;
|
90
|
writer.close();
|
91
|
writer = null;
|
92
|
}
|
93
|
|
94
|
if (state == STACK_STATE) {
|
95
|
swriter = new StringWriter();
|
96
|
writer = new PrintWriter(swriter, true);
|
97
|
writerState = STACK_STATE;
|
98
|
} else if (state == SESSION_STATE) {
|
99
|
session = new LogSession();
|
100
|
session.processLogLine(line);
|
101
|
swriter = new StringWriter();
|
102
|
writer = new PrintWriter(swriter, true);
|
103
|
writerState = SESSION_STATE;
|
104
|
updateCurrentSession(session);
|
105
|
if (!currentSession.equals(session) /*&& !memento.getString(LogView.P_SHOW_ALL_SESSIONS).equals("true")*/) //$NON-NLS-1$
|
106
|
entries.clear();
|
107
|
} else if (state == ENTRY_STATE) {
|
108
|
LogEntry entry = new LogEntry();
|
109
|
entry.setSession(session);
|
110
|
entry.processEntry(line);
|
111
|
setNewParent(parents, entry, 0);
|
112
|
current = entry;
|
113
|
addEntry(current, entries, memento, false);
|
114
|
} else if (state == SUBENTRY_STATE) {
|
115
|
if (parents.size() > 0) {
|
116
|
LogEntry entry = new LogEntry();
|
117
|
entry.setSession(session);
|
118
|
int depth = entry.processSubEntry(line);
|
119
|
setNewParent(parents, entry, depth);
|
120
|
current = entry;
|
121
|
LogEntry parent = (LogEntry) parents.get(depth - 1);
|
122
|
parent.addChild(entry);
|
123
|
}
|
124
|
} else if (state == MESSAGE_STATE) {
|
125
|
swriter = new StringWriter();
|
126
|
writer = new PrintWriter(swriter, true);
|
127
|
String message = ""; //$NON-NLS-1$
|
128
|
if (line.length() > 8)
|
129
|
message = line.substring(9).trim();
|
130
|
message = message.trim();
|
131
|
if (current != null)
|
132
|
current.setMessage(message);
|
133
|
writerState = MESSAGE_STATE;
|
134
|
}
|
135
|
}
|
136
|
|
137
|
if (swriter != null && current != null && writerState == STACK_STATE)
|
138
|
current.setStack(swriter.toString());
|
139
|
} catch (FileNotFoundException e) {
|
140
|
} catch (IOException e) {
|
141
|
} finally {
|
142
|
try {
|
143
|
if (reader != null)
|
144
|
reader.close();
|
145
|
} catch (IOException e1) {
|
146
|
}
|
147
|
if (writer != null)
|
148
|
writer.close();
|
149
|
}
|
150
|
}
|
151
|
|
152
|
private static void updateCurrentSession(LogSession session) {
|
153
|
if (currentSession == null) {
|
154
|
currentSession = session;
|
155
|
return;
|
156
|
}
|
157
|
Date currentDate = currentSession.getDate();
|
158
|
Date sessionDate = session.getDate();
|
159
|
if (currentDate == null && sessionDate != null)
|
160
|
currentSession = session;
|
161
|
else if (currentDate != null && sessionDate == null)
|
162
|
currentSession = session;
|
163
|
else if (currentDate != null && sessionDate != null && sessionDate.after(currentDate))
|
164
|
currentSession = session;
|
165
|
}
|
166
|
|
167
|
public synchronized static void addEntry(LogEntry current, ArrayList entries, IMemento memento, boolean useCurrentSession) {
|
168
|
int severity = current.getSeverity();
|
169
|
boolean doAdd = true;
|
170
|
// switch(severity) {
|
171
|
// case IStatus.INFO:
|
172
|
// doAdd = memento.getString(LogView.P_LOG_INFO).equals("true"); //$NON-NLS-1$
|
173
|
// break;
|
174
|
// case IStatus.WARNING:
|
175
|
// doAdd = memento.getString(LogView.P_LOG_WARNING).equals("true"); //$NON-NLS-1$
|
176
|
// break;
|
177
|
// case IStatus.ERROR:
|
178
|
// doAdd = memento.getString(LogView.P_LOG_ERROR).equals("true"); //$NON-NLS-1$
|
179
|
// break;
|
180
|
// }
|
181
|
if (doAdd) {
|
182
|
if (useCurrentSession)
|
183
|
current.setSession(currentSession);
|
184
|
entries.add(0, current);
|
185
|
|
186
|
// if (memento.getString(LogView.P_USE_LIMIT).equals("true") //$NON-NLS-1$
|
187
|
// && entries.size() > memento.getInteger(LogView.P_LOG_LIMIT).intValue())
|
188
|
// entries.remove(entries.size() - 1);
|
189
|
}
|
190
|
}
|
191
|
|
192
|
private static void setNewParent(
|
193
|
ArrayList parents,
|
194
|
LogEntry entry,
|
195
|
int depth) {
|
196
|
if (depth + 1 > parents.size())
|
197
|
parents.add(entry);
|
198
|
else
|
199
|
parents.set(depth, entry);
|
200
|
}
|
201
|
|
202
|
public static void reset() {
|
203
|
currentSession = null;
|
204
|
}
|
205
|
}
|