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