From: Hannu Niemistö Date: Fri, 24 Feb 2017 13:43:35 +0000 (+0200) Subject: SLF2J logging from SCL X-Git-Tag: v1.28.0~77^2 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=refs%2Fchanges%2F36%2F336%2F1;p=simantics%2Fplatform.git SLF2J logging from SCL Added a new module Logging with methods trace, debug, warn and error taking a string as a parameter. refs #7051 Change-Id: I8b11f82e3c15b56ee2c7fc87c8bf9cff4e315261 --- diff --git a/bundles/org.simantics.scl.compiler/META-INF/MANIFEST.MF b/bundles/org.simantics.scl.compiler/META-INF/MANIFEST.MF index 603cdaf48..10a24ddce 100644 --- a/bundles/org.simantics.scl.compiler/META-INF/MANIFEST.MF +++ b/bundles/org.simantics.scl.compiler/META-INF/MANIFEST.MF @@ -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 index 000000000..bad60b1da --- /dev/null +++ b/bundles/org.simantics.scl.compiler/OSGI-INF/org.simantics.scl.compiler.elaboration.java.LoggingModule.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/bundles/org.simantics.scl.compiler/build.properties b/bundles/org.simantics.scl.compiler/build.properties index 77e914284..55856a03e 100644 --- a/bundles/org.simantics.scl.compiler/build.properties +++ b/bundles/org.simantics.scl.compiler/build.properties @@ -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 index 000000000..fa5059626 --- /dev/null +++ b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/java/LoggingModule.java @@ -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()); + } + +} diff --git a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/header/ModuleHeader.java b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/header/ModuleHeader.java index c49b49abe..9ca588779 100644 --- a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/header/ModuleHeader.java +++ b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/header/ModuleHeader.java @@ -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 { diff --git a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/source/repository/BuiltinModuleSourceRepository.java b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/source/repository/BuiltinModuleSourceRepository.java index 4e1f00e8d..65a68a712 100644 --- a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/source/repository/BuiltinModuleSourceRepository.java +++ b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/source/repository/BuiltinModuleSourceRepository.java @@ -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()); } } diff --git a/tests/org.simantics.scl.compiler.tests/src/org/simantics/scl/compiler/tests/ModuleRegressionTests.java b/tests/org.simantics.scl.compiler.tests/src/org/simantics/scl/compiler/tests/ModuleRegressionTests.java index 7843bac52..1c134aff7 100644 --- a/tests/org.simantics.scl.compiler.tests/src/org/simantics/scl/compiler/tests/ModuleRegressionTests.java +++ b/tests/org.simantics.scl.compiler.tests/src/org/simantics/scl/compiler/tests/ModuleRegressionTests.java @@ -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 index 000000000..84a5030ec --- /dev/null +++ b/tests/org.simantics.scl.compiler.tests/src/org/simantics/scl/compiler/tests/scl/Logger.scl @@ -0,0 +1,5 @@ +import "Logging" + +main = info "Something happened." +-- +() \ No newline at end of file