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;
9 import org.slf4j.Logger;
10 import org.slf4j.LoggerFactory;
13 * Static methods for calling the currently active {@link SCLReportingHandler}.
15 * @author Hannu Niemistö
17 public class SCLReporting {
19 private static final Logger LOGGER = LoggerFactory.getLogger(SCLReporting.class);
21 public static void print(String text) {
22 SCLReportingHandler handler = ((SCLReportingHandler)SCLContext.getCurrent().get(SCLReportingHandler.REPORTING_HANDLER));
26 System.out.println(text);
29 public static void printError(String text) {
30 SCLReportingHandler handler = ((SCLReportingHandler)SCLContext.getCurrent().get(SCLReportingHandler.REPORTING_HANDLER));
32 handler.printError(text);
37 public static void didWork(double amount) {
38 SCLReportingHandler handler = ((SCLReportingHandler)SCLContext.getCurrent().get(SCLReportingHandler.REPORTING_HANDLER));
40 handler.didWork(amount);
43 private static class FileReportingHandler extends DelegatingSCLReportingHandler {
46 public FileReportingHandler(SCLReportingHandler baseHandler, FileWriter writer) {
52 public void print(String text) {
56 } catch(IOException e) {
62 public static Object printingToFile(String fileName, Function proc) throws IOException {
63 FileWriter writer = new FileWriter(fileName);
64 SCLContext context = SCLContext.getCurrent();
65 Object oldHandler = context.get(SCLReportingHandler.REPORTING_HANDLER);
66 context.put(SCLReportingHandler.REPORTING_HANDLER,
67 new FileReportingHandler(
68 oldHandler == null ? SCLReportingHandler.DEFAULT : (SCLReportingHandler)oldHandler,
71 return proc.apply(Tuple0.INSTANCE);
73 context.put(SCLReportingHandler.REPORTING_HANDLER, oldHandler);
78 public static Object printErrorsAsNormalPrints(Function proc) {
79 SCLContext context = SCLContext.getCurrent();
80 SCLReportingHandler handler = (SCLReportingHandler)context.get(SCLReportingHandler.REPORTING_HANDLER);
82 handler = SCLReportingHandler.DEFAULT;
84 context.put(SCLReportingHandler.REPORTING_HANDLER, new DelegatingSCLReportingHandler(handler) {
86 public void printError(String error) {
92 return proc.apply(Tuple0.INSTANCE);
94 context.put(SCLReportingHandler.REPORTING_HANDLER, handler);
98 public static Object disablePrintingForCommand(Function proc) {
99 SCLContext context = SCLContext.getCurrent();
100 SCLReportingHandler handler = (SCLReportingHandler)context.get(SCLReportingHandler.REPORTING_HANDLER);
102 handler = SCLReportingHandler.DEFAULT;
104 context.put(SCLReportingHandler.REPORTING_HANDLER, new DelegatingSCLReportingHandler(handler) {
106 public void print(String text) {
111 return proc.apply(Tuple0.INSTANCE);
113 context.put(SCLReportingHandler.REPORTING_HANDLER, handler);