]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/java/LoggingModule.java
c0e3be53c9efc1b20c1080ae3502d4a5be0b35fa
[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.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 static final TCon Throwable = Types.con("Prelude", "Throwable");
32
33     public LoggingModule() {
34         super("Logging");
35
36         // Logger
37         TCon Logger = Types.con(getName(), "Logger");
38         StandardTypeConstructor loggerConstructor = new StandardTypeConstructor(Logger, Kinds.STAR, TypeDesc.forClass(Logger.class));
39         loggerConstructor.external = true;
40         addTypeDescriptor("Logger", loggerConstructor);
41
42         // Common types
43         Type loggingType = Types.functionE(Types.STRING, Types.PROC, Types.UNIT);
44         Type loggingTypeWithException = Types.functionE(new Type[] { Types.STRING, Throwable }, Types.PROC, Types.UNIT);
45
46         // Add logging methods
47         for(String methodName : LOGGING_METHODS) {
48             {
49                 JavaMethod javaMethod = new JavaMethod(false, "org/slf4j/Logger", methodName, Types.PROC, Types.UNIT, Logger, Types.STRING);
50                 SCLValue value = new SCLValue(Name.create(getName(), methodName));
51                 value.setType(loggingType);
52                 value.setMacroRule(new MacroRule() {
53                     @Override
54                     public Expression apply(SimplificationContext context, Type[] typeParameters, EApply apply) {
55                         ConcreteModule module = context.getCompilationContext().module;
56                         String identifier;
57                         if (module != null)
58                             identifier = module.getName().replaceAll("/", ".");
59                         else
60                             identifier = CommandSession.class.getName();
61                         apply.set(new ELiteral(javaMethod), new Expression[] {
62                                 new EExternalConstant(LoggerFactory.getLogger(identifier), Logger),
63                                 apply.parameters[0]
64                         });
65                         return apply;
66                     }
67                 });
68                 addValue(value);
69             }
70             {
71                 JavaMethod javaMethod = new JavaMethod(false, "org/slf4j/Logger", methodName, Types.PROC, Types.UNIT, Logger, Types.STRING, Throwable);
72                 SCLValue value = new SCLValue(Name.create(getName(), methodName + "E"));
73                 value.setType(loggingTypeWithException);
74                 value.setMacroRule(new MacroRule() {
75                     @Override
76                     public Expression apply(SimplificationContext context, Type[] typeParameters, EApply apply) {
77                         ConcreteModule module = context.getCompilationContext().module;
78                         String identifier;
79                         if (module != null)
80                             identifier = module.getName().replaceAll("/", ".");
81                         else
82                             identifier = CommandSession.class.getName();
83                         apply.set(new ELiteral(javaMethod), new Expression[] {
84                                 new EExternalConstant(LoggerFactory.getLogger(identifier), Logger),
85                                 apply.parameters[0],
86                                 apply.parameters[1]
87                         });
88                         return apply;
89                     }
90                 });
91                 addValue(value);
92             }
93         }
94         
95         setParentClassLoader(LoggerFactory.class.getClassLoader());
96     }
97
98 }