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 SCLReportingHandler getCurrentReportingHandler() {
22 SCLReportingHandler handler = ((SCLReportingHandler)SCLContext.getCurrent().get(SCLReportingHandler.REPORTING_HANDLER));
23 return handler == null ? SCLReportingHandler.DEFAULT : handler;
26 public static void print(String text) {
27 SCLReportingHandler handler = ((SCLReportingHandler)SCLContext.getCurrent().get(SCLReportingHandler.REPORTING_HANDLER));
31 System.out.println(text);
34 public static void printError(String text) {
35 SCLReportingHandler handler = ((SCLReportingHandler)SCLContext.getCurrent().get(SCLReportingHandler.REPORTING_HANDLER));
37 handler.printError(text);
42 public static void didWork(double amount) {
43 SCLReportingHandler handler = ((SCLReportingHandler)SCLContext.getCurrent().get(SCLReportingHandler.REPORTING_HANDLER));
45 handler.didWork(amount);
48 private static class FileReportingHandler extends DelegatingSCLReportingHandler {
51 public FileReportingHandler(SCLReportingHandler baseHandler, FileWriter writer) {
57 public void print(String text) {
61 } catch(IOException e) {
67 public static Object printingToFile(String fileName, Function proc) throws IOException {
68 FileWriter writer = new FileWriter(fileName);
69 SCLContext context = SCLContext.getCurrent();
70 Object oldHandler = context.get(SCLReportingHandler.REPORTING_HANDLER);
71 context.put(SCLReportingHandler.REPORTING_HANDLER,
72 new FileReportingHandler(
73 oldHandler == null ? SCLReportingHandler.DEFAULT : (SCLReportingHandler)oldHandler,
76 return proc.apply(Tuple0.INSTANCE);
78 context.put(SCLReportingHandler.REPORTING_HANDLER, oldHandler);
83 public static Object printErrorsAsNormalPrints(Function proc) {
84 SCLContext context = SCLContext.getCurrent();
85 SCLReportingHandler handler = (SCLReportingHandler)context.get(SCLReportingHandler.REPORTING_HANDLER);
87 handler = SCLReportingHandler.DEFAULT;
89 context.put(SCLReportingHandler.REPORTING_HANDLER, new DelegatingSCLReportingHandler(handler) {
91 public void printError(String error) {
97 return proc.apply(Tuple0.INSTANCE);
99 context.put(SCLReportingHandler.REPORTING_HANDLER, handler);
103 public static Object disablePrintingForCommand(Function proc) {
104 SCLContext context = SCLContext.getCurrent();
105 SCLReportingHandler handler = (SCLReportingHandler)context.get(SCLReportingHandler.REPORTING_HANDLER);
107 handler = SCLReportingHandler.DEFAULT;
109 context.put(SCLReportingHandler.REPORTING_HANDLER, new DelegatingSCLReportingHandler(handler) {
111 public void print(String text) {
116 return proc.apply(Tuple0.INSTANCE);
118 context.put(SCLReportingHandler.REPORTING_HANDLER, handler);
122 public static Object printingToLogging(Function proc) {
123 SCLContext context = SCLContext.getCurrent();
124 SCLReportingHandler handler = (SCLReportingHandler)context.get(SCLReportingHandler.REPORTING_HANDLER);
126 handler = SCLReportingHandler.DEFAULT;
128 context.put(SCLReportingHandler.REPORTING_HANDLER, SCLReportingHandler.DEFAULT);
131 return proc.apply(Tuple0.INSTANCE);
133 context.put(SCLReportingHandler.REPORTING_HANDLER, handler);