]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/java/LoggingModule.java
Merge branch 'feature/funcwrite'
[simantics/platform.git] / bundles / org.simantics.scl.compiler / src / org / simantics / scl / compiler / elaboration / java / LoggingModule.java
1 package org.simantics.scl.compiler.elaboration.java;
2
3 import org.cojen.classfile.TypeDesc;
4 import org.osgi.service.component.annotations.Component;
5 import org.simantics.scl.compiler.common.names.Name;
6 import org.simantics.scl.compiler.constants.JavaMethod;
7 import org.simantics.scl.compiler.elaboration.contexts.SimplificationContext;
8 import org.simantics.scl.compiler.elaboration.expressions.EApply;
9 import org.simantics.scl.compiler.elaboration.expressions.EExternalConstant;
10 import org.simantics.scl.compiler.elaboration.expressions.ELiteral;
11 import org.simantics.scl.compiler.elaboration.expressions.Expression;
12 import org.simantics.scl.compiler.elaboration.macros.MacroRule;
13 import org.simantics.scl.compiler.elaboration.modules.SCLValue;
14 import org.simantics.scl.compiler.internal.codegen.types.StandardTypeConstructor;
15 import org.simantics.scl.compiler.module.ConcreteModule;
16 import org.simantics.scl.compiler.types.TCon;
17 import org.simantics.scl.compiler.types.Type;
18 import org.simantics.scl.compiler.types.Types;
19 import org.simantics.scl.compiler.types.kinds.Kinds;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 @Component
24 public class LoggingModule extends ConcreteModule {
25
26     private static String[] LOGGING_METHODS = new String[] {
27             "trace", "debug", "info", "warn", "error" 
28     };
29
30     public LoggingModule() {
31         super("Logging");
32
33         // Logger
34         TCon Logger = Types.con(getName(), "Logger");
35         StandardTypeConstructor loggerConstructor = new StandardTypeConstructor(Logger, Kinds.STAR, TypeDesc.forClass(Logger.class));
36         loggerConstructor.external = true;
37         addTypeDescriptor("Logger", loggerConstructor);
38
39         // Common types
40         Type loggingType = Types.functionE(Types.STRING, Types.PROC, Types.UNIT);
41
42         // Add logging methods
43         for(String methodName : LOGGING_METHODS) {
44             JavaMethod javaMethod = new JavaMethod(false, "org/slf4j/Logger", methodName, Types.PROC, Types.UNIT, Logger, Types.STRING);
45             SCLValue value = new SCLValue(Name.create(getName(), methodName));
46             value.setType(loggingType);
47             value.setMacroRule(new MacroRule() {
48                 @Override
49                 public Expression apply(SimplificationContext context, Type[] typeParameters, EApply apply) {
50                     ConcreteModule module = context.getCompilationContext().module;
51                     apply.set(new ELiteral(javaMethod), new Expression[] {
52                             new EExternalConstant(LoggerFactory.getLogger(module.getName().replaceAll("/", ".")), Logger),
53                             apply.parameters[0]
54                     });
55                     return apply;
56                 }
57             });
58             addValue(value);
59         }
60         
61         setParentClassLoader(LoggerFactory.class.getClassLoader());
62     }
63
64 }