001/**
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
004 *
005 * This program and the accompanying materials are dual-licensed under
006 * either the terms of the Eclipse Public License v1.0 as published by
007 * the Eclipse Foundation
008 *
009 *   or (per the licensee's choosing)
010 *
011 * under the terms of the GNU Lesser General Public License version 2.1
012 * as published by the Free Software Foundation.
013 */
014package ch.qos.logback.classic.boolex;
015
016import java.util.ArrayList;
017import java.util.List;
018import java.util.Map;
019
020import org.slf4j.Marker;
021
022import ch.qos.logback.classic.Level;
023import ch.qos.logback.classic.spi.ILoggingEvent;
024import ch.qos.logback.classic.spi.IThrowableProxy;
025import ch.qos.logback.classic.spi.LoggerContextVO;
026import ch.qos.logback.classic.spi.ThrowableProxy;
027import ch.qos.logback.core.CoreConstants;
028import ch.qos.logback.core.boolex.JaninoEventEvaluatorBase;
029import ch.qos.logback.core.boolex.Matcher;
030
031public class JaninoEventEvaluator extends JaninoEventEvaluatorBase<ILoggingEvent> {
032
033    public final static String IMPORT_LEVEL = "import ch.qos.logback.classic.Level;\r\n";
034
035    public final static List<String> DEFAULT_PARAM_NAME_LIST = new ArrayList<>();
036    public final static List<Class<?>> DEFAULT_PARAM_TYPE_LIST = new ArrayList<>();
037
038    static {
039        DEFAULT_PARAM_NAME_LIST.add("DEBUG");
040        DEFAULT_PARAM_NAME_LIST.add("INFO");
041        DEFAULT_PARAM_NAME_LIST.add("WARN");
042        DEFAULT_PARAM_NAME_LIST.add("ERROR");
043
044        DEFAULT_PARAM_NAME_LIST.add("event");
045        DEFAULT_PARAM_NAME_LIST.add("message");
046
047        DEFAULT_PARAM_NAME_LIST.add("formattedMessage");
048        DEFAULT_PARAM_NAME_LIST.add("logger");
049        DEFAULT_PARAM_NAME_LIST.add("loggerContext");
050        DEFAULT_PARAM_NAME_LIST.add("level");
051        DEFAULT_PARAM_NAME_LIST.add("timeStamp");
052        DEFAULT_PARAM_NAME_LIST.add("marker");
053        DEFAULT_PARAM_NAME_LIST.add("mdc");
054        DEFAULT_PARAM_NAME_LIST.add("throwableProxy");
055        DEFAULT_PARAM_NAME_LIST.add("throwable");
056
057        DEFAULT_PARAM_TYPE_LIST.add(int.class);
058        DEFAULT_PARAM_TYPE_LIST.add(int.class);
059        DEFAULT_PARAM_TYPE_LIST.add(int.class);
060        DEFAULT_PARAM_TYPE_LIST.add(int.class);
061
062        DEFAULT_PARAM_TYPE_LIST.add(ILoggingEvent.class);
063        DEFAULT_PARAM_TYPE_LIST.add(String.class);
064        DEFAULT_PARAM_TYPE_LIST.add(String.class);
065        DEFAULT_PARAM_TYPE_LIST.add(String.class);
066        DEFAULT_PARAM_TYPE_LIST.add(LoggerContextVO.class);
067        DEFAULT_PARAM_TYPE_LIST.add(int.class);
068        DEFAULT_PARAM_TYPE_LIST.add(long.class);
069        DEFAULT_PARAM_TYPE_LIST.add(Marker.class);
070        DEFAULT_PARAM_TYPE_LIST.add(Map.class);
071        DEFAULT_PARAM_TYPE_LIST.add(IThrowableProxy.class);
072        DEFAULT_PARAM_TYPE_LIST.add(Throwable.class);
073    }
074
075    protected String getDecoratedExpression() {
076        String expression = getExpression();
077        if (!expression.contains("return")) {
078            expression = "return " + expression + ";";
079            addInfo("Adding [return] prefix and a semicolon suffix. Expression becomes [" + expression + "]");
080            addInfo("See also " + CoreConstants.CODES_URL + "#block");
081
082        }
083        return IMPORT_LEVEL + expression;
084    }
085
086    protected String[] getParameterNames() {
087        List<String> fullNameList = new ArrayList<String>();
088        fullNameList.addAll(DEFAULT_PARAM_NAME_LIST);
089
090        for (int i = 0; i < matcherList.size(); i++) {
091            Matcher m = (Matcher) matcherList.get(i);
092            fullNameList.add(m.getName());
093        }
094
095        return (String[]) fullNameList.toArray(CoreConstants.EMPTY_STRING_ARRAY);
096    }
097
098    protected Class<?>[] getParameterTypes() {
099        List<Class<?>> fullTypeList = new ArrayList<>();
100        fullTypeList.addAll(DEFAULT_PARAM_TYPE_LIST);
101        for (int i = 0; i < matcherList.size(); i++) {
102            fullTypeList.add(Matcher.class);
103        }
104        return (Class[]) fullTypeList.toArray(CoreConstants.EMPTY_CLASS_ARRAY);
105    }
106
107    protected Object[] getParameterValues(ILoggingEvent loggingEvent) {
108        final int matcherListSize = matcherList.size();
109
110        int i = 0;
111        Object[] values = new Object[DEFAULT_PARAM_NAME_LIST.size() + matcherListSize];
112
113        values[i++] = Level.DEBUG_INTEGER;
114        values[i++] = Level.INFO_INTEGER;
115        values[i++] = Level.WARN_INTEGER;
116        values[i++] = Level.ERROR_INTEGER;
117
118        values[i++] = loggingEvent;
119        values[i++] = loggingEvent.getMessage();
120        values[i++] = loggingEvent.getFormattedMessage();
121        values[i++] = loggingEvent.getLoggerName();
122        values[i++] = loggingEvent.getLoggerContextVO();
123        values[i++] = loggingEvent.getLevel().toInteger();
124        values[i++] = loggingEvent.getTimeStamp();
125        // In order to avoid NullPointerException, we could push a dummy marker if
126        // the event's marker is null. However, this would surprise user who
127        // expect to see a null marker instead of a dummy one.
128        values[i++] = loggingEvent.getMarker();
129        values[i++] = loggingEvent.getMDCPropertyMap();
130
131        IThrowableProxy iThrowableProxy = loggingEvent.getThrowableProxy();
132
133        if (iThrowableProxy != null) {
134            values[i++] = iThrowableProxy;
135            if (iThrowableProxy instanceof ThrowableProxy) {
136                values[i++] = ((ThrowableProxy) iThrowableProxy).getThrowable();
137            } else {
138                values[i++] = null;
139            }
140        } else {
141            values[i++] = null;
142            values[i++] = null;
143        }
144
145        for (int j = 0; j < matcherListSize; j++) {
146            values[i++] = (Matcher) matcherList.get(j);
147        }
148
149        return values;
150    }
151
152}