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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpeclipse.xdebug.ui.views.logview;
13 import java.io.BufferedReader;
15 import java.io.FileInputStream;
16 import java.io.FileNotFoundException;
17 import java.io.IOException;
18 import java.io.InputStreamReader;
19 import java.io.PrintWriter;
20 import java.io.RandomAccessFile;
21 import java.io.StringWriter;
22 import java.util.ArrayList;
23 import java.util.Date;
25 import org.eclipse.core.runtime.IStatus;
26 import org.eclipse.ui.IMemento;
29 private static final int SESSION_STATE = 10;
30 public static final long MAX_FILE_LENGTH = 1024*1024;
31 private static final int ENTRY_STATE = 20;
32 private static final int SUBENTRY_STATE = 30;
33 private static final int MESSAGE_STATE = 40;
34 private static final int STACK_STATE = 50;
35 private static final int TEXT_STATE = 60;
36 private static final int UNKNOWN_STATE = 70;
38 private static LogSession currentSession;
40 public static void parseLargeFile(File file, ArrayList entries, IMemento memento) {
41 ArrayList parents = new ArrayList();
42 LogEntry current = null;
43 LogSession session = null;
44 int writerState = UNKNOWN_STATE;
45 StringWriter swriter = null;
46 PrintWriter writer = null;
47 int state = UNKNOWN_STATE;
48 currentSession = null;
49 RandomAccessFile random = null;
51 random = new RandomAccessFile(file, "r"); //$NON-NLS-1$
52 random.seek(file.length() - MAX_FILE_LENGTH);
54 String line = random.readLine();
59 if (line.length() == 0)
62 if (line.startsWith("!SESSION")) { //$NON-NLS-1$
63 state = SESSION_STATE;
64 } else if (line.startsWith("!ENTRY")) { //$NON-NLS-1$
66 } else if (line.startsWith("!SUBENTRY")) { //$NON-NLS-1$
67 state = SUBENTRY_STATE;
68 } else if (line.startsWith("!MESSAGE")) { //$NON-NLS-1$
69 state = MESSAGE_STATE;
70 } else if (line.startsWith("!STACK")) { //$NON-NLS-1$
75 if (state == TEXT_STATE) {
82 if (writerState == STACK_STATE && current != null) {
83 current.setStack(swriter.toString());
84 } else if (writerState == SESSION_STATE && session != null) {
85 session.setSessionData(swriter.toString());
86 } else if (writerState == MESSAGE_STATE && current != null){
87 String message = current.getMessage() + swriter.toString();
88 message = message.trim();
89 current.setMessage(message);
91 writerState = UNKNOWN_STATE;
97 if (state == STACK_STATE) {
98 swriter = new StringWriter();
99 writer = new PrintWriter(swriter, true);
100 writerState = STACK_STATE;
101 } else if (state == SESSION_STATE) {
102 session = new LogSession();
103 session.processLogLine(line);
104 swriter = new StringWriter();
105 writer = new PrintWriter(swriter, true);
106 writerState = SESSION_STATE;
107 updateCurrentSession(session);
108 if (currentSession.equals(session) && !memento.getString(LogView.P_SHOW_ALL_SESSIONS).equals("true")) //$NON-NLS-1$
110 } else if (state == ENTRY_STATE) {
111 LogEntry entry = new LogEntry();
112 entry.setSession(session);
113 entry.processLogLine(line, true);
114 setNewParent(parents, entry, 0);
116 addEntry(current, entries, memento, false);
117 } else if (state == SUBENTRY_STATE) {
118 LogEntry entry = new LogEntry();
119 entry.setSession(session);
120 int depth = entry.processLogLine(line, false);
121 setNewParent(parents, entry, depth);
123 LogEntry parent = (LogEntry) parents.get(depth - 1);
124 parent.addChild(entry);
125 } else if (state == MESSAGE_STATE) {
126 swriter = new StringWriter();
127 writer = new PrintWriter(swriter, true);
128 String message = ""; //$NON-NLS-1$
129 if (line.length() > 8)
130 message = line.substring(9).trim();
131 message = message.trim();
133 current.setMessage(message);
134 writerState = MESSAGE_STATE;
137 } catch (FileNotFoundException e) {
138 } catch (IOException e) {
143 } catch (IOException e1) {
148 public static void parseLogFile(File file, ArrayList entries, IMemento memento) {
149 ArrayList parents = new ArrayList();
150 LogEntry current = null;
151 LogSession session = null;
152 int writerState = UNKNOWN_STATE;
153 StringWriter swriter = null;
154 PrintWriter writer = null;
155 int state = UNKNOWN_STATE;
156 currentSession = null;
157 BufferedReader reader = null;
160 reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8")); //$NON-NLS-1$
161 while(reader.ready()) {
162 String line = reader.readLine();
166 if (line.length() == 0)
169 if (line.startsWith("!SESSION")) { //$NON-NLS-1$
170 state = SESSION_STATE;
171 } else if (line.startsWith("!ENTRY")) { //$NON-NLS-1$
173 } else if (line.startsWith("!SUBENTRY")) { //$NON-NLS-1$
174 state = SUBENTRY_STATE;
175 } else if (line.startsWith("!MESSAGE")) { //$NON-NLS-1$
176 state = MESSAGE_STATE;
177 } else if (line.startsWith("!STACK")) { //$NON-NLS-1$
182 if (state == TEXT_STATE) {
184 writer.println(line);
188 if (writer != null) {
189 if (writerState == STACK_STATE && current != null) {
190 current.setStack(swriter.toString());
191 } else if (writerState == SESSION_STATE && session != null) {
192 session.setSessionData(swriter.toString());
193 } else if (writerState == MESSAGE_STATE && current != null){
194 String message = current.getMessage() + swriter.toString();
195 message = message.trim();
196 current.setMessage(message);
198 writerState = UNKNOWN_STATE;
204 if (state == STACK_STATE) {
205 swriter = new StringWriter();
206 writer = new PrintWriter(swriter, true);
207 writerState = STACK_STATE;
208 } else if (state == SESSION_STATE) {
209 session = new LogSession();
210 session.processLogLine(line);
211 swriter = new StringWriter();
212 writer = new PrintWriter(swriter, true);
213 writerState = SESSION_STATE;
214 updateCurrentSession(session);
215 if (currentSession.equals(session) && !memento.getString(LogView.P_SHOW_ALL_SESSIONS).equals("true")) //$NON-NLS-1$
217 } else if (state == ENTRY_STATE) {
218 LogEntry entry = new LogEntry();
219 entry.setSession(session);
220 entry.processLogLine(line, true);
221 setNewParent(parents, entry, 0);
223 addEntry(current, entries, memento, false);
224 } else if (state == SUBENTRY_STATE) {
225 LogEntry entry = new LogEntry();
226 entry.setSession(session);
227 int depth = entry.processLogLine(line, false);
228 setNewParent(parents, entry, depth);
230 LogEntry parent = (LogEntry) parents.get(depth - 1);
231 parent.addChild(entry);
232 } else if (state == MESSAGE_STATE) {
233 swriter = new StringWriter();
234 writer = new PrintWriter(swriter, true);
235 String message = ""; //$NON-NLS-1$
236 if (line.length() > 8)
237 message = line.substring(9).trim();
238 message = message.trim();
240 current.setMessage(message);
241 writerState = MESSAGE_STATE;
244 } catch (FileNotFoundException e) {
245 } catch (IOException e) {
250 } catch (IOException e1) {
255 private static void updateCurrentSession(LogSession session) {
256 if (currentSession == null) {
257 currentSession = session;
260 Date currentDate = currentSession.getDate();
261 Date sessionDate = session.getDate();
262 if (currentDate == null && sessionDate != null)
263 currentSession = session;
264 else if (currentDate != null && sessionDate == null)
265 currentSession = session;
266 else if (currentDate != null && sessionDate != null && sessionDate.after(currentDate))
267 currentSession = session;
270 public static void addEntry(LogEntry current, ArrayList entries, IMemento memento, boolean useCurrentSession) {
271 int severity = current.getSeverity();
272 boolean doAdd = true;
275 doAdd = memento.getString(LogView.P_LOG_INFO).equals("true"); //$NON-NLS-1$
277 case IStatus.WARNING:
278 doAdd = memento.getString(LogView.P_LOG_WARNING).equals("true"); //$NON-NLS-1$
281 doAdd = memento.getString(LogView.P_LOG_ERROR).equals("true"); //$NON-NLS-1$
285 if (useCurrentSession)
286 current.setSession(currentSession);
287 entries.add(0, current);
289 if (memento.getString(LogView.P_USE_LIMIT).equals("true") //$NON-NLS-1$
290 && entries.size() > memento.getInteger(LogView.P_LOG_LIMIT).intValue())
291 entries.remove(entries.size() - 1);
295 private static void setNewParent(
299 if (depth + 1 > parents.size())
302 parents.set(depth, entry);
305 public static void reset() {
306 currentSession = null;