001/*
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2026, 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 v2.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.model.processor;
015
016import java.util.List;
017import java.util.Map;
018
019import ch.qos.logback.classic.model.ConfigurationModel;
020import ch.qos.logback.core.Context;
021import ch.qos.logback.core.model.Model;
022import ch.qos.logback.core.model.processor.ModelHandlerBase;
023import ch.qos.logback.core.model.processor.ModelHandlerException;
024import ch.qos.logback.core.model.processor.ModelInterpretationContext;
025import ch.qos.logback.core.model.processor.PhaseIndicator;
026import ch.qos.logback.core.model.processor.ProcessingPhase;
027import ch.qos.logback.core.status.Status;
028
029/**
030 * Dependency-analysis handler for {@link ConfigurationModel} that warns when a
031 * configuration contains contradictory caller-data instructions, for example an
032 * {@code AsyncAppender}, {@code SocketAppender} or {@code SMTPAppender} with
033 * {@code includeCallerData=false} (the default) alongside an appender (or
034 * SMTP layout) whose pattern uses a caller-data converter ({@code %C},
035 * {@code %M}, {@code %L}, {@code %F}, {@code %l}, {@code %class},
036 * {@code %method}, {@code %line}, {@code %file}, {@code %caller}).
037 *
038 * <p>All work is done in {@link #postHandle} so that it runs after every child
039 * {@link ch.qos.logback.core.model.AppenderModel} has been visited by
040 * {@link CallerContradictionAnalyser}, regardless of declaration order.</p>
041 *
042 * <p>Analysis is skipped when the variable
043 * {@value CallerContradictionAnalyser#SKIP_CALLER_CONTRADICTION_ANALYSIS_PROPERTY}
044 * is set to {@code true}.</p>
045 *
046 * @since 1.6.2
047 * @see CallerContradictionAnalyser
048 */
049@PhaseIndicator(phase = ProcessingPhase.DEPENDENCY_ANALYSIS)
050public class CallerContradictionWarnAnalyser extends ModelHandlerBase {
051
052    public CallerContradictionWarnAnalyser(Context context) {
053        super(context);
054    }
055
056    @Override
057    protected Class<ConfigurationModel> getSupportedModelClass() {
058        return ConfigurationModel.class;
059    }
060
061    @Override
062    public void handle(ModelInterpretationContext mic, Model model) throws ModelHandlerException {
063        // no-op; all work is in postHandle after children are visited
064    }
065
066    @Override
067    public void postHandle(ModelInterpretationContext mic, Model model) throws ModelHandlerException {
068        if (CallerContradictionAnalyser.isSkipCallerContradictionAnalysis(mic)) {
069            return;
070        }
071
072        Map<String, CallerInstructionLogic.Instruction> appenderNameToCallerInstructionMap =
073                CallerContradictionAnalyser.getAppenderNameToCallerInstructionMap(mic);
074
075        CallerInstructionLogic callerInstructionLogic = new CallerInstructionLogic();
076
077        List<Status> messages = callerInstructionLogic.contradiction(appenderNameToCallerInstructionMap);
078
079        messages.forEach(message -> addStatus(message));
080    }
081}