1 package org.simantics.scl.compiler.elaboration.java;
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;
25 public class LoggingModule extends ConcreteModule {
27 private static String[] LOGGING_METHODS = new String[] {
28 "trace", "debug", "info", "warn", "error"
31 public LoggingModule() {
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);
41 Type loggingType = Types.functionE(Types.STRING, Types.PROC, Types.UNIT);
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() {
50 public Expression apply(SimplificationContext context, Type[] typeParameters, EApply apply) {
51 ConcreteModule module = context.getCompilationContext().module;
54 identifier = module.getName().replaceAll("/", ".");
56 identifier = CommandSession.class.getName();
57 apply.set(new ELiteral(javaMethod), new Expression[] {
58 new EExternalConstant(LoggerFactory.getLogger(identifier), Logger),
67 setParentClassLoader(LoggerFactory.class.getClassLoader());