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.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;
24 public class LoggingModule extends ConcreteModule {
26 private static String[] LOGGING_METHODS = new String[] {
27 "trace", "debug", "info", "warn", "error"
30 public LoggingModule() {
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);
40 Type loggingType = Types.functionE(Types.STRING, Types.PROC, Types.UNIT);
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() {
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),
61 setParentClassLoader(LoggerFactory.class.getClassLoader());