KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > runtime > logview > LogReader


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