X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.scl.runtime%2Fsrc%2Forg%2Fsimantics%2Fscl%2Fruntime%2Freporting%2FSCLReporting.java;fp=bundles%2Forg.simantics.scl.runtime%2Fsrc%2Forg%2Fsimantics%2Fscl%2Fruntime%2Freporting%2FSCLReporting.java;h=8870eb98ea16fd90a41179094a8269fb23fc803f;hp=0000000000000000000000000000000000000000;hb=969bd23cab98a79ca9101af33334000879fb60c5;hpb=866dba5cd5a3929bbeae85991796acb212338a08 diff --git a/bundles/org.simantics.scl.runtime/src/org/simantics/scl/runtime/reporting/SCLReporting.java b/bundles/org.simantics.scl.runtime/src/org/simantics/scl/runtime/reporting/SCLReporting.java new file mode 100644 index 000000000..8870eb98e --- /dev/null +++ b/bundles/org.simantics.scl.runtime/src/org/simantics/scl/runtime/reporting/SCLReporting.java @@ -0,0 +1,112 @@ +package org.simantics.scl.runtime.reporting; + +import java.io.FileWriter; +import java.io.IOException; + +import org.simantics.scl.runtime.SCLContext; +import org.simantics.scl.runtime.function.Function; +import org.simantics.scl.runtime.tuple.Tuple0; + +/** + * Static methods for calling the currently active {@link SCLReportingHandler}. + * + * @author Hannu Niemistö + */ +public class SCLReporting { + + public static void print(String text) { + SCLReportingHandler handler = ((SCLReportingHandler)SCLContext.getCurrent().get(SCLReportingHandler.REPORTING_HANDLER)); + if(handler != null) + handler.print(text); + else + System.out.println(text); + } + + public static void printError(String text) { + SCLReportingHandler handler = ((SCLReportingHandler)SCLContext.getCurrent().get(SCLReportingHandler.REPORTING_HANDLER)); + if(handler != null) + handler.printError(text); + else + System.err.println(text); + } + + public static void didWork(double amount) { + SCLReportingHandler handler = ((SCLReportingHandler)SCLContext.getCurrent().get(SCLReportingHandler.REPORTING_HANDLER)); + if(handler != null) + handler.didWork(amount); + } + + private static class FileReportingHandler extends DelegatingSCLReportingHandler { + FileWriter writer; + + public FileReportingHandler(SCLReportingHandler baseHandler, FileWriter writer) { + super(baseHandler); + this.writer = writer; + } + + @Override + public void print(String text) { + try { + writer.append(text); + writer.append('\n'); + } catch(IOException e) { + e.printStackTrace(); + } + } + } + + public static Object printingToFile(String fileName, Function proc) throws IOException { + FileWriter writer = new FileWriter(fileName); + SCLContext context = SCLContext.getCurrent(); + Object oldHandler = context.get(SCLReportingHandler.REPORTING_HANDLER); + context.put(SCLReportingHandler.REPORTING_HANDLER, + new FileReportingHandler( + oldHandler == null ? SCLReportingHandler.DEFAULT : (SCLReportingHandler)oldHandler, + writer)); + try { + return proc.apply(Tuple0.INSTANCE); + } finally { + context.put(SCLReportingHandler.REPORTING_HANDLER, oldHandler); + writer.close(); + } + } + + public static Object printErrorsAsNormalPrints(Function proc) { + SCLContext context = SCLContext.getCurrent(); + SCLReportingHandler handler = (SCLReportingHandler)context.get(SCLReportingHandler.REPORTING_HANDLER); + if(handler == null) + handler = SCLReportingHandler.DEFAULT; + + context.put(SCLReportingHandler.REPORTING_HANDLER, new DelegatingSCLReportingHandler(handler) { + @Override + public void printError(String error) { + print(error); + } + }); + + try { + return proc.apply(Tuple0.INSTANCE); + } finally { + context.put(SCLReportingHandler.REPORTING_HANDLER, handler); + } + } + + public static Object disablePrintingForCommand(Function proc) { + SCLContext context = SCLContext.getCurrent(); + SCLReportingHandler handler = (SCLReportingHandler)context.get(SCLReportingHandler.REPORTING_HANDLER); + if(handler == null) + handler = SCLReportingHandler.DEFAULT; + + context.put(SCLReportingHandler.REPORTING_HANDLER, new DelegatingSCLReportingHandler(handler) { + @Override + public void print(String text) { + } + }); + + try { + return proc.apply(Tuple0.INSTANCE); + } finally { + context.put(SCLReportingHandler.REPORTING_HANDLER, handler); + } + } +}