Ongoing XML log read/write
diff --git a/bundles/org.eclipse.core.runtime/src/org/eclipse/core/internal/runtime/PlatformLogReader.java b/bundles/org.eclipse.core.runtime/src/org/eclipse/core/internal/runtime/PlatformLogReader.java index 99f7c71..c3835a0 100644 --- a/bundles/org.eclipse.core.runtime/src/org/eclipse/core/internal/runtime/PlatformLogReader.java +++ b/bundles/org.eclipse.core.runtime/src/org/eclipse/core/internal/runtime/PlatformLogReader.java
@@ -14,7 +14,7 @@ * General strategy: log entries that are malformed in any way are skipped, and an extra * status is returned mentioned that there were problems. */ -class PlatformLogReader { +public class PlatformLogReader { /** * Temporary main class for testing... */ @@ -168,7 +168,6 @@ children.add(o); } else if (o instanceof Throwable) { exception = (Throwable)o; - exception.printStackTrace(); } } if (children.size() > 0) {
diff --git a/bundles/org.eclipse.core.runtime/src/org/eclipse/core/internal/runtime/PlatformLogWriter.java b/bundles/org.eclipse.core.runtime/src/org/eclipse/core/internal/runtime/PlatformLogWriter.java index 3ee2118..d7bad2c 100644 --- a/bundles/org.eclipse.core.runtime/src/org/eclipse/core/internal/runtime/PlatformLogWriter.java +++ b/bundles/org.eclipse.core.runtime/src/org/eclipse/core/internal/runtime/PlatformLogWriter.java
@@ -8,19 +8,18 @@ import java.io.*; import java.util.*; -import org.apache.xml.serialize.XMLSerializer; import org.eclipse.core.runtime.ILogListener; import org.eclipse.core.runtime.IStatus; -import org.eclipse.core.runtime.Platform; -import org.xml.sax.SAXException; -import org.xml.sax.helpers.AttributesImpl; +/** + * A log writer that writes log entries in XML format. + * See PlatformLogReader for reading logs back into memory. + */ +public class PlatformLogWriter implements ILogListener { + protected File logFile = null; + protected PrintWriter log = null; + protected int tabDepth; -class PlatformLogWriter implements ILogListener { - private PrintWriter log = null; - private boolean usingLogFile = false; - private int tab; - - private static final String XML_VERSION = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"; + protected static final String XML_VERSION = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"; protected static final String ATTRIBUTE_DATE = "date"; protected static final String ATTRIBUTE_SEVERITY = "severity"; @@ -34,20 +33,18 @@ protected static final String ELEMENT_STATUS = "status"; protected static final String ELEMENT_EXCEPTION = "exception"; - - -PlatformLogWriter() { - usingLogFile = true; +public PlatformLogWriter(File file) { + this.logFile = file; // remove old log file - InternalPlatform.getMetaArea().getLogLocation().toFile().delete(); + logFile.delete(); } /** - * It should only be used to pass System.out . + * This constructor should only be used to pass System.out . */ -PlatformLogWriter(OutputStream out) { +public PlatformLogWriter(OutputStream out) { log = new PrintWriter(out); } -private static void appendEscapedChar(StringBuffer buffer, char c) { +protected static void appendEscapedChar(StringBuffer buffer, char c) { String replacement = getReplacement(c); if (replacement != null) { buffer.append('&'); @@ -57,13 +54,13 @@ buffer.append(c); } } -public static String getEscaped(String s) { +protected static String getEscaped(String s) { StringBuffer result = new StringBuffer(s.length() + 10); for (int i = 0; i < s.length(); ++i) appendEscapedChar(result, s.charAt(i)); return result.toString(); } -private static String getReplacement(char c) { +protected static String getReplacement(char c) { // Encode special XML characters into the equivalent character references. // These five are defined by default for all XML documents. switch (c) { @@ -80,7 +77,7 @@ } return null; } -private void closeLogFile() { +protected void closeLogFile() { try { log.flush(); log.close(); @@ -113,13 +110,13 @@ pWriter.flush(); return sWriter.toString(); } -public void endTag(String name) { - tab--; +protected void endTag(String name) { + tabDepth--; printTag('/' + name, null); } public synchronized void logging(IStatus status, String plugin) { // thread safety: (Concurrency003) - if (usingLogFile) + if (logFile != null) openLogFile(); if (log == null) return; @@ -128,11 +125,11 @@ } catch (Exception e) { e.printStackTrace(); }finally { - if (usingLogFile) + if (logFile != null) closeLogFile(); } } -private void openLogFile() { +protected void openLogFile() { try { File file = InternalPlatform.getMetaArea().getLogLocation().toFile(); boolean newLog = !file.exists(); @@ -146,16 +143,16 @@ log = new PrintWriter(System.out); } } -public void printTabulation() { - for (int i = 0; i < tab; i++) +protected void printTabulation() { + for (int i = 0; i < tabDepth; i++) log.print(" "); } -public void printTag(String name, HashMap parameters) { +protected void printTag(String name, HashMap parameters) { printTabulation(); log.print('<'); log.print(name); - tab++; + tabDepth++; if (parameters != null) for (Enumeration enum = Collections.enumeration(parameters.keySet()); enum.hasMoreElements();) { //new line for each attribute if there's more than one @@ -170,20 +167,20 @@ log.print(getEscaped(String.valueOf(parameters.get(key)))); log.print("\""); } - tab--; + tabDepth--; log.println(">"); } - /** * @see ILogListener */ public synchronized void shutdown() { - if (usingLogFile) { + if (logFile != null) { try { openLogFile(); endTag(ELEMENT_LOG); } finally { closeLogFile(); + logFile = null; } } else { if (log != null) { @@ -194,11 +191,11 @@ } } } -public void startTag(String name, HashMap parameters) { +protected void startTag(String name, HashMap parameters) { printTag(name, parameters); - tab++; + tabDepth++; } -protected void write(Throwable throwable) throws SAXException { +protected void write(Throwable throwable) { if (throwable == null) return; HashMap attributes = new HashMap(); @@ -207,7 +204,7 @@ startTag(ELEMENT_EXCEPTION, attributes); endTag(ELEMENT_EXCEPTION); } -protected void write(IStatus status) throws SAXException { +protected void write(IStatus status) { HashMap attributes = new HashMap(); attributes.put(ATTRIBUTE_SEVERITY, encodeSeverity(status.getSeverity())); attributes.put(ATTRIBUTE_PLUGIN_ID, status.getPlugin()); @@ -224,15 +221,13 @@ } endTag(ELEMENT_STATUS); } -protected void writeLogEntry(IStatus status) throws SAXException { - tab = 0; +protected void writeLogEntry(IStatus status) { + tabDepth = 0; HashMap attributes = new HashMap(); attributes.put(ATTRIBUTE_DATE, new Date()); startTag(ELEMENT_LOG_ENTRY, attributes); - tab = 1; write(status); endTag(ELEMENT_LOG_ENTRY); - tab = 0; } }