Migrated source code from Simantics SVN
[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
10 /**
11  * Static methods for calling the currently active {@link SCLReportingHandler}.
12  * 
13  * @author Hannu Niemistö
14  */
15 public class SCLReporting {
16
17     public static void print(String text) {
18         SCLReportingHandler handler = ((SCLReportingHandler)SCLContext.getCurrent().get(SCLReportingHandler.REPORTING_HANDLER));
19         if(handler != null)
20             handler.print(text);
21         else
22             System.out.println(text);
23     }
24     
25     public static void printError(String text) {
26         SCLReportingHandler handler = ((SCLReportingHandler)SCLContext.getCurrent().get(SCLReportingHandler.REPORTING_HANDLER));
27         if(handler != null)
28             handler.printError(text);
29         else
30             System.err.println(text);
31     }
32     
33     public static void didWork(double amount) {
34         SCLReportingHandler handler = ((SCLReportingHandler)SCLContext.getCurrent().get(SCLReportingHandler.REPORTING_HANDLER));
35         if(handler != null)
36             handler.didWork(amount);
37     }
38     
39     private static class FileReportingHandler extends DelegatingSCLReportingHandler {
40         FileWriter writer;
41         
42         public FileReportingHandler(SCLReportingHandler baseHandler, FileWriter writer) {
43             super(baseHandler);
44             this.writer = writer;
45         }
46
47         @Override
48         public void print(String text) {
49             try {
50                 writer.append(text);
51                 writer.append('\n');
52             } catch(IOException e) {
53                 e.printStackTrace();
54             }
55         }
56     }
57
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,
65                         writer));
66         try {
67             return proc.apply(Tuple0.INSTANCE);
68         } finally {
69             context.put(SCLReportingHandler.REPORTING_HANDLER, oldHandler);
70             writer.close();
71         }
72     }
73     
74     public static Object printErrorsAsNormalPrints(Function proc) {
75         SCLContext context = SCLContext.getCurrent();
76         SCLReportingHandler handler = (SCLReportingHandler)context.get(SCLReportingHandler.REPORTING_HANDLER);
77         if(handler == null)
78             handler = SCLReportingHandler.DEFAULT;
79         
80         context.put(SCLReportingHandler.REPORTING_HANDLER, new DelegatingSCLReportingHandler(handler) {
81             @Override
82             public void printError(String error) {
83                 print(error);
84             }
85         });
86         
87         try {
88             return proc.apply(Tuple0.INSTANCE);
89         } finally {
90             context.put(SCLReportingHandler.REPORTING_HANDLER, handler);
91         }
92     }
93     
94     public static Object disablePrintingForCommand(Function proc) {
95         SCLContext context = SCLContext.getCurrent();
96         SCLReportingHandler handler = (SCLReportingHandler)context.get(SCLReportingHandler.REPORTING_HANDLER);
97         if(handler == null)
98             handler = SCLReportingHandler.DEFAULT;
99         
100         context.put(SCLReportingHandler.REPORTING_HANDLER, new DelegatingSCLReportingHandler(handler) {
101             @Override
102             public void print(String text) {
103             }
104         });
105         
106         try {
107             return proc.apply(Tuple0.INSTANCE);
108         } finally {
109             context.put(SCLReportingHandler.REPORTING_HANDLER, handler);
110         }
111     }
112 }