]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.runtime/src/org/simantics/scl/runtime/reporting/SCLReporting.java
Fixed multiple issues causing dangling references to discarded queries
[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 SCLReportingHandler getCurrentReportingHandler() {
22         SCLReportingHandler handler = ((SCLReportingHandler)SCLContext.getCurrent().get(SCLReportingHandler.REPORTING_HANDLER));
23         return handler == null ? SCLReportingHandler.DEFAULT : handler;
24     }
25     
26     public static void print(String text) {
27         SCLReportingHandler handler = ((SCLReportingHandler)SCLContext.getCurrent().get(SCLReportingHandler.REPORTING_HANDLER));
28         if(handler != null)
29             handler.print(text);
30         else
31             System.out.println(text);
32     }
33     
34     public static void printError(String text) {
35         SCLReportingHandler handler = ((SCLReportingHandler)SCLContext.getCurrent().get(SCLReportingHandler.REPORTING_HANDLER));
36         if(handler != null)
37             handler.printError(text);
38         else
39             LOGGER.error(text);
40     }
41     
42     public static void didWork(double amount) {
43         SCLReportingHandler handler = ((SCLReportingHandler)SCLContext.getCurrent().get(SCLReportingHandler.REPORTING_HANDLER));
44         if(handler != null)
45             handler.didWork(amount);
46     }
47     
48     private static class FileReportingHandler extends DelegatingSCLReportingHandler {
49         FileWriter writer;
50         
51         public FileReportingHandler(SCLReportingHandler baseHandler, FileWriter writer) {
52             super(baseHandler);
53             this.writer = writer;
54         }
55
56         @Override
57         public void print(String text) {
58             try {
59                 writer.append(text);
60                 writer.append('\n');
61             } catch(IOException e) {
62                 e.printStackTrace();
63             }
64         }
65     }
66
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,
74                         writer));
75         try {
76             return proc.apply(Tuple0.INSTANCE);
77         } finally {
78             context.put(SCLReportingHandler.REPORTING_HANDLER, oldHandler);
79             writer.close();
80         }
81     }
82     
83     public static Object printErrorsAsNormalPrints(Function proc) {
84         SCLContext context = SCLContext.getCurrent();
85         SCLReportingHandler handler = (SCLReportingHandler)context.get(SCLReportingHandler.REPORTING_HANDLER);
86         if(handler == null)
87             handler = SCLReportingHandler.DEFAULT;
88         
89         context.put(SCLReportingHandler.REPORTING_HANDLER, new DelegatingSCLReportingHandler(handler) {
90             @Override
91             public void printError(String error) {
92                 print(error);
93             }
94         });
95         
96         try {
97             return proc.apply(Tuple0.INSTANCE);
98         } finally {
99             context.put(SCLReportingHandler.REPORTING_HANDLER, handler);
100         }
101     }
102     
103     public static Object disablePrintingForCommand(Function proc) {
104         SCLContext context = SCLContext.getCurrent();
105         SCLReportingHandler handler = (SCLReportingHandler)context.get(SCLReportingHandler.REPORTING_HANDLER);
106         if(handler == null)
107             handler = SCLReportingHandler.DEFAULT;
108         
109         context.put(SCLReportingHandler.REPORTING_HANDLER, new DelegatingSCLReportingHandler(handler) {
110             @Override
111             public void print(String text) {
112             }
113         });
114         
115         try {
116             return proc.apply(Tuple0.INSTANCE);
117         } finally {
118             context.put(SCLReportingHandler.REPORTING_HANDLER, handler);
119         }
120     }
121     
122     public static Object printingToLogging(Function proc) {
123         SCLContext context = SCLContext.getCurrent();
124         SCLReportingHandler handler = (SCLReportingHandler)context.get(SCLReportingHandler.REPORTING_HANDLER);
125         if(handler == null)
126             handler = SCLReportingHandler.DEFAULT;
127         
128         context.put(SCLReportingHandler.REPORTING_HANDLER, SCLReportingHandler.DEFAULT);
129         
130         try {
131             return proc.apply(Tuple0.INSTANCE);
132         } finally {
133             context.put(SCLReportingHandler.REPORTING_HANDLER, handler);
134         }
135     }
136 }