001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.internal.runtime.logview;
011:
012: import java.io.BufferedReader;
013: import java.io.File;
014: import java.io.FileNotFoundException;
015: import java.io.IOException;
016: import java.io.InputStreamReader;
017: import java.io.PrintWriter;
018: import java.io.StringWriter;
019: import java.util.ArrayList;
020: import java.util.Date;
021:
022: import org.eclipse.core.runtime.IStatus;
023: import org.eclipse.ui.IMemento;
024:
025: class LogReader {
026: private static final int SESSION_STATE = 10;
027: public static final long MAX_FILE_LENGTH = 1024 * 1024;
028: private static final int ENTRY_STATE = 20;
029: private static final int SUBENTRY_STATE = 30;
030: private static final int MESSAGE_STATE = 40;
031: private static final int STACK_STATE = 50;
032: private static final int TEXT_STATE = 60;
033: private static final int UNKNOWN_STATE = 70;
034:
035: private static LogSession currentSession;
036:
037: public static void parseLogFile(File file, ArrayList entries,
038: IMemento memento) {
039: ArrayList parents = new ArrayList();
040: LogEntry current = null;
041: LogSession session = null;
042: int writerState = UNKNOWN_STATE;
043: StringWriter swriter = null;
044: PrintWriter writer = null;
045: int state = UNKNOWN_STATE;
046: currentSession = null;
047: BufferedReader reader = null;
048: try {
049:
050: reader = new BufferedReader(
051: new InputStreamReader(new TailInputStream(file,
052: MAX_FILE_LENGTH), "UTF-8")); //$NON-NLS-1$
053: for (;;) {
054: String line = reader.readLine();
055: if (line == null)
056: break;
057: line = line.trim();
058: if (line.length() == 0)
059: continue;
060:
061: if (line.startsWith("!SESSION")) { //$NON-NLS-1$
062: state = SESSION_STATE;
063: } else if (line.startsWith("!ENTRY")) { //$NON-NLS-1$
064: state = ENTRY_STATE;
065: } else if (line.startsWith("!SUBENTRY")) { //$NON-NLS-1$
066: state = SUBENTRY_STATE;
067: } else if (line.startsWith("!MESSAGE")) { //$NON-NLS-1$
068: state = MESSAGE_STATE;
069: } else if (line.startsWith("!STACK")) { //$NON-NLS-1$
070: state = STACK_STATE;
071: } else
072: state = TEXT_STATE;
073:
074: if (state == TEXT_STATE) {
075: if (writer != null)
076: writer.println(line);
077: continue;
078: }
079:
080: if (writer != null) {
081: if (writerState == STACK_STATE && current != null) {
082: current.setStack(swriter.toString());
083: } else if (writerState == SESSION_STATE
084: && session != null) {
085: session.setSessionData(swriter.toString());
086: } else if (writerState == MESSAGE_STATE
087: && current != null) {
088: StringBuffer sb = new StringBuffer(current
089: .getMessage());
090: sb.append(swriter.toString());
091: current.setMessage(sb.toString().trim());
092: }
093: writerState = UNKNOWN_STATE;
094: swriter = null;
095: writer.close();
096: writer = null;
097: }
098:
099: if (state == STACK_STATE) {
100: swriter = new StringWriter();
101: writer = new PrintWriter(swriter, true);
102: writerState = STACK_STATE;
103: } else if (state == SESSION_STATE) {
104: session = new LogSession();
105: session.processLogLine(line);
106: swriter = new StringWriter();
107: writer = new PrintWriter(swriter, true);
108: writerState = SESSION_STATE;
109: updateCurrentSession(session);
110: if (!currentSession.equals(session)
111: && !memento.getString(
112: LogView.P_SHOW_ALL_SESSIONS)
113: .equals("true")) //$NON-NLS-1$
114: entries.clear();
115: } else if (state == ENTRY_STATE) {
116: LogEntry entry = new LogEntry();
117: entry.setSession(session);
118: entry.processEntry(line);
119: setNewParent(parents, entry, 0);
120: current = entry;
121: addEntry(current, entries, memento, false);
122: } else if (state == SUBENTRY_STATE) {
123: if (parents.size() > 0) {
124: LogEntry entry = new LogEntry();
125: entry.setSession(session);
126: int depth = entry.processSubEntry(line);
127: setNewParent(parents, entry, depth);
128: current = entry;
129: LogEntry parent = (LogEntry) parents
130: .get(depth - 1);
131: parent.addChild(entry);
132: }
133: } else if (state == MESSAGE_STATE) {
134: swriter = new StringWriter();
135: writer = new PrintWriter(swriter, true);
136: String message = ""; //$NON-NLS-1$
137: if (line.length() > 8)
138: message = line.substring(9).trim();
139: message = message.trim();
140: if (current != null)
141: current.setMessage(message);
142: writerState = MESSAGE_STATE;
143: }
144: }
145:
146: if (swriter != null && current != null
147: && writerState == STACK_STATE)
148: current.setStack(swriter.toString());
149: } catch (FileNotFoundException e) {
150: } catch (IOException e) {
151: } finally {
152: try {
153: if (reader != null)
154: reader.close();
155: } catch (IOException e1) {
156: }
157: if (writer != null)
158: writer.close();
159: }
160: }
161:
162: private static void updateCurrentSession(LogSession session) {
163: if (currentSession == null) {
164: currentSession = session;
165: return;
166: }
167: Date currentDate = currentSession.getDate();
168: Date sessionDate = session.getDate();
169: if (currentDate == null && sessionDate != null)
170: currentSession = session;
171: else if (currentDate != null && sessionDate == null)
172: currentSession = session;
173: else if (currentDate != null && sessionDate != null
174: && sessionDate.after(currentDate))
175: currentSession = session;
176: }
177:
178: public synchronized static void addEntry(LogEntry current,
179: ArrayList entries, IMemento memento,
180: boolean useCurrentSession) {
181: int severity = current.getSeverity();
182: boolean doAdd = true;
183: switch (severity) {
184: case IStatus.INFO:
185: doAdd = memento.getString(LogView.P_LOG_INFO)
186: .equals("true"); //$NON-NLS-1$
187: break;
188: case IStatus.WARNING:
189: doAdd = memento.getString(LogView.P_LOG_WARNING).equals(
190: "true"); //$NON-NLS-1$
191: break;
192: case IStatus.ERROR:
193: doAdd = memento.getString(LogView.P_LOG_ERROR).equals(
194: "true"); //$NON-NLS-1$
195: break;
196: }
197: if (doAdd) {
198: if (useCurrentSession)
199: current.setSession(currentSession);
200: entries.add(0, current);
201:
202: if (memento.getString(LogView.P_USE_LIMIT).equals("true") //$NON-NLS-1$
203: && entries.size() > memento.getInteger(
204: LogView.P_LOG_LIMIT).intValue())
205: entries.remove(entries.size() - 1);
206: }
207: }
208:
209: private static void setNewParent(ArrayList parents, LogEntry entry,
210: int depth) {
211: if (depth + 1 > parents.size())
212: parents.add(entry);
213: else
214: parents.set(depth, entry);
215: }
216:
217: public static void reset() {
218: currentSession = null;
219: }
220: }
|