]> gerrit.simantics Code Review - simantics/platform.git/commitdiff
(refs #7448) Logging exceptions 85/885/2
authorHannu Niemistö <hannu.niemisto@semantum.fi>
Thu, 24 Aug 2017 09:23:17 +0000 (12:23 +0300)
committerJani Simomaa <jani.simomaa@semantum.fi>
Thu, 24 Aug 2017 09:24:31 +0000 (12:24 +0300)
Change-Id: I74cf2f40102adeb1c86abce3ecb4c9641d0fa7e5

bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/java/LoggingModule.java
tests/org.simantics.scl.compiler.tests/src/org/simantics/scl/compiler/tests/scl/Logger.scl

index 45596c5704e5aad359f197e445781cca148509b7..c0e3be53c9efc1b20c1080ae3502d4a5be0b35fa 100644 (file)
@@ -27,6 +27,8 @@ public class LoggingModule extends ConcreteModule {
     private static String[] LOGGING_METHODS = new String[] {
             "trace", "debug", "info", "warn", "error" 
     };
+    
+    public static final TCon Throwable = Types.con("Prelude", "Throwable");
 
     public LoggingModule() {
         super("Logging");
@@ -39,29 +41,55 @@ public class LoggingModule extends ConcreteModule {
 
         // Common types
         Type loggingType = Types.functionE(Types.STRING, Types.PROC, Types.UNIT);
+        Type loggingTypeWithException = Types.functionE(new Type[] { Types.STRING, Throwable }, Types.PROC, Types.UNIT);
 
         // Add logging methods
         for(String methodName : LOGGING_METHODS) {
-            JavaMethod javaMethod = new JavaMethod(false, "org/slf4j/Logger", methodName, Types.PROC, Types.UNIT, Logger, Types.STRING);
-            SCLValue value = new SCLValue(Name.create(getName(), methodName));
-            value.setType(loggingType);
-            value.setMacroRule(new MacroRule() {
-                @Override
-                public Expression apply(SimplificationContext context, Type[] typeParameters, EApply apply) {
-                    ConcreteModule module = context.getCompilationContext().module;
-                    String identifier;
-                    if (module != null)
-                        identifier = module.getName().replaceAll("/", ".");
-                    else
-                        identifier = CommandSession.class.getName();
-                    apply.set(new ELiteral(javaMethod), new Expression[] {
-                            new EExternalConstant(LoggerFactory.getLogger(identifier), Logger),
-                            apply.parameters[0]
-                    });
-                    return apply;
-                }
-            });
-            addValue(value);
+            {
+                JavaMethod javaMethod = new JavaMethod(false, "org/slf4j/Logger", methodName, Types.PROC, Types.UNIT, Logger, Types.STRING);
+                SCLValue value = new SCLValue(Name.create(getName(), methodName));
+                value.setType(loggingType);
+                value.setMacroRule(new MacroRule() {
+                    @Override
+                    public Expression apply(SimplificationContext context, Type[] typeParameters, EApply apply) {
+                        ConcreteModule module = context.getCompilationContext().module;
+                        String identifier;
+                        if (module != null)
+                            identifier = module.getName().replaceAll("/", ".");
+                        else
+                            identifier = CommandSession.class.getName();
+                        apply.set(new ELiteral(javaMethod), new Expression[] {
+                                new EExternalConstant(LoggerFactory.getLogger(identifier), Logger),
+                                apply.parameters[0]
+                        });
+                        return apply;
+                    }
+                });
+                addValue(value);
+            }
+            {
+                JavaMethod javaMethod = new JavaMethod(false, "org/slf4j/Logger", methodName, Types.PROC, Types.UNIT, Logger, Types.STRING, Throwable);
+                SCLValue value = new SCLValue(Name.create(getName(), methodName + "E"));
+                value.setType(loggingTypeWithException);
+                value.setMacroRule(new MacroRule() {
+                    @Override
+                    public Expression apply(SimplificationContext context, Type[] typeParameters, EApply apply) {
+                        ConcreteModule module = context.getCompilationContext().module;
+                        String identifier;
+                        if (module != null)
+                            identifier = module.getName().replaceAll("/", ".");
+                        else
+                            identifier = CommandSession.class.getName();
+                        apply.set(new ELiteral(javaMethod), new Expression[] {
+                                new EExternalConstant(LoggerFactory.getLogger(identifier), Logger),
+                                apply.parameters[0],
+                                apply.parameters[1]
+                        });
+                        return apply;
+                    }
+                });
+                addValue(value);
+            }
         }
         
         setParentClassLoader(LoggerFactory.class.getClassLoader());
index 84a5030ec73a11a454ea225d4768ffd00b5dbace..1a87375141b3511e5cdda7e624f3d4824f0ea75a 100644 (file)
@@ -1,5 +1,9 @@
 import "Logging"
+import "Prelude"
 
-main = info "Something happened."
+main = ()
+  where
+    info "Something happened."
+    print (read "XX" :: Double) `catch` (\(e :: Throwable) -> errorE "Something bad happened." e) 
 --
 ()
\ No newline at end of file