# HG changeset patch # User Andreas Woess # Date 1447410587 -3600 # Node ID a360c82ba357df4dcd50f8373a0607436659e26f # Parent 5d62f7cfe0e1a0b78a5d098e5e79310ebaadd9ab GraphPrintVisitor implementation can now be disabled via system property diff -r 5d62f7cfe0e1 -r a360c82ba357 truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/nodes/GraphPrintVisitor.java --- a/truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/nodes/GraphPrintVisitor.java Wed Nov 11 14:47:40 2015 +0100 +++ b/truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/nodes/GraphPrintVisitor.java Fri Nov 13 11:29:47 2015 +0100 @@ -59,6 +59,7 @@ public static final String GraphVisualizerAddress = "127.0.0.1"; public static final int GraphVisualizerPort = 4444; + private static final boolean ENABLED = !System.getProperty("truffle.graphprint", "").equalsIgnoreCase("false"); private static final String DEFAULT_GRAPH_NAME = "truffle tree"; private Map nodeMap; @@ -120,11 +121,29 @@ } } - private static class Impl { + private interface Impl { + void writeStartDocument(); + + void writeEndDocument(); + + void writeStartElement(String name); + + void writeEndElement(); + + void writeAttribute(String name, String value); + + void writeCharacters(String text); + + void flush(); + + void close(); + } + + private static class XMLImpl implements Impl { private static final XMLOutputFactory XML_OUTPUT_FACTORY = XMLOutputFactory.newInstance(); private final XMLStreamWriter xmlstream; - protected Impl(OutputStream outputStream) { + protected XMLImpl(OutputStream outputStream) { try { this.xmlstream = XML_OUTPUT_FACTORY.createXMLStreamWriter(outputStream); } catch (XMLStreamException | FactoryConfigurationError e) { @@ -203,11 +222,15 @@ public GraphPrintVisitor(OutputStream outputStream) { this.outputStream = outputStream; - this.xmlstream = new Impl(outputStream); + this.xmlstream = createImpl(outputStream); this.xmlstream.writeStartDocument(); this.xmlstream.writeStartElement("graphDocument"); } + private static Impl createImpl(OutputStream outputStream) { + return ENABLED ? new XMLImpl(outputStream) : new DummyImpl(); + } + private void ensureOpen() { if (xmlstream == null) { throw new IllegalStateException("printer is closed"); @@ -346,7 +369,7 @@ @Override public String toString() { - if (outputStream instanceof ByteArrayOutputStream) { + if (outputStream instanceof ByteArrayOutputStream && ENABLED) { return new String(((ByteArrayOutputStream) outputStream).toByteArray(), Charset.forName("UTF-8")); } return super.toString(); @@ -354,7 +377,7 @@ public void printToFile(File f) { close(); - if (outputStream instanceof ByteArrayOutputStream) { + if (outputStream instanceof ByteArrayOutputStream && ENABLED) { try (OutputStream os = new FileOutputStream(f)) { os.write(((ByteArrayOutputStream) outputStream).toByteArray()); } catch (IOException e) { @@ -365,7 +388,7 @@ public void printToSysout() { close(); - if (outputStream instanceof ByteArrayOutputStream) { + if (outputStream instanceof ByteArrayOutputStream && ENABLED) { PrintStream out = System.out; out.println(toString()); } @@ -373,11 +396,13 @@ public void printToNetwork(boolean ignoreErrors) { close(); - try (Socket socket = new Socket(GraphVisualizerAddress, GraphVisualizerPort); BufferedOutputStream os = new BufferedOutputStream(socket.getOutputStream(), 0x4000)) { - os.write(((ByteArrayOutputStream) outputStream).toByteArray()); - } catch (IOException e) { - if (!ignoreErrors) { - e.printStackTrace(); + if (outputStream instanceof ByteArrayOutputStream && ENABLED) { + try (Socket socket = new Socket(GraphVisualizerAddress, GraphVisualizerPort); BufferedOutputStream os = new BufferedOutputStream(socket.getOutputStream(), 0x4000)) { + os.write(((ByteArrayOutputStream) outputStream).toByteArray()); + } catch (IOException e) { + if (!ignoreErrors) { + e.printStackTrace(); + } } } } @@ -603,6 +628,32 @@ } } + private static class DummyImpl implements Impl { + public void writeStartDocument() { + } + + public void writeEndDocument() { + } + + public void writeStartElement(String name) { + } + + public void writeEndElement() { + } + + public void writeAttribute(String name, String value) { + } + + public void writeCharacters(String text) { + } + + public void flush() { + } + + public void close() { + } + } + @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) public @interface CustomGraphPrintHandler {