Fix missed files from cvs to svn migration. again!
[phpeclipse.git] / net.sourceforge.phpeclipse.xdebug.ui / src / net / sourceforge / phpeclipse / xdebug / ui / views / logview / LogReader.java
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
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpeclipse.xdebug.ui.views.logview;
12
13 import java.io.BufferedReader;
14 import java.io.File;
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;
24
25 import org.eclipse.core.runtime.IStatus;
26 import org.eclipse.ui.IMemento;
27
28 class LogReader {
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;
37         
38         private static LogSession currentSession;
39         
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;
50                 try {
51                         random = new RandomAccessFile(file, "r"); //$NON-NLS-1$
52                         random.seek(file.length() - MAX_FILE_LENGTH);
53                         for (;;) {
54                                 String line = random.readLine();
55                                 if (line == null)
56                                         break;
57                                 
58                                 line = line.trim();
59                                 if (line.length() == 0)
60                                         continue;
61
62                                 if (line.startsWith("!SESSION")) { //$NON-NLS-1$
63                                         state = SESSION_STATE;
64                                 } else if (line.startsWith("!ENTRY")) { //$NON-NLS-1$
65                                         state = ENTRY_STATE;
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$
71                                         state = STACK_STATE;
72                                 } else
73                                         state = TEXT_STATE;
74                         
75                                 if (state == TEXT_STATE) {
76                                         if (writer != null)
77                                                 writer.println(line);
78                                         continue;
79                                 }
80                         
81                                 if (writer != null) {
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);
90                                         }
91                                         writerState = UNKNOWN_STATE;
92                                         swriter = null;
93                                         writer.close();
94                                         writer = null;
95                                 }
96                         
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$
109                                                 entries.clear();
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);
115                                         current = entry;
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);
122                                         current = entry;
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();
132                                         if (current != null)
133                                                 current.setMessage(message);
134                                         writerState = MESSAGE_STATE;
135                                 }
136                         }
137                 } catch (FileNotFoundException e) {
138                 } catch (IOException e) {
139                 } finally {
140                         try {
141                                 if (random != null)
142                                         random.close();
143                         } catch (IOException e1) {
144                         }
145                 }
146         }
147         
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;
158                 try {
159                                         
160                         reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8")); //$NON-NLS-1$
161                         while(reader.ready()) {
162                                 String line = reader.readLine();
163                                 if (line == null)
164                                         continue;
165                                 line = line.trim();
166                                 if (line.length() == 0)
167                                         continue;
168
169                                 if (line.startsWith("!SESSION")) { //$NON-NLS-1$
170                                         state = SESSION_STATE;
171                                 } else if (line.startsWith("!ENTRY")) { //$NON-NLS-1$
172                                         state = ENTRY_STATE;
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$
178                                         state = STACK_STATE;
179                                 } else
180                                         state = TEXT_STATE;
181                         
182                                 if (state == TEXT_STATE) {
183                                         if (writer != null)
184                                                 writer.println(line);
185                                         continue;
186                                 }
187                         
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);
197                                         }
198                                         writerState = UNKNOWN_STATE;
199                                         swriter = null;
200                                         writer.close();
201                                         writer = null;
202                                 }
203                         
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$
216                                                 entries.clear();
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);
222                                         current = entry;
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);
229                                         current = entry;
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();
239                                         if (current != null)
240                                                 current.setMessage(message);
241                                         writerState = MESSAGE_STATE;
242                                 }
243                         }
244                 } catch (FileNotFoundException e) {
245                 } catch (IOException e) {
246                 } finally {
247                         try {
248                                 if (reader != null)
249                                         reader.close();
250                         } catch (IOException e1) {
251                         }
252                 }
253         }
254                 
255         private static void updateCurrentSession(LogSession session) {
256                 if (currentSession == null) {
257                         currentSession = session;
258                         return;
259                 }               
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;       
268         }
269         
270         public static void addEntry(LogEntry current, ArrayList entries, IMemento memento, boolean useCurrentSession) {
271                 int severity = current.getSeverity();
272                 boolean doAdd = true;
273                 switch(severity) {
274                         case IStatus.INFO:
275                                 doAdd = memento.getString(LogView.P_LOG_INFO).equals("true"); //$NON-NLS-1$
276                                 break;
277                         case IStatus.WARNING:
278                                 doAdd = memento.getString(LogView.P_LOG_WARNING).equals("true"); //$NON-NLS-1$
279                                 break;
280                         case IStatus.ERROR:
281                                 doAdd = memento.getString(LogView.P_LOG_ERROR).equals("true"); //$NON-NLS-1$
282                                 break;
283                 }
284                 if (doAdd) {
285                         if (useCurrentSession)
286                                 current.setSession(currentSession);
287                         entries.add(0, current);
288                         
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);
292                 }
293         }
294
295         private static void setNewParent(
296                 ArrayList parents,
297                 LogEntry entry,
298                 int depth) {
299                 if (depth + 1 > parents.size())
300                         parents.add(entry);
301                 else
302                         parents.set(depth, entry);
303         }
304         
305         public static void reset() {
306                 currentSession = null;
307         }
308 }