97ef75f8b498e0d6a7382c7f84e626d0ceac8ee9
[simantics/platform.git] / bundles / org.simantics.scl.runtime / src / org / simantics / scl / runtime / reporting / SCLReporting.java
1 package org.simantics.scl.runtime.reporting;
2
3 import java.io.FileWriter;
4 import java.io.IOException;
5
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;
11
12 /**
13  * Static methods for calling the currently active {@link SCLReportingHandler}.
14  * 
15  * @author Hannu Niemistö
16  */
17 public class SCLReporting {
18
19     private static final Logger LOGGER = LoggerFactory.getLogger(SCLReporting.class);
20
21     public static void print(String text) {
22         SCLReportingHandler handler = ((SCLReportingHandler)SCLContext.getCurrent().get(SCLReportingHandler.REPORTING_HANDLER));
23         if(handler != null)
24             handler.print(text);
25         else
26             System.out.println(text);
27     }
28     
29     public static void printError(String text) {
30         SCLReportingHandler handler = ((SCLReportingHandler)SCLContext.getCurrent().get(SCLReportingHandler.REPORTING_HANDLER));
31         if(handler != null)
32             handler.printError(text);
33         else
34             LOGGER.error(text);
35     }
36     
37     public static void didWork(double amount) {
38         SCLReportingHandler handler = ((SCLReportingHandler)SCLContext.getCurrent().get(SCLReportingHandler.REPORTING_HANDLER));
39         if(handler != null)
40             handler.didWork(amount);
41     }
42     
43     private static class FileReportingHandler extends DelegatingSCLReportingHandler {
44         FileWriter writer;
45         
46         public FileReportingHandler(SCLReportingHandler baseHandler, FileWriter writer) {
47             super(baseHandler);
48             this.writer = writer;
49         }
50
51         @Override
52         public void print(String text) {
53             try {
54                 writer.append(text);
55                 writer.append('\n');
56             } catch(IOException e) {
57                 e.printStackTrace();
58             }
59         }
60     }
61
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,
69                         writer));
70         try {
71             return proc.apply(Tuple0.INSTANCE);
72         } finally {
73             context.put(SCLReportingHandler.REPORTING_HANDLER, oldHandler);
74             writer.close();
75         }
76     }
77     
78     public static Object printErrorsAsNormalPrints(Function proc) {
79         SCLContext context = SCLContext.getCurrent();
80         SCLReportingHandler handler = (SCLReportingHandler)context.get(SCLReportingHandler.REPORTING_HANDLER);
81         if(handler == null)
82             handler = SCLReportingHandler.DEFAULT;
83         
84         context.put(SCLReportingHandler.REPORTING_HANDLER, new DelegatingSCLReportingHandler(handler) {
85             @Override
86             public void printError(String error) {
87                 print(error);
88             }
89         });
90         
91         try {
92             return proc.apply(Tuple0.INSTANCE);
93         } finally {
94             context.put(SCLReportingHandler.REPORTING_HANDLER, handler);
95         }
96     }
97     
98     public static Object disablePrintingForCommand(Function proc) {
99         SCLContext context = SCLContext.getCurrent();
100         SCLReportingHandler handler = (SCLReportingHandler)context.get(SCLReportingHandler.REPORTING_HANDLER);
101         if(handler == null)
102             handler = SCLReportingHandler.DEFAULT;
103         
104         context.put(SCLReportingHandler.REPORTING_HANDLER, new DelegatingSCLReportingHandler(handler) {
105             @Override
106             public void print(String text) {
107             }
108         });
109         
110         try {
111             return proc.apply(Tuple0.INSTANCE);
112         } finally {
113             context.put(SCLReportingHandler.REPORTING_HANDLER, handler);
114         }
115     }
116     
117     public static Object printingToLogging(Function proc) {
118         SCLContext context = SCLContext.getCurrent();
119         SCLReportingHandler handler = (SCLReportingHandler)context.get(SCLReportingHandler.REPORTING_HANDLER);
120         if(handler == null)
121             handler = SCLReportingHandler.DEFAULT;
122         
123         context.put(SCLReportingHandler.REPORTING_HANDLER, SCLReportingHandler.DEFAULT);
124         
125         try {
126             return proc.apply(Tuple0.INSTANCE);
127         } finally {
128             context.put(SCLReportingHandler.REPORTING_HANDLER, handler);
129         }
130     }
131 }