1 package org.simantics.scl.runtime.reporting;
3 import java.io.FileWriter;
4 import java.io.IOException;
6 import org.simantics.scl.runtime.SCLContext;
7 import org.simantics.scl.runtime.function.Function;
8 import org.simantics.scl.runtime.tuple.Tuple0;
11 * Static methods for calling the currently active {@link SCLReportingHandler}.
13 * @author Hannu Niemistö
15 public class SCLReporting {
17 public static void print(String text) {
18 SCLReportingHandler handler = ((SCLReportingHandler)SCLContext.getCurrent().get(SCLReportingHandler.REPORTING_HANDLER));
22 System.out.println(text);
25 public static void printError(String text) {
26 SCLReportingHandler handler = ((SCLReportingHandler)SCLContext.getCurrent().get(SCLReportingHandler.REPORTING_HANDLER));
28 handler.printError(text);
30 System.err.println(text);
33 public static void didWork(double amount) {
34 SCLReportingHandler handler = ((SCLReportingHandler)SCLContext.getCurrent().get(SCLReportingHandler.REPORTING_HANDLER));
36 handler.didWork(amount);
39 private static class FileReportingHandler extends DelegatingSCLReportingHandler {
42 public FileReportingHandler(SCLReportingHandler baseHandler, FileWriter writer) {
48 public void print(String text) {
52 } catch(IOException e) {
58 public static Object printingToFile(String fileName, Function proc) throws IOException {
59 FileWriter writer = new FileWriter(fileName);
60 SCLContext context = SCLContext.getCurrent();
61 Object oldHandler = context.get(SCLReportingHandler.REPORTING_HANDLER);
62 context.put(SCLReportingHandler.REPORTING_HANDLER,
63 new FileReportingHandler(
64 oldHandler == null ? SCLReportingHandler.DEFAULT : (SCLReportingHandler)oldHandler,
67 return proc.apply(Tuple0.INSTANCE);
69 context.put(SCLReportingHandler.REPORTING_HANDLER, oldHandler);
74 public static Object printErrorsAsNormalPrints(Function proc) {
75 SCLContext context = SCLContext.getCurrent();
76 SCLReportingHandler handler = (SCLReportingHandler)context.get(SCLReportingHandler.REPORTING_HANDLER);
78 handler = SCLReportingHandler.DEFAULT;
80 context.put(SCLReportingHandler.REPORTING_HANDLER, new DelegatingSCLReportingHandler(handler) {
82 public void printError(String error) {
88 return proc.apply(Tuple0.INSTANCE);
90 context.put(SCLReportingHandler.REPORTING_HANDLER, handler);
94 public static Object disablePrintingForCommand(Function proc) {
95 SCLContext context = SCLContext.getCurrent();
96 SCLReportingHandler handler = (SCLReportingHandler)context.get(SCLReportingHandler.REPORTING_HANDLER);
98 handler = SCLReportingHandler.DEFAULT;
100 context.put(SCLReportingHandler.REPORTING_HANDLER, new DelegatingSCLReportingHandler(handler) {
102 public void print(String text) {
107 return proc.apply(Tuple0.INSTANCE);
109 context.put(SCLReportingHandler.REPORTING_HANDLER, handler);