]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.runtime/src/org/simantics/scl/runtime/reporting/SCLReporting.java
Added empty .keep file to org.simantics.jdbc.ontology/src
[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 }