SLF2J logging from SCL 36/336/1
authorHannu Niemistö <hannu.niemisto@semantum.fi>
Fri, 24 Feb 2017 13:43:35 +0000 (15:43 +0200)
committerHannu Niemistö <hannu.niemisto@semantum.fi>
Fri, 24 Feb 2017 13:43:35 +0000 (15:43 +0200)
Added a new module Logging with methods trace, debug, warn and error
taking a string as a parameter.

refs #7051

Change-Id: I8b11f82e3c15b56ee2c7fc87c8bf9cff4e315261

bundles/org.simantics.scl.compiler/META-INF/MANIFEST.MF
bundles/org.simantics.scl.compiler/OSGI-INF/org.simantics.scl.compiler.elaboration.java.LoggingModule.xml [new file with mode: 0644]
bundles/org.simantics.scl.compiler/build.properties
bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/java/LoggingModule.java [new file with mode: 0644]
bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/header/ModuleHeader.java
bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/source/repository/BuiltinModuleSourceRepository.java
tests/org.simantics.scl.compiler.tests/src/org/simantics/scl/compiler/tests/ModuleRegressionTests.java
tests/org.simantics.scl.compiler.tests/src/org/simantics/scl/compiler/tests/scl/Logger.scl [new file with mode: 0644]

index 603cdaf4881c8a8eea8b1fca9f21d3908144eb28..10a24ddce9b324f127f628c017734b8079a6fd5e 100644 (file)
@@ -73,5 +73,6 @@ Export-Package: org.cojen.classfile,
  org.simantics.scl.compiler.types.kinds,
  org.simantics.scl.compiler.types.util
 Bundle-ClassPath: .
-Service-Component: OSGI-INF/org.simantics.scl.compiler.source.repository.BuiltinModuleSourceRepository.xml
+Service-Component: OSGI-INF/org.simantics.scl.compiler.source.repository.BuiltinModuleSourceRepository.xml,
+ OSGI-INF/org.simantics.scl.compiler.elaboration.java.LoggingModule.xml
 Import-Package: org.osgi.service.component.annotations
diff --git a/bundles/org.simantics.scl.compiler/OSGI-INF/org.simantics.scl.compiler.elaboration.java.LoggingModule.xml b/bundles/org.simantics.scl.compiler/OSGI-INF/org.simantics.scl.compiler.elaboration.java.LoggingModule.xml
new file mode 100644 (file)
index 0000000..bad60b1
--- /dev/null
@@ -0,0 +1,4 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" name="org.simantics.scl.compiler.elaboration.java.LoggingModule">
+   <implementation class="org.simantics.scl.compiler.elaboration.java.LoggingModule"/>
+</scr:component>
\ No newline at end of file
index 77e91428448cd64cc4d5e3e880710d3498b286d8..55856a03e90fdc9712885feb95cf76bd983aa8a7 100644 (file)
@@ -2,4 +2,5 @@ source.. = src/
 output.. = bin/
 bin.includes = META-INF/,\
                .,\
