1 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.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 file, ArrayList entries, IMemento memento) { 39 ArrayList parents = new ArrayList (); 40 LogEntry current = null; 41 LogSession session = null; 42 int writerState = UNKNOWN_STATE; 43 StringWriter swriter = null; 44 PrintWriter writer = null; 45 int state = UNKNOWN_STATE; 46 currentSession = null; 47 BufferedReader reader = null; 48 try { 49 50 reader = new BufferedReader (new InputStreamReader ( 51 new TailInputStream(file, MAX_FILE_LENGTH), "UTF-8")); for (;;) { 53 String 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")) { state = SESSION_STATE; 62 } else if (line.startsWith("!ENTRY")) { state = ENTRY_STATE; 64 } else if (line.startsWith("!SUBENTRY")) { state = SUBENTRY_STATE; 66 } else if (line.startsWith("!MESSAGE")) { state = MESSAGE_STATE; 68 } else if (line.startsWith("!STACK")) { 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 sb = new StringBuffer (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 (); 97 writer = new PrintWriter (swriter, true); 98 writerState = STACK_STATE; 99 } else if (state == SESSION_STATE) { 100 session = new LogSession(); 101 session.processLogLine(line); 102 swriter = new StringWriter (); 103 writer = new PrintWriter (swriter, true); 104 writerState = SESSION_STATE; 105 updateCurrentSession(session); 106 if (!currentSession.equals(session) && !memento.getString(LogView.P_SHOW_ALL_SESSIONS).equals("true")) 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 (); 127 writer = new PrintWriter (swriter, true); 128 String message = ""; 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 e) { 141 } catch (IOException e) { 142 } finally { 143 try { 144 if (reader != null) 145 reader.close(); 146 } catch (IOException 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 currentDate = currentSession.getDate(); 159 Date 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 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"); break; 175 case IStatus.WARNING: 176 doAdd = memento.getString(LogView.P_LOG_WARNING).equals("true"); break; 178 case IStatus.ERROR: 179 doAdd = memento.getString(LogView.P_LOG_ERROR).equals("true"); 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") && 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 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 |