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 static final TCon Throwable = Types.con("Prelude", "Throwable");
33 public LoggingModule() {
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);
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);
46 // Add logging methods
47 for(String methodName : LOGGING_METHODS) {
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() {
54 public Expression apply(SimplificationContext context, Type[] typeParameters, EApply apply) {
55 ConcreteModule module = context.getCompilationContext().module;
58 identifier = module.getName().replaceAll("/", ".");
60 identifier = CommandSession.class.getName();
61 apply.set(new ELiteral(javaMethod), new Expression[] {
62 new EExternalConstant(LoggerFactory.getLogger(identifier), Logger),
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() {
76 public Expression apply(SimplificationContext context, Type[] typeParameters, EApply apply) {
77 ConcreteModule module = context.getCompilationContext().module;
80 identifier = module.getName().replaceAll("/", ".");
82 identifier = CommandSession.class.getName();
83 apply.set(new ELiteral(javaMethod), new Expression[] {
84 new EExternalConstant(LoggerFactory.getLogger(identifier), Logger),
95 setParentClassLoader(LoggerFactory.class.getClassLoader());