-               OSGI-INF/org.simantics.scl.compiler.source.repository.BuiltinModuleSourceRepository.xml
+               OSGI-INF/org.simantics.scl.compiler.source.repository.BuiltinModuleSourceRepository.xml,\
+               OSGI-INF/org.simantics.scl.compiler.elaboration.java.LoggingModule.xml
diff --git a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/java/LoggingModule.java b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/java/LoggingModule.java
new file mode 100644 (file)
index 0000000..fa50596
--- /dev/null
@@ -0,0 +1,64 @@
+package org.simantics.scl.compiler.elaboration.java;
+
+import org.cojen.classfile.TypeDesc;
+import org.osgi.service.component.annotations.Component;
+import org.simantics.scl.compiler.common.names.Name;
+import org.simantics.scl.compiler.constants.JavaMethod;
+import org.simantics.scl.compiler.elaboration.contexts.SimplificationContext;
+import org.simantics.scl.compiler.elaboration.expressions.EApply;
+import org.simantics.scl.compiler.elaboration.expressions.EExternalConstant;
+import org.simantics.scl.compiler.elaboration.expressions.ELiteral;
+import org.simantics.scl.compiler.elaboration.expressions.Expression;
+import org.simantics.scl.compiler.elaboration.macros.MacroRule;
+import org.simantics.scl.compiler.elaboration.modules.SCLValue;
+import org.simantics.scl.compiler.internal.codegen.types.StandardTypeConstructor;
+import org.simantics.scl.compiler.module.ConcreteModule;
+import org.simantics.scl.compiler.types.TCon;
+import org.simantics.scl.compiler.types.Type;
+import org.simantics.scl.compiler.types.Types;
+import org.simantics.scl.compiler.types.kinds.Kinds;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+@Component
+public class LoggingModule extends ConcreteModule {
+
+    private static String[] LOGGING_METHODS = new String[] {
+            "trace", "debug", "info", "warn", "error" 
+    };
+
+    public LoggingModule() {
+        super("Logging");
+
+        // Logger
+        TCon Logger = Types.con(getName(), "Logger");
+        StandardTypeConstructor loggerConstructor = new StandardTypeConstructor(Logger, Kinds.STAR, TypeDesc.forClass(Logger.class));
+        loggerConstructor.external = true;
+        addTypeDescriptor("Logger", loggerConstructor);
+
+        // Common types
+        Type loggingType = Types.functionE(Types.STRING, 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;
+                    apply.set(new ELiteral(javaMethod), new Expression[] {
+                            new EExternalConstant(LoggerFactory.getLogger(module.getName().replaceAll("/", ".")), Logger),
+                            apply.parameters[0]
+                    });
+                    return apply;
+                }
+            });
+            addValue(value);
+        }
+        
+        setParentClassLoader(LoggerFactory.class.getClassLoader());
+    }
+
+}
index c49b49abed9da680c60a525602bd28a4b17026e2..9ca588779866f7022b32957195b70ff0bc580580 100644 (file)
@@ -12,7 +12,7 @@ public class ModuleHeader {
     private void read(ErrorLog errorLog, DModuleHeader header) {
         for(FieldAssignment assignment : header.fields)
             switch(assignment.name) {
-            case "classLoader":
+            case "bundle":
                 if(assignment.value == null)
                     errorLog.log(assignment.location, "Property classLoader needs to be given a string value.");
                 else {
index 4e1f00e8d4aadeca52094adb3e8dc378a2deefed..65a68a712106fb78480f23e4951a35c2f6e129a3 100644 (file)
@@ -3,6 +3,7 @@ package org.simantics.scl.compiler.source.repository;
 import org.osgi.service.component.annotations.Component;
 import org.simantics.scl.compiler.elaboration.java.Builtins;
 import org.simantics.scl.compiler.elaboration.java.JavaModule;
+import org.simantics.scl.compiler.elaboration.java.LoggingModule;
 import org.simantics.scl.compiler.elaboration.java.MinigraphModule;
 
 @Component
@@ -10,6 +11,7 @@ public class BuiltinModuleSourceRepository extends MapModuleSourceRepository imp
     public BuiltinModuleSourceRepository() {
         super(Builtins.INSTANCE,
                 JavaModule.INSTANCE,
-                MinigraphModule.INSTANCE);
+                MinigraphModule.INSTANCE,
+                new LoggingModule());
     }
 }
index 7843bac52d535de9cede7d54950dec3954781992..1c134aff7054ea81445f858f73c6e16fe6eff850 100644 (file)
@@ -136,6 +136,7 @@ public class ModuleRegressionTests extends TestBase {
     @Test public void LocalDefinitions3() { test(); }
     @Test public void LocalDefinitions4() { test(); }
     @Test public void LocalDefinitions5() { test(); }
+    @Test public void Logger() { test(); }
     @Test public void Macros1() { test(); }
     @Test public void Macros2() { test(); }
     @Test public void Macros4() { test(); }
diff --git a/tests/org.simantics.scl.compiler.tests/src/org/simantics/scl/compiler/tests/scl/Logger.scl b/tests/org.simantics.scl.compiler.tests/src/org/simantics/scl/compiler/tests/scl/Logger.scl
new file mode 100644 (file)
index 0000000..84a5030
--- /dev/null
@@ -0,0 +1,5 @@
+import "Logging"
+
+main = info "Something happened."
+--
+()
\ No newline at end of file